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: retransmission of 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, 30)
61 * DIFFERENT TESTS TO RUN
68 * Which test are we running?
73 * How many events have happened
77 static int peers_in_tunnel;
79 static int peers_responded;
87 * Total number of peers in the test.
89 static unsigned long long num_peers;
92 * Global configuration file
94 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
97 * Total number of currently running peers.
99 static unsigned long long peers_running;
102 * Total number of connections in the whole network.
104 static unsigned int total_connections;
107 * The currently running peer group.
109 static struct GNUNET_TESTING_PeerGroup *pg;
112 * File to report results to.
114 static struct GNUNET_DISK_FileHandle *output_file;
117 * File to log connection info, statistics to.
119 static struct GNUNET_DISK_FileHandle *data_file;
122 * How many data points to capture before triggering next round?
124 static struct GNUNET_TIME_Relative wait_time;
127 * Task called to disconnect peers.
129 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
132 * Task To perform tests
134 static GNUNET_SCHEDULER_TaskIdentifier test_task;
137 * Task called to shutdown test.
139 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
141 static char *topology_file;
143 static char *data_filename;
145 static struct GNUNET_TESTING_Daemon *d1;
147 static GNUNET_PEER_Id pid1;
149 static struct GNUNET_TESTING_Daemon *d2;
151 static struct GNUNET_TESTING_Daemon *d3;
153 static struct GNUNET_MESH_Handle *h1;
155 static struct GNUNET_MESH_Handle *h2;
157 static struct GNUNET_MESH_Handle *h3;
159 static struct GNUNET_MESH_Tunnel *t;
161 static struct GNUNET_MESH_Tunnel *incoming_t;
163 static struct GNUNET_MESH_Tunnel *incoming_t2;
165 static uint16_t *mesh_peers;
168 * Check whether peers successfully shut down.
171 shutdown_callback (void *cls, const char *emsg)
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Shutdown of peers failed!\n");
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: All peers successfully shut down!\n");
190 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Ending test.\n");
196 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
198 GNUNET_SCHEDULER_cancel (disconnect_task);
199 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
202 if (data_file != NULL)
203 GNUNET_DISK_file_close (data_file);
204 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
205 GNUNET_CONFIGURATION_destroy (testing_cfg);
210 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "***************** test: disconnecting mesh service of peers\n");
214 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
215 GNUNET_MESH_disconnect(h1);
216 GNUNET_MESH_disconnect(h2);
217 if (test == MULTICAST)
218 GNUNET_MESH_disconnect(h3);
219 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
221 GNUNET_SCHEDULER_cancel (shutdown_handle);
222 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
228 * Transmit ready callback
231 tmt_rdy (void *cls, size_t size, void *buf)
233 struct GNUNET_MessageHeader *msg = buf;
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: tmt_rdy called\n");
236 if (size < sizeof(struct GNUNET_MessageHeader) || NULL == buf)
238 msg->size = htons (sizeof(struct GNUNET_MessageHeader));
239 msg->type = htons ((long) cls);
240 return sizeof(struct GNUNET_MessageHeader);
245 * Function is called whenever a message is received.
247 * @param cls closure (set from GNUNET_MESH_connect)
248 * @param tunnel connection to the other end
249 * @param tunnel_ctx place to store local state associated with the tunnel
250 * @param sender who sent the message
251 * @param message the actual message
252 * @param atsi performance data for the connection
253 * @return GNUNET_OK to keep the connection open,
254 * GNUNET_SYSERR to close it (signal serious error)
257 data_callback (void *cls,
258 struct GNUNET_MESH_Tunnel * tunnel,
260 const struct GNUNET_PeerIdentity *sender,
261 const struct GNUNET_MessageHeader *message,
262 const struct GNUNET_ATS_Information *atsi)
264 long client = (long) cls;
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "***************** test: Origin client got a response!\n");
273 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
275 GNUNET_SCHEDULER_cancel (disconnect_task);
276 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
277 &disconnect_mesh_peers,
280 if (test == MULTICAST && peers_responded < 2)
282 GNUNET_MESH_tunnel_destroy (tunnel);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "***************** test: Destination client %u got a message.\n",
290 GNUNET_MESH_notify_transmit_ready(tunnel,
293 GNUNET_TIME_UNIT_FOREVER_REL,
295 sizeof(struct GNUNET_MessageHeader),
298 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
300 GNUNET_SCHEDULER_cancel (disconnect_task);
301 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
302 &disconnect_mesh_peers,
314 * Handlers, for diverse services
316 static struct GNUNET_MESH_MessageHandler handlers[] = {
317 {&data_callback, 1, sizeof(struct GNUNET_MessageHeader)},
323 * Method called whenever another peer has added us to a tunnel
324 * the other peer initiated.
327 * @param tunnel new handle to the tunnel
328 * @param initiator peer that started the tunnel
329 * @param atsi performance information for the tunnel
330 * @return initial tunnel context for the tunnel
331 * (can be NULL -- that's not an error)
334 incoming_tunnel (void *cls,
335 struct GNUNET_MESH_Tunnel * tunnel,
336 const struct GNUNET_PeerIdentity * initiator,
337 const struct GNUNET_ATS_Information * atsi)
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "***************** test: Incoming tunnel from %s to peer %d\n",
341 GNUNET_i2s(initiator),
344 if ((long) cls == 1L)
347 incoming_t2 = tunnel;
348 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
350 GNUNET_SCHEDULER_cancel (disconnect_task);
351 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
352 &disconnect_mesh_peers,
359 * Function called whenever an inbound tunnel is destroyed. Should clean up
360 * any associated state.
362 * @param cls closure (set from GNUNET_MESH_connect)
363 * @param tunnel connection to the other end (henceforth invalid)
364 * @param tunnel_ctx place where local state associated
365 * with the tunnel is stored
368 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "***************** test: Incoming tunnel disconnected at peer %d\n",
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "***************** test: Unknown peer! %d\n",
385 if (peers_in_tunnel > 0)
388 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
390 GNUNET_SCHEDULER_cancel (disconnect_task);
391 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, NULL);
399 * Method called whenever a tunnel falls apart.
402 * @param peer peer identity the tunnel stopped working with
405 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "***************** test: peer %s disconnected\n",
415 * Method called whenever a peer connects to a tunnel.
418 * @param peer peer identity the tunnel was created to, NULL on timeout
419 * @param atsi performance data for the connection
422 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
423 const struct GNUNET_ATS_Information *atsi)
425 struct GNUNET_PeerIdentity *dest;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "***************** test: peer %s connected\n",
431 if (0 == memcmp(&d2->id, peer, sizeof(d2->id)) && (long) cls == 1L)
433 if (test == MULTICAST &&
434 0 == memcmp(&d3->id, peer, sizeof(d3->id)) &&
444 if (peers_in_tunnel < 2)
451 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
453 GNUNET_SCHEDULER_cancel (disconnect_task);
454 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
455 &disconnect_mesh_peers,
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "***************** test: Sending data...\n");
460 GNUNET_MESH_notify_transmit_ready(t,
463 GNUNET_TIME_UNIT_FOREVER_REL,
465 sizeof(struct GNUNET_MessageHeader),
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Disconnect already run?\n");
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Aborting...\n");
479 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: test_task\n");
482 GNUNET_MESH_peer_request_connect_add(t, &d2->id);
483 if (test == MULTICAST)
485 GNUNET_MESH_peer_request_connect_add(t, &d3->id);
487 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
489 GNUNET_SCHEDULER_cancel (disconnect_task);
490 disconnect_task = GNUNET_SCHEDULER_add_delayed(
491 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
492 &disconnect_mesh_peers,
499 * Prototype of a callback function indicating that two peers
500 * are currently connected.
503 * @param first peer id for first daemon
504 * @param second peer id for the second daemon
505 * @param distance distance between the connected peers
506 * @param emsg error message (NULL on success)
510 const struct GNUNET_PeerIdentity* first,
511 const struct GNUNET_PeerIdentity* second,
516 struct GNUNET_PeerIdentity id;
518 GNUNET_PEER_resolve(1, &id);
519 p1 = GNUNET_PEER_search(first);
522 p2 = GNUNET_PEER_search(second);
523 GNUNET_assert(p2 < num_peers);
524 GNUNET_assert(p2 > 0);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "***************** test: %s IS a neighbor\n",
531 p1 = GNUNET_PEER_search(second);
534 p2 = GNUNET_PEER_search(first);
535 GNUNET_assert(p2 < num_peers);
536 GNUNET_assert(p2 > 0);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "***************** test: %s IS a neighbor\n",
546 * connect_mesh_service: connect to the mesh service of one of the peers
550 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552 GNUNET_MESH_ApplicationType app;
554 struct GNUNET_PeerIdentity id;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: connect_mesh_service\n");
558 for (i = 1; i <= num_peers; i++)
560 GNUNET_PEER_resolve(i, &id);
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "***************** test: peer %s has %u conns to d1\n",
565 if (mesh_peers[i] == 0)
568 GNUNET_assert (i < num_peers);
569 d2 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
570 if (test == MULTICAST)
572 for (i++; i <= num_peers; i++)
574 GNUNET_PEER_resolve(i, &id);
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "***************** test: peer %s has %u conns to d1\n",
579 if (mesh_peers[i] == 0)
582 GNUNET_assert (i < num_peers);
583 d3 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
585 app = (GNUNET_MESH_ApplicationType) 0;
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "***************** test: connecting to mesh service of peer %s (%u)\n",
590 GNUNET_i2s (&d1->id),
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "***************** test: connecting to mesh service of peer %s (%u)\n",
594 GNUNET_i2s (&d2->id),
596 if (test == MULTICAST)
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "***************** test: connecting to mesh service of peer %s (%u)\n",
600 GNUNET_i2s (&d3->id),
604 h1 = GNUNET_MESH_connect (d1->cfg,
611 h2 = GNUNET_MESH_connect (d2->cfg,
618 if (test == MULTICAST)
620 h3 = GNUNET_MESH_connect (d3->cfg,
628 t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
631 GNUNET_SCHEDULER_add_delayed(
632 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 6),
639 * peergroup_ready: start test when all peers are connected
641 * @param emsg error message
644 peergroup_ready (void *cls, const char *emsg)
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "***************** test: Peergroup callback called with error, aborting test!\n");
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "***************** test: Error from testing: `%s'\n", emsg);
657 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "************************************************************\n");
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "***************** test: Peer Group started successfully!\n");
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "***************** test: Have %u connections\n",
670 if (data_file != NULL)
673 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
675 GNUNET_DISK_file_write (data_file, buf, buf_len);
678 peers_running = GNUNET_TESTING_daemons_running (pg);
679 for (i = 0; i < num_peers; i++)
681 d1 = GNUNET_TESTING_daemon_get (pg, i);
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "***************** test: %u: %s\n",
684 GNUNET_PEER_intern(&d1->id),
685 GNUNET_i2s (&d1->id));
687 d1 = GNUNET_TESTING_daemon_get (pg, 0);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "***************** test: Peer looking: %s\n",
690 GNUNET_i2s (&d1->id));
691 pid1 = GNUNET_PEER_intern(&d1->id);
692 mesh_peers[pid1] = 100;
693 GNUNET_TESTING_get_topology(pg, &topo_cb, NULL);
695 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
696 GNUNET_TIME_UNIT_SECONDS,
698 &connect_mesh_service,
701 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
707 * Function that will be called whenever two daemons are connected by
708 * the testing library.
711 * @param first peer id for first daemon
712 * @param second peer id for the second daemon
713 * @param distance distance between the connected peers
714 * @param first_cfg config for the first daemon
715 * @param second_cfg config for the second daemon
716 * @param first_daemon handle for the first daemon
717 * @param second_daemon handle for the second daemon
718 * @param emsg error message (NULL on success)
721 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
722 const struct GNUNET_PeerIdentity *second, uint32_t distance,
723 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
724 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
725 struct GNUNET_TESTING_Daemon *first_daemon,
726 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "***************** test: Problem with new connection (%s)\n",
743 * run: load configuration options and schedule test to run (start peergroup)
746 * @param cfgfile configuration file name (can be NULL)
747 * @param cfg configuration handle
750 run (void *cls, char *const *args, const char *cfgfile,
751 const struct GNUNET_CONFIGURATION_Handle *cfg)
754 unsigned long long temp_wait;
755 struct GNUNET_TESTING_Host *hosts;
758 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
760 GNUNET_log_setup ("test_mesh_small",
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Starting daemons.\n");
770 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
771 "use_progressbars", "YES");
775 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
776 "num_peers", &num_peers))
778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
779 "Option TESTING:NUM_PEERS is required!\n");
783 mesh_peers = GNUNET_malloc (sizeof(GNUNET_PEER_Id) * (num_peers + 1));
786 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
787 "wait_time", &temp_wait))
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790 "Option test_mesh_small:wait_time is required!\n");
795 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
796 "topology_output_file",
799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
800 "Option test_mesh_small:topology_output_file is required!\n");
805 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
810 "Option test_mesh_small:data_output_file is required!\n");
815 GNUNET_DISK_file_open (data_filename,
816 GNUNET_DISK_OPEN_READWRITE |
817 GNUNET_DISK_OPEN_CREATE,
818 GNUNET_DISK_PERM_USER_READ |
819 GNUNET_DISK_PERM_USER_WRITE);
820 if (data_file == NULL)
822 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
824 GNUNET_free (data_filename);
828 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
831 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
832 "output_file", &temp_str))
835 GNUNET_DISK_file_open (temp_str,
836 GNUNET_DISK_OPEN_READWRITE |
837 GNUNET_DISK_OPEN_CREATE,
838 GNUNET_DISK_PERM_USER_READ |
839 GNUNET_DISK_PERM_USER_WRITE);
840 if (output_file == NULL)
841 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
844 GNUNET_free_non_null (temp_str);
846 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
848 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
849 &connect_cb, &peergroup_ready, NULL,
851 GNUNET_assert (pg != NULL);
853 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
854 &shutdown_task, NULL);
860 * test_mesh_small command line options
862 static struct GNUNET_GETOPT_CommandLineOption options[] = {
863 {'V', "verbose", NULL,
864 gettext_noop ("be verbose (print progress information)"),
865 0, &GNUNET_GETOPT_set_one, &verbose},
866 GNUNET_GETOPT_OPTION_END
874 main (int argc, char *argv[])
876 char *const argv2[] = {
879 "test_mesh_small.conf",
887 /* Each peer is supposed to generate the following callbacks:
888 * 1 incoming tunnel (@dest)
889 * 1 connected peer (@orig)
890 * 1 received data packet (@dest)
891 * 1 received data packet (@orig)
892 * 1 received tunnel destroy (@dest)
893 * _________________________________
894 * 5 x ok expected per peer
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: Start\n");
899 if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: UNICAST\n");
905 else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: MULTICAST\n");
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "***************** test: UNKNOWN\n");
918 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
920 gettext_noop ("Test mesh in a small network."),
921 options, &run, NULL);
923 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
928 "***************** test: FAILED! (%d/%d)\n", ok, ok_goal);
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "***************** test: success\n");
936 /* end of test_mesh_small.c */