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
32 #define MESH_REGEX_PEERS 4
35 * How long until we give up on connecting the peers?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 * Time to wait for stuff that should be rather fast
42 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45 * Which strings have been found & connected.
47 static int ok[MESH_REGEX_PEERS];
50 * How many connects have happened.
52 static int regex_peers;
60 * Total number of peers in the test.
62 static unsigned long long num_peers;
65 * Global configuration file
67 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
70 * Total number of currently running peers.
72 static unsigned long long peers_running;
75 * Total number of successful connections in the whole network.
77 static unsigned int total_connections;
80 * Total number of failed connections in the whole network.
82 static unsigned int failed_connections;
85 * The currently running peer group.
87 static struct GNUNET_TESTING_PeerGroup *pg;
90 * Task called to disconnect peers
92 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
95 * Task called to shutdown test.
97 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
100 * Mesh handle for connecting peer.
102 static struct GNUNET_MESH_Handle *h1;
105 * Mesh handle for announcing peers.
107 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
110 * Tunnel handles for announcing peer.
112 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
115 * Incoming tunnels for announcing peers.
117 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
120 * Regular expressions for the announces.
122 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
130 "GNUNETVPN0001000IPEX401110011101100100000111(0|1)*"};
134 * Service strings to look for.
136 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
142 "GNUNETVPN0001000IPEX401110011101100100000111"};
145 * Check whether peers successfully shut down.
147 * @param cls Closure (unused).
148 * @param emsg Error message, NULL on success.
151 shutdown_callback (void *cls, const char *emsg)
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
158 "test: Shutdown of peers failed! (%s)\n", emsg);
159 for (i = 0; i < MESH_REGEX_PEERS; i++)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "test: All peers successfully shut down!\n");
169 GNUNET_CONFIGURATION_destroy (testing_cfg);
174 * Task to run for shutdown: stops peers, ends test.
176 * @param cls Closure (not used).
177 * @param tc TaskContext.
181 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
186 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
187 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
192 * Ends test: Disconnects peers and calls shutdown.
193 * @param cls Closure (not used).
194 * @param tc TaskContext.
197 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
203 for (i = 0; i < MESH_REGEX_PEERS; i++)
205 GNUNET_MESH_tunnel_destroy (t[i]);
206 GNUNET_MESH_disconnect (h2[i]);
208 GNUNET_MESH_disconnect (h1);
209 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
211 GNUNET_SCHEDULER_cancel (shutdown_handle);
213 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
217 * Function called whenever an inbound tunnel is destroyed. Should clean up
218 * any associated state.
220 * @param cls closure (set from GNUNET_MESH_connect)
221 * @param tunnel connection to the other end (henceforth invalid)
222 * @param tunnel_ctx place where local state associated
223 * with the tunnel is stored
226 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Incoming tunnel disconnected at peer %d\n",
239 * Method called whenever a tunnel falls apart.
242 * @param peer peer identity the tunnel stopped working with
245 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "peer %s disconnected\n",
254 * Function called to notify a client about the connection
255 * begin ready to queue more data. "buf" will be
256 * NULL and "size" zero if the connection was closed for
257 * writing in the meantime.
260 * @param size number of bytes available in buf
261 * @param buf where the callee should write the message
262 * @return number of bytes written to buf
265 data_ready (void *cls, size_t size, void *buf)
267 struct GNUNET_MessageHeader *m = buf;
269 if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
272 m->size = htons (sizeof(struct GNUNET_MessageHeader));
273 return sizeof(struct GNUNET_MessageHeader);
277 * Method called whenever a peer connects to a tunnel.
280 * @param peer peer identity the tunnel was created to, NULL on timeout
281 * @param atsi performance data for the connection
284 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
285 const struct GNUNET_ATS_Information *atsi)
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 "Peer connected: %s\n",
294 GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
295 GNUNET_TIME_UNIT_FOREVER_REL,
297 sizeof(struct GNUNET_MessageHeader),
302 * Method called whenever another peer has added us to a tunnel
303 * the other peer initiated.
306 * @param tunnel new handle to the tunnel
307 * @param initiator peer that started the tunnel
308 * @param atsi performance information for the tunnel
309 * @return initial tunnel context for the tunnel
310 * (can be NULL -- that's not an error)
313 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
314 const struct GNUNET_PeerIdentity *initiator,
315 const struct GNUNET_ATS_Information *atsi)
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
319 "Incoming tunnel from %s to peer %d\n",
320 GNUNET_i2s (initiator), (long) cls);
321 if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
323 incoming_t[i - 2] = tunnel;
324 ok[i - 2] = GNUNET_OK;
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
329 "Incoming tunnel for unknown client %lu\n", (long) cls);
331 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
333 GNUNET_SCHEDULER_cancel (disconnect_task);
335 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
341 * Function is called whenever a message is received.
343 * @param cls closure (set from GNUNET_MESH_connect)
344 * @param tunnel connection to the other end
345 * @param tunnel_ctx place to store local state associated with the tunnel
346 * @param sender who sent the message
347 * @param message the actual message
348 * @param atsi performance data for the connection
349 * @return GNUNET_OK to keep the connection open,
350 * GNUNET_SYSERR to close it (signal serious error)
353 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
354 const struct GNUNET_PeerIdentity *sender,
355 const struct GNUNET_MessageHeader *message,
356 const struct GNUNET_ATS_Information *atsi)
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360 "test: GOT DATA!\n");
361 for (i = 0; i < MESH_REGEX_PEERS; i++)
363 if (GNUNET_OK != ok[i]) {
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "test: %u DATA MISSING!\n", i);
369 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
370 "test: EVERYONE GOT DATA, FINISHING!\n");
371 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
373 GNUNET_SCHEDULER_cancel (disconnect_task);
375 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
381 * Handlers, for diverse services
383 static struct GNUNET_MESH_MessageHandler handlers[] = {
384 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
390 * peergroup_ready: start test when all peers are connected
392 * @param emsg error message
395 peergroup_ready (void *cls, const char *emsg)
397 GNUNET_MESH_ApplicationType app;
398 struct GNUNET_TESTING_Daemon *d;
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
404 "test: Peergroup callback called with error, aborting test!\n");
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error from testing: `%s'\n",
407 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
411 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
412 "test: Peer Group started successfully!\n");
413 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
417 peers_running = GNUNET_TESTING_daemons_running (pg);
418 if (0 < failed_connections)
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
422 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
426 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
427 d = GNUNET_TESTING_daemon_get (pg, 1);
429 app = (GNUNET_MESH_ApplicationType) 0;
431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432 "Connect to mesh\n");
433 h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
439 for (i = 0; i < MESH_REGEX_PEERS; i++)
442 d = GNUNET_TESTING_daemon_get (pg, 10 + i);
443 h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
448 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
449 "Announce REGEX %u: %s\n", i, regexes[i]);
450 GNUNET_MESH_announce_regex (h2[i], regexes[i], 1);
453 for (i = 0; i < MESH_REGEX_PEERS; i++)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
458 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
459 "Connect by string %s\n", strings[i]);
460 GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
462 /* connect handler = success, timeout = error */
468 * Function that will be called whenever two daemons are connected by
469 * the testing library.
472 * @param first peer id for first daemon
473 * @param second peer id for the second daemon
474 * @param distance distance between the connected peers
475 * @param first_cfg config for the first daemon
476 * @param second_cfg config for the second daemon
477 * @param first_daemon handle for the first daemon
478 * @param second_daemon handle for the second daemon
479 * @param emsg error message (NULL on success)
482 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
483 const struct GNUNET_PeerIdentity *second, uint32_t distance,
484 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
485 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
486 struct GNUNET_TESTING_Daemon *first_daemon,
487 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
495 failed_connections++;
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "test: Problem with new connection (%s)\n", emsg);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
505 * run: load configuration options and schedule test to run (start peergroup)
508 * @param cfgfile configuration file name (can be NULL)
509 * @param cfg configuration handle
512 run (void *cls, char *const *args, const char *cfgfile,
513 const struct GNUNET_CONFIGURATION_Handle *cfg)
515 struct GNUNET_TESTING_Host *hosts;
517 total_connections = 0;
518 failed_connections = 0;
519 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
521 GNUNET_log_setup ("test_mesh_regex",
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
531 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
532 "use_progressbars", "YES");
536 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
537 "num_peers", &num_peers))
539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
540 "Option TESTING:NUM_PEERS is required!\n");
544 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
546 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
547 &connect_cb, &peergroup_ready, NULL,
549 GNUNET_assert (pg != NULL);
551 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
552 &shutdown_task, NULL);
557 * test_mesh_regex command line options
559 static struct GNUNET_GETOPT_CommandLineOption options[] = {
560 {'V', "verbose", NULL,
561 gettext_noop ("be verbose (print progress information)"),
562 0, &GNUNET_GETOPT_set_one, &verbose},
563 GNUNET_GETOPT_OPTION_END
571 main (int argc, char *argv[])
573 char *const argv2[] = {
576 "test_mesh_2dtorus.conf",
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
589 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
591 gettext_noop ("Test mesh regex integration."),
592 options, &run, NULL);
594 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
597 for (i = 0; i < MESH_REGEX_PEERS; i++)
599 if (GNUNET_OK != ok[i])
601 result = GNUNET_SYSERR;
602 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
603 "COULD NOT CONNECT TO %u: %s!\n",
607 if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610 "test: FAILED! %u connected peers\n",
614 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
618 /* end of test_mesh_regex.c */