free in proper place
[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 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 struct GNUNET_TestMessage
89 {
90   /**
91    * Header of the message
92    */
93   struct GNUNET_MessageHeader header;
94
95   /**
96    * Unique identifier for this message.
97    */
98   uint32_t uid;
99 };
100
101
102 /**
103  * Check whether peers successfully shut down.
104  */
105 void
106 shutdown_callback (void *cls, const char *emsg)
107 {
108   if (emsg != NULL)
109     {
110 #if VERBOSE
111       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
112 #endif
113       if (ok == 0)
114         ok = 666;
115     }
116   else
117     {
118 #if VERBOSE
119       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120                   "All peers successfully shut down!\n");
121 #endif
122     }
123 }
124
125 static void
126 finish_testing ()
127 {
128   GNUNET_assert (pg != NULL);
129
130 #if VERBOSE
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132               "Called finish testing, stopping daemons.\n");
133 #endif
134   sleep (1);
135 #if VERBOSE
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
137 #endif
138   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
139 #if VERBOSE
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
141 #endif
142   if (dotOutFile != NULL)
143     {
144       fprintf (dotOutFile, "}");
145       fclose (dotOutFile);
146     }
147
148   ok = 0;
149 }
150
151 static void
152 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 {
154   char *msg = cls;
155   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
156               "End badly was called (%s)... stopping daemons.\n", msg);
157
158   if (pg != NULL)
159     {
160       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
161       ok = 7331;                /* Opposite of leet */
162     }
163   else
164     ok = 401;                   /* Never got peers started */
165
166   if (dotOutFile != NULL)
167     {
168       fprintf (dotOutFile, "}");
169       fclose (dotOutFile);
170     }
171 }
172
173
174
175 void
176 topology_callback (void *cls,
177                    const struct GNUNET_PeerIdentity *first,
178                    const struct GNUNET_PeerIdentity *second,
179                    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,
264                                          connect_attempts,
265                                          NULL, NULL);
266 #if VERBOSE
267       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268                   "Have %d expected connections\n", expected_connections);
269 #endif
270     }
271
272   GNUNET_SCHEDULER_cancel (die_task);
273   if (expected_connections == GNUNET_SYSERR)
274     {
275       die_task =
276         GNUNET_SCHEDULER_add_now (&end_badly,
277                                   "from connect topology (bad return)");
278     }
279
280   die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
281                                            &end_badly,
282                                            "from connect topology (timeout)");
283 }
284
285 static void
286 create_topology ()
287 {
288   peers_left = num_peers;       /* Reset counter */
289   if (GNUNET_TESTING_create_topology
290       (pg, topology, blacklist_topology,
291        blacklist_transports) != GNUNET_SYSERR)
292     {
293 #if VERBOSE
294       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295                   "Topology set up, now starting peers!\n");
296 #endif
297       GNUNET_TESTING_daemons_continue_startup (pg);
298     }
299   else
300     {
301       GNUNET_SCHEDULER_cancel (die_task);
302       die_task =
303         GNUNET_SCHEDULER_add_now (&end_badly,
304                                   "from create topology (bad return)");
305     }
306   GNUNET_SCHEDULER_cancel (die_task);
307   die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
308                                            &end_badly,
309                                            "from continue startup (timeout)");
310 }
311
312
313 static void
314 peers_started_callback (void *cls,
315                         const struct GNUNET_PeerIdentity *id,
316                         const struct GNUNET_CONFIGURATION_Handle *cfg,
317                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
318 {
319   if (emsg != NULL)
320     {
321       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322                   "Failed to start daemon with error: `%s'\n", emsg);
323       return;
324     }
325   GNUNET_assert (id != NULL);
326 #if VERBOSE
327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
328               (num_peers - peers_left) + 1, num_peers);
329 #endif
330   peers_left--;
331   if (peers_left == 0)
332     {
333 #if VERBOSE
334       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335                   "All %d daemons started, now creating topology!\n",
336                   num_peers);
337 #endif
338       GNUNET_SCHEDULER_cancel (die_task);
339       /* Set up task in case topology creation doesn't finish
340        * within a reasonable amount of time */
341       die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
342                                                (GNUNET_TIME_UNIT_MINUTES, 5),
343                                                &end_badly,
344                                                "from peers_started_callback");
345       connect_topology ();
346       ok = 0;
347     }
348 }
349
350 /**
351  * Callback indicating that the hostkey was created for a peer.
352  *
353  * @param cls NULL
354  * @param id the peer identity
355  * @param d the daemon handle (pretty useless at this point, remove?)
356  * @param emsg non-null on failure
357  */
358 void
359 hostkey_callback (void *cls,
360                   const struct GNUNET_PeerIdentity *id,
361                   struct GNUNET_TESTING_Daemon *d, const char *emsg)
362 {
363   if (emsg != NULL)
364     {
365       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366                   "Hostkey callback received error: %s\n", emsg);
367     }
368
369 #if VERBOSE
370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371               "Hostkey created for peer `%s'\n", GNUNET_i2s (id));
372 #endif
373   peers_left--;
374   if (peers_left == 0)
375     {
376 #if VERBOSE
377       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378                   "All %d hostkeys created, now creating topology!\n",
379                   num_peers);
380 #endif
381       GNUNET_SCHEDULER_cancel (die_task);
382       /* Set up task in case topology creation doesn't finish
383        * within a reasonable amount of time */
384       die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
385                                                (GNUNET_TIME_UNIT_MINUTES, 5),
386                                                &end_badly,
387                                                "from hostkey_callback");
388       GNUNET_SCHEDULER_add_now (&create_topology, NULL);
389       ok = 0;
390     }
391 }
392
393 static void
394 run (void *cls,
395      char *const *args,
396      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
397 {
398   unsigned long long topology_num;
399   unsigned long long connect_topology_num;
400   unsigned long long blacklist_topology_num;
401   unsigned long long connect_topology_option_num;
402   unsigned long long temp_connect;
403   char *connect_topology_option_modifier_string;
404   ok = 1;
405
406   dotOutFile = fopen (dotOutFileName, "w");
407   if (dotOutFile != NULL)
408     {
409       fprintf (dotOutFile, "strict graph G {\n");
410     }
411
412 #if VERBOSE
413   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414               "Starting daemons based on config file %s\n", cfgfile);
415 #endif
416
417   if (GNUNET_YES !=
418       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
419                                              &test_directory))
420     {
421       ok = 404;
422       if (dotOutFile != NULL)
423         {
424           fclose (dotOutFile);
425         }
426       return;
427     }
428
429   if (GNUNET_YES ==
430       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
431                                              &topology_num))
432     topology = topology_num;
433
434   if (GNUNET_YES ==
435       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
436                                              "connect_topology",
437                                              &connect_topology_num))
438     connection_topology = connect_topology_num;
439
440   if (GNUNET_YES ==
441       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
442                                              "connect_topology_option",
443                                              &connect_topology_option_num))
444     connect_topology_option = connect_topology_option_num;
445
446   if (GNUNET_YES ==
447       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
448                                              "connect_topology_option_modifier",
449                                              &connect_topology_option_modifier_string))
450     {
451       if (sscanf
452           (connect_topology_option_modifier_string, "%lf",
453            &connect_topology_option_modifier) != 1)
454         {
455           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
456                       _
457                       ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
458                       connect_topology_option_modifier_string,
459                       "connect_topology_option_modifier", "TESTING");
460           GNUNET_free (connect_topology_option_modifier_string);
461           ok = 707;
462           if (dotOutFile != NULL)
463             {
464               fclose (dotOutFile);
465             }
466           return;
467         }
468       GNUNET_free (connect_topology_option_modifier_string);
469     }
470
471   if (GNUNET_OK !=
472       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
473                                              "blacklist_transports",
474                                              &blacklist_transports))
475     {
476       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
477                   "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
478       ok = 808;
479       if (dotOutFile != NULL)
480         {
481           fclose (dotOutFile);
482         }
483       return;
484     }
485
486   if (GNUNET_YES ==
487       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
488                                              "blacklist_topology",
489                                              &blacklist_topology_num))
490     blacklist_topology = blacklist_topology_num;
491
492   if (GNUNET_SYSERR ==
493       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
494                                              &num_peers))
495     num_peers = DEFAULT_NUM_PEERS;
496
497   if (GNUNET_OK ==
498       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_timeout",
499                                              &temp_connect))
500     connect_timeout =
501       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_connect);
502   else
503     {
504       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_timeout");
505       return;
506     }
507
508
509   if (GNUNET_OK !=
510         GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
511                                                &connect_attempts))
512     {
513       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_attempts");
514       return;
515     }
516
517   main_cfg = cfg;
518
519   GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
520   peers_left = num_peers;
521
522   /* For this specific test we only really want a CLIQUE topology as the
523    * overlay allowed topology, and a RING topology as the underlying connection
524    * allowed topology.  So we will expect only num_peers * 2 connections to
525    * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
526    */
527   expected_connections = num_peers * (num_peers - 1);
528   expected_failed_connections = expected_connections - (num_peers * 2);
529
530
531   /* Set up a task to end testing if peer start fails */
532   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
533                                            (GNUNET_TIME_UNIT_MINUTES, 5),
534                                            &end_badly,
535                                            "didn't start all daemons in reasonable amount of time!!!");
536
537   pg = GNUNET_TESTING_daemons_start (cfg,
538                                      peers_left, peers_left, peers_left,
539                                      TIMEOUT, &hostkey_callback,
540                                      NULL, &peers_started_callback, NULL,
541                                      &topology_callback, NULL, NULL);
542
543 }
544
545 static int
546 check ()
547 {
548   int ret;
549   char *const argv[] = { "test-testing-topology-blacklist",
550     "-c",
551     "test_testing_data_topology_blacklist.conf",
552 #if VERBOSE
553     "-L", "DEBUG",
554 #endif
555     NULL
556   };
557   struct GNUNET_GETOPT_CommandLineOption options[] = {
558     GNUNET_GETOPT_OPTION_END
559   };
560   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
561                             argv, "test-testing-topology-blacklist", "nohelp",
562                             options, &run, &ok);
563   if (ret != GNUNET_OK)
564     {
565       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566                   "`test-testing-topology-blacklist': Failed with error code %d\n",
567                   ret);
568     }
569
570   return ok;
571 }
572
573 int
574 main (int argc, char *argv[])
575 {
576   int ret;
577
578   GNUNET_log_setup ("test_testing_topology_blacklist",
579 #if VERBOSE
580                     "DEBUG",
581 #else
582                     "WARNING",
583 #endif
584                     NULL);
585   ret = check ();
586
587   /**
588    * Need to remove base directory, subdirectories taken care
589    * of by the testing framework.
590    */
591   if (test_directory != NULL)
592     {
593       if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
594         {
595           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
596                       "Failed to remove testing directory %s\n",
597                       test_directory);
598         }
599     }
600
601   return ret;
602 }
603
604 /* end of test_testing_topology_blacklist.c */