- fix #2315
[oweals/gnunet.git] / src / mesh / test_mesh_local_2.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 /**
22  * @file mesh/test_mesh_local.c
23  * @brief test mesh local: test of tunnels with just one peer
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_mesh_service.h"
31
32 #define VERBOSE 1
33 #define VERBOSE_ARM 0
34
35 static struct GNUNET_OS_Process *arm_pid;
36 static struct GNUNET_MESH_Handle *mesh_peer_1;
37 static struct GNUNET_MESH_Handle *mesh_peer_2;
38 static struct GNUNET_MESH_Tunnel *t;
39 static unsigned int one = 1;
40 static unsigned int two = 2;
41
42 static int result;
43 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
44 static GNUNET_SCHEDULER_TaskIdentifier test_task;
45
46
47 /**
48  * Shutdown nicely
49  */
50 static void
51 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
52 {
53   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
54   if (0 != abort_task)
55   {
56     GNUNET_SCHEDULER_cancel (abort_task);
57   }
58   if (NULL != t)
59   {
60     GNUNET_MESH_tunnel_destroy(t);
61   }
62   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D1\n");
63   if (NULL != mesh_peer_1)
64   {
65     GNUNET_MESH_disconnect (mesh_peer_1);
66   }
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: D2\n");
68   if (NULL != mesh_peer_2)
69   {
70     GNUNET_MESH_disconnect (mesh_peer_2);
71   }
72   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
73   if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
74   {
75     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
76   }
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
78   GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
79   GNUNET_OS_process_destroy (arm_pid);
80 }
81
82
83 /**
84  * Something went wrong and timed out. Kill everything and set error flag
85  */
86 static void
87 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
90   if (0 != test_task)
91   {
92     GNUNET_SCHEDULER_cancel (test_task);
93   }
94   result = GNUNET_SYSERR;
95   abort_task = 0;
96   do_shutdown (cls, tc);
97 }
98
99
100 /**
101  * Function is called whenever a message is received.
102  *
103  * @param cls closure (set from GNUNET_MESH_connect)
104  * @param tunnel connection to the other end
105  * @param tunnel_ctx place to store local state associated with the tunnel
106  * @param sender who sent the message
107  * @param message the actual message
108  * @param atsi performance data for the connection
109  * @return GNUNET_OK to keep the connection open,
110  *         GNUNET_SYSERR to close it (signal serious error)
111  */
112 static int
113 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
114                const struct GNUNET_PeerIdentity *sender,
115                const struct GNUNET_MessageHeader *message,
116                const struct GNUNET_ATS_Information *atsi)
117 {
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Data callback\n");
119   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
120                                 (GNUNET_TIME_UNIT_SECONDS, 2), &do_shutdown,
121                                 NULL);
122   return GNUNET_OK;
123 }
124
125
126 /**
127  * Method called whenever another peer has added us to a tunnel
128  * the other peer initiated.
129  *
130  * @param cls closure
131  * @param tunnel new handle to the tunnel
132  * @param initiator peer that started the tunnel
133  * @param atsi performance information for the tunnel
134  * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
135  */
136 static void *
137 inbound_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
138                 const struct GNUNET_PeerIdentity *initiator,
139                 const struct GNUNET_ATS_Information *atsi)
140 {
141   unsigned int id = *(unsigned int *) cls;
142
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: received incoming tunnel\n");
144   if (id != 1)
145   {
146     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147                 "test: received incoming tunnel on peer 2\n");
148     result = GNUNET_SYSERR;
149   }
150   return NULL;
151 }
152
153
154 /**
155  * Function called whenever an inbound tunnel is destroyed.  Should clean up
156  * any associated state.
157  *
158  * @param cls closure (set from GNUNET_MESH_connect)
159  * @param tunnel connection to the other end (henceforth invalid)
160  * @param tunnel_ctx place where local state associated
161  *                   with the tunnel is stored
162  */
163 static void
164 inbound_end (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
165              void *tunnel_ctx)
166 {
167   unsigned int id = *(unsigned int *) cls;
168
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: incoming tunnel closed\n");
170   if (id != 1)
171   {
172     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173                 "test: received closing tunnel on peer 2\n");
174     result = GNUNET_SYSERR;
175   }
176 }
177
178
179 /**
180  * Method called whenever a peer has disconnected from the tunnel.
181  *
182  * @param cls closure
183  * @param peer peer identity the tunnel stopped working with
184  */
185 static void
186 peer_conected (void *cls, const struct GNUNET_PeerIdentity *peer,
187                const struct GNUNET_ATS_Information *atsi)
188 {
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer connected\n");
190   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, NULL);
191 }
192
193
194 /**
195  * Method called whenever a peer has connected to the tunnel.
196  *
197  * @param cls closure
198  * @param peer peer identity the tunnel was created to, NULL on timeout
199  * @param atsi performance data for the connection
200  */
201 static void
202 peer_disconnected (void *cls, const struct GNUNET_PeerIdentity *peer)
203 {
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: peer disconnected\n");
205 }
206
207
208 /**
209  * Handler array for traffic received on peer1
210  */
211 static struct GNUNET_MESH_MessageHandler handlers1[] = {
212   {&data_callback, 1, 0},
213   {NULL, 0, 0}
214 };
215
216
217 /**
218  * Handler array for traffic received on peer2 (none expected)
219  */
220 static struct GNUNET_MESH_MessageHandler handlers2[] = { {NULL, 0, 0} };
221
222
223 /**
224  * Start looking for a peer by type
225  */
226 static void
227 do_connect_peer_1 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
230   static const GNUNET_MESH_ApplicationType app1[] = { 1, 0 };
231
232   test_task = GNUNET_SCHEDULER_NO_TASK;
233   mesh_peer_1 = GNUNET_MESH_connect (cfg,       /* configuration */
234                                      10,        /* queue size */
235                                      (void *) &one,     /* cls */
236                                      &inbound_tunnel,   /* inbound new hndlr */
237                                      &inbound_end,      /* inbound end hndlr */
238                                      handlers1, /* traffic handlers */
239                                      app1);     /* apps offered */
240 }
241
242
243 /**
244  * Main test function
245  */
246 static void
247 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 {
249   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
250   static const GNUNET_MESH_ApplicationType app2[] = { 0 };
251
252   test_task = GNUNET_SCHEDULER_NO_TASK;
253
254   mesh_peer_2 = GNUNET_MESH_connect (cfg,       /* configuration */
255                                      10,        /* queue size */
256                                      (void *) &two,     /* cls */
257                                      &inbound_tunnel,   /* inbound new hndlr */
258                                      &inbound_end,      /* inbound end hndlr */
259                                      handlers2, /* traffic handlers */
260                                      app2);     /* apps offered */
261   if (NULL == mesh_peer_2)
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Couldn't connect to mesh :(\n");
264     return;
265   }
266   else
267   {
268     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: YAY! CONNECTED TO MESH :D\n");
269   }
270
271   t = GNUNET_MESH_tunnel_create (mesh_peer_2, NULL, &peer_conected,
272                                  &peer_disconnected, (void *) &two);
273   GNUNET_MESH_peer_request_connect_by_type (t, 1);
274   test_task =
275       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
276                                     (GNUNET_TIME_UNIT_SECONDS, 5),
277                                     &do_connect_peer_1, cfg);
278 }
279
280
281 /**
282  * Initialize framework and start test
283  */
284 static void
285 run (void *cls, char *const *args, const char *cfgfile,
286      const struct GNUNET_CONFIGURATION_Handle *cfg)
287 {
288   GNUNET_log_setup ("test_mesh_local",
289 #if VERBOSE
290                     "DEBUG",
291 #else
292                     "WARNING",
293 #endif
294                     NULL);
295   arm_pid =
296       GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
297                                "gnunet-service-arm",
298 #if VERBOSE_ARM
299                                "-L", "DEBUG",
300 #endif
301                                "-c", "test_mesh.conf", NULL);
302
303   abort_task =
304       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
305                                     (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
306                                     NULL);
307
308   test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
309
310 }
311
312
313 /**
314  * Main
315  */
316 int
317 main (int argc, char *argv[])
318 {
319   int ret;
320
321   char *const argv2[] = { "test-mesh-local",
322     "-c", "test_mesh.conf",
323 #if VERBOSE
324     "-L", "DEBUG",
325 #endif
326     NULL
327   };
328   struct GNUNET_GETOPT_CommandLineOption options[] = {
329     GNUNET_GETOPT_OPTION_END
330   };
331
332   ret =
333       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
334                           "test-mesh-local", "nohelp", options, &run, NULL);
335
336   if (GNUNET_OK != ret)
337   {
338     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "run failed with error code %d\n",
339                 ret);
340     return 1;
341   }
342   if (GNUNET_SYSERR == result)
343   {
344     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
345                 "test failed: find_by_type, then connect\n");
346     return 1;
347   }
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
349   return 0;
350 }