2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 PeerContext *p1;
59 struct PeerContext *p2;
61 static 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 GNUNET_SCHEDULER_TaskIdentifier die_task;
78 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
80 static GNUNET_SCHEDULER_TaskIdentifier 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)
89 run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
92 end(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
96 if (die_task != GNUNET_SCHEDULER_NO_TASK )
98 GNUNET_SCHEDULER_cancel (die_task);
99 die_task = GNUNET_SCHEDULER_NO_TASK;
102 if (timeout_task != GNUNET_SCHEDULER_NO_TASK )
104 GNUNET_SCHEDULER_cancel (timeout_task);
105 timeout_task = GNUNET_SCHEDULER_NO_TASK;
108 if (stage_task != GNUNET_SCHEDULER_NO_TASK )
110 GNUNET_SCHEDULER_cancel (stage_task);
111 stage_task = GNUNET_SCHEDULER_NO_TASK;
116 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
122 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
127 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
133 end_badly(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 die_task = GNUNET_SCHEDULER_NO_TASK;
137 if (timeout_task != GNUNET_SCHEDULER_NO_TASK )
139 GNUNET_SCHEDULER_cancel (timeout_task);
140 timeout_task = GNUNET_SCHEDULER_NO_TASK;
143 if (stage_task != GNUNET_SCHEDULER_NO_TASK )
145 GNUNET_SCHEDULER_cancel (stage_task);
146 stage_task = GNUNET_SCHEDULER_NO_TASK;
151 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
155 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
157 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
163 testing_connect_cb(struct PeerContext *p1, struct PeerContext *p2, void *cls)
166 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
168 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
169 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
171 connected = GNUNET_YES;
172 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
176 connect_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peers not connected, next stage\n");
179 timeout_task = GNUNET_SCHEDULER_NO_TASK;
180 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
186 start_cb(struct PeerContext *p, void *cls)
190 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
191 GNUNET_i2s_full (&p->id));
196 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
199 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", p1->no,
200 sender_c, p2->no, GNUNET_i2s (&p2->id));
201 GNUNET_free(sender_c);
203 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
208 static int check_blacklist_config (char *cfg_file,
209 struct GNUNET_PeerIdentity *peer, struct GNUNET_PeerIdentity *bl_peer)
211 struct GNUNET_CONFIGURATION_Handle *cfg;
214 cfg = GNUNET_CONFIGURATION_create ();
215 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
217 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n", cfg_file);
218 GNUNET_CONFIGURATION_destroy (cfg);
219 return GNUNET_SYSERR;
222 peer_str = GNUNET_strdup (GNUNET_i2s_full(peer));
223 GNUNET_asprintf (§ion, "transport-blacklist-%s", peer_str);
225 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section, GNUNET_i2s_full(bl_peer)))
227 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
228 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
229 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
230 GNUNET_CONFIGURATION_destroy (cfg);
231 GNUNET_free (section);
232 GNUNET_free (peer_str);
233 return GNUNET_SYSERR;
236 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
237 "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
238 cfg_file, peer_str, GNUNET_i2s_full(bl_peer));
240 GNUNET_CONFIGURATION_destroy (cfg);
241 GNUNET_free (section);
242 GNUNET_free (peer_str);
247 run_stage(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
249 stage_task = GNUNET_SCHEDULER_NO_TASK;
250 if (GNUNET_SCHEDULER_NO_TASK != die_task)
251 GNUNET_SCHEDULER_cancel (die_task);
252 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
253 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
258 connected = GNUNET_NO;
259 if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
261 /* Try to connect peers successfully */
262 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
263 "test_transport_blacklisting_cfg_peer1.conf", 1, NULL, NULL, NULL,
266 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
267 "test_transport_blacklisting_cfg_peer2.conf", 2, NULL, NULL, NULL,
271 == strcmp (test_name, "test_transport_blacklisting_outbound_bl_full"))
273 char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_full.conf";
274 char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
275 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
276 cfg_p1 , 1, NULL, NULL, NULL, &start_cb, NULL );
278 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
279 cfg_p2, 2, NULL, NULL, NULL,
282 /* check if configuration contain correct blacklist entries */
283 if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
284 (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
286 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
288 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
291 GNUNET_SCHEDULER_add_now (&end, NULL );
296 == strcmp (test_name, "test_transport_blacklisting_outbound_bl_plugin"))
298 char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
299 char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
301 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
303 NULL, NULL, &start_cb, NULL );
305 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
306 cfg_p2, 2, NULL, NULL, NULL,
309 /* check if configuration contain correct blacklist entries */
310 if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
311 (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
313 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
315 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
318 GNUNET_SCHEDULER_add_now (&end, NULL );
322 == strcmp (test_name, "test_transport_blacklisting_inbound_bl_full"))
324 char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
325 char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_full.conf";
327 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
328 cfg_p1, 1, NULL, NULL, NULL,
331 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
332 cfg_p2, 2, NULL, NULL,
333 NULL, &start_cb, NULL );
335 /* check if configuration contain correct blacklist entries */
336 if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
338 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
340 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
343 GNUNET_SCHEDULER_add_now (&end, NULL );
347 == strcmp (test_name, "test_transport_blacklisting_inbound_bl_plugin"))
349 char * cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
350 char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
352 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
353 cfg_p1, 1, NULL, NULL, NULL,
356 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
357 cfg_p2, 2, NULL, NULL,
358 NULL, &start_cb, NULL );
360 /* check if configuration contain correct blacklist entries */
361 if ((GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)) )
363 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
365 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
368 GNUNET_SCHEDULER_add_now (&end, NULL );
373 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins"))
375 char * cfg_p1 = "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
376 char * cfg_p2 = "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
378 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
380 NULL, NULL, NULL, &start_cb, NULL );
382 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
384 NULL, NULL, NULL, &start_cb, NULL );
386 /* check if configuration contain correct blacklist entries */
387 if ((GNUNET_SYSERR == check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
388 (GNUNET_SYSERR == check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
390 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p1);
392 GNUNET_TRANSPORT_TESTING_stop_peer(tth, p2);
395 GNUNET_SCHEDULER_add_now (&end, NULL );
401 GNUNET_SCHEDULER_add_now (&end, NULL );
404 if ((NULL == p1) || (NULL == p2))
406 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
408 GNUNET_SCHEDULER_add_now (&end, NULL );
411 timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
412 &connect_timeout, NULL );
419 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
425 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
430 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
434 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
435 stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
436 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
438 if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
439 || (0 == strcmp (test_name, "test_transport_blacklisting_multiple_plugins")))
441 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
451 if ((GNUNET_NO != started) && (GNUNET_YES != connected))
458 GNUNET_SCHEDULER_add_now (&end, NULL );
462 run(void *cls, char * const *args, const char *cfgfile,
463 const struct GNUNET_CONFIGURATION_Handle *cfg)
465 connected = GNUNET_NO;
467 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
468 stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL );
472 main(int argc, char *argv0[])
476 GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0], &test_name);
478 GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL );
480 static char * const argv[] =
481 { "date", "-c", "test_transport_api_data.conf", NULL };
482 static struct GNUNET_GETOPT_CommandLineOption options[] =
483 { GNUNET_GETOPT_OPTION_END };
485 tth = GNUNET_TRANSPORT_TESTING_init ();
487 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
488 "test-transport-api-blacklisting", "nohelp", options, &run, NULL );
490 GNUNET_TRANSPORT_TESTING_done (tth);
495 /* end of transport_api_blacklisting.c */