-do not leave PPT timeout tasks behind
[oweals/gnunet.git] / src / transport / test_transport_blacklisting.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 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., 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 PeerContext *p1;
58
59 struct PeerContext *p2;
60
61 static 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 static void
89 run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
90
91 static void
92 end(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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 (tth, cc);
117     cc = NULL;
118   }
119
120   if (p1 != NULL )
121   {
122     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
123     p1 = NULL;
124   }
125   if (p2 != NULL )
126   {
127     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
128     p2 = NULL;
129   }
130 }
131
132 static void
133 end_badly(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134 {
135   die_task = NULL;
136
137   if (timeout_task != NULL )
138   {
139     GNUNET_SCHEDULER_cancel (timeout_task);
140     timeout_task = NULL;
141   }
142
143   if (stage_task != NULL )
144   {
145     GNUNET_SCHEDULER_cancel (stage_task);
146     stage_task = NULL;
147   }
148
149   if (cc != NULL )
150   {
151     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
152     cc = NULL;
153   }
154   if (p1 != NULL )
155     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
156   if (p2 != NULL )
157     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
158
159   ok = GNUNET_SYSERR;
160 }
161
162 static void
163 testing_connect_cb(struct PeerContext *p1, struct PeerContext *p2, void *cls)
164 {
165   cc = NULL;
166   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
167
168   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
169       p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
170   GNUNET_free(p1_c);
171   connected = GNUNET_YES;
172   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
173 }
174
175 static void
176 connect_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers not connected, next stage\n");
179   timeout_task = NULL;
180   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
181 }
182
183 static int started;
184
185 void
186 start_cb(struct PeerContext *p, void *cls)
187 {
188
189   started++;
190   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
191       GNUNET_i2s_full (&p->id));
192
193   if (started != 2)
194     return;
195
196   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
197
198   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
199       "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", p1->no,
200       sender_c, p2->no, GNUNET_i2s (&p2->id));
201   GNUNET_free(sender_c);
202
203   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
204       NULL );
205
206 }
207
208 static int check_blacklist_config (char *cfg_file,
209     struct GNUNET_PeerIdentity *peer, struct GNUNET_PeerIdentity *bl_peer)
210 {
211   struct GNUNET_CONFIGURATION_Handle *cfg;
212   char *section;
213   char *peer_str;
214   cfg = GNUNET_CONFIGURATION_create ();
215   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
216   {
217     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
218     GNUNET_CONFIGURATION_destroy (cfg);
219     return GNUNET_SYSERR;
220   }
221
222   peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
223   GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
224
225   if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
226   {
227     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
228         "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
229         cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
230     GNUNET_CONFIGURATION_destroy (cfg);
231     GNUNET_free (section);
232     GNUNET_free (peer_str);
233     return GNUNET_SYSERR;
234   }
235
236   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
237       "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
238       cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
239
240   GNUNET_CONFIGURATION_destroy (cfg);
241   GNUNET_free (section);
242   GNUNET_free (peer_str);
243   return GNUNET_OK;
244 }
245
246 static void
247 run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 {
249   stage_task = NULL;
250   if (NULL != die_task)
251     GNUNET_SCHEDULER_cancel (die_task);
252   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
253   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
254
255   if (0 == stage)
256   {
257     started = GNUNET_NO;
258     connected = GNUNET_NO;
259     if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
260     {
261       /* Try to connect peers successfully */
262       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
263           "test_transport_blacklisting_cfg_peer1.conf", 1, NULL, NULL, NULL,
264           &start_cb, NULL );
265
266       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
267           "test_transport_blacklisting_cfg_peer2.conf", 2, NULL, NULL, NULL,
268           &start_cb, NULL );
269     }
270     else if (0
271         == strcmp (test_name, "test_transport_blacklisting_outbound_bl_full"))
272     {
273       char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
274       char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
275       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
276           cfg_p1 , 1, NULL, NULL, NULL, &start_cb, NULL );
277
278       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
279           cfg_p2, 2, NULL, NULL, NULL,
280           &start_cb, NULL );
281
282       /* check if configuration contain correct blacklist entries */
283       if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
284           (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
285       {
286         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
287         p1 = NULL;
288         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
289         p2 = NULL;
290         ok = 1;
291         GNUNET_SCHEDULER_add_now (&end, NULL );
292       }
293
294     }
295     else if (0
296         == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
297     {
298       char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
299       char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
300
301       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
302           cfg_p1, 1, NULL,
303           NULL, NULL, &start_cb, NULL );
304
305       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
306           cfg_p2, 2, NULL, NULL, NULL,
307           &start_cb, NULL );
308
309       /* check if configuration contain correct blacklist entries */
310       if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
311           (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
312       {
313         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
314         p1 = NULL;
315         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
316         p2 = NULL;
317         ok = 1;
318         GNUNET_SCHEDULER_add_now (&end, NULL );
319       }
320     }
321     else if (0
322         == strcmp (test_name, "test_transport_blacklisting_inbound_bl_full"))
323     {
324       char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
325       char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
326
327       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
328           cfg_p1, 1, NULL, NULL, NULL,
329           &start_cb, NULL );
330
331       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
332           cfg_p2, 2, NULL, NULL,
333           NULL, &start_cb, NULL );
334
335       /* check if configuration contain correct blacklist entries */
336       if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
337       {
338         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
339         p1 = NULL;
340         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
341         p2 = NULL;
342         ok = 1;
343         GNUNET_SCHEDULER_add_now (&end, NULL );
344       }
345     }
346     else if (0
347         == strcmp (test_name, "test_transport_blacklisting_inbound_bl_plugin"))
348     {
349       char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
350       char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
351
352       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
353           cfg_p1, 1, NULL, NULL, NULL,
354           &start_cb, NULL );
355
356       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
357           cfg_p2, 2, NULL, NULL,
358           NULL, &start_cb, NULL );
359
360       /* check if configuration contain correct blacklist entries */
361       if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
362       {
363         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
364         p1 = NULL;
365         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
366         p2 = NULL;
367         ok = 1;
368         GNUNET_SCHEDULER_add_now (&end, NULL );
369       }
370
371     }
372     else if (0
373         == strcmp (test_name, "test_transport_blacklisting_multiple_plugins"))
374     {
375       char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
376       char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
377
378       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
379           cfg_p1, 1,
380           NULL, NULL, NULL, &start_cb, NULL );
381
382       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
383           cfg_p2, 2,
384           NULL, NULL, NULL, &start_cb, NULL );
385
386       /* check if configuration contain correct blacklist entries */
387       if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
388           (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
389       {
390         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
391         p1 = NULL;
392         GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
393         p2 = NULL;
394         ok = 1;
395         GNUNET_SCHEDULER_add_now (&end, NULL );
396       }
397     }
398     else
399     {
400       GNUNET_break (0);
401       GNUNET_SCHEDULER_add_now (&end, NULL );
402     }
403
404     if ((NULL == p1) || (NULL == p2))
405     {
406       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
407       ok = 1;
408       GNUNET_SCHEDULER_add_now (&end, NULL );
409     }
410
411     timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
412         &connect_timeout, NULL );
413     stage++;
414     return;
415   }
416
417   if (cc != NULL )
418   {
419     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
420     cc = NULL;
421   }
422
423   if (p1 != NULL )
424   {
425     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
426     p1 = NULL;
427   }
428   if (p2 != NULL )
429   {
430     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
431     p2 = NULL;
432   }
433
434   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
435       stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
436       (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
437
438   if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
439       || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
440   {
441     if ((GNUNET_NO != started) && (GNUNET_YES == connected))
442       ok = 0;
443     else
444     {
445       GNUNET_break(0);
446       ok = 1;
447     }
448   }
449   else
450   {
451     if ((GNUNET_NO != started) && (GNUNET_YES != connected))
452       ok = 0;
453     else
454     {
455       ok = 1;
456     }
457   }
458   GNUNET_SCHEDULER_add_now (&end, NULL );
459 }
460
461 static void
462 run(void *cls, char * const *args, const char *cfgfile,
463     const struct GNUNET_CONFIGURATION_Handle *cfg)
464 {
465   connected = GNUNET_NO;
466   stage = 0;
467   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
468   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
469 }
470
471 int
472 main(int argc, char *argv0[])
473 {
474   ok = 1;
475
476   GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0], &test_name);
477
478   GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
479
480   static char * const argv[] =
481   { "date", "-c", "test_transport_api_data.conf", NULL };
482   static struct GNUNET_GETOPT_CommandLineOption options[] =
483   { GNUNET_GETOPT_OPTION_END };
484
485   tth = GNUNET_TRANSPORT_TESTING_init ();
486
487   GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
488       "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
489
490   GNUNET_TRANSPORT_TESTING_done (tth);
491
492   return ok;
493 }
494
495 /* end of transport_api_blacklisting.c */