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