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