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