Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / testbed / test_testbed_api_topology_clique.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2008--2013 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU Affero General Public License as published
7   by the Free Software Foundation, either version 3 of the License,
8   or (at your option) any later version.
9
10   GNUnet is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Affero General Public License for more details.
14  
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file src/testbed/test_testbed_api_topology.c
21  * @brief testing cases for testing high level testbed api helper functions
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28
29 /**
30  * Number of peers we want to start
31  */
32 #define NUM_PEERS 10
33
34 /**
35  * Array of peers
36  */
37 static struct GNUNET_TESTBED_Peer **peers;
38
39 /**
40  * Operation handle
41  */
42 static struct GNUNET_TESTBED_Operation *op;
43
44 /**
45  * Shutdown task
46  */
47 static struct GNUNET_SCHEDULER_Task * shutdown_task;
48
49 /**
50  * Testing result
51  */
52 static int result;
53
54 /**
55  * Counter for counting overlay connections
56  */
57 static unsigned int overlay_connects;
58
59
60 /**
61  * Shutdown nicely
62  *
63  * @param cls NULL
64  */
65 static void
66 do_shutdown (void *cls)
67 {
68   shutdown_task = NULL;
69   if (NULL != op)
70   {
71     GNUNET_TESTBED_operation_done (op);
72     op = NULL;
73   }
74   GNUNET_SCHEDULER_shutdown ();
75 }
76
77 /**
78  * Controller event callback
79  *
80  * @param cls NULL
81  * @param event the controller event
82  */
83 static void
84 controller_event_cb (void *cls,
85                      const struct GNUNET_TESTBED_EventInformation *event)
86 {
87   switch (event->type)
88   {
89   case GNUNET_TESTBED_ET_CONNECT:
90     overlay_connects++;
91     if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
92     {
93       result = GNUNET_OK;
94       GNUNET_SCHEDULER_cancel (shutdown_task);
95       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
96     }
97     break;
98   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
99     GNUNET_assert (NULL != event->details.operation_finished.emsg);
100     break;
101   default:
102     GNUNET_break (0);
103     result = GNUNET_SYSERR;
104     GNUNET_SCHEDULER_cancel (shutdown_task);
105     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
106   }
107 }
108
109
110 /**
111  * Signature of a main function for a testcase.
112  *
113  * @param cls closure
114  * @param h the run handle
115  * @param num_peers number of peers in 'peers'
116  * @param peers_ handle to peers run in the testbed
117  * @param links_succeeded the number of overlay link connection attempts that
118  *          succeeded
119  * @param links_failed the number of overlay link connection attempts that
120  *          failed
121  */
122 static void
123 test_master (void *cls,
124              struct GNUNET_TESTBED_RunHandle *h,
125              unsigned int num_peers,
126              struct GNUNET_TESTBED_Peer **peers_,
127              unsigned int links_succeeded,
128              unsigned int links_failed)
129 {
130   unsigned int peer;
131
132   GNUNET_assert (NULL == cls);
133   if (NULL == peers_)
134   {
135     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
136     return;
137   }
138   GNUNET_assert (NUM_PEERS == num_peers);
139   for (peer = 0; peer < num_peers; peer++)
140     GNUNET_assert (NULL != peers_[peer]);
141   peers = peers_;
142   overlay_connects = 0;
143   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
144                                                   NULL,
145                                                   NULL,
146                                                   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
147                                                   /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
148                                                   /* NUM_PEERS, */
149                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
150   GNUNET_assert (NULL != op);
151   shutdown_task =
152       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
153                                     (GNUNET_TIME_UNIT_SECONDS, 300),
154                                     do_shutdown, NULL);
155 }
156
157
158 /**
159  * Main function
160  */
161 int
162 main (int argc, char **argv)
163 {
164   uint64_t event_mask;
165
166   result = GNUNET_SYSERR;
167   event_mask = 0;
168   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
169   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
170   (void) GNUNET_TESTBED_test_run ("test_testbed_api_test",
171                                   "test_testbed_api.conf", NUM_PEERS,
172                                   event_mask, &controller_event_cb, NULL,
173                                   &test_master, NULL);
174   if (GNUNET_OK != result)
175     return 1;
176   return 0;
177 }
178
179 /* end of test_testbed_api_topology.c */