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 static unsigned long long num_peers;
48 static unsigned int total_connections;
50 static unsigned int failed_connections;
52 static unsigned int expected_connections;
54 static unsigned int expected_failed_connections;
56 static unsigned long long peers_left;
58 static struct GNUNET_TESTING_PeerGroup *pg;
60 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
62 GNUNET_SCHEDULER_TaskIdentifier die_task;
64 static char *dotOutFileName;
66 static FILE *dotOutFile;
68 static char *blacklist_transports;
70 static enum GNUNET_TESTING_Topology topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Overlay should allow all connections */
72 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_RING; /* Blacklist underlay into a ring */
74 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
76 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Try to connect all possible OVERLAY connections */
78 static double connect_topology_option_modifier = 0.0;
80 static char *test_directory;
84 struct GNUNET_TestMessage
87 * Header of the message
89 struct GNUNET_MessageHeader header;
92 * Unique identifier for this message.
99 * Check whether peers successfully shut down.
102 shutdown_callback (void *cls, const char *emsg)
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "All peers successfully shut down!\n");
124 GNUNET_assert (pg != NULL);
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Called finish testing, stopping daemons.\n");
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
134 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
138 if (dotOutFile != NULL)
140 fprintf (dotOutFile, "}");
148 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152 "End badly was called (%s)... stopping daemons.\n", msg);
156 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
157 ok = 7331; /* Opposite of leet */
160 ok = 401; /* Never got peers started */
162 if (dotOutFile != NULL)
164 fprintf (dotOutFile, "}");
172 topology_callback (void *cls,
173 const struct GNUNET_PeerIdentity *first,
174 const struct GNUNET_PeerIdentity *second,
176 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
177 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
178 struct GNUNET_TESTING_Daemon *first_daemon,
179 struct GNUNET_TESTING_Daemon *second_daemon,
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
187 first_daemon->shortname, second_daemon->shortname);
189 if (dotOutFile != NULL)
190 fprintf (dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname,
191 second_daemon->shortname);
196 failed_connections++;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Failed to connect peer %s to peer %s with error :\n%s\n",
200 first_daemon->shortname, second_daemon->shortname, emsg);
205 if (total_connections == expected_connections)
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Created %d total connections, which is our target number (that's bad)!\n",
213 GNUNET_SCHEDULER_cancel (die_task);
214 die_task = GNUNET_SCHEDULER_NO_TASK;
216 GNUNET_SCHEDULER_add_now (&end_badly,
217 "from topology_callback (too many successful connections)");
219 else if (total_connections + failed_connections == expected_connections)
221 if ((failed_connections == expected_failed_connections)
222 && (total_connections ==
223 expected_connections - expected_failed_connections))
225 GNUNET_SCHEDULER_cancel (die_task);
226 die_task = GNUNET_SCHEDULER_NO_TASK;
227 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
231 GNUNET_SCHEDULER_cancel (die_task);
233 GNUNET_SCHEDULER_add_now (&end_badly,
234 "from topology_callback (wrong number of failed connections)");
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Have %d total connections, %d failed connections, Want %d (failed) and %d (successful)\n",
242 total_connections, failed_connections,
243 expected_failed_connections,
244 expected_connections - expected_failed_connections);
252 expected_connections = -1;
253 if ((pg != NULL) && (peers_left == 0))
255 expected_connections =
256 GNUNET_TESTING_connect_topology (pg, connection_topology,
257 connect_topology_option,
258 connect_topology_option_modifier,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Have %d expected connections\n", expected_connections);
266 GNUNET_SCHEDULER_cancel (die_task);
267 if (expected_connections == GNUNET_SYSERR)
270 GNUNET_SCHEDULER_add_now (&end_badly,
271 "from connect topology (bad return)");
274 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
276 "from connect topology (timeout)");
282 peers_left = num_peers; /* Reset counter */
283 if (GNUNET_TESTING_create_topology
284 (pg, topology, blacklist_topology,
285 blacklist_transports) != GNUNET_SYSERR)
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Topology set up, now starting peers!\n");
291 GNUNET_TESTING_daemons_continue_startup (pg);
295 GNUNET_SCHEDULER_cancel (die_task);
297 GNUNET_SCHEDULER_add_now (&end_badly,
298 "from create topology (bad return)");
300 GNUNET_SCHEDULER_cancel (die_task);
301 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
303 "from continue startup (timeout)");
308 peers_started_callback (void *cls,
309 const struct GNUNET_PeerIdentity *id,
310 const struct GNUNET_CONFIGURATION_Handle *cfg,
311 struct GNUNET_TESTING_Daemon *d, const char *emsg)
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Failed to start daemon with error: `%s'\n", emsg);
319 GNUNET_assert (id != NULL);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
322 (num_peers - peers_left) + 1, num_peers);
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "All %d daemons started, now creating topology!\n",
332 GNUNET_SCHEDULER_cancel (die_task);
333 /* Set up task in case topology creation doesn't finish
334 * within a reasonable amount of time */
335 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
336 (GNUNET_TIME_UNIT_MINUTES, 5),
338 "from peers_started_callback");
345 * Callback indicating that the hostkey was created for a peer.
348 * @param id the peer identity
349 * @param d the daemon handle (pretty useless at this point, remove?)
350 * @param emsg non-null on failure
353 hostkey_callback (void *cls,
354 const struct GNUNET_PeerIdentity *id,
355 struct GNUNET_TESTING_Daemon *d, const char *emsg)
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360 "Hostkey callback received error: %s\n", emsg);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Hostkey created for peer `%s'\n", GNUNET_i2s (id));
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "All %d hostkeys created, now creating topology!\n",
375 GNUNET_SCHEDULER_cancel (die_task);
376 /* Set up task in case topology creation doesn't finish
377 * within a reasonable amount of time */
378 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
379 (GNUNET_TIME_UNIT_MINUTES, 5),
381 "from hostkey_callback");
382 GNUNET_SCHEDULER_add_now (&create_topology, NULL);
390 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
392 unsigned long long topology_num;
393 unsigned long long connect_topology_num;
394 unsigned long long blacklist_topology_num;
395 unsigned long long connect_topology_option_num;
396 char *connect_topology_option_modifier_string;
399 dotOutFile = fopen (dotOutFileName, "w");
400 if (dotOutFile != NULL)
402 fprintf (dotOutFile, "strict graph G {\n");
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Starting daemons based on config file %s\n", cfgfile);
411 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
415 if (dotOutFile != NULL)
423 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
425 topology = topology_num;
428 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
430 &connect_topology_num))
431 connection_topology = connect_topology_num;
434 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
435 "connect_topology_option",
436 &connect_topology_option_num))
437 connect_topology_option = connect_topology_option_num;
440 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
441 "connect_topology_option_modifier",
442 &connect_topology_option_modifier_string))
445 (connect_topology_option_modifier_string, "%lf",
446 &connect_topology_option_modifier) != 1)
448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
450 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
451 connect_topology_option_modifier_string,
452 "connect_topology_option_modifier", "TESTING");
453 GNUNET_free (connect_topology_option_modifier_string);
455 if (dotOutFile != NULL)
461 GNUNET_free (connect_topology_option_modifier_string);
465 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
466 "blacklist_transports",
467 &blacklist_transports))
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
472 if (dotOutFile != NULL)
480 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
481 "blacklist_topology",
482 &blacklist_topology_num))
483 blacklist_topology = blacklist_topology_num;
486 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
488 num_peers = DEFAULT_NUM_PEERS;
492 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
493 peers_left = num_peers;
495 /* For this specific test we only really want a CLIQUE topology as the
496 * overlay allowed topology, and a RING topology as the underlying connection
497 * allowed topology. So we will expect only num_peers * 2 connections to
498 * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
500 expected_connections = num_peers * (num_peers - 1);
501 expected_failed_connections = expected_connections - (num_peers * 2);
504 /* Set up a task to end testing if peer start fails */
505 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
506 (GNUNET_TIME_UNIT_MINUTES, 5),
508 "didn't start all daemons in reasonable amount of time!!!");
510 pg = GNUNET_TESTING_daemons_start (cfg,
511 peers_left, TIMEOUT, &hostkey_callback,
512 NULL, &peers_started_callback, NULL,
513 &topology_callback, NULL, NULL);
521 char *const argv[] = { "test-testing-topology-blacklist",
523 "test_testing_data_topology_blacklist.conf",
529 struct GNUNET_GETOPT_CommandLineOption options[] = {
530 GNUNET_GETOPT_OPTION_END
532 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
533 argv, "test-testing-topology-blacklist", "nohelp",
535 if (ret != GNUNET_OK)
537 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
538 "`test-testing-topology-blacklist': Failed with error code %d\n",
546 main (int argc, char *argv[])
550 GNUNET_log_setup ("test_testing_topology_blacklist",
560 * Need to remove base directory, subdirectories taken care
561 * of by the testing framework.
563 if (test_directory != NULL)
565 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
568 "Failed to remove testing directory %s\n",
576 /* end of test_testing_topology_blacklist.c */