2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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.
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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file transport/transport_api_blacklisting.c
21 * @brief test for the blacklisting with blacklistings defined in cfg
23 * this file contains multiple tests:
25 * test_transport_blacklisting_no_bl:
26 * no blacklisting entries
27 * peers are expected to connect
28 * test_transport_blacklisting_outbound_bl_full:
29 * both peers contain bl entries for full peer
30 * test is expected to not connect
31 * test_transport_blacklisting_outbound_bl_plugin:
32 * both peers contain bl entries for plugin
33 * test is expected to not connect
34 * test_transport_blacklisting_inbound_bl_plugin:
35 * peer 1 contains no bl entries
36 * peer 2 contain bl entries for full peer
37 * test is expected to not connect
38 * test_transport_blacklisting_inbound_bl_full:
39 * peer 1 contains no bl entries
40 * peer 2 contain bl entries for plugin
41 * test is expected to not connect
42 * test_transport_blacklisting_multiple_plugins:
43 * both peers contain bl entries for plugin
44 * test is expected to connect with not bl'ed plugin
46 * @author Matthias Wachs
50 #include "gnunet_transport_service.h"
51 #include "transport-testing.h"
55 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
57 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
59 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
61 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
64 * How long until we give up on transmitting the message?
66 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
68 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
74 static struct GNUNET_SCHEDULER_Task * die_task;
76 static struct GNUNET_SCHEDULER_Task * timeout_task;
78 static struct GNUNET_SCHEDULER_Task * stage_task;
81 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
83 #define OKPP do { ok++; } while (0)
94 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
96 if (die_task != NULL )
98 GNUNET_SCHEDULER_cancel (die_task);
102 if (timeout_task != NULL )
104 GNUNET_SCHEDULER_cancel (timeout_task);
108 if (stage_task != NULL )
110 GNUNET_SCHEDULER_cancel (stage_task);
116 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
122 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
127 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
134 end_badly (void *cls)
138 if (timeout_task != NULL )
140 GNUNET_SCHEDULER_cancel (timeout_task);
144 if (stage_task != NULL )
146 GNUNET_SCHEDULER_cancel (stage_task);
152 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
156 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
158 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
164 testing_connect_cb (void *cls)
167 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
169 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
170 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
172 connected = GNUNET_YES;
173 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
178 connect_timeout (void *cls)
180 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
181 "Peers not connected, next stage\n");
183 stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
194 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
196 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
197 "Peer %u (`%s') started\n",
199 GNUNET_i2s_full (&p->id));
204 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
207 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
211 GNUNET_i2s (&p2->id));
212 GNUNET_free(sender_c);
214 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
223 check_blacklist_config (const char *cfg_file,
224 struct GNUNET_PeerIdentity *peer,
225 struct GNUNET_PeerIdentity *bl_peer)
227 struct GNUNET_CONFIGURATION_Handle *cfg;
230 cfg = GNUNET_CONFIGURATION_create ();
231 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
233 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
234 GNUNET_CONFIGURATION_destroy (cfg);
235 return GNUNET_SYSERR;
238 peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
239 GNUNET_asprintf (§ion, "transport-blacklist-%s", peer_str);
241 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
243 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
244 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
245 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
246 GNUNET_CONFIGURATION_destroy (cfg);
247 GNUNET_free (section);
248 GNUNET_free (peer_str);
249 return GNUNET_SYSERR;
252 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
253 "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
254 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
256 GNUNET_CONFIGURATION_destroy (cfg);
257 GNUNET_free (section);
258 GNUNET_free (peer_str);
264 run_stage (void *cls)
267 if (NULL != die_task)
268 GNUNET_SCHEDULER_cancel (die_task);
269 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
270 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
275 connected = GNUNET_NO;
276 if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
278 /* Try to connect peers successfully */
279 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
280 "test_transport_blacklisting_cfg_peer1.conf",
289 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
290 "test_transport_blacklisting_cfg_peer2.conf",
299 else if (0 == strcmp (test_name,
300 "test_transport_blacklisting_outbound_bl_full"))
302 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
303 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
305 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
310 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
316 /* check if configuration contain correct blacklist entries */
317 if ( (GNUNET_SYSERR ==
318 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
320 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
322 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
324 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
327 GNUNET_SCHEDULER_add_now (&end, NULL );
332 == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
334 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
335 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
337 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
347 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
356 /* check if configuration contain correct blacklist entries */
357 if ( (GNUNET_SYSERR ==
358 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
360 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
362 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
364 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
367 GNUNET_SCHEDULER_add_now (&end, NULL );
370 else if (0 == strcmp (test_name,
371 "test_transport_blacklisting_inbound_bl_full"))
373 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
374 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
376 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
382 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
388 /* check if configuration contain correct blacklist entries */
389 if ( (GNUNET_SYSERR ==
390 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
392 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
394 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
397 GNUNET_SCHEDULER_add_now (&end, NULL );
400 else if (0 == strcmp (test_name,
401 "test_transport_blacklisting_inbound_bl_plugin"))
403 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
404 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
406 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
412 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
419 /* check if configuration contain correct blacklist entries */
420 if ( (GNUNET_SYSERR ==
421 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
423 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
425 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
428 GNUNET_SCHEDULER_add_now (&end, NULL );
432 else if (0 == strcmp (test_name,
433 "test_transport_blacklisting_multiple_plugins"))
435 const char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
436 const char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
438 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
444 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
450 /* check if configuration contain correct blacklist entries */
451 if ( (GNUNET_SYSERR ==
452 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
454 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
456 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
458 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
461 GNUNET_SCHEDULER_add_now (&end, NULL);
467 GNUNET_SCHEDULER_add_now (&end, NULL);
470 if ((NULL == p1) || (NULL == p2))
472 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
474 GNUNET_SCHEDULER_add_now (&end, NULL);
477 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
486 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
492 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
497 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
501 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
502 stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
503 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
505 if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
506 || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
508 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
518 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
525 GNUNET_SCHEDULER_add_now (&end, NULL );
529 run(void *cls, char * const *args, const char *cfgfile,
530 const struct GNUNET_CONFIGURATION_Handle *cfg)
532 connected = GNUNET_NO;
534 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
535 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
539 main(int argc, char *argv0[])
543 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
545 GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
547 static char * const argv[] =
548 { "date", "-c", "test_transport_api_data.conf", NULL };
549 static struct GNUNET_GETOPT_CommandLineOption options[] =
550 { GNUNET_GETOPT_OPTION_END };
552 tth = GNUNET_TRANSPORT_TESTING_init ();
554 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
555 "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
557 GNUNET_TRANSPORT_TESTING_done (tth);
562 /* end of transport_api_blacklisting.c */