cleaning up set handlers, eliminating 2nd level demultiplexing and improving use...
[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,
183              "Peers not connected, next stage\n");
184   timeout_task = NULL;
185   stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
186                                          NULL);
187 }
188
189
190 static int started;
191
192
193 static void
194 start_cb (void *cls)
195 {
196   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
197   started++;
198   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
199              "Peer %u (`%s') started\n",
200              p->no,
201              GNUNET_i2s_full (&p->id));
202
203   if (started != 2)
204     return;
205
206   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
207
208   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
209              "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
210              p1->no,
211              sender_c,
212              p2->no,
213              GNUNET_i2s (&p2->id));
214   GNUNET_free(sender_c);
215
216   cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
217                                                p2,
218                                                &testing_connect_cb,
219                                                NULL);
220
221 }
222
223
224 static int
225 check_blacklist_config (const char *cfg_file,
226                         struct GNUNET_PeerIdentity *peer,
227                         struct GNUNET_PeerIdentity *bl_peer)
228 {
229   struct GNUNET_CONFIGURATION_Handle *cfg;
230   char *section;
231   char *peer_str;
232   cfg = GNUNET_CONFIGURATION_create ();
233   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
234   {
235     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
236     GNUNET_CONFIGURATION_destroy (cfg);
237     return GNUNET_SYSERR;
238   }
239
240   peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
241   GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
242
243   if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
244   {
245     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
246         "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
247         cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
248     GNUNET_CONFIGURATION_destroy (cfg);
249     GNUNET_free (section);
250     GNUNET_free (peer_str);
251     return GNUNET_SYSERR;
252   }
253
254   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
255       "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
256       cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
257
258   GNUNET_CONFIGURATION_destroy (cfg);
259   GNUNET_free (section);
260   GNUNET_free (peer_str);
261   return GNUNET_OK;
262 }
263
264
265 static void
266 run_stage (void *cls)
267 {
268   stage_task = NULL;
269   if (NULL != die_task)
270     GNUNET_SCHEDULER_cancel (die_task);
271   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
272   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
273
274   if (0 == stage)
275   {
276     started = GNUNET_NO;
277     connected = GNUNET_NO;
278     if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
279     {
280       /* Try to connect peers successfully */
281       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
282                                                 "test_transport_blacklisting_cfg_peer1.conf",
283                                                 1,
284                                                 NULL,
285                                                 NULL,
286                                                 NULL,
287                                                 NULL,
288                                                 &start_cb,
289                                                 NULL);
290
291       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
292                                                 "test_transport_blacklisting_cfg_peer2.conf",
293                                                 2,
294                                                 NULL,
295                                                 NULL,
296                                                 NULL,
297                                                 NULL,
298                                                 &start_cb,
299                                                 NULL);
300     }
301     else if (0 == strcmp (test_name,
302                           "test_transport_blacklisting_outbound_bl_full"))
303     {
304       const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
305       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
306
307       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
308                                                 cfg_p1,
309                                                 1, NULL, NULL, NULL,
310                                                 NULL,
311                                                 &start_cb, NULL);
312       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
313                                                 cfg_p2, 2,
314                                                 NULL, NULL, NULL,
315                                                 NULL,
316                                                 &start_cb, NULL);
317
318       /* check if configuration contain correct blacklist entries */
319       if ( (GNUNET_SYSERR ==
320             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
321            (GNUNET_SYSERR ==
322             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
323       {
324         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
325         p1 = NULL;
326         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
327         p2 = NULL;
328         ok = 1;
329         GNUNET_SCHEDULER_add_now (&end, NULL );
330       }
331
332     }
333     else if (0
334         == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
335     {
336       const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
337       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
338
339       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
340                                                 cfg_p1,
341                                                 1,
342                                                 NULL,
343                                                 NULL,
344                                                 NULL,
345                                                 NULL,
346                                                 &start_cb,
347                                                 NULL);
348
349       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
350                                                 cfg_p2, 2,
351                                                 NULL,
352                                                 NULL,
353                                                 NULL,
354                                                 NULL,
355                                                 &start_cb,
356                                                 NULL);
357
358       /* check if configuration contain correct blacklist entries */
359       if ( (GNUNET_SYSERR ==
360             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
361            (GNUNET_SYSERR ==
362             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
363       {
364         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
365         p1 = NULL;
366         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
367         p2 = NULL;
368         ok = 1;
369         GNUNET_SCHEDULER_add_now (&end, NULL );
370       }
371     }
372     else if (0 == strcmp (test_name,
373                           "test_transport_blacklisting_inbound_bl_full"))
374     {
375       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
376       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
377
378       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
379                                                 cfg_p1, 1,
380                                                 NULL,
381                                                 NULL, NULL, NULL,
382                                                 &start_cb, NULL);
383
384       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
385                                                 cfg_p2, 2,
386                                                 NULL,
387                                                 NULL, NULL, NULL,
388                                                 &start_cb, NULL);
389
390       /* check if configuration contain correct blacklist entries */
391       if ( (GNUNET_SYSERR ==
392             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
393       {
394         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
395         p1 = NULL;
396         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
397         p2 = NULL;
398         ok = 1;
399         GNUNET_SCHEDULER_add_now (&end, NULL );
400       }
401     }
402     else if (0 == strcmp (test_name,
403                           "test_transport_blacklisting_inbound_bl_plugin"))
404     {
405       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
406       const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
407
408       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
409                                                 cfg_p1, 1,
410                                                 NULL,
411                                                 NULL, NULL, NULL,
412                                                 &start_cb, NULL);
413
414       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
415                                                 cfg_p2, 2,
416                                                 NULL,
417                                                 NULL, NULL,
418                                                 NULL,
419                                                 &start_cb, NULL);
420
421       /* check if configuration contain correct blacklist entries */
422       if ( (GNUNET_SYSERR ==
423             check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
424       {
425         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
426         p1 = NULL;
427         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
428         p2 = NULL;
429         ok = 1;
430         GNUNET_SCHEDULER_add_now (&end, NULL );
431       }
432
433     }
434     else if (0 == strcmp (test_name,
435                           "test_transport_blacklisting_multiple_plugins"))
436     {
437       const char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
438       const char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
439
440       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
441                                                 cfg_p1, 1,
442                                                 NULL,
443                                                 NULL, NULL, NULL,
444                                                 &start_cb, NULL);
445
446       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
447                                                 cfg_p2, 2,
448                                                 NULL,
449                                                 NULL, NULL, NULL,
450                                                 &start_cb, NULL);
451
452       /* check if configuration contain correct blacklist entries */
453       if ( (GNUNET_SYSERR ==
454             check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
455            (GNUNET_SYSERR ==
456             check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
457       {
458         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
459         p1 = NULL;
460         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
461         p2 = NULL;
462         ok = 1;
463         GNUNET_SCHEDULER_add_now (&end, NULL);
464       }
465     }
466     else
467     {
468       GNUNET_break (0);
469       GNUNET_SCHEDULER_add_now (&end, NULL);
470     }
471
472     if ((NULL == p1) || (NULL == p2))
473     {
474       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
475       ok = 1;
476       GNUNET_SCHEDULER_add_now (&end, NULL);
477     }
478
479     timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
480                                                  &connect_timeout,
481                                                  NULL);
482     stage++;
483     return;
484   }
485
486   if (cc != NULL )
487   {
488     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
489     cc = NULL;
490   }
491
492   if (p1 != NULL )
493   {
494     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
495     p1 = NULL;
496   }
497   if (p2 != NULL )
498   {
499     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
500     p2 = NULL;
501   }
502
503   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
504       stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
505       (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
506
507   if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
508       || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
509   {
510     if ((GNUNET_NO != started) && (GNUNET_YES == connected))
511       ok = 0;
512     else
513     {
514       GNUNET_break(0);
515       ok = 1;
516     }
517   }
518   else
519   {
520     if ((GNUNET_NO != started) && (GNUNET_YES != connected))
521       ok = 0;
522     else
523     {
524       ok = 1;
525     }
526   }
527   GNUNET_SCHEDULER_add_now (&end, NULL );
528 }
529
530 static void
531 run(void *cls, char * const *args, const char *cfgfile,
532     const struct GNUNET_CONFIGURATION_Handle *cfg)
533 {
534   connected = GNUNET_NO;
535   stage = 0;
536   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
537   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
538 }
539
540 int
541 main(int argc, char *argv0[])
542 {
543   ok = 1;
544
545   test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
546
547   GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
548
549   static char * const argv[] =
550   { "date", "-c", "test_transport_api_data.conf", NULL };
551   static struct GNUNET_GETOPT_CommandLineOption options[] =
552   { GNUNET_GETOPT_OPTION_END };
553
554   tth = GNUNET_TRANSPORT_TESTING_init ();
555
556   GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
557       "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
558
559   GNUNET_TRANSPORT_TESTING_done (tth);
560
561   return ok;
562 }
563
564 /* end of transport_api_blacklisting.c */