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)
45 * Initial regex padding.
47 #define REGEX_INITIAL_PADDING "PADPADPADPADPADP"
50 * Which strings have been found & connected.
52 static int ok[MESH_REGEX_PEERS];
55 * How many connects have happened.
57 static int regex_peers;
65 * Total number of peers in the test.
67 static unsigned long long num_peers;
70 * Global configuration file
72 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
75 * Total number of currently running peers.
77 static unsigned long long peers_running;
80 * Total number of successful connections in the whole network.
82 static unsigned int total_connections;
85 * Total number of failed connections in the whole network.
87 static unsigned int failed_connections;
90 * The currently running peer group.
92 static struct GNUNET_TESTING_PeerGroup *pg;
95 * Task called to disconnect peers
97 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
100 * Task called to shutdown test.
102 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
105 * Mesh handle for connecting peer.
107 static struct GNUNET_MESH_Handle *h1;
110 * Mesh handle for announcing peers.
112 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
115 * Tunnel handles for announcing peer.
117 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
120 * Incoming tunnels for announcing peers.
122 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
125 * Regular expressions for the announces.
127 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
137 * Service strings to look for.
139 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
143 "00123456789ABCCCC"};
146 * Check whether peers successfully shut down.
148 * @param cls Closure (unused).
149 * @param emsg Error message, NULL on success.
152 shutdown_callback (void *cls, const char *emsg)
158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
159 "test: Shutdown of peers failed! (%s)\n", emsg);
160 for (i = 0; i < MESH_REGEX_PEERS; i++)
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "test: All peers successfully shut down!\n");
170 GNUNET_CONFIGURATION_destroy (testing_cfg);
175 * Task to run for shutdown: stops peers, ends test.
177 * @param cls Closure (not used).
178 * @param tc TaskContext.
182 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
187 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
193 * Ends test: Disconnects peers and calls shutdown.
194 * @param cls Closure (not used).
195 * @param tc TaskContext.
198 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
204 for (i = 0; i < MESH_REGEX_PEERS; i++)
206 GNUNET_MESH_tunnel_destroy (t[i]);
207 GNUNET_MESH_disconnect (h2[i]);
209 GNUNET_MESH_disconnect (h1);
210 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
212 GNUNET_SCHEDULER_cancel (shutdown_handle);
214 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
218 * Function called whenever an inbound tunnel is destroyed. Should clean up
219 * any associated state.
221 * @param cls closure (set from GNUNET_MESH_connect)
222 * @param tunnel connection to the other end (henceforth invalid)
223 * @param tunnel_ctx place where local state associated
224 * with the tunnel is stored
227 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Incoming tunnel disconnected at peer %d\n",
240 * Method called whenever a tunnel falls apart.
243 * @param peer peer identity the tunnel stopped working with
246 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "peer %s disconnected\n",
255 * Function called to notify a client about the connection
256 * begin ready to queue more data. "buf" will be
257 * NULL and "size" zero if the connection was closed for
258 * writing in the meantime.
261 * @param size number of bytes available in buf
262 * @param buf where the callee should write the message
263 * @return number of bytes written to buf
266 data_ready (void *cls, size_t size, void *buf)
268 struct GNUNET_MessageHeader *m = buf;
270 if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
273 m->size = htons (sizeof(struct GNUNET_MessageHeader));
274 return sizeof(struct GNUNET_MessageHeader);
278 * Method called whenever a peer connects to a tunnel.
281 * @param peer peer identity the tunnel was created to, NULL on timeout
282 * @param atsi performance data for the connection
285 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
286 const struct GNUNET_ATS_Information *atsi)
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291 "Peer connected: %s\n",
295 GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
296 GNUNET_TIME_UNIT_FOREVER_REL,
298 sizeof(struct GNUNET_MessageHeader),
303 * Method called whenever another peer has added us to a tunnel
304 * the other peer initiated.
307 * @param tunnel new handle to the tunnel
308 * @param initiator peer that started the tunnel
309 * @param atsi performance information for the tunnel
310 * @return initial tunnel context for the tunnel
311 * (can be NULL -- that's not an error)
314 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
315 const struct GNUNET_PeerIdentity *initiator,
316 const struct GNUNET_ATS_Information *atsi)
319 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
320 "Incoming tunnel from %s to peer %d\n",
321 GNUNET_i2s (initiator), (long) cls);
322 if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
324 incoming_t[i - 2] = tunnel;
325 ok[i - 2] = GNUNET_OK;
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330 "Incoming tunnel for unknown client %lu\n", (long) cls);
332 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
334 GNUNET_SCHEDULER_cancel (disconnect_task);
336 GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
342 * Function is called whenever a message is received.
344 * @param cls closure (set from GNUNET_MESH_connect)
345 * @param tunnel connection to the other end
346 * @param tunnel_ctx place to store local state associated with the tunnel
347 * @param sender who sent the message
348 * @param message the actual message
349 * @param atsi performance data for the connection
350 * @return GNUNET_OK to keep the connection open,
351 * GNUNET_SYSERR to close it (signal serious error)
354 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
355 const struct GNUNET_PeerIdentity *sender,
356 const struct GNUNET_MessageHeader *message,
357 const struct GNUNET_ATS_Information *atsi)
360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361 "test: GOT DATA!\n");
362 for (i = 0; i < MESH_REGEX_PEERS; i++)
364 if (GNUNET_OK != ok[i]) {
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "test: %u DATA MISSING!\n", i);
370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371 "test: EVERYONE GOT DATA, FINISHING!\n");
372 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
374 GNUNET_SCHEDULER_cancel (disconnect_task);
376 GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
382 * Handlers, for diverse services
384 static struct GNUNET_MESH_MessageHandler handlers[] = {
385 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
391 * peergroup_ready: start test when all peers are connected
393 * @param emsg error message
396 peergroup_ready (void *cls, const char *emsg)
398 GNUNET_MESH_ApplicationType app;
399 struct GNUNET_TESTING_Daemon *d;
404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
405 "test: Peergroup callback called with error, aborting test!\n");
406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error from testing: `%s'\n",
408 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
412 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
413 "test: Peer Group started successfully!\n");
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
418 peers_running = GNUNET_TESTING_daemons_running (pg);
419 if (0 < failed_connections)
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
423 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
427 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
428 d = GNUNET_TESTING_daemon_get (pg, 1);
430 app = (GNUNET_MESH_ApplicationType) 0;
432 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433 "Connect to mesh\n");
434 h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
440 for (i = 0; i < MESH_REGEX_PEERS; i++)
443 d = GNUNET_TESTING_daemon_get (pg, 10 + i);
444 h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
449 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
450 "Announce REGEX %u: %s\n", i, regexes[i]);
451 GNUNET_MESH_announce_regex (h2[i], regexes[i]);
454 for (i = 0; i < MESH_REGEX_PEERS; i++)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
460 "Connect by string %s\n", strings[i]);
461 GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
463 /* connect handler = success, timeout = error */
469 * Function that will be called whenever two daemons are connected by
470 * the testing library.
473 * @param first peer id for first daemon
474 * @param second peer id for the second daemon
475 * @param distance distance between the connected peers
476 * @param first_cfg config for the first daemon
477 * @param second_cfg config for the second daemon
478 * @param first_daemon handle for the first daemon
479 * @param second_daemon handle for the second daemon
480 * @param emsg error message (NULL on success)
483 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
484 const struct GNUNET_PeerIdentity *second, uint32_t distance,
485 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
486 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
487 struct GNUNET_TESTING_Daemon *first_daemon,
488 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
496 failed_connections++;
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "test: Problem with new connection (%s)\n", emsg);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (first));
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: (%s)\n", GNUNET_i2s (second));
506 * run: load configuration options and schedule test to run (start peergroup)
509 * @param cfgfile configuration file name (can be NULL)
510 * @param cfg configuration handle
513 run (void *cls, char *const *args, const char *cfgfile,
514 const struct GNUNET_CONFIGURATION_Handle *cfg)
516 struct GNUNET_TESTING_Host *hosts;
518 total_connections = 0;
519 failed_connections = 0;
520 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
522 GNUNET_log_setup ("test_mesh_regex",
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
532 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
533 "use_progressbars", "YES");
537 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
538 "num_peers", &num_peers))
540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
541 "Option TESTING:NUM_PEERS is required!\n");
545 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
547 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
548 &connect_cb, &peergroup_ready, NULL,
550 GNUNET_assert (pg != NULL);
552 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
553 &shutdown_task, NULL);
558 * test_mesh_regex command line options
560 static struct GNUNET_GETOPT_CommandLineOption options[] = {
561 {'V', "verbose", NULL,
562 gettext_noop ("be verbose (print progress information)"),
563 0, &GNUNET_GETOPT_set_one, &verbose},
564 GNUNET_GETOPT_OPTION_END
572 main (int argc, char *argv[])
574 char *const argv2[] = {
577 "test_mesh_2dtorus.conf",
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
590 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
592 gettext_noop ("Test mesh regex integration."),
593 options, &run, NULL);
595 GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
598 for (i = 0; i < MESH_REGEX_PEERS; i++)
600 if (GNUNET_OK != ok[i])
602 result = GNUNET_SYSERR;
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 "COULD NOT CONNECT TO %u: %s!\n",
608 if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611 "test: FAILED! %u connected peers\n",
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
619 /* end of test_mesh_regex.c */