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