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 REMOVE_DIR GNUNET_YES
31 #define MESH_REGEX_PEERS 4
34 * How long until we give up on connecting the peers?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39 * Time to wait for stuff that should be rather fast
41 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 * Which strings have been found & connected.
46 static int ok[MESH_REGEX_PEERS];
49 * How many connects have happened.
51 static int regex_peers;
59 * Total number of peers in the test.
61 static unsigned long long num_peers;
64 * Global configuration file
66 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
69 * Total number of currently running peers.
71 static unsigned long long peers_running;
74 * Total number of successful connections in the whole network.
76 static unsigned int total_connections;
79 * Total number of failed connections in the whole network.
81 static unsigned int failed_connections;
84 * The currently running peer group.
86 static struct GNUNET_TESTING_PeerGroup *pg;
89 * Task called to disconnect peers
91 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
94 * Task called to shutdown test.
96 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
99 * Mesh handle for connecting peer.
101 static struct GNUNET_MESH_Handle *h1;
104 * Mesh handle for announcing peers.
106 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
109 * Tunnel handles for announcing peer.
111 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
114 * Incoming tunnels for announcing peers.
116 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
119 * Regular expressions for the announces.
121 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
129 "GNUNETVPN0001000IPEX401110011101100100000111(0|1)*"};
133 * Service strings to look for.
135 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
141 "GNUNETVPN0001000IPEX401110011101100100000111"};
144 * Check whether peers successfully shut down.
146 * @param cls Closure (unused).
147 * @param emsg Error message, NULL on success.
150 shutdown_callback (void *cls, const char *emsg)
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 "test: Shutdown of peers failed! (%s)\n", emsg);
158 for (i = 0; i < MESH_REGEX_PEERS; i++)
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "test: All peers successfully shut down!\n");
166 GNUNET_CONFIGURATION_destroy (testing_cfg);
171 * Task to run for shutdown: stops peers, ends test.
173 * @param cls Closure (not used).
174 * @param tc TaskContext.
178 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
181 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
182 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
187 * Ends test: Disconnects peers and calls shutdown.
188 * @param cls Closure (not used).
189 * @param tc TaskContext.
192 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
198 for (i = 0; i < MESH_REGEX_PEERS; i++)
200 GNUNET_MESH_tunnel_destroy (t[i]);
201 GNUNET_MESH_disconnect (h2[i]);
203 GNUNET_MESH_disconnect (h1);
204 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
206 GNUNET_SCHEDULER_cancel (shutdown_handle);
208 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
212 * Function called whenever an inbound tunnel is destroyed. Should clean up
213 * any associated state.
215 * @param cls closure (set from GNUNET_MESH_connect)
216 * @param tunnel connection to the other end (henceforth invalid)
217 * @param tunnel_ctx place where local state associated
218 * with the tunnel is stored
221 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Incoming tunnel disconnected at peer %d\n",
234 * Method called whenever a tunnel falls apart.
237 * @param peer peer identity the tunnel stopped working with
240 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "peer %s disconnected\n",
249 * Function called to notify a client about the connection
250 * begin ready to queue more data. "buf" will be
251 * NULL and "size" zero if the connection was closed for
252 * writing in the meantime.
255 * @param size number of bytes available in buf
256 * @param buf where the callee should write the message
257 * @return number of bytes written to buf
260 data_ready (void *cls, size_t size, void *buf)
262 struct GNUNET_MessageHeader *m = buf;
264 if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
267 m->size = htons (sizeof(struct GNUNET_MessageHeader));
268 return sizeof(struct GNUNET_MessageHeader);
272 * Method called whenever a peer connects to a tunnel.
275 * @param peer peer identity the tunnel was created to, NULL on timeout
276 * @param atsi performance data for the connection
279 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
280 const struct GNUNET_ATS_Information *atsi)
284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
285 "Peer connected: %s\n",
289 GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
290 GNUNET_TIME_UNIT_FOREVER_REL,
292 sizeof(struct GNUNET_MessageHeader),
297 * Method called whenever another peer has added us to a tunnel
298 * the other peer initiated.
301 * @param tunnel new handle to the tunnel
302 * @param initiator peer that started the tunnel
303 * @param atsi performance information for the tunnel
304 * @return initial tunnel context for the tunnel
305 * (can be NULL -- that's not an error)
308 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
309 const struct GNUNET_PeerIdentity *initiator,
310 const struct GNUNET_ATS_Information *atsi)
313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
314 "Incoming tunnel from %s to peer %d\n",
315 GNUNET_i2s (initiator), (long) cls);
316 if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
318 incoming_t[i - 2] = tunnel;
319 ok[i - 2] = GNUNET_OK;
323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324 "Incoming tunnel for unknown client %lu\n", (long) cls);
326 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
328 GNUNET_SCHEDULER_cancel (disconnect_task);
330 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
336 * Function is called whenever a message is received.
338 * @param cls closure (set from GNUNET_MESH_connect)
339 * @param tunnel connection to the other end
340 * @param tunnel_ctx place to store local state associated with the tunnel
341 * @param sender who sent the message
342 * @param message the actual message
343 * @param atsi performance data for the connection
344 * @return GNUNET_OK to keep the connection open,
345 * GNUNET_SYSERR to close it (signal serious error)
348 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
349 const struct GNUNET_PeerIdentity *sender,
350 const struct GNUNET_MessageHeader *message,
351 const struct GNUNET_ATS_Information *atsi)
354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
355 "test: GOT DATA!\n");
356 for (i = 0; i < MESH_REGEX_PEERS; i++)
358 if (GNUNET_OK != ok[i]) {
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "test: %u DATA MISSING!\n", i);
364 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
365 "test: EVERYONE GOT DATA, FINISHING!\n");
366 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
368 GNUNET_SCHEDULER_cancel (disconnect_task);
370 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
376 * Handlers, for diverse services
378 static struct GNUNET_MESH_MessageHandler handlers[] = {
379 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
385 * peergroup_ready: start test when all peers are connected
387 * @param emsg error message
390 peergroup_ready (void *cls, const char *emsg)
392 GNUNET_MESH_ApplicationType app;
393 struct GNUNET_TESTING_Daemon *d;
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399 "test: Peergroup callback called with error, aborting test!\n");
400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error from testing: `%s'\n",
402 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Peer Group started successfully with %u connections\n",
408 peers_running = GNUNET_TESTING_daemons_running (pg);
409 if (0 < failed_connections)
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
413 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
417 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
418 d = GNUNET_TESTING_daemon_get (pg, 1);
420 app = (GNUNET_MESH_ApplicationType) 0;
422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
423 "Connect to mesh\n");
424 h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
430 for (i = 0; i < MESH_REGEX_PEERS; i++)
433 d = GNUNET_TESTING_daemon_get (pg, 10 + i);
434 h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
440 "Announce REGEX %u: %s\n", i, regexes[i]);
441 GNUNET_MESH_announce_regex (h2[i], regexes[i], 1);
444 for (i = 0; i < MESH_REGEX_PEERS; i++)
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
449 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
450 "Connect by string %s\n", strings[i]);
451 GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
453 /* connect handler = success, timeout = error */
459 * Function that will be called whenever two daemons are connected by
460 * the testing library.
463 * @param first peer id for first daemon
464 * @param second peer id for the second daemon
465 * @param distance distance between the connected peers
466 * @param first_cfg config for the first daemon
467 * @param second_cfg config for the second daemon
468 * @param first_daemon handle for the first daemon
469 * @param second_daemon handle for the second daemon
470 * @param emsg error message (NULL on success)
473 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
474 const struct GNUNET_PeerIdentity *second, uint32_t distance,
475 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
476 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
477 struct GNUNET_TESTING_Daemon *first_daemon,
478 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
486 failed_connections++;
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "test: Problem with new connection (%s)\n", emsg);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
496 * run: load configuration options and schedule test to run (start peergroup)
499 * @param cfgfile configuration file name (can be NULL)
500 * @param cfg configuration handle
503 run (void *cls, char *const *args, const char *cfgfile,
504 const struct GNUNET_CONFIGURATION_Handle *cfg)
506 struct GNUNET_TESTING_Host *hosts;
508 total_connections = 0;
509 failed_connections = 0;
510 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
512 GNUNET_log_setup ("test_mesh_regex",
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
517 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
518 "num_peers", &num_peers))
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521 "Option TESTING:NUM_PEERS is required!\n");
525 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
527 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
528 &connect_cb, &peergroup_ready, NULL,
530 GNUNET_assert (pg != NULL);
532 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
533 &shutdown_task, NULL);
538 * test_mesh_regex command line options
540 static struct GNUNET_GETOPT_CommandLineOption options[] = {
541 {'V', "verbose", NULL,
542 gettext_noop ("be verbose (print progress information)"),
543 0, &GNUNET_GETOPT_set_one, &verbose},
544 GNUNET_GETOPT_OPTION_END
552 main (int argc, char *argv[])
554 char *const argv2[] = {
557 "test_mesh_2dtorus.conf",
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
566 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
568 gettext_noop ("Test mesh regex integration."),
569 options, &run, NULL);
571 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
574 for (i = 0; i < MESH_REGEX_PEERS; i++)
576 if (GNUNET_OK != ok[i])
578 result = GNUNET_SYSERR;
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 "COULD NOT CONNECT TO %u: %s!\n",
584 if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 "test: FAILED! %u connected peers\n",
591 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
595 /* end of test_mesh_regex.c */