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