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_unicast_far.c
23 * @brief Test for the mesh service: unicast to opposite point of 2d grid.
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service.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 * How many data points to capture before triggering next round?
104 static struct GNUNET_TIME_Relative wait_time;
107 * Task called to disconnect peers.
109 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
112 * Task To perform tests
114 static GNUNET_SCHEDULER_TaskIdentifier test_task;
117 * Task called to shutdown test.
119 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
121 static char *topology_file;
123 static struct GNUNET_TESTING_Daemon *d1;
125 static struct GNUNET_TESTING_Daemon *d2;
127 static struct GNUNET_MESH_Handle *h1;
129 static struct GNUNET_MESH_Handle *h2;
131 static struct GNUNET_MESH_Tunnel *t;
133 static uint16_t *mesh_peers;
136 * Check whether peers successfully shut down.
139 shutdown_callback (void *cls, const char *emsg)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
164 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
166 GNUNET_SCHEDULER_cancel (disconnect_task);
167 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170 if (data_file != NULL)
171 GNUNET_DISK_file_close (data_file);
172 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
173 GNUNET_CONFIGURATION_destroy (testing_cfg);
178 * Handlers, for diverse services
180 static struct GNUNET_MESH_MessageHandler handlers[] = {
181 // {&callback, 1, 0},
187 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "test: disconnecting mesh service of peers\n");
191 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
192 GNUNET_MESH_disconnect(h1);
193 GNUNET_MESH_disconnect(h2);
194 GNUNET_SCHEDULER_cancel (shutdown_handle);
195 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
200 * Method called whenever another peer has added us to a tunnel
201 * the other peer initiated.
204 * @param tunnel new handle to the tunnel
205 * @param initiator peer that started the tunnel
206 * @param atsi performance information for the tunnel
207 * @return initial tunnel context for the tunnel
208 * (can be NULL -- that's not an error)
211 incoming_tunnel (void *cls,
212 struct GNUNET_MESH_Tunnel * tunnel,
213 const struct GNUNET_PeerIdentity * initiator,
214 const struct GNUNET_ATS_Information * atsi)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "test: Incoming tunnel from %s\n",
218 GNUNET_i2s(initiator));
219 GNUNET_SCHEDULER_cancel (disconnect_task);
220 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_mesh_peers, NULL);
226 * Function called whenever an inbound tunnel is destroyed. Should clean up
227 * any associated state.
229 * @param cls closure (set from GNUNET_MESH_connect)
230 * @param tunnel connection to the other end (henceforth invalid)
231 * @param tunnel_ctx place where local state associated
232 * with the tunnel is stored
235 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: tunnel disconnected\n");
246 * Method called whenever a tunnel falls apart.
249 * @param peer peer identity the tunnel stopped working with
252 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "test: peer %s disconnected\n",
262 * Method called whenever a tunnel is established.
265 * @param peer peer identity the tunnel was created to, NULL on timeout
266 * @param atsi performance data for the connection
269 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
270 const struct GNUNET_ATS_Information *atsi)
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "test: peer %s connected\n",
280 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
283 GNUNET_MESH_peer_request_connect_add(t, &d2->id);
284 GNUNET_SCHEDULER_cancel (disconnect_task);
285 disconnect_task = GNUNET_SCHEDULER_add_delayed(
286 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
287 &disconnect_mesh_peers, NULL);
292 * connect_mesh_service: connect to the mesh service of one of the peers
296 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 GNUNET_MESH_ApplicationType app;
299 struct GNUNET_PeerIdentity id;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: connect_mesh_service\n");
303 GNUNET_PEER_resolve(11, &id);
304 d2 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "test: Peer searched: %s\n",
307 GNUNET_i2s (&d2->id));
308 app = (GNUNET_MESH_ApplicationType) 0;
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "test: connecting to mesh service of peer %s\n",
313 GNUNET_i2s (&d1->id));
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "test: connecting to mesh service of peer %s\n",
316 GNUNET_i2s (&d2->id));
318 h1 = GNUNET_MESH_connect (d1->cfg,
325 h2 = GNUNET_MESH_connect (d2->cfg,
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "test: connected to mesh service of peer %s\n",
335 GNUNET_i2s (&d1->id));
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "test: connected to mesh service of peer %s\n",
338 GNUNET_i2s (&d2->id));
340 t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, NULL);
342 GNUNET_SCHEDULER_add_delayed(
343 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 6),
350 * peergroup_ready: start test when all peers are connected
352 * @param emsg error message
355 peergroup_ready (void *cls, const char *emsg)
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "test: Peergroup callback called with error, aborting test!\n");
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "test: Error from testing: `%s'\n", emsg);
368 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "************************************************************\n");
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "test: Peer Group started successfully!\n");
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "test: Have %u connections\n",
381 if (data_file != NULL)
384 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
386 GNUNET_DISK_file_write (data_file, buf, buf_len);
389 peers_running = GNUNET_TESTING_daemons_running (pg);
390 for (i = 0; i < num_peers; i++)
392 d1 = GNUNET_TESTING_daemon_get (pg, i);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 GNUNET_PEER_intern(&d1->id),
396 GNUNET_i2s (&d1->id));
398 d1 = GNUNET_TESTING_daemon_get (pg, 0);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "test: Peer looking: %s\n",
401 GNUNET_i2s (&d1->id));
403 GNUNET_SCHEDULER_add_now (&connect_mesh_service,
406 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
412 * Function that will be called whenever two daemons are connected by
413 * the testing library.
416 * @param first peer id for first daemon
417 * @param second peer id for the second daemon
418 * @param distance distance between the connected peers
419 * @param first_cfg config for the first daemon
420 * @param second_cfg config for the second daemon
421 * @param first_daemon handle for the first daemon
422 * @param second_daemon handle for the second daemon
423 * @param emsg error message (NULL on success)
426 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
427 const struct GNUNET_PeerIdentity *second, uint32_t distance,
428 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
429 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
430 struct GNUNET_TESTING_Daemon *first_daemon,
431 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "test: Problem with new connection (%s)\n",
448 * run: load configuration options and schedule test to run (start peergroup)
451 * @param cfgfile configuration file name (can be NULL)
452 * @param cfg configuration handle
455 run (void *cls, char *const *args, const char *cfgfile,
456 const struct GNUNET_CONFIGURATION_Handle *cfg)
459 unsigned long long temp_wait;
460 struct GNUNET_TESTING_Host *hosts;
465 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
467 GNUNET_log_setup ("test_mesh_small_unicast",
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
477 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
478 "use_progressbars", "YES");
482 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
483 "num_peers", &num_peers))
485 GNUNET_assert (GNUNET_OK ==
486 GNUNET_CONFIGURATION_load (testing_cfg,
487 "test_mesh_small.conf"));
489 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
490 "num_peers", &num_peers))
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493 "Option TESTING:NUM_PEERS is required!\n");
498 mesh_peers = GNUNET_malloc (sizeof(GNUNET_PEER_Id) * (num_peers + 1));
501 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
502 "wait_time", &temp_wait))
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505 "Option test_mesh_small:wait_time is required!\n");
510 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
511 "topology_output_file",
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 "Option test_mesh_small:topology_output_file is required!\n");
520 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
525 GNUNET_DISK_file_open (data_filename,
526 GNUNET_DISK_OPEN_READWRITE |
527 GNUNET_DISK_OPEN_CREATE,
528 GNUNET_DISK_PERM_USER_READ |
529 GNUNET_DISK_PERM_USER_WRITE);
530 if (data_file == NULL)
532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
534 GNUNET_free (data_filename);
539 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
542 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
543 "output_file", &temp_str))
546 GNUNET_DISK_file_open (temp_str,
547 GNUNET_DISK_OPEN_READWRITE |
548 GNUNET_DISK_OPEN_CREATE,
549 GNUNET_DISK_PERM_USER_READ |
550 GNUNET_DISK_PERM_USER_WRITE);
551 if (output_file == NULL)
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
555 GNUNET_free_non_null (temp_str);
557 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
559 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
560 &connect_cb, &peergroup_ready, NULL,
562 GNUNET_assert (pg != NULL);
564 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
565 &shutdown_task, NULL);
571 * test_mesh_small command line options
573 static struct GNUNET_GETOPT_CommandLineOption options[] = {
574 {'V', "verbose", NULL,
575 gettext_noop ("be verbose (print progress information)"),
576 0, &GNUNET_GETOPT_set_one, &verbose},
577 GNUNET_GETOPT_OPTION_END
585 main (int argc, char *argv[])
587 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small_unicast",
588 gettext_noop ("Test mesh unicast in a small network."), options,
591 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small_unicast");
595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
600 /* end of test_mesh_small_unicast.c */