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_small.c
23 * @brief Test for the mesh service.
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service_new.h"
29 #define VERBOSE GNUNET_YES
30 #define REMOVE_DIR GNUNET_YES
34 struct MeshPeer *prev;
36 struct MeshPeer *next;
38 struct GNUNET_TESTING_Daemon *daemon;
40 struct GNUNET_MESH_Handle *mesh_handle;
46 unsigned long long total_mesh_bytes;
50 // static struct MeshPeer *peer_head;
52 // static struct MeshPeer *peer_tail;
55 * How long until we give up on connecting the peers?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
67 * Total number of peers in the test.
69 static unsigned long long num_peers;
72 * Global configuration file
74 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
77 * Total number of currently running peers.
79 static unsigned long long peers_running;
82 * Total number of connections in the whole network.
84 static unsigned int total_connections;
87 * The currently running peer group.
89 static struct GNUNET_TESTING_PeerGroup *pg;
92 * File to report results to.
94 static struct GNUNET_DISK_FileHandle *output_file;
97 * File to log connection info, statistics to.
99 static struct GNUNET_DISK_FileHandle *data_file;
102 * Task called to disconnect peers.
104 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
107 * Task called to shutdown test.
109 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
111 static char *topology_file;
113 static char *data_filename;
115 static struct GNUNET_TIME_Relative time_out;
118 * Check whether peers successfully shut down.
121 shutdown_callback (void *cls, const char *emsg)
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
142 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 fprintf (stderr, "Ending test.\n");
148 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_cancel (disconnect_task);
151 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
153 if (data_file != NULL)
154 GNUNET_DISK_file_close (data_file);
155 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
156 GNUNET_CONFIGURATION_destroy (testing_cfg);
161 * Handlers, for diverse services
163 static struct GNUNET_MESH_MessageHandler handlers[] = {
164 // {&callback, 1, 0},
170 disconnect_mesh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "test: disconnecting mesh service of peers\n");
174 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_SCHEDULER_cancel (shutdown_handle);
176 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
181 * Function called whenever an inbound tunnel is destroyed. Should clean up
182 * any associated state.
184 * @param cls closure (set from GNUNET_MESH_connect)
185 * @param tunnel connection to the other end (henceforth invalid)
186 * @param tunnel_ctx place where local state associated
187 * with the tunnel is stored
190 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel disconnected\n");
200 * Method called whenever a tunnel falls apart.
203 * @param peer peer identity the tunnel stopped working with
206 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
214 * Method called whenever a tunnel is established.
217 * @param peer peer identity the tunnel was created to, NULL on timeout
218 * @param atsi performance data for the connection
221 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
222 const struct GNUNET_ATS_Information *atsi)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
230 * connect_mesh_service: connect to the mesh service of one of the peers
234 connect_mesh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 struct GNUNET_TESTING_Daemon *d;
237 struct GNUNET_MESH_Handle *h;
238 struct GNUNET_MESH_Tunnel *t;
239 GNUNET_MESH_ApplicationType app;
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect_mesh_service\n");
243 d = GNUNET_TESTING_daemon_get (pg, 1);
244 app = (GNUNET_MESH_ApplicationType) 0;
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "connecting to mesh service of peer %s\n", GNUNET_i2s (&d->id));
250 h = GNUNET_MESH_connect (d->cfg, 10, NULL, NULL, &tunnel_cleaner, handlers,
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected to mesh service of peer %s\n",
254 GNUNET_i2s (&d->id));
256 t = GNUNET_MESH_tunnel_create (h, NULL, &ch, &dh, NULL);
257 GNUNET_MESH_tunnel_destroy (t);
258 GNUNET_MESH_disconnect (h);
263 * peergroup_ready: start test when all peers are connected
265 * @param emsg error message
268 peergroup_ready (void *cls, const char *emsg)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Peergroup callback called with error, aborting test!\n");
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
279 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
288 if (data_file != NULL)
291 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
293 GNUNET_DISK_file_write (data_file, buf, buf_len);
296 peers_running = GNUNET_TESTING_daemons_running (pg);
298 GNUNET_SCHEDULER_add_now (&connect_mesh, NULL);
300 GNUNET_SCHEDULER_add_delayed (time_out, &disconnect_mesh, NULL);
306 * Function that will be called whenever two daemons are connected by
307 * the testing library.
310 * @param first peer id for first daemon
311 * @param second peer id for the second daemon
312 * @param distance distance between the connected peers
313 * @param first_cfg config for the first daemon
314 * @param second_cfg config for the second daemon
315 * @param first_daemon handle for the first daemon
316 * @param second_daemon handle for the second daemon
317 * @param emsg error message (NULL on success)
320 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
321 const struct GNUNET_PeerIdentity *second, uint32_t distance,
322 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
323 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
324 struct GNUNET_TESTING_Daemon *first_daemon,
325 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
333 * run: load configuration options and schedule test to run (start peergroup)
336 * @param cfgfile configuration file name (can be NULL)
337 * @param cfg configuration handle
340 run (void *cls, char *const *args, const char *cfgfile,
341 const struct GNUNET_CONFIGURATION_Handle *cfg)
344 struct GNUNET_TESTING_Host *hosts;
347 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
349 GNUNET_log_setup ("test_mesh_small",
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
359 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
360 "use_progressbars", "YES");
363 time_out = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
366 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
367 "num_peers", &num_peers))
369 GNUNET_assert (GNUNET_OK ==
370 GNUNET_CONFIGURATION_load (testing_cfg,
371 "test_mesh_small.conf"));
373 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
374 "num_peers", &num_peers))
376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377 "Option TESTING:NUM_PEERS is required!\n");
383 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
384 "topology_output_file",
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
388 "Option test_mesh_small:topology_output_file is required!\n");
393 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398 "Option test_mesh_small:data_output_file is required!\n");
403 GNUNET_DISK_file_open (data_filename,
404 GNUNET_DISK_OPEN_READWRITE |
405 GNUNET_DISK_OPEN_CREATE,
406 GNUNET_DISK_PERM_USER_READ |
407 GNUNET_DISK_PERM_USER_WRITE);
408 if (data_file == NULL)
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
412 GNUNET_free (data_filename);
416 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
417 "output_file", &temp_str))
420 GNUNET_DISK_file_open (temp_str,
421 GNUNET_DISK_OPEN_READWRITE |
422 GNUNET_DISK_OPEN_CREATE,
423 GNUNET_DISK_PERM_USER_READ |
424 GNUNET_DISK_PERM_USER_WRITE);
425 if (output_file == NULL)
426 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
429 GNUNET_free_non_null (temp_str);
431 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
433 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
434 &connect_cb, &peergroup_ready, NULL,
436 GNUNET_assert (pg != NULL);
438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
439 &shutdown_task, NULL);
445 * test_mesh_small command line options
447 static struct GNUNET_GETOPT_CommandLineOption options[] = {
448 {'V', "verbose", NULL,
449 gettext_noop ("be verbose (print progress information)"),
450 0, &GNUNET_GETOPT_set_one, &verbose},
451 GNUNET_GETOPT_OPTION_END
459 main (int argc, char *argv[])
461 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
462 gettext_noop ("Test mesh in a small network."), options,
465 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
470 /* end of test_mesh_small.c */