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