2 This file is part of GNUnet.
3 (C) 2012 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_regex.c
23 * @brief Test for regex announce / by_string connect.
24 * based on the 2dtorus testcase
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_mesh_service.h"
30 #define VERBOSE GNUNET_YES
31 #define REMOVE_DIR GNUNET_YES
34 * How long until we give up on connecting the peers?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
39 * Time to wait for stuff that should be rather fast
41 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45 * How many events have happened
55 * Total number of peers in the test.
57 static unsigned long long num_peers;
60 * Global configuration file
62 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
65 * Total number of currently running peers.
67 static unsigned long long peers_running;
70 * Total number of successful connections in the whole network.
72 static unsigned int total_connections;
75 * Total number of failed connections in the whole network.
77 static unsigned int failed_connections;
80 * The currently running peer group.
82 static struct GNUNET_TESTING_PeerGroup *pg;
85 * Task called to disconnect peers
87 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
90 * Task called to shutdown test.
92 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
95 static struct GNUNET_TESTING_Daemon *d1;
97 static struct GNUNET_TESTING_Daemon *d2;
99 static struct GNUNET_MESH_Handle *h1;
101 static struct GNUNET_MESH_Handle *h2;
103 static struct GNUNET_MESH_Tunnel *t;
105 static struct GNUNET_MESH_Tunnel *incoming_t;
108 * Check whether peers successfully shut down.
110 * @param cls Closure (unused).
111 * @param emsg Error message, NULL on success.
114 shutdown_callback (void *cls, const char *emsg)
118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
119 "test: Shutdown of peers failed! (%s)\n", emsg);
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "test: All peers successfully shut down!\n");
129 GNUNET_CONFIGURATION_destroy (testing_cfg);
134 * Task to run for shutdown: stops peers, ends test.
136 * @param cls Closure (not used).
137 * @param tc TaskContext.
141 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
146 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
147 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
152 * Ends test: Disconnects peers and calls shutdown.
153 * @param cls Closure (not used).
154 * @param tc TaskContext.
157 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
161 GNUNET_MESH_tunnel_destroy (t);
162 GNUNET_MESH_disconnect (h1);
163 GNUNET_MESH_disconnect (h2);
164 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
166 GNUNET_SCHEDULER_cancel (shutdown_handle);
168 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
172 * Function called whenever an inbound tunnel is destroyed. Should clean up
173 * any associated state.
175 * @param cls closure (set from GNUNET_MESH_connect)
176 * @param tunnel connection to the other end (henceforth invalid)
177 * @param tunnel_ctx place where local state associated
178 * with the tunnel is stored
181 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
187 "Incoming tunnel disconnected at peer %d\n",
194 * Method called whenever a tunnel falls apart.
197 * @param peer peer identity the tunnel stopped working with
200 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "peer %s disconnected\n",
210 * Method called whenever a peer connects to a tunnel.
213 * @param peer peer identity the tunnel was created to, NULL on timeout
214 * @param atsi performance data for the connection
217 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
218 const struct GNUNET_ATS_Information *atsi)
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "************************************************************\n");
224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
225 "Peer connected: %s\n",
228 if (i != 1L || peer == NULL)
232 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
234 GNUNET_SCHEDULER_cancel (disconnect_task);
236 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
241 * Method called whenever another peer has added us to a tunnel
242 * the other peer initiated.
245 * @param tunnel new handle to the tunnel
246 * @param initiator peer that started the tunnel
247 * @param atsi performance information for the tunnel
248 * @return initial tunnel context for the tunnel
249 * (can be NULL -- that's not an error)
252 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
253 const struct GNUNET_PeerIdentity *initiator,
254 const struct GNUNET_ATS_Information *atsi)
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257 "Incoming tunnel from %s to peer %d\n",
258 GNUNET_i2s (initiator), (long) cls);
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
261 if ((long) cls == 2L)
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266 "Incoming tunnel for unknown client %lu\n", (long) cls);
268 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
270 GNUNET_SCHEDULER_cancel (disconnect_task);
272 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
278 * Function is called whenever a message is received.
280 * @param cls closure (set from GNUNET_MESH_connect)
281 * @param tunnel connection to the other end
282 * @param tunnel_ctx place to store local state associated with the tunnel
283 * @param sender who sent the message
284 * @param message the actual message
285 * @param atsi performance data for the connection
286 * @return GNUNET_OK to keep the connection open,
287 * GNUNET_SYSERR to close it (signal serious error)
290 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
291 const struct GNUNET_PeerIdentity *sender,
292 const struct GNUNET_MessageHeader *message,
293 const struct GNUNET_ATS_Information *atsi)
300 * Handlers, for diverse services
302 static struct GNUNET_MESH_MessageHandler handlers[] = {
303 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
309 * peergroup_ready: start test when all peers are connected
311 * @param emsg error message
314 peergroup_ready (void *cls, const char *emsg)
316 GNUNET_MESH_ApplicationType app;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "test: Peergroup callback called with error, aborting test!\n");
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
325 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
330 "************************************************************\n");
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
332 "test: Peer Group started successfully!\n");
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
337 peers_running = GNUNET_TESTING_daemons_running (pg);
338 if (0 < failed_connections)
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
343 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
348 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
349 d1 = GNUNET_TESTING_daemon_get (pg, 1);
350 d2 = GNUNET_TESTING_daemon_get (pg, 10);
351 app = (GNUNET_MESH_ApplicationType) 0;
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "************************************************************\n");
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 "Connect to mesh\n");
357 h1 = GNUNET_MESH_connect (d1->cfg, 5, (void *) 1L,
362 h2 = GNUNET_MESH_connect (d2->cfg, 5, (void *) 2L,
367 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
368 "************************************************************\n");
369 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371 GNUNET_MESH_announce_regex (h2, "abc");
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
374 "************************************************************\n");
375 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
376 "Create tunnel & connect by string\n");
377 t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, NULL);
378 GNUNET_MESH_peer_request_connect_by_string (t, "abc");
379 /* connect handler = success, timeout = error */
385 * Function that will be called whenever two daemons are connected by
386 * the testing library.
389 * @param first peer id for first daemon
390 * @param second peer id for the second daemon
391 * @param distance distance between the connected peers
392 * @param first_cfg config for the first daemon
393 * @param second_cfg config for the second daemon
394 * @param first_daemon handle for the first daemon
395 * @param second_daemon handle for the second daemon
396 * @param emsg error message (NULL on success)
399 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
400 const struct GNUNET_PeerIdentity *second, uint32_t distance,
401 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
402 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
403 struct GNUNET_TESTING_Daemon *first_daemon,
404 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
412 failed_connections++;
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "test: Problem with new connection (%s)\n", emsg);
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
422 * run: load configuration options and schedule test to run (start peergroup)
425 * @param cfgfile configuration file name (can be NULL)
426 * @param cfg configuration handle
429 run (void *cls, char *const *args, const char *cfgfile,
430 const struct GNUNET_CONFIGURATION_Handle *cfg)
432 struct GNUNET_TESTING_Host *hosts;
435 total_connections = 0;
436 failed_connections = 0;
437 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
439 GNUNET_log_setup ("test_mesh_regex",
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
449 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
450 "use_progressbars", "YES");
454 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
455 "num_peers", &num_peers))
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 "Option TESTING:NUM_PEERS is required!\n");
462 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
464 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
465 &connect_cb, &peergroup_ready, NULL,
467 GNUNET_assert (pg != NULL);
469 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
470 &shutdown_task, NULL);
475 * test_mesh_regex command line options
477 static struct GNUNET_GETOPT_CommandLineOption options[] = {
478 {'V', "verbose", NULL,
479 gettext_noop ("be verbose (print progress information)"),
480 0, &GNUNET_GETOPT_set_one, &verbose},
481 GNUNET_GETOPT_OPTION_END
489 main (int argc, char *argv[])
491 char *const argv2[] = {
494 "test_mesh_2dtorus.conf",
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
505 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
507 gettext_noop ("Test mesh regex integration."),
508 options, &run, NULL);
510 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
521 /* end of test_mesh_regex.c */