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