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 3
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)
46 * Which strings have been found & connected.
48 static int ok[MESH_REGEX_PEERS];
51 * How many connects have happened.
53 static int regex_peers;
61 * Total number of peers in the test.
63 static unsigned long long num_peers;
66 * Global configuration file
68 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
71 * Total number of currently running peers.
73 static unsigned long long peers_running;
76 * Total number of successful connections in the whole network.
78 static unsigned int total_connections;
81 * Total number of failed connections in the whole network.
83 static unsigned int failed_connections;
86 * The currently running peer group.
88 static struct GNUNET_TESTING_PeerGroup *pg;
91 * Task called to disconnect peers
93 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
96 * Task called to shutdown test.
98 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
101 * Mesh handle for connecting peer.
103 static struct GNUNET_MESH_Handle *h1;
106 * Mesh handle for announcing peers.
108 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
111 * Tunnel handles for announcing peer.
113 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
116 * Incoming tunnels for announcing peers.
118 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
121 * Regular expressions for the announces.
123 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
133 * Service strings to look for.
135 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
139 "00123456789ABCCCC"};
142 * Check whether peers successfully shut down.
144 * @param cls Closure (unused).
145 * @param emsg Error message, NULL on success.
148 shutdown_callback (void *cls, const char *emsg)
154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
155 "test: Shutdown of peers failed! (%s)\n", emsg);
156 for (i = 0; i < MESH_REGEX_PEERS; i++)
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "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)
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
183 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
184 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
189 * Ends test: Disconnects peers and calls shutdown.
190 * @param cls Closure (not used).
191 * @param tc TaskContext.
194 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
200 for (i = 0; i < MESH_REGEX_PEERS; i++)
202 GNUNET_MESH_tunnel_destroy (t[i]);
203 GNUNET_MESH_disconnect (h2[i]);
205 GNUNET_MESH_disconnect (h1);
206 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
208 GNUNET_SCHEDULER_cancel (shutdown_handle);
210 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
214 * Function called whenever an inbound tunnel is destroyed. Should clean up
215 * any associated state.
217 * @param cls closure (set from GNUNET_MESH_connect)
218 * @param tunnel connection to the other end (henceforth invalid)
219 * @param tunnel_ctx place where local state associated
220 * with the tunnel is stored
223 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Incoming tunnel disconnected at peer %d\n",
236 * Method called whenever a tunnel falls apart.
239 * @param peer peer identity the tunnel stopped working with
242 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "peer %s disconnected\n",
251 * Function called to notify a client about the connection
252 * begin ready to queue more data. "buf" will be
253 * NULL and "size" zero if the connection was closed for
254 * writing in the meantime.
257 * @param size number of bytes available in buf
258 * @param buf where the callee should write the message
259 * @return number of bytes written to buf
262 data_ready (void *cls, size_t size, void *buf)
264 struct GNUNET_MessageHeader *m = buf;
266 if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
269 m->size = htons (sizeof(struct GNUNET_MessageHeader));
270 return sizeof(struct GNUNET_MessageHeader);
274 * Method called whenever a peer connects to a tunnel.
277 * @param peer peer identity the tunnel was created to, NULL on timeout
278 * @param atsi performance data for the connection
281 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
282 const struct GNUNET_ATS_Information *atsi)
286 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
287 "Peer connected: %s\n",
291 GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
292 GNUNET_TIME_UNIT_FOREVER_REL,
294 sizeof(struct GNUNET_MessageHeader),
299 * Method called whenever another peer has added us to a tunnel
300 * the other peer initiated.
303 * @param tunnel new handle to the tunnel
304 * @param initiator peer that started the tunnel
305 * @param atsi performance information for the tunnel
306 * @return initial tunnel context for the tunnel
307 * (can be NULL -- that's not an error)
310 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
311 const struct GNUNET_PeerIdentity *initiator,
312 const struct GNUNET_ATS_Information *atsi)
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 "Incoming tunnel from %s to peer %d\n",
317 GNUNET_i2s (initiator), (long) cls);
318 if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
320 incoming_t[i - 2] = tunnel;
321 ok[i - 2] = GNUNET_OK;
325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
326 "Incoming tunnel for unknown client %lu\n", (long) cls);
328 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
330 GNUNET_SCHEDULER_cancel (disconnect_task);
332 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
338 * Function is called whenever a message is received.
340 * @param cls closure (set from GNUNET_MESH_connect)
341 * @param tunnel connection to the other end
342 * @param tunnel_ctx place to store local state associated with the tunnel
343 * @param sender who sent the message
344 * @param message the actual message
345 * @param atsi performance data for the connection
346 * @return GNUNET_OK to keep the connection open,
347 * GNUNET_SYSERR to close it (signal serious error)
350 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
351 const struct GNUNET_PeerIdentity *sender,
352 const struct GNUNET_MessageHeader *message,
353 const struct GNUNET_ATS_Information *atsi)
356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
357 "test: GOT DATA!\n");
358 for (i = 0; i < MESH_REGEX_PEERS; i++)
360 if (GNUNET_OK != ok[i]) {
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "test: %u DATA MISSING!\n", i);
366 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
367 "test: EVERYONE GOT DATA, FINISHING!\n");
368 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
370 GNUNET_SCHEDULER_cancel (disconnect_task);
372 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
378 * Handlers, for diverse services
380 static struct GNUNET_MESH_MessageHandler handlers[] = {
381 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
387 * peergroup_ready: start test when all peers are connected
389 * @param emsg error message
392 peergroup_ready (void *cls, const char *emsg)
394 GNUNET_MESH_ApplicationType app;
395 struct GNUNET_TESTING_Daemon *d;
400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
401 "test: Peergroup callback called with error, aborting test!\n");
402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error from testing: `%s'\n",
404 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
409 "test: Peer Group started successfully!\n");
410 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
414 peers_running = GNUNET_TESTING_daemons_running (pg);
415 if (0 < failed_connections)
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
419 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
423 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
424 d = GNUNET_TESTING_daemon_get (pg, 1);
426 app = (GNUNET_MESH_ApplicationType) 0;
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
429 "Connect to mesh\n");
430 h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
436 for (i = 0; i < MESH_REGEX_PEERS; i++)
439 d = GNUNET_TESTING_daemon_get (pg, 10 + i);
440 h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
445 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
446 "Announce REGEX %u: %s\n", i, regexes[i]);
447 GNUNET_MESH_announce_regex (h2[i], regexes[i]);
450 for (i = 0; i < MESH_REGEX_PEERS; i++)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
456 "Connect by string %s\n", strings[i]);
457 GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
459 /* connect handler = success, timeout = error */
465 * Function that will be called whenever two daemons are connected by
466 * the testing library.
469 * @param first peer id for first daemon
470 * @param second peer id for the second daemon
471 * @param distance distance between the connected peers
472 * @param first_cfg config for the first daemon
473 * @param second_cfg config for the second daemon
474 * @param first_daemon handle for the first daemon
475 * @param second_daemon handle for the second daemon
476 * @param emsg error message (NULL on success)
479 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
480 const struct GNUNET_PeerIdentity *second, uint32_t distance,
481 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
482 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
483 struct GNUNET_TESTING_Daemon *first_daemon,
484 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
492 failed_connections++;
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "test: Problem with new connection (%s)\n", emsg);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
502 * run: load configuration options and schedule test to run (start peergroup)
505 * @param cfgfile configuration file name (can be NULL)
506 * @param cfg configuration handle
509 run (void *cls, char *const *args, const char *cfgfile,
510 const struct GNUNET_CONFIGURATION_Handle *cfg)
512 struct GNUNET_TESTING_Host *hosts;
514 total_connections = 0;
515 failed_connections = 0;
516 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
518 GNUNET_log_setup ("test_mesh_regex",
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
528 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
529 "use_progressbars", "YES");
533 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
534 "num_peers", &num_peers))
536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
537 "Option TESTING:NUM_PEERS is required!\n");
541 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
543 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
544 &connect_cb, &peergroup_ready, NULL,
546 GNUNET_assert (pg != NULL);
548 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
549 &shutdown_task, NULL);
554 * test_mesh_regex command line options
556 static struct GNUNET_GETOPT_CommandLineOption options[] = {
557 {'V', "verbose", NULL,
558 gettext_noop ("be verbose (print progress information)"),
559 0, &GNUNET_GETOPT_set_one, &verbose},
560 GNUNET_GETOPT_OPTION_END
568 main (int argc, char *argv[])
570 char *const argv2[] = {
573 "test_mesh_2dtorus.conf",
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
586 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
588 gettext_noop ("Test mesh regex integration."),
589 options, &run, NULL);
591 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
594 for (i = 0; i < MESH_REGEX_PEERS; i++)
596 if (GNUNET_OK != ok[i])
598 result = GNUNET_SYSERR;
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "COULD NOT CONNECT TO %u: %s!\n",
604 if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 "test: FAILED! %u connected peers\n",
611 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
615 /* end of test_mesh_regex.c */