878ca2f6847438c2834e0c12688ec8ffbf7cd3d8
[oweals/gnunet.git] / src / mesh / test_mesh_regex.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file mesh/test_mesh_regex.c
22  *
23  * @brief Test for regex announce / by_string connect.
24  * based on the 2dtorus testcase
25  */
26 #include "platform.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_mesh_service.h"
29
30 #define VERBOSE GNUNET_YES
31 #define REMOVE_DIR GNUNET_YES
32 #define MESH_REGEX_PEERS 3
33
34 /**
35  * How long until we give up on connecting the peers?
36  */
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
38
39 /**
40  * Time to wait for stuff that should be rather fast
41  */
42 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43
44 /**
45  * Initial regex padding.
46  */
47 #define REGEX_INITIAL_PADDING "PADPADPADPADPADP"
48
49 /**
50  * Which strings have been found & connected.
51  */
52 static int ok[MESH_REGEX_PEERS];
53
54 /**
55  * How many connects have happened.
56  */
57 static int regex_peers;
58
59 /**
60  * Be verbose
61  */
62 static int verbose;
63
64 /**
65  * Total number of peers in the test.
66  */
67 static unsigned long long num_peers;
68
69 /**
70  * Global configuration file
71  */
72 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
73
74 /**
75  * Total number of currently running peers.
76  */
77 static unsigned long long peers_running;
78
79 /**
80  * Total number of successful connections in the whole network.
81  */
82 static unsigned int total_connections;
83
84 /**
85  * Total number of failed connections in the whole network.
86  */
87 static unsigned int failed_connections;
88
89 /**
90  * The currently running peer group.
91  */
92 static struct GNUNET_TESTING_PeerGroup *pg;
93
94 /**
95  * Task called to disconnect peers
96  */
97 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
98
99 /**
100  * Task called to shutdown test.
101  */
102 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
103
104 /**
105  * Mesh handle for connecting peer.
106  */
107 static struct GNUNET_MESH_Handle *h1;
108
109 /**
110  * Mesh handle for announcing peers.
111  */
112 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
113
114 /**
115  * Tunnel handles for announcing peer.
116  */
117 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
118
119 /**
120  * Incoming tunnels for announcing peers.
121  */
122 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
123
124 /**
125  * Regular expressions for the announces.
126  */
127 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
128                                           "(0|1)"
129                                           "23456789ABC",
130
131                                           "0123456789A*BC",
132
133                                           "0*123456789ABC*"};
134
135
136 /**
137  * Service strings to look for.
138  */
139 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
140
141                                           "0123456789AABC",
142
143                                           "00123456789ABCCCC"};
144
145 /**
146  * Check whether peers successfully shut down.
147  *
148  * @param cls Closure (unused).
149  * @param emsg Error message, NULL on success.
150  */
151 static void
152 shutdown_callback (void *cls, const char *emsg)
153 {
154   unsigned int i;
155
156   if (emsg != NULL)
157   {
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++)
161       ok[i] = GNUNET_NO;
162   }
163 #if VERBOSE
164   else
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167                 "test: All peers successfully shut down!\n");
168   }
169 #endif
170   GNUNET_CONFIGURATION_destroy (testing_cfg);
171 }
172
173
174 /**
175  * Task to run for shutdown: stops peers, ends test.
176  *
177  * @param cls Closure (not used).
178  * @param tc TaskContext.
179  *
180  */
181 static void
182 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 {
184 #if VERBOSE
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
186 #endif
187   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
188   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
189 }
190
191
192 /**
193  * Ends test: Disconnects peers and calls shutdown.
194  * @param cls Closure (not used).
195  * @param tc TaskContext.
196  */
197 static void
198 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 {
200   unsigned int i;
201
202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
203
204   for (i = 0; i < MESH_REGEX_PEERS; i++)
205   {
206     GNUNET_MESH_tunnel_destroy (t[i]);
207     GNUNET_MESH_disconnect (h2[i]);
208   }
209   GNUNET_MESH_disconnect (h1);
210   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
211   {
212     GNUNET_SCHEDULER_cancel (shutdown_handle);
213   }
214   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
215 }
216
217 /**
218  * Function called whenever an inbound tunnel is destroyed.  Should clean up
219  * any associated state.
220  *
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
225  */
226 static void
227 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
228                 void *tunnel_ctx)
229 {
230   long i = (long) cls;
231
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233               "Incoming tunnel disconnected at peer %d\n",
234               i);
235   return;
236 }
237
238
239 /**
240  * Method called whenever a tunnel falls apart.
241  *
242  * @param cls closure
243  * @param peer peer identity the tunnel stopped working with
244  */
245 static void
246 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
247 {
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "peer %s disconnected\n",
250               GNUNET_i2s (peer));
251   return;
252 }
253
254 /**
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.
259  *
260  * @param cls closure
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
264  */
265 static size_t
266 data_ready (void *cls, size_t size, void *buf)
267 {
268   struct GNUNET_MessageHeader *m = buf;
269
270   if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
271     return 0;
272   m->type = htons (1);
273   m->size = htons (sizeof(struct GNUNET_MessageHeader));
274   return sizeof(struct GNUNET_MessageHeader);
275 }
276
277 /**
278  * Method called whenever a peer connects to a tunnel.
279  *
280  * @param cls closure
281  * @param peer peer identity the tunnel was created to, NULL on timeout
282  * @param atsi performance data for the connection
283  */
284 static void
285 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
286     const struct GNUNET_ATS_Information *atsi)
287 {
288   long i = (long) cls;
289
290   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291               "Peer connected: %s\n",
292               GNUNET_i2s (peer));
293   regex_peers++;
294
295   GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
296                                     GNUNET_TIME_UNIT_FOREVER_REL,
297                                     peer,
298                                     sizeof(struct GNUNET_MessageHeader),
299                                     &data_ready, NULL);
300 }
301
302 /**
303  * Method called whenever another peer has added us to a tunnel
304  * the other peer initiated.
305  *
306  * @param cls closure
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)
312  */
313 static void *
314 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
315                  const struct GNUNET_PeerIdentity *initiator,
316                  const struct GNUNET_ATS_Information *atsi)
317 {
318   long i = (long) cls;
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)
323   {
324     incoming_t[i - 2] = tunnel;
325     ok[i - 2] = GNUNET_OK;
326   }
327   else
328   {
329     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330                 "Incoming tunnel for unknown client %lu\n", (long) cls);
331   }
332   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
333   {
334     GNUNET_SCHEDULER_cancel (disconnect_task);
335     disconnect_task =
336         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
337   }
338   return NULL;
339 }
340
341 /**
342  * Function is called whenever a message is received.
343  *
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)
352  */
353 int
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)
358 {
359   int i;
360   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361               "test: GOT DATA!\n");
362   for (i = 0; i < MESH_REGEX_PEERS; i++)
363   {
364     if (GNUNET_OK != ok[i]) {
365       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366               "test: %u DATA MISSING!\n", i);
367       return GNUNET_OK;
368     }
369   }
370   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371               "test: EVERYONE GOT DATA, FINISHING!\n");
372   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
373   {
374     GNUNET_SCHEDULER_cancel (disconnect_task);
375     disconnect_task =
376         GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
377   }
378   return GNUNET_OK;
379 }
380
381 /**
382  * Handlers, for diverse services
383  */
384 static struct GNUNET_MESH_MessageHandler handlers[] = {
385   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
386   {NULL, 0, 0}
387 };
388
389
390 /**
391  * peergroup_ready: start test when all peers are connected
392  * @param cls closure
393  * @param emsg error message
394  */
395 static void
396 peergroup_ready (void *cls, const char *emsg)
397 {
398   GNUNET_MESH_ApplicationType app;
399   struct GNUNET_TESTING_Daemon *d;
400   unsigned int i;
401
402   if (emsg != NULL)
403   {
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",
407                 emsg);
408     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
409     return;
410   }
411 #if VERBOSE
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",
415               total_connections);
416 #endif
417
418   peers_running = GNUNET_TESTING_daemons_running (pg);
419   if (0 < failed_connections)
420   {
421     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
422                 failed_connections);
423     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
424     return;
425   }
426   disconnect_task =
427     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
428   d = GNUNET_TESTING_daemon_get (pg, 1);
429
430   app = (GNUNET_MESH_ApplicationType) 0;
431
432   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433               "Connect to mesh\n");
434   h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
435                             NULL,
436                             NULL,
437                             handlers,
438                             &app);
439   regex_peers = 0;
440   for (i = 0; i < MESH_REGEX_PEERS; i++)
441   {
442     ok[i] = GNUNET_NO;
443     d = GNUNET_TESTING_daemon_get (pg, 10 + i);
444     h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
445                                  &incoming_tunnel,
446                                  &tunnel_cleaner,
447                                  handlers,
448                                  &app);
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]);
452   }
453
454   for (i = 0; i < MESH_REGEX_PEERS; i++)
455   {
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457                 "Create tunnel\n");
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]);
462   }
463   /* connect handler = success, timeout = error */
464
465 }
466
467
468 /**
469  * Function that will be called whenever two daemons are connected by
470  * the testing library.
471  *
472  * @param cls closure
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)
481  */
482 static void
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)
489 {
490   if (emsg == NULL)
491   {
492     total_connections++;
493   }
494   else
495   {
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));
501   }
502 }
503
504
505 /**
506  * run: load configuration options and schedule test to run (start peergroup)
507  * @param cls closure
508  * @param args argv
509  * @param cfgfile configuration file name (can be NULL)
510  * @param cfg configuration handle
511  */
512 static void
513 run (void *cls, char *const *args, const char *cfgfile,
514      const struct GNUNET_CONFIGURATION_Handle *cfg)
515 {
516   struct GNUNET_TESTING_Host *hosts;
517
518   total_connections = 0;
519   failed_connections = 0;
520   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
521
522   GNUNET_log_setup ("test_mesh_regex",
523 #if VERBOSE
524                     "DEBUG",
525 #else
526                     "WARNING",
527 #endif
528                     NULL);
529
530 #if VERBOSE
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");
534 #endif
535
536   if (GNUNET_OK !=
537       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
538                                              "num_peers", &num_peers))
539   {
540     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
541                 "Option TESTING:NUM_PEERS is required!\n");
542     return;
543   }
544
545   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
546
547   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
548                                        &connect_cb, &peergroup_ready, NULL,
549                                        hosts);
550   GNUNET_assert (pg != NULL);
551   shutdown_handle =
552     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
553                                     &shutdown_task, NULL);
554 }
555
556
557 /**
558  * test_mesh_regex command line options
559  */
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
565 };
566
567
568 /**
569  * Main: start test
570  */
571 int
572 main (int argc, char *argv[])
573 {
574   char *const argv2[] = {
575     argv[0],
576     "-c",
577     "test_mesh_2dtorus.conf",
578 #if VERBOSE
579     "-L",
580     "DEBUG",
581 #endif
582     NULL
583   };
584   int result;
585   unsigned int i;
586
587   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
588
589
590   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
591                       "test_mesh_regex",
592                       gettext_noop ("Test mesh regex integration."),
593                       options, &run, NULL);
594 #if REMOVE_DIR
595   GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
596 #endif
597   result = GNUNET_OK;
598   for (i = 0; i < MESH_REGEX_PEERS; i++)
599   {
600     if (GNUNET_OK != ok[i])
601     {
602       result = GNUNET_SYSERR;
603       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604                   "COULD NOT CONNECT TO %u: %s!\n",
605                   i, strings[i]);
606     }
607   }
608   if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
609   {
610     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611                 "test: FAILED! %u connected peers\n",
612                 regex_peers);
613     return 1;
614   }
615   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
616   return 0;
617 }
618
619 /* end of test_mesh_regex.c */