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