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