- port regex testcase to new lib
[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 "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
29
30 #define REMOVE_DIR GNUNET_YES
31 #define MESH_REGEX_PEERS 4
32
33 /**
34  * How long until we give up on connecting the peers?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37
38 /**
39  * Time to wait for stuff that should be rather fast
40  */
41 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43 /**
44  * Which strings have been found & connected.
45  */
46 static int ok[MESH_REGEX_PEERS];
47
48 /**
49  * How many connects have happened.
50  */
51 static int regex_peers;
52
53 /**
54  * Total number of currently running peers.
55  */
56 static unsigned long long peers_running;
57
58 /**
59  * Task called to disconnect peers
60  */
61 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
62
63 /**
64  * Task called to shutdown test.
65  */
66 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
67
68 /**
69  * Mesh handle for connecting peer.
70  */
71 static struct GNUNET_MESH_Handle *h1;
72
73 /**
74  * Mesh handle for announcing peers.
75  */
76 static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
77
78 /**
79  * Tunnel handles for announcing peer.
80  */
81 static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
82
83 /**
84  * Incoming tunnels for announcing peers.
85  */
86 static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
87
88 /**
89  * Test context (to shut down).
90  */
91 struct GNUNET_MESH_TEST_Context *test_ctx;
92
93 /**
94  * Regular expressions for the announces.
95  */
96 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
97                                           "(0|1)"
98                                           "23456789ABC",
99
100                                           "0123456789A*BC",
101
102                                           "1234567890123456789012340*123456789ABC*",
103
104                                           "GNUNETVPN0001000IPEX401110011101100100000111(0|1)*"};
105
106
107 /**
108  * Service strings to look for.
109  */
110 static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
111
112                                           "0123456789AABC",
113
114                                           "12345678901234567890123400123456789ABCCCC",
115
116                                           "GNUNETVPN0001000IPEX401110011101100100000111"};
117
118
119
120 /**
121  * Task to run for shutdown: stops peers, ends test.
122  *
123  * @param cls Closure (not used).
124  * @param tc TaskContext.
125  *
126  */
127 static void
128 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
129 {
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
131   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
132 }
133
134
135 /**
136  * Ends test: Disconnects peers and calls shutdown.
137  * @param cls Closure (not used).
138  * @param tc TaskContext.
139  */
140 static void
141 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 {
143   unsigned int i;
144
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
146
147   for (i = 0; i < MESH_REGEX_PEERS; i++)
148   {
149     GNUNET_MESH_tunnel_destroy (t[i]);
150     GNUNET_MESH_disconnect (h2[i]);
151   }
152   GNUNET_MESH_disconnect (h1);
153   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
154   {
155     GNUNET_SCHEDULER_cancel (shutdown_handle);
156   }
157   GNUNET_MESH_TEST_cleanup (test_ctx);
158   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
159 }
160
161 /**
162  * Function called whenever an inbound tunnel is destroyed.  Should clean up
163  * any associated state.
164  *
165  * @param cls closure (set from GNUNET_MESH_connect)
166  * @param tunnel connection to the other end (henceforth invalid)
167  * @param tunnel_ctx place where local state associated
168  *                   with the tunnel is stored
169  */
170 static void
171 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
172                 void *tunnel_ctx)
173 {
174   long i = (long) cls;
175
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177               "Incoming tunnel disconnected at peer %d\n",
178               i);
179   return;
180 }
181
182
183 /**
184  * Method called whenever a tunnel falls apart.
185  *
186  * @param cls closure
187  * @param peer peer identity the tunnel stopped working with
188  */
189 static void
190 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
191 {
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193               "peer %s disconnected\n",
194               GNUNET_i2s (peer));
195   return;
196 }
197
198 /**
199  * Function called to notify a client about the connection
200  * begin ready to queue more data.  "buf" will be
201  * NULL and "size" zero if the connection was closed for
202  * writing in the meantime.
203  *
204  * @param cls closure
205  * @param size number of bytes available in buf
206  * @param buf where the callee should write the message
207  * @return number of bytes written to buf
208  */
209 static size_t
210 data_ready (void *cls, size_t size, void *buf)
211 {
212   struct GNUNET_MessageHeader *m = buf;
213
214   if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
215     return 0;
216   m->type = htons (1);
217   m->size = htons (sizeof(struct GNUNET_MessageHeader));
218   return sizeof(struct GNUNET_MessageHeader);
219 }
220
221 /**
222  * Method called whenever a peer connects to a tunnel.
223  *
224  * @param cls closure
225  * @param peer peer identity the tunnel was created to, NULL on timeout
226  * @param atsi performance data for the connection
227  */
228 static void
229 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
230     const struct GNUNET_ATS_Information *atsi)
231 {
232   long i = (long) cls;
233
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235               "Peer connected: %s\n",
236               GNUNET_i2s (peer));
237   regex_peers++;
238
239   GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
240                                     GNUNET_TIME_UNIT_FOREVER_REL,
241                                     peer,
242                                     sizeof(struct GNUNET_MessageHeader),
243                                     &data_ready, NULL);
244 }
245
246 /**
247  * Method called whenever another peer has added us to a tunnel
248  * the other peer initiated.
249  *
250  * @param cls closure
251  * @param tunnel new handle to the tunnel
252  * @param initiator peer that started the tunnel
253  * @param atsi performance information for the tunnel
254  * @return initial tunnel context for the tunnel
255  *         (can be NULL -- that's not an error)
256  */
257 static void *
258 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
259                  const struct GNUNET_PeerIdentity *initiator,
260                  const struct GNUNET_ATS_Information *atsi)
261 {
262   long i = (long) cls;
263
264   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265               "Incoming tunnel from %s to peer %d\n",
266               GNUNET_i2s (initiator), i);
267   if (i >= 10L && i <= 10L + MESH_REGEX_PEERS)
268   {
269     incoming_t[i - 10L] = tunnel;
270     ok[i - 10L] = GNUNET_OK;
271   }
272   else
273   {
274     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
275                 "Incoming tunnel for unknown client %lu\n", (long) cls);
276   }
277   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
278   {
279     GNUNET_SCHEDULER_cancel (disconnect_task);
280     disconnect_task =
281         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_peers, NULL);
282   }
283   return NULL;
284 }
285
286 /**
287  * Function is called whenever a message is received.
288  *
289  * @param cls closure (set from GNUNET_MESH_connect)
290  * @param tunnel connection to the other end
291  * @param tunnel_ctx place to store local state associated with the tunnel
292  * @param sender who sent the message
293  * @param message the actual message
294  * @param atsi performance data for the connection
295  * @return GNUNET_OK to keep the connection open,
296  *         GNUNET_SYSERR to close it (signal serious error)
297  */
298 int
299 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
300                const struct GNUNET_PeerIdentity *sender,
301                const struct GNUNET_MessageHeader *message,
302                const struct GNUNET_ATS_Information *atsi)
303 {
304   unsigned int i;
305   long peer_number = (long) cls;
306
307   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got data on peer %ld!\n", peer_number);
308   for (i = 0; i < MESH_REGEX_PEERS; i++)
309   {
310     if (GNUNET_OK != ok[i]) {
311       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312               "data from peer %u still missing!\n", i + 10);
313       return GNUNET_OK;
314     }
315   }
316   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
317               "test: EVERYONE GOT DATA, FINISHING!\n");
318   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
319   {
320     GNUNET_SCHEDULER_cancel (disconnect_task);
321   }
322   disconnect_task =
323       GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
324   return GNUNET_OK;
325 }
326
327 /**
328  * Handlers, for diverse services
329  */
330 static struct GNUNET_MESH_MessageHandler handlers[] = {
331   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
332   {NULL, 0, 0}
333 };
334
335
336 /**
337  * test main: start test when all peers are connected
338  *
339  * @param cls Closure.
340  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
341  * @param num_peers Number of peers that are running.
342  * @param peers Array of peers.
343  * @param meshes Handle to each of the MESHs of the peers.
344  */
345 static void
346 tmain (void *cls,
347        struct GNUNET_MESH_TEST_Context *ctx,
348        unsigned int num_peers,
349        struct GNUNET_TESTBED_Peer **peers,
350        struct GNUNET_MESH_Handle **meshes)
351 {
352   unsigned int i;
353
354   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
355                                                   &shutdown_task, NULL);
356   test_ctx = ctx;
357   if (16 != num_peers)
358   {
359     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360                 "running peers mismatch, aborting test!\n");
361     GNUNET_MESH_TEST_cleanup (ctx);
362     return;
363   }
364   peers_running = num_peers;
365   disconnect_task =
366     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
367
368 //   h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
369 //                             NULL,
370 //                             NULL,
371 //                             handlers,
372 //                             &app);
373   h1 = meshes[0];
374   regex_peers = 0;
375   for (i = 0; i < MESH_REGEX_PEERS; i++)
376   {
377     ok[i] = GNUNET_NO;
378 //     h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
379 //                                  &incoming_tunnel,
380 //                                  &tunnel_cleaner,
381 //                                  handlers,
382 //                                  &app);
383     h2[i] = meshes[10 + i];
384     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
385                 "Announce REGEX %u: %s\n", i, regexes[i]);
386     GNUNET_MESH_announce_regex (h2[i], regexes[i], 1);
387   }
388
389   for (i = 0; i < MESH_REGEX_PEERS; i++)
390   {
391     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392                 "Create tunnel\n");
393     t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
394     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
395                 "Connect by string %s\n", strings[i]);
396     GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
397   }
398   /* connect handler = success, timeout = error */
399
400 }
401
402
403 /**
404  * Main: start test
405  */
406 int
407 main (int argc, char *argv[])
408 {
409   int result;
410   unsigned int i;
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
413
414   GNUNET_MESH_TEST_run ("test_mesh_regex",
415                         "test_mesh_2dtorus.conf",
416                         16,
417                         &tmain,
418                         NULL,
419                         &incoming_tunnel,
420                         &tunnel_cleaner,
421                         handlers,
422                         NULL);
423
424   result = GNUNET_OK;
425   for (i = 0; i < MESH_REGEX_PEERS; i++)
426   {
427     if (GNUNET_OK != ok[i])
428     {
429       result = GNUNET_SYSERR;
430       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431                   "COULD NOT CONNECT TO %u: %s!\n",
432                   i, strings[i]);
433     }
434   }
435   if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
436   {
437     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438                 "test: FAILED! %u connected peers\n",
439                 regex_peers);
440     return 1;
441   }
442   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
443   return 0;
444 }
445
446 /* end of test_mesh_regex.c */