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