-moving old testing code to legacy location
[oweals/gnunet.git] / src / testing_old / test_testing_2dtorus.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  * @file testing/test_testing_2dtorus.c
22  *
23  * @brief Test for creating a 2dtorus.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27
28 #define VERBOSE GNUNET_YES
29 #define REMOVE_DIR GNUNET_YES
30
31 /**
32  * How long until we give up on connecting the peers?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
35
36 /**
37  * Time to wait for stuff that should be rather fast
38  */
39 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
40
41
42 /**
43  * How many events have happened
44  */
45 static int ok;
46
47 /**
48  * Be verbose
49  */
50 static int verbose;
51
52 /**
53  * Total number of peers in the test.
54  */
55 static unsigned long long num_peers;
56
57 /**
58  * Global configuration file
59  */
60 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
61
62 /**
63  * Total number of currently running peers.
64  */
65 static unsigned long long peers_running;
66
67 /**
68  * Total number of successful connections in the whole network.
69  */
70 static unsigned int total_connections;
71
72 /**
73  * Total number of counted topo connections
74  */
75 static unsigned int topo_connections;
76
77 /**
78  * Total number of failed connections in the whole network.
79  */
80 static unsigned int failed_connections;
81
82 /**
83  * The currently running peer group.
84  */
85 static struct GNUNET_TESTING_PeerGroup *pg;
86
87 /**
88  * Task called to disconnect peers
89  */
90 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
91
92 /**
93  * Task called to shutdown test.
94  */
95 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
96
97
98 /**
99  * Check whether peers successfully shut down.
100  */
101 static void
102 shutdown_callback (void *cls, const char *emsg)
103 {
104   if (emsg != NULL)
105   {
106 #if VERBOSE
107     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
108 #endif
109     ok--;
110   }
111   else
112   {
113 #if VERBOSE
114     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115                 "test: All peers successfully shut down!\n");
116 #endif
117   }
118 }
119
120
121 static void
122 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
123 {
124 #if VERBOSE
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
126 #endif
127
128   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
129   GNUNET_CONFIGURATION_destroy (testing_cfg);
130 }
131
132
133 static void
134 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
137
138   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
139   {
140     GNUNET_SCHEDULER_cancel (shutdown_handle);
141     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
142   }
143 }
144
145
146 /**
147  * Prototype of a callback function indicating that two peers
148  * are currently connected.
149  *
150  * @param cls closure
151  * @param first peer id for first daemon
152  * @param second peer id for the second daemon
153  * @param distance distance between the connected peers
154  * @param emsg error message (NULL on success)
155  */
156 void
157 topo_cb (void *cls, const struct GNUNET_PeerIdentity *first,
158          const struct GNUNET_PeerIdentity *second, const char *emsg)
159 {
160   topo_connections++;
161   if (NULL != emsg)
162   {
163     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error by topo %u: %s\n",
164                 topo_connections, emsg);
165   }
166   else
167   {
168     if (first == NULL || second == NULL)
169     {
170       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u NULL\n",
171                   topo_connections);
172       if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
173       {
174         GNUNET_SCHEDULER_cancel (disconnect_task);
175         GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
176       }
177       return;
178     }
179     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u ok\n",
180                 topo_connections);
181     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:   %s\n", GNUNET_i2s (first));
182     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:   %s\n", GNUNET_i2s (second));
183   }
184 }
185
186
187 /**
188  * peergroup_ready: start test when all peers are connected
189  * @param cls closure
190  * @param emsg error message
191  */
192 static void
193 peergroup_ready (void *cls, const char *emsg)
194 {
195   if (emsg != NULL)
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                 "test: Peergroup callback called with error, aborting test!\n");
199     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
200                 emsg);
201     ok--;
202     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
203     return;
204   }
205 #if VERBOSE
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207               "************************************************************\n");
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209               "test: Peer Group started successfully!\n");
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Have %u connections\n",
211               total_connections);
212 #endif
213
214   peers_running = GNUNET_TESTING_daemons_running (pg);
215   if (0 < failed_connections)
216   {
217     ok = GNUNET_SYSERR;
218     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
219                 failed_connections);
220     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
221
222   }
223   else
224   {
225     GNUNET_TESTING_get_topology (pg, &topo_cb, NULL);
226     disconnect_task =
227         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
228     ok = GNUNET_OK;
229   }
230
231 }
232
233
234 /**
235  * Function that will be called whenever two daemons are connected by
236  * the testing library.
237  *
238  * @param cls closure
239  * @param first peer id for first daemon
240  * @param second peer id for the second daemon
241  * @param distance distance between the connected peers
242  * @param first_cfg config for the first daemon
243  * @param second_cfg config for the second daemon
244  * @param first_daemon handle for the first daemon
245  * @param second_daemon handle for the second daemon
246  * @param emsg error message (NULL on success)
247  */
248 static void
249 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
250             const struct GNUNET_PeerIdentity *second, uint32_t distance,
251             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
252             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
253             struct GNUNET_TESTING_Daemon *first_daemon,
254             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
255 {
256   if (emsg == NULL)
257   {
258     total_connections++;
259   }
260   else
261   {
262     failed_connections++;
263     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264                 "test: Problem with new connection (%s)\n", emsg);
265     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:   (%s)\n", GNUNET_i2s (first));
266     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test:   (%s)\n", GNUNET_i2s (second));
267   }
268
269 }
270
271
272 /**
273  * run: load configuration options and schedule test to run (start peergroup)
274  * @param cls closure
275  * @param args argv
276  * @param cfgfile configuration file name (can be NULL)
277  * @param cfg configuration handle
278  */
279 static void
280 run (void *cls, char *const *args, const char *cfgfile,
281      const struct GNUNET_CONFIGURATION_Handle *cfg)
282 {
283   struct GNUNET_TESTING_Host *hosts;
284
285   ok = GNUNET_NO;
286   total_connections = 0;
287   failed_connections = 0;
288   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
289
290   GNUNET_log_setup ("test_testing_2dtorus",
291 #if VERBOSE
292                     "DEBUG",
293 #else
294                     "WARNING",
295 #endif
296                     NULL);
297
298 #if VERBOSE
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
300   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
301                                          "use_progressbars", "YES");
302 #endif
303
304   if (GNUNET_OK !=
305       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
306                                              "num_peers", &num_peers))
307   {
308     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309                 "Option TESTING:NUM_PEERS is required!\n");
310     return;
311   }
312
313   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
314
315   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
316                                        &connect_cb, &peergroup_ready, NULL,
317                                        hosts);
318   GNUNET_assert (pg != NULL);
319   shutdown_handle =
320     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
321                                     &shutdown_task, NULL);
322 }
323
324
325 /**
326  * test_testing_2dtorus command line options
327  */
328 static struct GNUNET_GETOPT_CommandLineOption options[] = {
329   {'V', "verbose", NULL,
330    gettext_noop ("be verbose (print progress information)"),
331    0, &GNUNET_GETOPT_set_one, &verbose},
332   GNUNET_GETOPT_OPTION_END
333 };
334
335
336 /**
337  * Main: start test
338  */
339 int
340 main (int argc, char *argv[])
341 {
342   char *const argv2[] = {
343     argv[0],
344     "-c",
345     "test_testing_2dtorus.conf",
346 #if VERBOSE
347     "-L",
348     "DEBUG",
349 #endif
350     NULL
351   };
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
354
355
356   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
357                       "test_testing_2dtorus",
358                       gettext_noop ("Test testing 2d torus."), options, &run,
359                       NULL);
360 #if REMOVE_DIR
361   GNUNET_DISK_directory_remove ("/tmp/test_testing_2dtorus");
362 #endif
363   if (GNUNET_OK != ok)
364   {
365     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
366     return 1;
367   }
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
369   return 0;
370 }
371
372 /* end of test_testing_2dtorus.c */