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