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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, \
77 static struct GNUNET_SCHEDULER_Task *die_task;
79 static struct GNUNET_SCHEDULER_Task *timeout_task;
81 static struct GNUNET_SCHEDULER_Task *stage_task;
84 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, \
85 __FILE__, __LINE__); } while (0)
87 #define OKPP do { ok++; } while (0)
92 run_stage (void *cls);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
100 if (die_task != NULL)
102 GNUNET_SCHEDULER_cancel (die_task);
106 if (timeout_task != NULL)
108 GNUNET_SCHEDULER_cancel (timeout_task);
112 if (stage_task != NULL)
114 GNUNET_SCHEDULER_cancel (stage_task);
120 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
126 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
131 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
138 end_badly (void *cls)
142 if (timeout_task != NULL)
144 GNUNET_SCHEDULER_cancel (timeout_task);
148 if (stage_task != NULL)
150 GNUNET_SCHEDULER_cancel (stage_task);
156 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
160 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
162 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
169 testing_connect_cb (void *cls)
172 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
174 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
175 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
177 connected = GNUNET_YES;
178 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
183 connect_timeout (void *cls)
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186 "Peers not connected, next stage\n");
188 stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
199 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203 "Peer %u (`%s') started\n",
205 GNUNET_i2s_full (&p->id));
210 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
217 GNUNET_i2s (&p2->id));
218 GNUNET_free (sender_c);
220 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
228 check_blacklist_config (const char *cfg_file,
229 struct GNUNET_PeerIdentity *peer,
230 struct GNUNET_PeerIdentity *bl_peer)
232 struct GNUNET_CONFIGURATION_Handle *cfg;
236 cfg = GNUNET_CONFIGURATION_create ();
237 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n",
241 GNUNET_CONFIGURATION_destroy (cfg);
242 return GNUNET_SYSERR;
245 peer_str = GNUNET_strdup (GNUNET_i2s_full (peer));
246 GNUNET_asprintf (§ion, "transport-blacklist-%s", peer_str);
248 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section,
249 GNUNET_i2s_full (bl_peer)))
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
253 cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
254 GNUNET_CONFIGURATION_destroy (cfg);
255 GNUNET_free (section);
256 GNUNET_free (peer_str);
257 return GNUNET_SYSERR;
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261 "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
262 cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
264 GNUNET_CONFIGURATION_destroy (cfg);
265 GNUNET_free (section);
266 GNUNET_free (peer_str);
272 run_stage (void *cls)
275 if (NULL != die_task)
276 GNUNET_SCHEDULER_cancel (die_task);
277 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
283 connected = GNUNET_NO;
284 if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
286 /* Try to connect peers successfully */
287 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
288 "test_transport_blacklisting_cfg_peer1.conf",
297 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
298 "test_transport_blacklisting_cfg_peer2.conf",
307 else if (0 == strcmp (test_name,
308 "test_transport_blacklisting_outbound_bl_full"))
311 "test_transport_blacklisting_cfg_blp_peer1_full.conf";
313 "test_transport_blacklisting_cfg_blp_peer2_full.conf";
315 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
320 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
326 /* check if configuration contain correct blacklist entries */
327 if ((GNUNET_SYSERR ==
328 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
330 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
332 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
334 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
337 GNUNET_SCHEDULER_add_now (&end, NULL);
341 == strcmp (test_name,
342 "test_transport_blacklisting_outbound_bl_plugin"))
345 "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
347 "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
349 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
359 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
368 /* check if configuration contain correct blacklist entries */
369 if ((GNUNET_SYSERR ==
370 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
372 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
374 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
376 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
379 GNUNET_SCHEDULER_add_now (&end, NULL);
382 else if (0 == strcmp (test_name,
383 "test_transport_blacklisting_inbound_bl_full"))
385 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
387 "test_transport_blacklisting_cfg_blp_peer2_full.conf";
389 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
395 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
401 /* check if configuration contain correct blacklist entries */
402 if ((GNUNET_SYSERR ==
403 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
405 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
407 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
410 GNUNET_SCHEDULER_add_now (&end, NULL);
413 else if (0 == strcmp (test_name,
414 "test_transport_blacklisting_inbound_bl_plugin"))
416 const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
418 "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
420 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
426 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
433 /* check if configuration contain correct blacklist entries */
434 if ((GNUNET_SYSERR ==
435 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
437 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
439 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
442 GNUNET_SCHEDULER_add_now (&end, NULL);
445 else if (0 == strcmp (test_name,
446 "test_transport_blacklisting_multiple_plugins"))
449 "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
451 "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
453 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
459 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
465 /* check if configuration contain correct blacklist entries */
466 if ((GNUNET_SYSERR ==
467 check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
469 check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
471 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
473 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
476 GNUNET_SCHEDULER_add_now (&end, NULL);
482 GNUNET_SCHEDULER_add_now (&end, NULL);
485 if ((NULL == p1) || (NULL == p2))
487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
489 GNUNET_SCHEDULER_add_now (&end, NULL);
492 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
501 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
507 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
512 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
516 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
517 stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
518 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
520 if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
521 || (0 == strcmp (test_name,
522 "test_transport_blacklisting_multiple_plugins")))
524 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
534 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
541 GNUNET_SCHEDULER_add_now (&end, NULL);
546 run (void *cls, char *const *args, const char *cfgfile,
547 const struct GNUNET_CONFIGURATION_Handle *cfg)
549 connected = GNUNET_NO;
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
552 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
557 main (int argc, char *argv0[])
561 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
563 GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL);
565 static char *const argv[] =
566 { "date", "-c", "test_transport_api_data.conf", NULL };
567 static struct GNUNET_GETOPT_CommandLineOption options[] =
568 { GNUNET_GETOPT_OPTION_END };
570 tth = GNUNET_TRANSPORT_TESTING_init ();
572 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
573 "test-transport-api-blacklisting", "nohelp", options,
576 GNUNET_TRANSPORT_TESTING_done (tth);
582 /* end of transport_api_blacklisting.c */