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