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 REMOVE_DIR GNUNET_YES
31 * How long until we give up on connecting the peers?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
36 * Time to wait for stuff that should be rather fast
38 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42 * How many events have happened
52 * Total number of peers in the test.
54 static unsigned long long num_peers;
57 * Global configuration file
59 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
62 * Total number of currently running peers.
64 static unsigned long long peers_running;
67 * Total number of successful connections in the whole network.
69 static unsigned int total_connections;
72 * Total number of counted topo connections
74 static unsigned int topo_connections;
77 * Total number of failed connections in the whole network.
79 static unsigned int failed_connections;
82 * The currently running peer group.
84 static struct GNUNET_TESTING_PeerGroup *pg;
87 * Task called to disconnect peers
89 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
92 * Task called to shutdown test.
94 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
98 * Check whether peers successfully shut down.
101 shutdown_callback (void *cls, const char *emsg)
105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
106 "test: Shutdown of peers failed! (%s)\n", emsg);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "test: All peers successfully shut down!\n");
114 GNUNET_CONFIGURATION_destroy (testing_cfg);
119 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
122 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
127 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
131 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
133 GNUNET_SCHEDULER_cancel (shutdown_handle);
134 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
140 * Prototype of a callback function indicating that two peers
141 * are currently connected.
144 * @param first peer id for first daemon
145 * @param second peer id for the second daemon
146 * @param distance distance between the connected peers
147 * @param emsg error message (NULL on success)
150 topo_cb (void *cls, const struct GNUNET_PeerIdentity *first,
151 const struct GNUNET_PeerIdentity *second, const char *emsg)
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error by topo %u: %s\n",
157 topo_connections, emsg);
161 if (first == NULL || second == NULL)
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u NULL\n",
165 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
167 GNUNET_SCHEDULER_cancel (disconnect_task);
168 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Connection %u ok\n",
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (first));
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n", GNUNET_i2s (second));
181 * peergroup_ready: start test when all peers are connected
183 * @param emsg error message
186 peergroup_ready (void *cls, const char *emsg)
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "test: Peergroup callback called with error, aborting test!\n");
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
195 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "test: Peer Group started successfully with %u connections\n",
201 peers_running = GNUNET_TESTING_daemons_running (pg);
202 if (0 < failed_connections)
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
207 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
212 GNUNET_TESTING_get_topology (pg, &topo_cb, NULL);
214 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
222 * Function that will be called whenever two daemons are connected by
223 * the testing library.
226 * @param first peer id for first daemon
227 * @param second peer id for the second daemon
228 * @param distance distance between the connected peers
229 * @param first_cfg config for the first daemon
230 * @param second_cfg config for the second daemon
231 * @param first_daemon handle for the first daemon
232 * @param second_daemon handle for the second daemon
233 * @param emsg error message (NULL on success)
236 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
237 const struct GNUNET_PeerIdentity *second, uint32_t distance,
238 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
239 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
240 struct GNUNET_TESTING_Daemon *first_daemon,
241 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
249 failed_connections++;
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "test: Problem with new connection (%s)\n", emsg);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
260 * run: load configuration options and schedule test to run (start peergroup)
263 * @param cfgfile configuration file name (can be NULL)
264 * @param cfg configuration handle
267 run (void *cls, char *const *args, const char *cfgfile,
268 const struct GNUNET_CONFIGURATION_Handle *cfg)
270 struct GNUNET_TESTING_Host *hosts;
273 total_connections = 0;
274 failed_connections = 0;
275 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
277 GNUNET_log_setup ("test_mesh_2dtorus",
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
284 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
285 "num_peers", &num_peers))
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288 "Option TESTING:NUM_PEERS is required!\n");
292 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
294 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
295 &connect_cb, &peergroup_ready, NULL,
297 GNUNET_assert (pg != NULL);
299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
300 &shutdown_task, NULL);
305 * test_mesh_2dtorus command line options
307 static struct GNUNET_GETOPT_CommandLineOption options[] = {
308 {'V', "verbose", NULL,
309 gettext_noop ("be verbose (print progress information)"),
310 0, &GNUNET_GETOPT_set_one, &verbose},
311 GNUNET_GETOPT_OPTION_END
319 main (int argc, char *argv[])
321 char *const argv2[] = {
324 "test_mesh_2dtorus.conf",
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
331 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
332 "test_mesh_2dtorus", gettext_noop ("Test mesh 2d torus."),
333 options, &run, NULL);
335 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
346 /* end of test_mesh_2dtorus.c */