2 This file is part of GNUnet.
3 (C) 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.
21 * @file mesh/test_mesh_2dtorus.c
23 * @brief Test for creating a 2dtorus.
26 #include "gnunet_testing_lib.h"
28 #define VERBOSE GNUNET_YES
29 #define REMOVE_DIR GNUNET_YES
32 * How long until we give up on connecting the peers?
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
37 * Time to wait for stuff that should be rather fast
39 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 * How many events have happened
53 * Total number of peers in the test.
55 static unsigned long long num_peers;
58 * Global configuration file
60 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
63 * Total number of currently running peers.
65 static unsigned long long peers_running;
68 * Total number of successful connections in the whole network.
70 static unsigned int total_connections;
73 * Total number of counted topo connections
75 static unsigned int topo_connections;
78 * Total number of failed connections in the whole network.
80 static unsigned int failed_connections;
83 * The currently running peer group.
85 static struct GNUNET_TESTING_PeerGroup *pg;
88 * Task called to disconnect peers
90 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
93 * Task called to shutdown test.
95 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
99 * Check whether peers successfully shut down.
102 shutdown_callback (void *cls, const char *emsg)
106 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
107 "test: Shutdown of peers failed! (%s)\n", emsg);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "test: All peers successfully shut down!\n");
121 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
127 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
128 GNUNET_CONFIGURATION_destroy (testing_cfg);
133 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
137 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
139 GNUNET_SCHEDULER_cancel (shutdown_handle);
140 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
146 * Prototype of a callback function indicating that two peers
147 * are currently connected.
150 * @param first peer id for first daemon
151 * @param second peer id for the second daemon
152 * @param distance distance between the connected peers
153 * @param emsg error message (NULL on success)
156 topo_cb (void *cls, const struct GNUNET_PeerIdentity *first,
157 const struct GNUNET_PeerIdentity *second, const char *emsg)
162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error by topo %u: %s\n",
163 topo_connections, emsg);
167 if (first == NULL || second == NULL)
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u NULL\n",
171 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_cancel (disconnect_task);
174 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u ok\n",
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (first));
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (second));
187 * peergroup_ready: start test when all peers are connected
189 * @param emsg error message
192 peergroup_ready (void *cls, const char *emsg)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "test: Peergroup callback called with error, aborting test!\n");
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
201 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "************************************************************\n");
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "test: Peer Group started successfully!\n");
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Have %u connections\n",
213 peers_running = GNUNET_TESTING_daemons_running (pg);
214 if (0 < failed_connections)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
219 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
224 GNUNET_TESTING_get_topology (pg, &topo_cb, NULL);
226 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
234 * Function that will be called whenever two daemons are connected by
235 * the testing library.
238 * @param first peer id for first daemon
239 * @param second peer id for the second daemon
240 * @param distance distance between the connected peers
241 * @param first_cfg config for the first daemon
242 * @param second_cfg config for the second daemon
243 * @param first_daemon handle for the first daemon
244 * @param second_daemon handle for the second daemon
245 * @param emsg error message (NULL on success)
248 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
249 const struct GNUNET_PeerIdentity *second, uint32_t distance,
250 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
251 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
252 struct GNUNET_TESTING_Daemon *first_daemon,
253 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
261 failed_connections++;
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "test: Problem with new connection (%s)\n", emsg);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
272 * run: load configuration options and schedule test to run (start peergroup)
275 * @param cfgfile configuration file name (can be NULL)
276 * @param cfg configuration handle
279 run (void *cls, char *const *args, const char *cfgfile,
280 const struct GNUNET_CONFIGURATION_Handle *cfg)
282 struct GNUNET_TESTING_Host *hosts;
285 total_connections = 0;
286 failed_connections = 0;
287 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
289 GNUNET_log_setup ("test_mesh_2dtorus",
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
299 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
300 "use_progressbars", "YES");
304 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
305 "num_peers", &num_peers))
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 "Option TESTING:NUM_PEERS is required!\n");
312 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
314 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
315 &connect_cb, &peergroup_ready, NULL,
317 GNUNET_assert (pg != NULL);
319 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
320 &shutdown_task, NULL);
325 * test_mesh_2dtorus command line options
327 static struct GNUNET_GETOPT_CommandLineOption options[] = {
328 {'V', "verbose", NULL,
329 gettext_noop ("be verbose (print progress information)"),
330 0, &GNUNET_GETOPT_set_one, &verbose},
331 GNUNET_GETOPT_OPTION_END
339 main (int argc, char *argv[])
341 char *const argv2[] = {
344 "test_mesh_2dtorus.conf",
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
355 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
356 "test_mesh_2dtorus", gettext_noop ("Test mesh 2d torus."),
357 options, &run, NULL);
359 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
370 /* end of test_mesh_2dtorus.c */