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