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 GNUNET_NETWORK_STRUCT_BEGIN
90 struct GNUNET_TestMessage
93 * Header of the message
95 struct GNUNET_MessageHeader header;
98 * Unique identifier for this message.
102 GNUNET_NETWORK_STRUCT_END
105 * Check whether peers successfully shut down.
108 shutdown_callback (void *cls, const char *emsg)
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
129 GNUNET_assert (pg != NULL);
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Called finish testing, stopping daemons.\n");
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
139 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
143 if (dotOutFile != NULL)
145 FPRINTF (dotOutFile, "%s", "}");
153 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
158 "End badly was called (%s)... stopping daemons.\n", msg);
162 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
163 ok = 7331; /* Opposite of leet */
166 ok = 401; /* Never got peers started */
168 if (dotOutFile != NULL)
170 FPRINTF (dotOutFile, "%s", "}");
178 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
179 const struct GNUNET_PeerIdentity *second, uint32_t distance,
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,
263 connect_timeout, connect_attempts,
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
267 expected_connections);
271 GNUNET_SCHEDULER_cancel (die_task);
272 if (expected_connections == GNUNET_SYSERR)
275 GNUNET_SCHEDULER_add_now (&end_badly,
276 "from connect topology (bad return)");
280 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
281 "from connect topology (timeout)");
287 peers_left = num_peers; /* Reset counter */
288 if (GNUNET_TESTING_create_topology
289 (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Topology set up, now starting peers!\n");
295 GNUNET_TESTING_daemons_continue_startup (pg);
299 GNUNET_SCHEDULER_cancel (die_task);
301 GNUNET_SCHEDULER_add_now (&end_badly,
302 "from create topology (bad return)");
304 GNUNET_SCHEDULER_cancel (die_task);
306 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
307 "from continue startup (timeout)");
312 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
313 const struct GNUNET_CONFIGURATION_Handle *cfg,
314 struct GNUNET_TESTING_Daemon *d, const char *emsg)
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Failed to start daemon with error: `%s'\n", emsg);
322 GNUNET_assert (id != NULL);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
325 (num_peers - peers_left) + 1, num_peers);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "All %d daemons started, now creating topology!\n", num_peers);
334 GNUNET_SCHEDULER_cancel (die_task);
335 /* Set up task in case topology creation doesn't finish
336 * within a reasonable amount of time */
338 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
339 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
340 "from peers_started_callback");
347 * Callback indicating that the hostkey was created for a peer.
350 * @param id the peer identity
351 * @param d the daemon handle (pretty useless at this point, remove?)
352 * @param emsg non-null on failure
355 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
356 struct GNUNET_TESTING_Daemon *d, const char *emsg)
360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
361 "Hostkey callback received error: %s\n", emsg);
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostkey created for peer `%s'\n",
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "All %d hostkeys created, now creating topology!\n", num_peers);
375 GNUNET_SCHEDULER_cancel (die_task);
376 /* Set up task in case topology creation doesn't finish
377 * within a reasonable amount of time */
379 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
380 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
381 "from hostkey_callback");
382 GNUNET_SCHEDULER_add_now (&create_topology, NULL);
388 run (void *cls, char *const *args, const char *cfgfile,
389 const struct GNUNET_CONFIGURATION_Handle *cfg)
391 unsigned long long topology_num;
392 unsigned long long connect_topology_num;
393 unsigned long long blacklist_topology_num;
394 unsigned long long connect_topology_option_num;
395 char *connect_topology_option_modifier_string;
399 dotOutFile = FOPEN (dotOutFileName, "w");
400 if (dotOutFile != NULL)
402 FPRINTF (dotOutFile, "%s", "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", "connect_topology",
429 &connect_topology_num))
430 connection_topology = connect_topology_num;
433 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
434 "connect_topology_option",
435 &connect_topology_option_num))
436 connect_topology_option = connect_topology_option_num;
439 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
440 "connect_topology_option_modifier",
441 &connect_topology_option_modifier_string))
444 (connect_topology_option_modifier_string, "%lf",
445 &connect_topology_option_modifier) != 1)
447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
449 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
450 connect_topology_option_modifier_string,
451 "connect_topology_option_modifier", "TESTING");
452 GNUNET_free (connect_topology_option_modifier_string);
454 if (dotOutFile != NULL)
460 GNUNET_free (connect_topology_option_modifier_string);
464 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
465 "blacklist_transports",
466 &blacklist_transports))
468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
469 "No transports specified for blacklisting in blacklist testcase (this shouldn't happen!)\n");
471 if (dotOutFile != NULL)
479 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
480 "blacklist_topology",
481 &blacklist_topology_num))
482 blacklist_topology = blacklist_topology_num;
485 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
487 num_peers = DEFAULT_NUM_PEERS;
490 GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "CONNECT_TIMEOUT",
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
494 "testing", "CONNECT_TIMEOUT");
500 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
504 "testing", "connect_attempts");
510 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
511 peers_left = num_peers;
513 /* For this specific test we only really want a CLIQUE topology as the
514 * overlay allowed topology, and a RING topology as the underlying connection
515 * allowed topology. So we will expect only num_peers * 2 connections to
516 * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
518 expected_connections = num_peers * (num_peers - 1);
519 expected_failed_connections = expected_connections - (num_peers * 2);
522 /* Set up a task to end testing if peer start fails */
524 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
525 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
526 "didn't start all daemons in reasonable amount of time!!!");
528 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, peers_left, peers_left,
529 TIMEOUT, &hostkey_callback, NULL,
530 &peers_started_callback, NULL,
531 &topology_callback, NULL, NULL);
540 char *const argv[] = { "test-testing-topology-blacklist",
542 "test_testing_data_topology_blacklist.conf",
548 struct GNUNET_GETOPT_CommandLineOption options[] = {
549 GNUNET_GETOPT_OPTION_END
552 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
553 "test-testing-topology-blacklist", "nohelp", options,
555 if (ret != GNUNET_OK)
557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
558 "`test-testing-topology-blacklist': Failed with error code %d\n",
566 main (int argc, char *argv[])
570 GNUNET_log_setup ("test_testing_topology_blacklist",
580 * Need to remove base directory, subdirectories taken care
581 * of by the testing framework.
583 if (test_directory != NULL)
585 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
588 "Failed to remove testing directory %s\n", test_directory);
595 /* end of test_testing_topology_blacklist.c */