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.
17 * @file transport/transport_api_blacklisting.c
18 * @brief test for the blacklisting with blacklistings defined in cfg
20 * this file contains multiple tests:
22 * test_transport_blacklisting_no_bl:
23 * no blacklisting entries
24 * peers are expected to connect
25 * test_transport_blacklisting_outbound_bl_full:
26 * both peers contain bl entries for full peer
27 * test is expected to not connect
28 * test_transport_blacklisting_outbound_bl_plugin:
29 * both peers contain bl entries for plugin
30 * test is expected to not connect
31 * test_transport_blacklisting_inbound_bl_plugin:
32 * peer 1 contains no bl entries
33 * peer 2 contain bl entries for full peer
34 * test is expected to not connect
35 * test_transport_blacklisting_inbound_bl_full:
36 * peer 1 contains no bl entries
37 * peer 2 contain bl entries for plugin
38 * test is expected to not connect
39 * test_transport_blacklisting_multiple_plugins:
40 * both peers contain bl entries for plugin
41 * test is expected to connect with not bl'ed plugin
43 * @author Matthias Wachs
47 #include "gnunet_transport_service.h"
48 #include "transport-testing.h"
52 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
54 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
56 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
58 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
61 * How long until we give up on transmitting the message?
63 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
65 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
71 static struct GNUNET_SCHEDULER_Task * die_task;
73 static struct GNUNET_SCHEDULER_Task * timeout_task;
75 static struct GNUNET_SCHEDULER_Task * stage_task;
78 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
80 #define OKPP do { ok++; } while (0)
91 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
93 if (die_task != NULL )
95 GNUNET_SCHEDULER_cancel (die_task);
99 if (timeout_task != NULL )
101 GNUNET_SCHEDULER_cancel (timeout_task);
105 if (stage_task != NULL )
107 GNUNET_SCHEDULER_cancel (stage_task);
113 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
119 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
124 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
131 end_badly (void *cls)
135 if (timeout_task != NULL )
137 GNUNET_SCHEDULER_cancel (timeout_task);
141 if (stage_task != NULL )
143 GNUNET_SCHEDULER_cancel (stage_task);
149 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
153 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
155 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
161 testing_connect_cb (void *cls)
164 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
166 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
167 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
169 connected = GNUNET_YES;
170 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
175 connect_timeout (void *cls)
177 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
178 "Peers not connected, next stage\n");
180 stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
191 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
193 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
194 "Peer %u (`%s') started\n",
196 GNUNET_i2s_full (&p->id));
201 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
203 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
204 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
208 GNUNET_i2s (&p2->id));
209 GNUNET_free(sender_c);
211 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
220 check_blacklist_config (const char *cfg_file,
221 struct GNUNET_PeerIdentity *peer,
222 struct GNUNET_PeerIdentity *bl_peer)
224 struct GNUNET_CONFIGURATION_Handle *cfg;
227 cfg = GNUNET_CONFIGURATION_create ();
228 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
230 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
231 GNUNET_CONFIGURATION_destroy (cfg);
232 return GNUNET_SYSERR;
235 peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
236 GNUNET_asprintf (§ion, "transport-blacklist-%s", peer_str);
238 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
240 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
241 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
242 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
243 GNUNET_CONFIGURATION_destroy (cfg);
244 GNUNET_free (section);
245 GNUNET_free (peer_str);
246 return GNUNET_SYSERR;
249 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250 "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
251 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
253 GNUNET_CONFIGURATION_destroy (cfg);
254 GNUNET_free (section);
255 GNUNET_free (peer_str);
261 run_stage (void *cls)
264 if (NULL != die_task)
265 GNUNET_SCHEDULER_cancel (die_task);
266 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
267 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
272 connected = GNUNET_NO;
273 if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
275 /* Try to connect peers successfully */
276 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
277 "test_transport_blacklisting_cfg_peer1.conf",
286 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
287 "test_transport_blacklisting_cfg_peer2.conf",
296 else if (0 == strcmp (test_name,
297 "test_transport_blacklisting_outbound_bl_full"))
299 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
300 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
302 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
307 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
313 /* check if configuration contain correct blacklist entries */
314 if ( (GNUNET_SYSERR ==
315 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
317 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
319 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
321 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
324 GNUNET_SCHEDULER_add_now (&end, NULL );
329 == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
331 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
332 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
334 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
344 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
353 /* check if configuration contain correct blacklist entries */
354 if ( (GNUNET_SYSERR ==
355 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
357 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
359 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
361 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
364 GNUNET_SCHEDULER_add_now (&end, NULL );
367 else if (0 == strcmp (test_name,
368 "test_transport_blacklisting_inbound_bl_full"))
370 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
371 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
373 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
379 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
385 /* check if configuration contain correct blacklist entries */
386 if ( (GNUNET_SYSERR ==
387 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
389 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
391 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
394 GNUNET_SCHEDULER_add_now (&end, NULL );
397 else if (0 == strcmp (test_name,
398 "test_transport_blacklisting_inbound_bl_plugin"))
400 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
401 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
403 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
409 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
416 /* check if configuration contain correct blacklist entries */
417 if ( (GNUNET_SYSERR ==
418 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
420 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
422 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
425 GNUNET_SCHEDULER_add_now (&end, NULL );
429 else if (0 == strcmp (test_name,
430 "test_transport_blacklisting_multiple_plugins"))
432 const char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
433 const char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
435 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
441 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
447 /* check if configuration contain correct blacklist entries */
448 if ( (GNUNET_SYSERR ==
449 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
451 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
453 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
455 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
458 GNUNET_SCHEDULER_add_now (&end, NULL);
464 GNUNET_SCHEDULER_add_now (&end, NULL);
467 if ((NULL == p1) || (NULL == p2))
469 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
471 GNUNET_SCHEDULER_add_now (&end, NULL);
474 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
483 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
489 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
494 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
498 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
499 stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
500 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
502 if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
503 || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
505 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
515 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
522 GNUNET_SCHEDULER_add_now (&end, NULL );
526 run(void *cls, char * const *args, const char *cfgfile,
527 const struct GNUNET_CONFIGURATION_Handle *cfg)
529 connected = GNUNET_NO;
531 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
532 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
536 main(int argc, char *argv0[])
540 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
542 GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
544 static char * const argv[] =
545 { "date", "-c", "test_transport_api_data.conf", NULL };
546 static struct GNUNET_GETOPT_CommandLineOption options[] =
547 { GNUNET_GETOPT_OPTION_END };
549 tth = GNUNET_TRANSPORT_TESTING_init ();
551 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
552 "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
554 GNUNET_TRANSPORT_TESTING_done (tth);
559 /* end of transport_api_blacklisting.c */