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_churn.c
22 * @brief base testcase for testing simple churn functionality
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
28 #define VERBOSE GNUNET_YES
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
44 static unsigned long long num_peers;
46 static unsigned int expected_connections;
48 static unsigned int expected_failed_connections;
50 static unsigned long long peers_left;
52 static struct GNUNET_TESTING_PeerGroup *pg;
54 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
56 GNUNET_SCHEDULER_TaskIdentifier die_task;
58 static char *test_directory;
62 GNUNET_NETWORK_STRUCT_BEGIN
64 struct GNUNET_TestMessage
67 * Header of the message
69 struct GNUNET_MessageHeader header;
72 * Unique identifier for this message.
76 GNUNET_NETWORK_STRUCT_END
79 * Check whether peers successfully shut down.
82 shutdown_callback (void *cls, const char *emsg)
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
103 GNUNET_assert (pg != NULL);
105 if (die_task != GNUNET_SCHEDULER_NO_TASK)
106 GNUNET_SCHEDULER_cancel (die_task);
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Called finish testing, stopping daemons.\n");
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
116 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
125 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
130 "End badly was called (%s)... stopping daemons.\n", msg);
134 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
135 ok = 7331; /* Opposite of leet */
138 ok = 401; /* Never got peers started */
142 struct ChurnTestContext
144 GNUNET_SCHEDULER_Task next_task;
148 static struct ChurnTestContext churn_ctx;
151 * Churn callback, report on success or failure of churn operation.
154 * @param emsg NULL on success
157 churn_callback (void *cls, const char *emsg)
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Successfully churned peers!\n",
163 GNUNET_SCHEDULER_add_now (churn_ctx.next_task, NULL);
167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
168 "Failed to churn peers with error `%s'\n", emsg);
169 GNUNET_SCHEDULER_cancel (die_task);
170 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
178 churn_ctx.next_task = &finish_testing;
179 GNUNET_TESTING_daemons_churn (pg, NULL, 1, 1, TIMEOUT, &churn_callback, NULL);
183 churn_peers_off_again ()
185 churn_ctx.next_task = &churn_peers_both;
186 GNUNET_TESTING_daemons_churn (pg, NULL, 2, 0, TIMEOUT, &churn_callback, NULL);
192 churn_ctx.next_task = &churn_peers_off_again;
193 GNUNET_TESTING_daemons_churn (pg, NULL, 0, 2, TIMEOUT, &churn_callback, NULL);
199 churn_ctx.next_task = &churn_peers_on;
200 GNUNET_TESTING_daemons_churn (pg, NULL, 2, 0, TIMEOUT, &churn_callback, NULL);
204 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
205 const struct GNUNET_CONFIGURATION_Handle *cfg,
206 struct GNUNET_TESTING_Daemon *d, const char *emsg)
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Failed to start daemon with error: `%s'\n", emsg);
214 GNUNET_assert (id != NULL);
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
217 (num_peers - peers_left) + 1, num_peers);
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "All %d daemons started, now testing churn!\n", num_peers);
226 GNUNET_SCHEDULER_cancel (die_task);
227 /* Set up task in case topology creation doesn't finish
228 * within a reasonable amount of time */
230 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
231 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
232 "from peers_started_callback");
240 run (void *cls, char *const *args, const char *cfgfile,
241 const struct GNUNET_CONFIGURATION_Handle *cfg)
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Starting daemons based on config file %s\n", cfgfile);
251 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
259 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
261 num_peers = DEFAULT_NUM_PEERS;
265 peers_left = num_peers;
266 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
268 /* For this specific test we only really want a CLIQUE topology as the
269 * overlay allowed topology, and a RING topology as the underlying connection
270 * allowed topology. So we will expect only num_peers * 2 connections to
271 * work, and (num_peers * (num_peers - 1)) - (num_peers * 2) to fail.
273 expected_connections = num_peers * (num_peers - 1);
274 expected_failed_connections = expected_connections - (num_peers * 2);
277 /* Set up a task to end testing if peer start fails */
279 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
280 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
281 "didn't start all daemons in reasonable amount of time!!!");
283 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, peers_left, peers_left,
285 &peers_started_callback, NULL, NULL, NULL,
295 char *const argv[] = { "test-testing-topology-churn",
297 "test_testing_data_topology_churn.conf",
303 struct GNUNET_GETOPT_CommandLineOption options[] = {
304 GNUNET_GETOPT_OPTION_END
307 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
308 "test-testing-topology-churn", "nohelp", options,
310 if (ret != GNUNET_OK)
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313 "`test-testing-topology-churn': Failed with error code %d\n",
321 main (int argc, char *argv[])
325 GNUNET_log_setup ("test_testing_topology_churn",
335 * Need to remove base directory, subdirectories taken care
336 * of by the testing framework.
338 if (test_directory != NULL)
340 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
342 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
343 "Failed to remove testing directory %s\n", test_directory);
350 /* end of test_testing_topology_churn.c */