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