fix div by zero
[oweals/gnunet.git] / src / transport / test_transport_blacklisting.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file transport/transport_api_blacklisting.c
21  * @brief test for the blacklisting with blacklistings defined in cfg
22  *
23  * this file contains multiple tests:
24  *
25  * test_transport_blacklisting_no_bl:
26  *      no blacklisting entries
27  *      peers are expected to connect
28  * test_transport_blacklisting_outbound_bl_full:
29  *      both peers contain bl entries for full peer
30  *      test is expected to not connect
31  * test_transport_blacklisting_outbound_bl_plugin:
32  *      both peers contain bl entries for plugin
33  *      test is expected to not connect
34  * test_transport_blacklisting_inbound_bl_plugin:
35  *      peer 1 contains no bl entries
36  *      peer 2 contain bl entries for full peer
37  *      test is expected to not connect
38  * test_transport_blacklisting_inbound_bl_full:
39  *      peer 1 contains no bl entries
40  *      peer 2 contain bl entries for plugin
41  *      test is expected to not connect
42  * test_transport_blacklisting_multiple_plugins:
43  *      both peers contain bl entries for plugin
44  *      test is expected to  connect with not bl'ed plugin
45  *
46  * @author Matthias Wachs
47  *
48  */
49 #include "platform.h"
50 #include "gnunet_transport_service.h"
51 #include "transport-testing.h"
52
53 char *test_name;
54
55 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
56
57 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
58
59 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
60
61 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
62
63 /**
64  * How long until we give up on transmitting the message?
65  */
66 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
67
68 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
69
70 static int stage;
71 static int ok;
72 static int connected;
73
74 static struct GNUNET_SCHEDULER_Task * die_task;
75
76 static struct GNUNET_SCHEDULER_Task * timeout_task;
77
78 static struct GNUNET_SCHEDULER_Task * stage_task;
79
80 #if VERBOSE
81 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
82 #else
83 #define OKPP do { ok++; } while (0)
84 #endif
85
86
87 static void
88 run_stage(void *cls);
89
90
91 static void
92 end (void *cls)
93 {
94   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
95
96   if (die_task != NULL )
97   {
98     GNUNET_SCHEDULER_cancel (die_task);
99     die_task = NULL;
100   }
101
102   if (timeout_task != NULL )
103   {
104     GNUNET_SCHEDULER_cancel (timeout_task);
105     timeout_task = NULL;
106   }
107
108   if (stage_task != NULL )
109   {
110     GNUNET_SCHEDULER_cancel (stage_task);
111     stage_task = NULL;
112   }
113
114   if (cc != NULL )
115   {
116     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
117     cc = NULL;
118   }
119
120   if (p1 != NULL )
121   {
122     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
123     p1 = NULL;
124   }
125   if (p2 != NULL )
126   {
127     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
128     p2 = NULL;
129   }
130 }
131
132
133 static void
134 end_badly (void *cls)
135 {
136   die_task = NULL;
137
138   if (timeout_task != NULL )
139   {
140     GNUNET_SCHEDULER_cancel (timeout_task);
141     timeout_task = NULL;
142   }
143
144   if (stage_task != NULL )
145   {
146     GNUNET_SCHEDULER_cancel (stage_task);
147     stage_task = NULL;
148   }
149
150   if (cc != NULL )
151   {
152     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
153     cc = NULL;
154   }
155   if (p1 != NULL )
156     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
157   if (p2 != NULL )
158     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
159
160   ok = GNUNET_SYSERR;
161 }
162
163 static void
164 testing_connect_cb (void *cls)
165 {
166   cc = NULL;
167   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
168
169   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
170       p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
171   GNUNET_free(p1_c);
172   connected = GNUNET_YES;
173   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
174 }
175
176
177 static void
178 connect_timeout (void *cls)
179 {
180   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
181              "Peers not connected, next stage\n");
182   timeout_task = NULL;
183   stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
184                                          NULL);
185 }
186
187
188 static int started;
189
190
191 static void
192 start_cb (void *cls)
193 {
194   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
195   started++;
196   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
197              "Peer %u (`%s') started\n",
198              p->no,
199              GNUNET_i2s_full (&p->id));
200
201   if (started != 2)
202     return;
203
204   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
205
206   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
207              "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
208              p1->no,
209              sender_c,
210              p2->no,
211              GNUNET_i2s (&p2->id));
212   GNUNET_free(sender_c);
213
214   cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
215                                                p2,
216                                                &testing_connect_cb,
217                                                NULL);
218
219 }
220
221
222 static int
223 check_blacklist_config (const char *cfg_file,
224                         struct GNUNET_PeerIdentity *peer,
225                         struct GNUNET_PeerIdentity *bl_peer)
226 {
227   struct GNUNET_CONFIGURATION_Handle *cfg;
228   char *section;
229   char *peer_str;
230   cfg = GNUNET_CONFIGURATION_create ();
231   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
232   {
233     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
234     GNUNET_CONFIGURATION_destroy (cfg);
235     return GNUNET_SYSERR;
236   }
237
238   peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
239   GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
240
241   if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
242   {
243     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
244         "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
245         cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
246     GNUNET_CONFIGURATION_destroy (cfg);
247     GNUNET_free (section);
248     GNUNET_free (peer_str);
249     return GNUNET_SYSERR;
250   }
251
252   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
253       "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
254       cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
255
256   GNUNET_CONFIGURATION_destroy (cfg);
257   GNUNET_free (section);
258   GNUNET_free (peer_str);
259   return GNUNET_OK;
260 }
261
262
263 static void
264 run_stage (void *cls)
265 {
266   stage_task = NULL;
267   if (NULL != die_task)
268     GNUNET_SCHEDULER_cancel (die_task);
269   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
270   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
271
272   if (0 == stage)
273   {
274     started = GNUNET_NO;
275     connected = GNUNET_NO;
276     if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
277     {
278       /* Try to connect peers successfully */
279       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
280                                                 "test_transport_blacklisting_cfg_peer1.conf",
281                                                 1,
282                                                 NULL,
283                                                 NULL,
284                                                 NULL,
285                                                 NULL,
286                                                 &start_cb,
287                                                 NULL);
288
289       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
290                                                 "test_transport_blacklisting_cfg_peer2.conf",
291                                                 2,
292                                                 NULL,
293                                                 NULL,
294                                                 NULL,
295                                                 NULL,
296                                                 &start_cb,
297                                                 NULL);
298     }
299     else if (0 == strcmp (test_name,
300                           "test_transport_blacklisting_outbound_bl_full"))
301     {
302       const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
303       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
304
305       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
306                                                 cfg_p1,
307                                                 1, NULL, NULL, NULL,
308                                                 NULL,
309                                                 &start_cb, NULL);
310       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
311                                                 cfg_p2, 2,
312                                                 NULL, NULL, NULL,
313                                                 NULL,
314                                                 &start_cb, NULL);
315
316       /* check if configuration contain correct blacklist entries */
317       if ( (GNUNET_SYSERR ==
318             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
319            (GNUNET_SYSERR ==
320             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
321       {
322         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
323         p1 = NULL;
324         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
325         p2 = NULL;
326         ok = 1;
327         GNUNET_SCHEDULER_add_now (&end, NULL );
328       }
329
330     }
331     else if (0
332         == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
333     {
334       const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
335       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
336
337       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
338                                                 cfg_p1,
339                                                 1,
340                                                 NULL,
341                                                 NULL,
342                                                 NULL,
343                                                 NULL,
344                                                 &start_cb,
345                                                 NULL);
346
347       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
348                                                 cfg_p2, 2,
349                                                 NULL,
350                                                 NULL,
351                                                 NULL,
352                                                 NULL,
353                                                 &start_cb,
354                                                 NULL);
355
356       /* check if configuration contain correct blacklist entries */
357       if ( (GNUNET_SYSERR ==
358             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
359            (GNUNET_SYSERR ==
360             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
361       {
362         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
363         p1 = NULL;
364         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
365         p2 = NULL;
366         ok = 1;
367         GNUNET_SCHEDULER_add_now (&end, NULL );
368       }
369     }
370     else if (0 == strcmp (test_name,
371                           "test_transport_blacklisting_inbound_bl_full"))
372     {
373       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
374       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
375
376       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
377                                                 cfg_p1, 1,
378                                                 NULL,
379                                                 NULL, NULL, NULL,
380                                                 &start_cb, NULL);
381
382       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
383                                                 cfg_p2, 2,
384                                                 NULL,
385                                                 NULL, NULL, NULL,
386                                                 &start_cb, NULL);
387
388       /* check if configuration contain correct blacklist entries */
389       if ( (GNUNET_SYSERR ==
390             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
391       {
392         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
393         p1 = NULL;
394         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
395         p2 = NULL;
396         ok = 1;
397         GNUNET_SCHEDULER_add_now (&end, NULL );
398       }
399     }
400     else if (0 == strcmp (test_name,
401                           "test_transport_blacklisting_inbound_bl_plugin"))
402     {
403       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
404       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
405
406       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
407                                                 cfg_p1, 1,
408                                                 NULL,
409                                                 NULL, NULL, NULL,
410                                                 &start_cb, NULL);
411
412       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
413                                                 cfg_p2, 2,
414                                                 NULL,
415                                                 NULL, NULL,
416                                                 NULL,
417                                                 &start_cb, NULL);
418
419       /* check if configuration contain correct blacklist entries */
420       if ( (GNUNET_SYSERR ==
421             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
422       {
423         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
424         p1 = NULL;
425         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
426         p2 = NULL;
427         ok = 1;
428         GNUNET_SCHEDULER_add_now (&end, NULL );
429       }
430
431     }
432     else if (0 == strcmp (test_name,
433                           "test_transport_blacklisting_multiple_plugins"))
434     {
435       const char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
436       const char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
437
438       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
439                                                 cfg_p1, 1,
440                                                 NULL,
441                                                 NULL, NULL, NULL,
442                                                 &start_cb, NULL);
443
444       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
445                                                 cfg_p2, 2,
446                                                 NULL,
447                                                 NULL, NULL, NULL,
448                                                 &start_cb, NULL);
449
450       /* check if configuration contain correct blacklist entries */
451       if ( (GNUNET_SYSERR ==
452             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
453            (GNUNET_SYSERR ==
454             check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
455       {
456         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
457         p1 = NULL;
458         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
459         p2 = NULL;
460         ok = 1;
461         GNUNET_SCHEDULER_add_now (&end, NULL);
462       }
463     }
464     else
465     {
466       GNUNET_break (0);
467       GNUNET_SCHEDULER_add_now (&end, NULL);
468     }
469
470     if ((NULL == p1) || (NULL == p2))
471     {
472       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
473       ok = 1;
474       GNUNET_SCHEDULER_add_now (&end, NULL);
475     }
476
477     timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
478                                                  &connect_timeout,
479                                                  NULL);
480     stage++;
481     return;
482   }
483
484   if (cc != NULL )
485   {
486     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
487     cc = NULL;
488   }
489
490   if (p1 != NULL )
491   {
492     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
493     p1 = NULL;
494   }
495   if (p2 != NULL )
496   {
497     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
498     p2 = NULL;
499   }
500
501   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
502       stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
503       (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
504
505   if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
506       || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
507   {
508     if ((GNUNET_NO != started) && (GNUNET_YES == connected))
509       ok = 0;
510     else
511     {
512       GNUNET_break(0);
513       ok = 1;
514     }
515   }
516   else
517   {
518     if ((GNUNET_NO != started) && (GNUNET_YES != connected))
519       ok = 0;
520     else
521     {
522       ok = 1;
523     }
524   }
525   GNUNET_SCHEDULER_add_now (&end, NULL );
526 }
527
528 static void
529 run(void *cls, char * const *args, const char *cfgfile,
530     const struct GNUNET_CONFIGURATION_Handle *cfg)
531 {
532   connected = GNUNET_NO;
533   stage = 0;
534   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
535   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
536 }
537
538 int
539 main(int argc, char *argv0[])
540 {
541   ok = 1;
542
543   test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
544
545   GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
546
547   static char * const argv[] =
548   { "date", "-c", "test_transport_api_data.conf", NULL };
549   static struct GNUNET_GETOPT_CommandLineOption options[] =
550   { GNUNET_GETOPT_OPTION_END };
551
552   tth = GNUNET_TRANSPORT_TESTING_init ();
553
554   GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
555       "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
556
557   GNUNET_TRANSPORT_TESTING_done (tth);
558
559   return ok;
560 }
561
562 /* end of transport_api_blacklisting.c */