installing all service, daemon and helper binaries to lib/gnunet/libexec/; updating...
[oweals/gnunet.git] / src / core / test_core_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 core/test_core_api.c
22  * @brief testcase for core_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_transport_service.h"
33
34 #define VERBOSE GNUNET_NO
35
36 #define START_ARM GNUNET_YES
37
38 #define MTYPE 12345
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_CORE_Handle *ch;
44   struct GNUNET_PeerIdentity id;
45   struct GNUNET_TRANSPORT_Handle *th;
46   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
47   struct GNUNET_MessageHeader *hello;
48   int connect_status;
49 #if START_ARM
50   struct GNUNET_OS_Process *arm_proc;
51 #endif
52 };
53
54 static struct PeerContext p1;
55
56 static struct PeerContext p2;
57
58 static GNUNET_SCHEDULER_TaskIdentifier err_task;
59
60 static GNUNET_SCHEDULER_TaskIdentifier con_task;
61
62 static int ok;
63
64 #if VERBOSE
65 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66 #else
67 #define OKPP do { ok++; } while (0)
68 #endif
69
70
71 static void
72 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
73 {
74   struct PeerContext *p = cls;
75
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77               "Received (my) `%s' from transport service\n", "HELLO");
78   GNUNET_assert (message != NULL);
79   if ((p == &p1) && (p2.th != NULL))
80     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
81   if ((p == &p2) && (p1.th != NULL))
82     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
83 }
84
85
86 static void
87 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   GNUNET_assert (ok == 6);
90   GNUNET_CORE_disconnect (p1.ch);
91   p1.ch = NULL;
92   GNUNET_CORE_disconnect (p2.ch);
93   p2.ch = NULL;
94   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
95   p1.ghh = NULL;
96   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
97   p2.ghh = NULL;
98   GNUNET_TRANSPORT_disconnect (p1.th);
99   p1.th = NULL;
100   GNUNET_TRANSPORT_disconnect (p2.th);
101   p2.th = NULL;
102   if (GNUNET_SCHEDULER_NO_TASK != con_task)
103   {
104     GNUNET_SCHEDULER_cancel (con_task);
105     con_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107   ok = 0;
108 }
109
110
111 static void
112 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114 #if VERBOSE
115   FPRINTF (stderr, "ENDING ANGRILY %u\n", ok);
116 #endif
117   GNUNET_break (0);
118   if (NULL != p1.ch)
119   {
120     GNUNET_CORE_disconnect (p1.ch);
121     p1.ch = NULL;
122   }
123   if (NULL != p2.ch)
124   {
125     GNUNET_CORE_disconnect (p2.ch);
126     p2.ch = NULL;
127   }
128   if (p1.th != NULL)
129   {
130     GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
131     GNUNET_TRANSPORT_disconnect (p1.th);
132     p1.th = NULL;
133   }
134   if (p2.th != NULL)
135   {
136     GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
137     GNUNET_TRANSPORT_disconnect (p2.th);
138     p2.th = NULL;
139   }
140   if (GNUNET_SCHEDULER_NO_TASK != con_task)
141   {
142     GNUNET_SCHEDULER_cancel (con_task);
143     con_task = GNUNET_SCHEDULER_NO_TASK;
144   }
145   ok = 42;
146 }
147
148
149 static size_t
150 transmit_ready (void *cls, size_t size, void *buf)
151 {
152   struct PeerContext *p = cls;
153   struct GNUNET_MessageHeader *m;
154
155   GNUNET_assert (ok == 4);
156   OKPP;
157   GNUNET_assert (p == &p1);
158   GNUNET_assert (buf != NULL);
159   m = (struct GNUNET_MessageHeader *) buf;
160   m->type = htons (MTYPE);
161   m->size = htons (sizeof (struct GNUNET_MessageHeader));
162   return sizeof (struct GNUNET_MessageHeader);
163 }
164
165
166 static void
167 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
168                 const struct GNUNET_ATS_Information *atsi,
169                 unsigned int atsi_count)
170 {
171   struct PeerContext *pc = cls;
172
173   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
174     return;
175   GNUNET_assert (pc->connect_status == 0);
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177               "Encrypted connection established to peer `%4s'\n",
178               GNUNET_i2s (peer));
179   if (GNUNET_SCHEDULER_NO_TASK != con_task)
180   {
181     GNUNET_SCHEDULER_cancel (con_task);
182     con_task = GNUNET_SCHEDULER_NO_TASK;
183   }
184   pc->connect_status = 1;
185   if (pc == &p1)
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188                 "Asking core (1) for transmission to peer `%4s'\n",
189                 GNUNET_i2s (&p2.id));
190     if (NULL ==
191         GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES, 0,
192                                            GNUNET_TIME_relative_multiply
193                                            (GNUNET_TIME_UNIT_SECONDS, 145),
194                                            &p2.id,
195                                            sizeof (struct GNUNET_MessageHeader),
196                                            &transmit_ready, &p1))
197     {
198       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
200                   GNUNET_i2s (&p2.id));
201     }
202   }
203 }
204
205
206 static void
207 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
208 {
209   struct PeerContext *pc = cls;
210
211   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
212     return;
213   pc->connect_status = 0;
214   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
215               GNUNET_i2s (peer));
216 }
217
218
219 static int
220 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
221                 const struct GNUNET_MessageHeader *message,
222                 const struct GNUNET_ATS_Information *atsi,
223                 unsigned int atsi_count)
224 {
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
227   return GNUNET_OK;
228 }
229
230
231 static int
232 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
233                  const struct GNUNET_MessageHeader *message,
234                  const struct GNUNET_ATS_Information *atsi,
235                  unsigned int atsi_count)
236 {
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Core notifies about outbound data for `%4s'.\n",
239               GNUNET_i2s (other));
240   return GNUNET_OK;
241 }
242
243
244
245 static int
246 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
247                const struct GNUNET_MessageHeader *message,
248                const struct GNUNET_ATS_Information *atsi,
249                unsigned int atsi_count)
250 {
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving message from `%4s'.\n",
252               GNUNET_i2s (peer));
253   GNUNET_assert (ok == 5);
254   OKPP;
255   GNUNET_SCHEDULER_cancel (err_task);
256   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
257   return GNUNET_OK;
258 }
259
260
261 static struct GNUNET_CORE_MessageHandler handlers[] = {
262   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
263   {NULL, 0, 0}
264 };
265
266
267 static void
268 connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
271   {
272     con_task = GNUNET_SCHEDULER_NO_TASK;
273     return;
274   }
275   con_task =
276       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &connect_task,
277                                     NULL);
278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279               "Asking transport (1) to connect to peer `%4s'\n",
280               GNUNET_i2s (&p2.id));
281   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
282 }
283
284
285 static void
286 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
287              const struct GNUNET_PeerIdentity *my_identity)
288 {
289   struct PeerContext *p = cls;
290
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core connection to `%4s' established\n",
292               GNUNET_i2s (my_identity));
293   GNUNET_assert (server != NULL);
294   p->id = *my_identity;
295   p->ch = server;
296   if (cls == &p1)
297   {
298     GNUNET_assert (ok == 2);
299     OKPP;
300     /* connect p2 */
301     p2.ch =
302         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
303                              &disconnect_notify, &inbound_notify, GNUNET_YES,
304                              &outbound_notify, GNUNET_YES, handlers);
305   }
306   else
307   {
308     GNUNET_assert (ok == 3);
309     OKPP;
310     GNUNET_assert (cls == &p2);
311     con_task = GNUNET_SCHEDULER_add_now (&connect_task, NULL);
312   }
313 }
314
315
316 static void
317 setup_peer (struct PeerContext *p, const char *cfgname)
318 {
319   char *binary;
320
321   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
322   p->cfg = GNUNET_CONFIGURATION_create ();
323 #if START_ARM
324   p->arm_proc =
325     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 
326                              NULL, NULL, 
327                              binary,
328                              "gnunet-service-arm",
329                                "-c", cfgname, NULL);
330 #endif
331   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
332   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
333   GNUNET_assert (p->th != NULL);
334   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
335   GNUNET_free (binary);
336 }
337
338
339 static void
340 run (void *cls, char *const *args, const char *cfgfile,
341      const struct GNUNET_CONFIGURATION_Handle *cfg)
342 {
343   GNUNET_assert (ok == 1);
344   OKPP;
345   setup_peer (&p1, "test_core_api_peer1.conf");
346   setup_peer (&p2, "test_core_api_peer2.conf");
347   err_task =
348       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
349                                     (GNUNET_TIME_UNIT_SECONDS, 300),
350                                     &terminate_task_error, NULL);
351   p1.ch =
352       GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
353                            &disconnect_notify, &inbound_notify, GNUNET_YES,
354                            &outbound_notify, GNUNET_YES, handlers);
355 }
356
357
358 static void
359 stop_arm (struct PeerContext *p)
360 {
361 #if START_ARM
362   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
363     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
364   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
365     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
366   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
367               GNUNET_OS_process_get_pid (p->arm_proc));
368   GNUNET_OS_process_destroy (p->arm_proc);
369   p->arm_proc = NULL;
370 #endif
371   GNUNET_CONFIGURATION_destroy (p->cfg);
372 }
373
374 static int
375 check ()
376 {
377   char *const argv[] = { "test-core-api",
378     "-c",
379     "test_core_api_data.conf",
380 #if VERBOSE
381     "-L", "DEBUG",
382 #endif
383     NULL
384   };
385   struct GNUNET_GETOPT_CommandLineOption options[] = {
386     GNUNET_GETOPT_OPTION_END
387   };
388   ok = 1;
389   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
390                       "test-core-api", "nohelp", options, &run, &ok);
391   stop_arm (&p1);
392   stop_arm (&p2);
393   return ok;
394 }
395
396 int
397 main (int argc, char *argv[])
398 {
399   int ret;
400
401   GNUNET_log_setup ("test-core-api",
402 #if VERBOSE
403                     "DEBUG",
404 #else
405                     "WARNING",
406 #endif
407                     NULL);
408   ret = check ();
409   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
410   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
411
412   return ret;
413 }
414
415 /* end of test_core_api.c */