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