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