fix
[oweals/gnunet.git] / src / testing / test_testing_topology_blacklist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_topology_blacklist.c
22  * @brief base testcase for testing transport level blacklisting
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
27
28 #define VERBOSE GNUNET_NO
29
30 /**
31  * How long until we fail the whole testcase?
32  */
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
34
35 /**
36  * How long until we give up on starting the peers? (Must be longer than the connect timeout!)
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39
40 #define DEFAULT_NUM_PEERS 4
41
42 #define MAX_OUTSTANDING_CONNECTIONS 300
43
44 static int ok;
45
46 static unsigned long long num_peers;
47
48 static unsigned int total_connections;
49
50 static unsigned int failed_connections;
51
52 static unsigned int expected_connections;
53
54 static unsigned int expected_failed_connections;
55
56 static unsigned long long peers_left;
57
58 static struct GNUNET_TESTING_PeerGroup *pg;
59
60 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
61
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
63
64 static char *dotOutFileName;
65
66 static FILE *dotOutFile;
67
68 static char *blacklist_transports;
69
70 static enum GNUNET_TESTING_Topology topology = GNUNET_TESTING_TOPOLOGY_CLIQUE;  /* Overlay should allow all connections */
71
72 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_RING;  /* Blacklist underlay into a ring */
73
74 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
75
76 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Try to connect all possible OVERLAY connections */
77
78 static double connect_topology_option_modifier = 0.0;
79
80 static char *test_directory;
81
82 #define MTYPE 12345
83
84 struct GNUNET_TestMessage
85 {
86   /**
87    * Header of the message
88    */
89   struct GNUNET_MessageHeader header;
90
91   /**
92    * Unique identifier for this message.
93    */
94   uint32_t uid;
95 };
96
97
98 /**
99  * Check whether peers successfully shut down.
100  */
101 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, "Shutdown of peers failed!\n");
108 #endif
109       if (ok == 0)
110         ok = 666;
111     }
112   else
113     {
114 #if VERBOSE
115       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116                   "All peers successfully shut down!\n");
117 #endif
118     }
119 }
120
121 static void
122 finish_testing ()
123 {
124   GNUNET_assert (pg != NULL);
125
126 #if VERBOSE
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Called finish testing, stopping daemons.\n");
129 #endif
130   sleep (1);
131 #if VERBOSE
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
133 #endif
134   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
135 #if VERBOSE
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
137 #endif
138   if (dotOutFile != NULL)
139     {
140       fprintf (dotOutFile, "}");
141       fclose (dotOutFile);
142     }
143
144   ok = 0;
145 }
146
147 static void
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
149 {
150   char *msg = cls;
151   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152               "End badly was called (%s)... stopping daemons.\n", msg);
153
154   if (pg != NULL)
155     {
156       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
157       ok = 7331;                /* Opposite of leet */
158     }
159   else
160     ok = 401;                   /* Never got peers started */
161
162   if (dotOutFile != NULL)
163     {
164       fprintf (dotOutFile, "}");
165       fclose (dotOutFile);
166     }
167 }
168
169
170
171 void
172 topology_callback (void *cls,
173                    const struct GNUNET_PeerIdentity *first,
174                    const struct GNUNET_PeerIdentity *second,
175                    uint32_t distance,
176                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
177                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
178                    struct GNUNET_TESTING_Daemon *first_daemon,
179                    struct GNUNET_TESTING_Daemon *second_daemon,
180                    const char *emsg)
181 {
182   if (emsg == NULL)
183     {
184       total_connections++;
185 #if VERBOSE
186       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
187                   first_daemon->shortname, second_daemon->shortname);
188 #endif
189       if (dotOutFile != NULL)
190         fprintf (dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname,
191                  second_daemon->shortname);
192     }
193
194   else
195     {
196       failed_connections++;
197 #if VERBOSE
198       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199                   "Failed to connect peer %s to peer %s with error :\n%s\n",
200                   first_daemon->shortname, second_daemon->shortname, emsg);
201 #endif
202     }
203
204
205   if (total_connections == expected_connections)
206     {
207 #if VERBOSE
208       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209                   "Created %d total connections, which is our target number (that's bad)!\n",
210                   total_connections);
211 #endif
212
213       GNUNET_SCHEDULER_cancel (die_task);
214       die_task = GNUNET_SCHEDULER_NO_TASK;
215       die_task =
216         GNUNET_SCHEDULER_add_now (&end_badly,
217                                   "from topology_callback (too many successful connections)");
218     }
219   else if (total_connections + failed_connections == expected_connections)
220     {
221       if ((failed_connections == expected_failed_connections)
222           && (total_connections ==
223               expected_connections - expected_failed_connections))
224         {
225           GNUNET_SCHEDULER_cancel (die_task);
226           die_task = GNUNET_SCHEDULER_NO_TASK;
227           die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
228         }
229       else
230         {
231           GNUNET_SCHEDULER_cancel (die_task);
232           die_task =
233             GNUNET_SCHEDULER_add_now (&end_badly,
234                                       "from topology_callback (wrong number of failed connections)");
235         }
236     }
237   else
238     {
239 #if VERBOSE
240       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241                   "Have %d total connections, %d failed connections, Want %d (failed) and %d (successful)\n",
242                   total_connections, failed_connections,
243                   expected_failed_connections,
244                   expected_connections - expected_failed_connections);
245 #endif
246     }
247 }
248
249 static void
250 connect_topology ()
251 {
252   expected_connections = -1;
253   if ((pg != NULL) && (peers_left == 0))
254     {
255       expected_connections =
256         GNUNET_TESTING_connect_topology (pg, connection_topology,
257                                          connect_topology_option,
258                                          connect_topology_option_modifier,
259                                          NULL, NULL);
260 #if VERBOSE
261       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262                   "Have %d expected connections\n", expected_connections);
263 #endif
264     }
265
266   GNUNET_SCHEDULER_cancel (die_task);
267   if (expected_connections == GNUNET_SYSERR)
268     {
269       die_task =
270         GNUNET_SCHEDULER_add_now (&end_badly,
271                                   "from connect topology (bad return)");
272     }
273
274   die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
275                                            &end_badly,
276                                            "from connect topology (timeout)");
277 }
278
279 static void
280 create_topology ()
281 {
282   peers_left = num_peers;       /* Reset counter */
283   if (GNUNET_TESTING_create_topology
284       (pg, topology, blacklist_topology,
285        blacklist_transports) != GNUNET_SYSERR)
286     {
287 #if VERBOSE
288       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289                   "Topology set up, now starting peers!\n");
290 #endif
291       GNUNET_TESTING_daemons_continue_startup (pg);
292     }
293   else
294     {
295       GNUNET_SCHEDULER_cancel (die_task);
296       die_task =
297         GNUNET_SCHEDULER_add_now (&end_badly,
298                                   "from create topology (bad return)");
299     }
300   GNUNET_SCHEDULER_cancel (die_task);
301   die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
302                                            &end_badly,
303                                            "from continue startup (timeout)");
304 }
305
306
307 static void
308 peers_started_callback (void *cls,
309                         const struct GNUNET_PeerIdentity *id,
310                         const struct GNUNET_CONFIGURATION_Handle *cfg,
311                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
312 {
313   if (emsg != NULL)
314     {
315       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316                   "Failed to start daemon with error: `%s'\n", emsg);
317       return;
318     }
319   GNUNET_assert (id != NULL);
320 #if VERBOSE
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
322               (num_peers - peers_left) + 1, num_peers);
323 #endif
324   peers_left--;
325   if (peers_left == 0)
326     {
327 #if VERBOSE
328       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329                   "All %d daemons started, now creating topology!\n",
330                   num_peers);
331 #endif
332       GNUNET_SCHEDULER_cancel (die_task);
333       /* Set up task in case topology creation doesn't finish
334        * within a reasonable amount of time */
335       die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
336                                                (GNUNET_TIME_UNIT_MINUTES, 5),
337                                                &end_badly,
338                                                "from peers_started_callback");
339       connect_topology ();
340       ok = 0;
341     }
342 }
343
344 /**
345  * Callback indicating that the hostkey was created for a peer.
346  *
347  * @param cls NULL
348  * @param id the peer identity
349  * @param d the daemon handle (pretty useless at this point, remove?)
350  * @param emsg non-null on failure
351  */
352 void
353 hostkey_callback (void *cls,
354                   const struct GNUNET_PeerIdentity *id,
355                   struct GNUNET_TESTING_Daemon *d, const char *emsg)
356 {
357   if (emsg != NULL)
358     {
359       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360                   "Hostkey callback received error: %s\n", emsg);
361     }
362
363 #if VERBOSE
364   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365               "Hostkey created for peer `%s'\n", GNUNET_i2s (id));
366 #endif
367   peers_left--;
368   if (peers_left == 0)
369     {
370 #if VERBOSE
371       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372                   "All %d hostkeys created, now creating topology!\n",
373                   num_peers);
374 #endif
375       GNUNET_SCHEDULER_cancel (die_task);
376       /* Set up task in case topology creation doesn't finish
377        * within a reasonable amount of time */
378       die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
379                                                (GNUNET_TIME_UNIT_MINUTES, 5),
380                                                &end_badly,
381                                                "from hostkey_callback");
382       GNUNET_SCHEDULER_add_now (&create_topology, NULL);
383       ok = 0;
384     }
385 }
386
387 static void
388 run (void *cls,
389      char *const *args,
390      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
391 {
392   unsigned long long topology_num;
393   unsigned long long connect_topology_num;
394   unsigned long long blacklist_topology_num;
395   unsigned long long connect_topology_option_num;
396   char *connect_topology_option_modifier_string;
397   ok = 1;
398
399   dotOutFile = fopen (dotOutFileName, "w");
400   if (dotOutFile != NULL)
401     {
402       fprintf (dotOutFile, "strict graph G {\n");
403     }
404
405 #if VERBOSE
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407               "Starting daemons based on config file %s\n", cfgfile);
408 #endif
409
410   if (GNUNET_YES !=
411       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
412                                              &test_directory))
413     {
414       ok = 404;
415       if (dotOutFile != NULL)
416         {
417           fclose (dotOutFile);
418         }
419       return;
420     }
421
422   if (GNUNET_YES ==
423       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
424                                              &topology_num))
425     topology = topology_num;
426
427   if (GNUNET_YES ==
428       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
429                                              "connect_topology",
430                                              &connect_topology_num))
431     connection_topology = connect_topology_num;
432
433   if (GNUNET_YES ==
434       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
435                                              "connect_topology_option",
436                                              &connect_topology_option_num))
437     connect_topology_option = connect_topology_option_num;
438
439   if (GNUNET_YES ==
440       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
441                                              "connect_topology_option_modifier",
442                                              &connect_topology_option_modifier_string))
443     {
444       if (sscanf
445           (connect_topology_option_modifier_string, "%lf",
446            &connect_topology_option_modifier) != 1)
447         {
448           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
449                       _
450                       ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
451                       connect_topology_option_modifier_string,
452                       "connect_topology_option_modifier", "TESTING");
453           GNUNET_free (connect_topology_option_modifier_string);
454           ok = 707;
455           if (dotOutFile != NULL)
456             {
457               fclose (dotOutFile);
458             }
459           return;
460         }
461       GNUNET_free (connect_topology_option_modifier_string);
462     }
463
464   if (GNUNET_OK !=
465       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
466                                              "blacklist_transports",
467                                              &blacklist_transports))
468     {
469       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470                   "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
471       ok = 808;
472       if (dotOutFile != NULL)
473         {
474           fclose (dotOutFile);
475         }
476       return;
477     }
478
479   if (GNUNET_YES ==
480       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
481                                              "blacklist_topology",
482                                              &blacklist_topology_num))
483     blacklist_topology = blacklist_topology_num;
484
485   if (GNUNET_SYSERR ==
486       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
487                                              &num_peers))
488     num_peers = DEFAULT_NUM_PEERS;
489
490   main_cfg = cfg;
491
492   GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
493   peers_left = num_peers;
494
495   /* For this specific test we only really want a CLIQUE topology as the
496    * overlay allowed topology, and a RING topology as the underlying connection
497    * allowed topology.  So we will expect only num_peers * 2 connections to
498    * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
499    */
500   expected_connections = num_peers * (num_peers - 1);
501   expected_failed_connections = expected_connections - (num_peers * 2);
502
503
504   /* Set up a task to end testing if peer start fails */
505   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
506                                            (GNUNET_TIME_UNIT_MINUTES, 5),
507                                            &end_badly,
508                                            "didn't start all daemons in reasonable amount of time!!!");
509
510   pg = GNUNET_TESTING_daemons_start (cfg,
511                                      peers_left, TIMEOUT, &hostkey_callback,
512                                      NULL, &peers_started_callback, NULL,
513                                      &topology_callback, NULL, NULL);
514
515 }
516
517 static int
518 check ()
519 {
520   int ret;
521   char *const argv[] = { "test-testing-topology-blacklist",
522     "-c",
523     "test_testing_data_topology_blacklist.conf",
524 #if VERBOSE
525     "-L", "DEBUG",
526 #endif
527     NULL
528   };
529   struct GNUNET_GETOPT_CommandLineOption options[] = {
530     GNUNET_GETOPT_OPTION_END
531   };
532   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
533                             argv, "test-testing-topology-blacklist", "nohelp",
534                             options, &run, &ok);
535   if (ret != GNUNET_OK)
536     {
537       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
538                   "`test-testing-topology-blacklist': Failed with error code %d\n",
539                   ret);
540     }
541
542   return ok;
543 }
544
545 int
546 main (int argc, char *argv[])
547 {
548   int ret;
549
550   GNUNET_log_setup ("test_testing_topology_blacklist",
551 #if VERBOSE
552                     "DEBUG",
553 #else
554                     "WARNING",
555 #endif
556                     NULL);
557   ret = check ();
558
559   /**
560    * Need to remove base directory, subdirectories taken care
561    * of by the testing framework.
562    */
563   if (test_directory != NULL)
564     {
565       if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
566         {
567           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
568                       "Failed to remove testing directory %s\n",
569                       test_directory);
570         }
571     }
572
573   return ret;
574 }
575
576 /* end of test_testing_topology_blacklist.c */