2 This file is part of GNUnet.
3 (C) 2009 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.
21 * @file testing/test_testing_topology_blacklist.c
22 * @brief base testcase for testing transport level blacklisting
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
28 #define VERBOSE GNUNET_NO
31 * How long until we fail the whole testcase?
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
36 * How long until we give up on starting the peers? (Must be longer than the connect timeout!)
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 #define DEFAULT_NUM_PEERS 4
42 #define MAX_OUTSTANDING_CONNECTIONS 300
46 struct GNUNET_TIME_Relative connect_timeout;
48 static unsigned long long connect_attempts;
50 static unsigned long long num_peers;
52 static unsigned int total_connections;
54 static unsigned int failed_connections;
56 static unsigned int expected_connections;
58 static unsigned int expected_failed_connections;
60 static unsigned long long peers_left;
62 static struct GNUNET_TESTING_PeerGroup *pg;
64 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
66 GNUNET_SCHEDULER_TaskIdentifier die_task;
68 static char *dotOutFileName;
70 static FILE *dotOutFile;
72 static char *blacklist_transports;
74 static enum GNUNET_TESTING_Topology topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Overlay should allow all connections */
76 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_RING; /* Blacklist underlay into a ring */
78 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
80 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Try to connect all possible OVERLAY connections */
82 static double connect_topology_option_modifier = 0.0;
84 static char *test_directory;
88 struct GNUNET_TestMessage
91 * Header of the message
93 struct GNUNET_MessageHeader header;
96 * Unique identifier for this message.
103 * Check whether peers successfully shut down.
106 shutdown_callback (void *cls, const char *emsg)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "All peers successfully shut down!\n");
128 GNUNET_assert (pg != NULL);
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Called finish testing, stopping daemons.\n");
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
138 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
142 if (dotOutFile != NULL)
144 fprintf (dotOutFile, "}");
152 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
156 "End badly was called (%s)... stopping daemons.\n", msg);
160 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
161 ok = 7331; /* Opposite of leet */
164 ok = 401; /* Never got peers started */
166 if (dotOutFile != NULL)
168 fprintf (dotOutFile, "}");
176 topology_callback (void *cls,
177 const struct GNUNET_PeerIdentity *first,
178 const struct GNUNET_PeerIdentity *second,
180 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
181 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
182 struct GNUNET_TESTING_Daemon *first_daemon,
183 struct GNUNET_TESTING_Daemon *second_daemon,
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
191 first_daemon->shortname, second_daemon->shortname);
193 if (dotOutFile != NULL)
194 fprintf (dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname,
195 second_daemon->shortname);
200 failed_connections++;
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Failed to connect peer %s to peer %s with error :\n%s\n",
204 first_daemon->shortname, second_daemon->shortname, emsg);
209 if (total_connections == expected_connections)
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Created %d total connections, which is our target number (that's bad)!\n",
217 GNUNET_SCHEDULER_cancel (die_task);
218 die_task = GNUNET_SCHEDULER_NO_TASK;
220 GNUNET_SCHEDULER_add_now (&end_badly,
221 "from topology_callback (too many successful connections)");
223 else if (total_connections + failed_connections == expected_connections)
225 if ((failed_connections == expected_failed_connections)
226 && (total_connections ==
227 expected_connections - expected_failed_connections))
229 GNUNET_SCHEDULER_cancel (die_task);
230 die_task = GNUNET_SCHEDULER_NO_TASK;
231 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
235 GNUNET_SCHEDULER_cancel (die_task);
237 GNUNET_SCHEDULER_add_now (&end_badly,
238 "from topology_callback (wrong number of failed connections)");
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Have %d total connections, %d failed connections, Want %d (failed) and %d (successful)\n",
246 total_connections, failed_connections,
247 expected_failed_connections,
248 expected_connections - expected_failed_connections);
256 expected_connections = -1;
257 if ((pg != NULL) && (peers_left == 0))
259 expected_connections =
260 GNUNET_TESTING_connect_topology (pg, connection_topology,
261 connect_topology_option,
262 connect_topology_option_modifier,
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Have %d expected connections\n", expected_connections);
272 GNUNET_SCHEDULER_cancel (die_task);
273 if (expected_connections == GNUNET_SYSERR)
276 GNUNET_SCHEDULER_add_now (&end_badly,
277 "from connect topology (bad return)");
280 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
282 "from connect topology (timeout)");
288 peers_left = num_peers; /* Reset counter */
289 if (GNUNET_TESTING_create_topology
290 (pg, topology, blacklist_topology,
291 blacklist_transports) != GNUNET_SYSERR)
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Topology set up, now starting peers!\n");
297 GNUNET_TESTING_daemons_continue_startup (pg);
301 GNUNET_SCHEDULER_cancel (die_task);
303 GNUNET_SCHEDULER_add_now (&end_badly,
304 "from create topology (bad return)");
306 GNUNET_SCHEDULER_cancel (die_task);
307 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
309 "from continue startup (timeout)");
314 peers_started_callback (void *cls,
315 const struct GNUNET_PeerIdentity *id,
316 const struct GNUNET_CONFIGURATION_Handle *cfg,
317 struct GNUNET_TESTING_Daemon *d, const char *emsg)
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Failed to start daemon with error: `%s'\n", emsg);
325 GNUNET_assert (id != NULL);
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
328 (num_peers - peers_left) + 1, num_peers);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "All %d daemons started, now creating topology!\n",
338 GNUNET_SCHEDULER_cancel (die_task);
339 /* Set up task in case topology creation doesn't finish
340 * within a reasonable amount of time */
341 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
342 (GNUNET_TIME_UNIT_MINUTES, 5),
344 "from peers_started_callback");
351 * Callback indicating that the hostkey was created for a peer.
354 * @param id the peer identity
355 * @param d the daemon handle (pretty useless at this point, remove?)
356 * @param emsg non-null on failure
359 hostkey_callback (void *cls,
360 const struct GNUNET_PeerIdentity *id,
361 struct GNUNET_TESTING_Daemon *d, const char *emsg)
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 "Hostkey callback received error: %s\n", emsg);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Hostkey created for peer `%s'\n", GNUNET_i2s (id));
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "All %d hostkeys created, now creating topology!\n",
381 GNUNET_SCHEDULER_cancel (die_task);
382 /* Set up task in case topology creation doesn't finish
383 * within a reasonable amount of time */
384 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
385 (GNUNET_TIME_UNIT_MINUTES, 5),
387 "from hostkey_callback");
388 GNUNET_SCHEDULER_add_now (&create_topology, NULL);
396 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
398 unsigned long long topology_num;
399 unsigned long long connect_topology_num;
400 unsigned long long blacklist_topology_num;
401 unsigned long long connect_topology_option_num;
402 unsigned long long temp_connect;
403 char *connect_topology_option_modifier_string;
406 dotOutFile = fopen (dotOutFileName, "w");
407 if (dotOutFile != NULL)
409 fprintf (dotOutFile, "strict graph G {\n");
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Starting daemons based on config file %s\n", cfgfile);
418 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
422 if (dotOutFile != NULL)
430 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
432 topology = topology_num;
435 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
437 &connect_topology_num))
438 connection_topology = connect_topology_num;
441 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
442 "connect_topology_option",
443 &connect_topology_option_num))
444 connect_topology_option = connect_topology_option_num;
447 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
448 "connect_topology_option_modifier",
449 &connect_topology_option_modifier_string))
452 (connect_topology_option_modifier_string, "%lf",
453 &connect_topology_option_modifier) != 1)
455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
458 connect_topology_option_modifier_string,
459 "connect_topology_option_modifier", "TESTING");
460 GNUNET_free (connect_topology_option_modifier_string);
462 if (dotOutFile != NULL)
468 GNUNET_free (connect_topology_option_modifier_string);
472 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
473 "blacklist_transports",
474 &blacklist_transports))
476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
477 "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
479 if (dotOutFile != NULL)
487 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
488 "blacklist_topology",
489 &blacklist_topology_num))
490 blacklist_topology = blacklist_topology_num;
493 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
495 num_peers = DEFAULT_NUM_PEERS;
498 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_timeout",
501 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_connect);
504 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_timeout");
510 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
513 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_attempts");
519 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
520 peers_left = num_peers;
522 /* For this specific test we only really want a CLIQUE topology as the
523 * overlay allowed topology, and a RING topology as the underlying connection
524 * allowed topology. So we will expect only num_peers * 2 connections to
525 * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
527 expected_connections = num_peers * (num_peers - 1);
528 expected_failed_connections = expected_connections - (num_peers * 2);
531 /* Set up a task to end testing if peer start fails */
532 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
533 (GNUNET_TIME_UNIT_MINUTES, 5),
535 "didn't start all daemons in reasonable amount of time!!!");
537 pg = GNUNET_TESTING_daemons_start (cfg,
538 peers_left, peers_left, peers_left,
539 TIMEOUT, &hostkey_callback,
540 NULL, &peers_started_callback, NULL,
541 &topology_callback, NULL, NULL);
549 char *const argv[] = { "test-testing-topology-blacklist",
551 "test_testing_data_topology_blacklist.conf",
557 struct GNUNET_GETOPT_CommandLineOption options[] = {
558 GNUNET_GETOPT_OPTION_END
560 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
561 argv, "test-testing-topology-blacklist", "nohelp",
563 if (ret != GNUNET_OK)
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566 "`test-testing-topology-blacklist': Failed with error code %d\n",
574 main (int argc, char *argv[])
578 GNUNET_log_setup ("test_testing_topology_blacklist",
588 * Need to remove base directory, subdirectories taken care
589 * of by the testing framework.
591 if (test_directory != NULL)
593 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
596 "Failed to remove testing directory %s\n",
604 /* end of test_testing_topology_blacklist.c */