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.c
23 * @brief Test for the mesh service: retransmission of unicast traffic.
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;
51 * How long until we give up on connecting the peers?
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
56 * Time to wait for stuff that should be rather fast
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
70 * Total number of peers in the test.
72 static unsigned long long num_peers;
75 * Global configuration file
77 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
80 * Total number of currently running peers.
82 static unsigned long long peers_running;
85 * Total number of connections in the whole network.
87 static unsigned int total_connections;
90 * The currently running peer group.
92 static struct GNUNET_TESTING_PeerGroup *pg;
95 * File to report results to.
97 static struct GNUNET_DISK_FileHandle *output_file;
100 * File to log connection info, statistics to.
102 static struct GNUNET_DISK_FileHandle *data_file;
105 * How many data points to capture before triggering next round?
107 static struct GNUNET_TIME_Relative wait_time;
110 * Task called to disconnect peers.
112 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
115 * Task To perform tests
117 static GNUNET_SCHEDULER_TaskIdentifier test_task;
120 * Task called to shutdown test.
122 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
124 static char *topology_file;
126 static char *data_filename;
128 static struct GNUNET_TESTING_Daemon *d1;
130 static GNUNET_PEER_Id pid1;
132 static struct GNUNET_TESTING_Daemon *d2;
134 static struct GNUNET_MESH_Handle *h1;
136 static struct GNUNET_MESH_Handle *h2;
138 static struct GNUNET_MESH_Tunnel *t;
140 static struct GNUNET_MESH_Tunnel *incoming_t;
142 static uint16_t *mesh_peers;
145 * Check whether peers successfully shut down.
148 shutdown_callback (void *cls, const char *emsg)
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
167 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
173 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
175 GNUNET_SCHEDULER_cancel (disconnect_task);
176 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
179 if (data_file != NULL)
180 GNUNET_DISK_file_close (data_file);
181 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
182 GNUNET_CONFIGURATION_destroy (testing_cfg);
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 * Transmit ready callback
203 tmt_rdy (void *cls, size_t size, void *buf)
205 struct GNUNET_MessageHeader *msg = buf;
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: tmt_rdy called\n");
208 if (size < sizeof(struct GNUNET_MessageHeader) || NULL == buf)
210 msg->size = htons (sizeof(struct GNUNET_MessageHeader));
211 msg->type = htons ((long) cls);
212 return sizeof(struct GNUNET_MessageHeader);
217 * Function is called whenever a message is received.
219 * @param cls closure (set from GNUNET_MESH_connect)
220 * @param tunnel connection to the other end
221 * @param tunnel_ctx place to store local state associated with the tunnel
222 * @param sender who sent the message
223 * @param message the actual message
224 * @param atsi performance data for the connection
225 * @return GNUNET_OK to keep the connection open,
226 * GNUNET_SYSERR to close it (signal serious error)
229 data_callback (void *cls,
230 struct GNUNET_MESH_Tunnel * tunnel,
232 const struct GNUNET_PeerIdentity *sender,
233 const struct GNUNET_MessageHeader *message,
234 const struct GNUNET_ATS_Information *atsi)
236 long client = (long) cls;
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "test: Origin client got a response!\n");
244 GNUNET_SCHEDULER_cancel (disconnect_task);
245 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_mesh_peers,
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "test: Destination client got a message \n");
252 GNUNET_MESH_notify_transmit_ready(incoming_t,
255 GNUNET_TIME_UNIT_FOREVER_REL,
257 sizeof(struct GNUNET_MessageHeader),
260 GNUNET_SCHEDULER_cancel (disconnect_task);
261 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
262 &disconnect_mesh_peers,
273 * Handlers, for diverse services
275 static struct GNUNET_MESH_MessageHandler handlers[] = {
276 {&data_callback, 1, sizeof(struct GNUNET_MessageHeader)},
282 * Method called whenever another peer has added us to a tunnel
283 * the other peer initiated.
286 * @param tunnel new handle to the tunnel
287 * @param initiator peer that started the tunnel
288 * @param atsi performance information for the tunnel
289 * @return initial tunnel context for the tunnel
290 * (can be NULL -- that's not an error)
293 incoming_tunnel (void *cls,
294 struct GNUNET_MESH_Tunnel * tunnel,
295 const struct GNUNET_PeerIdentity * initiator,
296 const struct GNUNET_ATS_Information * atsi)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "test: Incoming tunnel from %s\n",
300 GNUNET_i2s(initiator));
303 GNUNET_SCHEDULER_cancel (disconnect_task);
304 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
305 &disconnect_mesh_peers,
311 * Function called whenever an inbound tunnel is destroyed. Should clean up
312 * any associated state.
314 * @param cls closure (set from GNUNET_MESH_connect)
315 * @param tunnel connection to the other end (henceforth invalid)
316 * @param tunnel_ctx place where local state associated
317 * with the tunnel is stored
320 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: tunnel disconnected\n");
332 * Method called whenever a tunnel falls apart.
335 * @param peer peer identity the tunnel stopped working with
338 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "test: peer %s disconnected\n",
348 * Method called whenever a tunnel is established.
351 * @param peer peer identity the tunnel was created to, NULL on timeout
352 * @param atsi performance data for the connection
355 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
356 const struct GNUNET_ATS_Information *atsi)
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "test: peer %s connected\n",
361 if (0 == memcmp(&d2->id, peer, sizeof(d2->id)) && (long) cls == 1L)
363 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
365 GNUNET_SCHEDULER_cancel (disconnect_task);
366 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
367 &disconnect_mesh_peers,
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Sending data unicast...\n");
370 GNUNET_MESH_notify_transmit_ready(t,
373 GNUNET_TIME_UNIT_FOREVER_REL,
375 sizeof(struct GNUNET_MessageHeader),
384 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
387 GNUNET_MESH_peer_request_connect_add(t, &d2->id);
388 GNUNET_SCHEDULER_cancel (disconnect_task);
389 disconnect_task = GNUNET_SCHEDULER_add_delayed(
390 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
391 &disconnect_mesh_peers, NULL);
396 * Prototype of a callback function indicating that two peers
397 * are currently connected.
400 * @param first peer id for first daemon
401 * @param second peer id for the second daemon
402 * @param distance distance between the connected peers
403 * @param emsg error message (NULL on success)
407 const struct GNUNET_PeerIdentity* first,
408 const struct GNUNET_PeerIdentity* second,
413 struct GNUNET_PeerIdentity id;
415 GNUNET_PEER_resolve(1, &id);
416 p1 = GNUNET_PEER_search(first);
419 p2 = GNUNET_PEER_search(second);
420 GNUNET_assert(p2 < num_peers);
421 GNUNET_assert(p2 > 0);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "test: %s IS a neighbor\n",
428 p1 = GNUNET_PEER_search(second);
431 p2 = GNUNET_PEER_search(first);
432 GNUNET_assert(p2 < num_peers);
433 GNUNET_assert(p2 > 0);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "test: %s IS a neighbor\n",
443 * connect_mesh_service: connect to the mesh service of one of the peers
447 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
449 GNUNET_MESH_ApplicationType app;
451 struct GNUNET_PeerIdentity id;
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: connect_mesh_service\n");
455 for (i = 1; i <= num_peers; i++)
457 GNUNET_PEER_resolve(i, &id);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "test: peer %s has %u conns to d1\n",
462 if (mesh_peers[i] == 0)
465 GNUNET_assert (i < num_peers);
466 d2 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "test: Peer searched: %s\n",
469 GNUNET_i2s (&d2->id));
470 app = (GNUNET_MESH_ApplicationType) 0;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "test: connecting to mesh service of peer %s (%u)\n",
475 GNUNET_i2s (&d1->id),
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "test: connecting to mesh service of peer %s (%u)\n",
479 GNUNET_i2s (&d2->id),
482 h1 = GNUNET_MESH_connect (d1->cfg,
489 h2 = GNUNET_MESH_connect (d2->cfg,
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "test: connected to mesh service of peer %s\n",
499 GNUNET_i2s (&d1->id));
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "test: connected to mesh service of peer %s\n",
502 GNUNET_i2s (&d2->id));
504 t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
506 GNUNET_SCHEDULER_add_delayed(
507 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 6),
514 * peergroup_ready: start test when all peers are connected
516 * @param emsg error message
519 peergroup_ready (void *cls, const char *emsg)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "test: Peergroup callback called with error, aborting test!\n");
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "test: Error from testing: `%s'\n", emsg);
532 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "************************************************************\n");
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "test: Peer Group started successfully!\n");
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "test: Have %u connections\n",
545 if (data_file != NULL)
548 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
550 GNUNET_DISK_file_write (data_file, buf, buf_len);
553 peers_running = GNUNET_TESTING_daemons_running (pg);
554 for (i = 0; i < num_peers; i++)
556 d1 = GNUNET_TESTING_daemon_get (pg, i);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 GNUNET_PEER_intern(&d1->id),
560 GNUNET_i2s (&d1->id));
562 d1 = GNUNET_TESTING_daemon_get (pg, 0);
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "test: Peer looking: %s\n",
565 GNUNET_i2s (&d1->id));
566 pid1 = GNUNET_PEER_intern(&d1->id);
567 mesh_peers[pid1] = 100;
568 GNUNET_TESTING_get_topology(pg, &topo_cb, NULL);
570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
571 GNUNET_TIME_UNIT_SECONDS,
573 &connect_mesh_service,
576 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
582 * Function that will be called whenever two daemons are connected by
583 * the testing library.
586 * @param first peer id for first daemon
587 * @param second peer id for the second daemon
588 * @param distance distance between the connected peers
589 * @param first_cfg config for the first daemon
590 * @param second_cfg config for the second daemon
591 * @param first_daemon handle for the first daemon
592 * @param second_daemon handle for the second daemon
593 * @param emsg error message (NULL on success)
596 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
597 const struct GNUNET_PeerIdentity *second, uint32_t distance,
598 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
599 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
600 struct GNUNET_TESTING_Daemon *first_daemon,
601 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "test: Problem with new connection (%s)\n",
618 * run: load configuration options and schedule test to run (start peergroup)
621 * @param cfgfile configuration file name (can be NULL)
622 * @param cfg configuration handle
625 run (void *cls, char *const *args, const char *cfgfile,
626 const struct GNUNET_CONFIGURATION_Handle *cfg)
629 unsigned long long temp_wait;
630 struct GNUNET_TESTING_Host *hosts;
633 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
635 GNUNET_log_setup ("test_mesh_small_unicast",
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
645 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
646 "use_progressbars", "YES");
650 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
651 "num_peers", &num_peers))
653 GNUNET_assert (GNUNET_OK ==
654 GNUNET_CONFIGURATION_load (testing_cfg,
655 "test_mesh_small.conf"));
657 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
658 "num_peers", &num_peers))
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 "Option TESTING:NUM_PEERS is required!\n");
666 mesh_peers = GNUNET_malloc (sizeof(GNUNET_PEER_Id) * (num_peers + 1));
669 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
670 "wait_time", &temp_wait))
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 "Option test_mesh_small:wait_time is required!\n");
678 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
679 "topology_output_file",
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
683 "Option test_mesh_small:topology_output_file is required!\n");
688 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
693 "Option test_mesh_small:data_output_file is required!\n");
698 GNUNET_DISK_file_open (data_filename,
699 GNUNET_DISK_OPEN_READWRITE |
700 GNUNET_DISK_OPEN_CREATE,
701 GNUNET_DISK_PERM_USER_READ |
702 GNUNET_DISK_PERM_USER_WRITE);
703 if (data_file == NULL)
705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
707 GNUNET_free (data_filename);
711 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
714 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
715 "output_file", &temp_str))
718 GNUNET_DISK_file_open (temp_str,
719 GNUNET_DISK_OPEN_READWRITE |
720 GNUNET_DISK_OPEN_CREATE,
721 GNUNET_DISK_PERM_USER_READ |
722 GNUNET_DISK_PERM_USER_WRITE);
723 if (output_file == NULL)
724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
727 GNUNET_free_non_null (temp_str);
729 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
731 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
732 &connect_cb, &peergroup_ready, NULL,
734 GNUNET_assert (pg != NULL);
736 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
737 &shutdown_task, NULL);
743 * test_mesh_small command line options
745 static struct GNUNET_GETOPT_CommandLineOption options[] = {
746 {'V', "verbose", NULL,
747 gettext_noop ("be verbose (print progress information)"),
748 0, &GNUNET_GETOPT_set_one, &verbose},
749 GNUNET_GETOPT_OPTION_END
757 main (int argc, char *argv[])
759 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small_unicast",
760 gettext_noop ("Test mesh unicast in a small network."),
761 options, &run, NULL);
763 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small_unicast");
767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
768 "test: FAILED! (%d/%d)\n",
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
776 /* end of test_mesh_small_unicast.c */