df354a8560cbf7c6d9c49b31f188e0f131655f7f
[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_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
86   GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, 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_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
100   GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, 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, unsigned int padding)
129 {
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131               "Core requests data for `%4s', I have none.\n",
132               GNUNET_i2s (receiver));
133   return 0;
134 }
135
136
137 static int
138 inbound_notify (void *cls,
139                 const struct GNUNET_PeerIdentity *other,
140                 const struct GNUNET_MessageHeader *message)
141 {
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
144   return GNUNET_OK;
145 }
146
147
148 static int
149 outbound_notify (void *cls,
150                  const struct GNUNET_PeerIdentity *other,
151                  const struct GNUNET_MessageHeader *message)
152 {
153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154               "Core notifies about outbound data for `%4s'.\n",
155               GNUNET_i2s (other));
156   return GNUNET_OK;
157 }
158
159
160 static GNUNET_SCHEDULER_TaskIdentifier err_task;
161
162
163 static int
164 process_mtype (void *cls,
165                const struct GNUNET_PeerIdentity *peer,
166                const struct GNUNET_MessageHeader *message)
167 {
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
170   GNUNET_assert (ok == 5);
171   OKPP;
172   GNUNET_SCHEDULER_cancel (sched, err_task);
173   GNUNET_SCHEDULER_add_delayed (sched,
174                                 GNUNET_NO,
175                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
176                                 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
177                                 GNUNET_TIME_UNIT_ZERO, &terminate_task, NULL);
178   return GNUNET_OK;
179 }
180
181
182 static struct GNUNET_CORE_MessageHandler handlers[] = {
183   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
184   {NULL, 0, 0}
185 };
186
187
188 static size_t
189 transmit_ready (void *cls, size_t size, void *buf)
190 {
191   struct PeerContext *p = cls;
192   struct GNUNET_MessageHeader *m;
193
194   GNUNET_assert (ok == 4);
195   OKPP;
196   GNUNET_assert (p == &p1);
197   GNUNET_assert (buf != NULL);
198   m = (struct GNUNET_MessageHeader *) buf;
199   m->type = htons (MTYPE);
200   m->size = htons (sizeof (struct GNUNET_MessageHeader));
201   err_task = 
202     GNUNET_SCHEDULER_add_delayed (sched,
203                                   GNUNET_NO,
204                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
205                                   GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
206                                   GNUNET_TIME_UNIT_MINUTES, &terminate_task_error, NULL);
207
208   return sizeof (struct GNUNET_MessageHeader);
209 }
210
211
212
213 static void
214 init_notify (void *cls,
215              struct GNUNET_CORE_Handle *server,
216              const struct GNUNET_PeerIdentity *my_identity,
217              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
218 {
219   struct PeerContext *p = cls;
220
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222               "Core connection to `%4s' established\n",
223               GNUNET_i2s (my_identity));
224   GNUNET_assert (server != NULL);
225   p->id = *my_identity;
226   p->ch = server;
227   if (cls == &p1)
228     {
229       GNUNET_assert (ok == 2);
230       OKPP;
231       /* connect p2 */
232       GNUNET_CORE_connect (sched,
233                            p2.cfg,
234                            TIMEOUT,
235                            &p2,
236                            &init_notify,
237                            &connect_notify,
238                            &disconnect_notify,
239                            &bfc_callback,
240                            &inbound_notify,
241                            GNUNET_YES,
242                            &outbound_notify, GNUNET_YES, handlers);
243     }
244   else
245     {
246       GNUNET_assert (ok == 3);
247       OKPP;
248       GNUNET_assert (cls == &p2);
249       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250                   "Asking core (1) for transmission to peer `%4s'\n",
251                   GNUNET_i2s (&p2.id));
252       GNUNET_CORE_notify_transmit_ready (p1.ch,
253                                          0,
254                                          TIMEOUT,
255                                          &p2.id,
256                                          sizeof (struct GNUNET_MessageHeader),
257                                          &transmit_ready, &p1);
258
259     }
260 }
261
262
263 static void
264 process_hello (void *cls,
265                struct GNUNET_TIME_Relative latency,
266                const struct GNUNET_PeerIdentity *peer,
267                const struct GNUNET_MessageHeader *message)
268 {
269   struct PeerContext *p = cls;
270
271   GNUNET_assert (peer != NULL);
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273               "Received (my) `%s' from transport service of `%4s'\n",
274               "HELLO", GNUNET_i2s (peer));
275   GNUNET_assert (message != NULL);
276   p->hello = GNUNET_malloc (ntohs (message->size));
277   memcpy (p->hello, message, ntohs (message->size));
278   if ((p == &p1) && (p2.th != NULL))
279     GNUNET_TRANSPORT_offer_hello (p2.th, message);
280   if ((p == &p2) && (p1.th != NULL))
281     GNUNET_TRANSPORT_offer_hello (p1.th, message);
282
283   if ((p == &p1) && (p2.hello != NULL))
284     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
285   if ((p == &p2) && (p1.hello != NULL))
286     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
287 }
288
289
290
291 static void
292 setup_peer (struct PeerContext *p, const char *cfgname)
293 {
294   p->cfg = GNUNET_CONFIGURATION_create ();
295 #if START_ARM
296   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
297                                         "gnunet-service-arm",
298 #if VERBOSE
299                                         "-L", "DEBUG",
300 #endif
301                                         "-c", cfgname, NULL);
302   sleep (1);                    /* allow ARM to start */
303 #endif
304   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
305   GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
306   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
307   GNUNET_assert (p->th != NULL);
308   GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
309 }
310
311
312 static void
313 run (void *cls,
314      struct GNUNET_SCHEDULER_Handle *s,
315      char *const *args,
316      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
317 {
318   GNUNET_assert (ok == 1);
319   OKPP;
320   sched = s;
321   setup_peer (&p1, "test_core_api_peer1.conf");
322   setup_peer (&p2, "test_core_api_peer2.conf");
323   GNUNET_CORE_connect (sched,
324                        p1.cfg,
325                        TIMEOUT,
326                        &p1,
327                        &init_notify,
328                        &connect_notify,
329                        &disconnect_notify,
330                        &bfc_callback,
331                        &inbound_notify,
332                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
333 }
334
335
336 static void
337 stop_arm (struct PeerContext *p)
338 {
339 #if START_ARM
340   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
341     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
342   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
343     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345               "ARM process %u stopped\n", p->arm_pid);
346 #endif
347   GNUNET_CONFIGURATION_destroy (p->cfg);
348 }
349
350 static int
351 check ()
352 {
353   char *const argv[] = { "test-core-api",
354     "-c",
355     "test_core_api_data.conf",
356 #if VERBOSE
357     "-L", "DEBUG",
358 #endif
359     NULL
360   };
361   struct GNUNET_GETOPT_CommandLineOption options[] = {
362     GNUNET_GETOPT_OPTION_END
363   };
364   // sleep (1); /* for 'make check': allow previous processes to fully terminate */
365   ok = 1;
366   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
367                       argv, "test-core-api", "nohelp", options, &run, &ok);
368   stop_arm (&p1);
369   stop_arm (&p2);
370   return ok;
371 }
372
373 int
374 main (int argc, char *argv[])
375 {
376   int ret;
377
378   GNUNET_log_setup ("test-core-api",
379 #if VERBOSE
380                     "DEBUG",
381 #else
382                     "WARNING",
383 #endif
384                     NULL);
385   ret = check ();
386
387   return ret;
388 }
389
390 /* end of test_core_api.c */