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