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