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
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.
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.
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.
22 * @file transport/transport_api_blacklisting.c
23 * @brief test for the blacklisting with blacklistings defined in cfg
25 * this file contains multiple tests:
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
48 * @author Matthias Wachs
52 #include "gnunet_transport_service.h"
53 #include "transport-testing.h"
57 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
59 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
61 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
63 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
66 * How long until we give up on transmitting the message?
68 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
70 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
76 static struct GNUNET_SCHEDULER_Task * die_task;
78 static struct GNUNET_SCHEDULER_Task * timeout_task;
80 static struct GNUNET_SCHEDULER_Task * stage_task;
83 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
85 #define OKPP do { ok++; } while (0)
96 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
98 if (die_task != NULL )
100 GNUNET_SCHEDULER_cancel (die_task);
104 if (timeout_task != NULL )
106 GNUNET_SCHEDULER_cancel (timeout_task);
110 if (stage_task != NULL )
112 GNUNET_SCHEDULER_cancel (stage_task);
118 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
124 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
129 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
136 end_badly (void *cls)
140 if (timeout_task != NULL )
142 GNUNET_SCHEDULER_cancel (timeout_task);
146 if (stage_task != NULL )
148 GNUNET_SCHEDULER_cancel (stage_task);
154 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
158 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
160 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
166 testing_connect_cb (void *cls)
169 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
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));
174 connected = GNUNET_YES;
175 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
180 connect_timeout(void *cls)
182 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers not connected, next stage\n");
184 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
192 start_cb(struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
196 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
197 GNUNET_i2s_full (&p->id));
202 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
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);
209 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
217 check_blacklist_config (const char *cfg_file,
218 struct GNUNET_PeerIdentity *peer,
219 struct GNUNET_PeerIdentity *bl_peer)
221 struct GNUNET_CONFIGURATION_Handle *cfg;
224 cfg = GNUNET_CONFIGURATION_create ();
225 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
227 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
228 GNUNET_CONFIGURATION_destroy (cfg);
229 return GNUNET_SYSERR;
232 peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
233 GNUNET_asprintf (§ion, "transport-blacklist-%s", peer_str);
235 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
237 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
238 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
239 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
240 GNUNET_CONFIGURATION_destroy (cfg);
241 GNUNET_free (section);
242 GNUNET_free (peer_str);
243 return GNUNET_SYSERR;
246 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
247 "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
248 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
250 GNUNET_CONFIGURATION_destroy (cfg);
251 GNUNET_free (section);
252 GNUNET_free (peer_str);
258 run_stage (void *cls)
261 if (NULL != die_task)
262 GNUNET_SCHEDULER_cancel (die_task);
263 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
264 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
269 connected = GNUNET_NO;
270 if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
272 /* Try to connect peers successfully */
273 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
274 "test_transport_blacklisting_cfg_peer1.conf",
283 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
284 "test_transport_blacklisting_cfg_peer2.conf",
293 else if (0 == strcmp (test_name,
294 "test_transport_blacklisting_outbound_bl_full"))
296 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
297 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
299 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
304 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
310 /* check if configuration contain correct blacklist entries */
311 if ( (GNUNET_SYSERR ==
312 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
314 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
316 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
318 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
321 GNUNET_SCHEDULER_add_now (&end, NULL );
326 == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
328 const char *cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
329 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
331 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
341 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
350 /* check if configuration contain correct blacklist entries */
351 if ( (GNUNET_SYSERR ==
352 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
354 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
356 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
358 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
361 GNUNET_SCHEDULER_add_now (&end, NULL );
364 else if (0 == strcmp (test_name,
365 "test_transport_blacklisting_inbound_bl_full"))
367 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
368 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
370 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
376 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
382 /* check if configuration contain correct blacklist entries */
383 if ( (GNUNET_SYSERR ==
384 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
386 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
388 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
391 GNUNET_SCHEDULER_add_now (&end, NULL );
394 else if (0 == strcmp (test_name,
395 "test_transport_blacklisting_inbound_bl_plugin"))
397 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
398 const char *cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
400 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
406 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
413 /* check if configuration contain correct blacklist entries */
414 if ( (GNUNET_SYSERR ==
415 check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
417 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
419 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
422 GNUNET_SCHEDULER_add_now (&end, NULL );
426 else if (0 == strcmp (test_name,
427 "test_transport_blacklisting_multiple_plugins"))
429 const char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
430 const char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
432 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
438 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
444 /* check if configuration contain correct blacklist entries */
445 if ( (GNUNET_SYSERR ==
446 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
448 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
450 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
452 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
455 GNUNET_SCHEDULER_add_now (&end, NULL);
461 GNUNET_SCHEDULER_add_now (&end, NULL);
464 if ((NULL == p1) || (NULL == p2))
466 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
468 GNUNET_SCHEDULER_add_now (&end, NULL);
471 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
480 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
486 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
491 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
495 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
496 stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
497 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
499 if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
500 || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
502 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
512 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
519 GNUNET_SCHEDULER_add_now (&end, NULL );
523 run(void *cls, char * const *args, const char *cfgfile,
524 const struct GNUNET_CONFIGURATION_Handle *cfg)
526 connected = GNUNET_NO;
528 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
529 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
533 main(int argc, char *argv0[])
537 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
539 GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
541 static char * const argv[] =
542 { "date", "-c", "test_transport_api_data.conf", NULL };
543 static struct GNUNET_GETOPT_CommandLineOption options[] =
544 { GNUNET_GETOPT_OPTION_END };
546 tth = GNUNET_TRANSPORT_TESTING_init ();
548 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
549 "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
551 GNUNET_TRANSPORT_TESTING_done (tth);
556 /* end of transport_api_blacklisting.c */