9f3aad016f0760047379d58d58769c852eab0241
[oweals/gnunet.git] / src / core / test_core_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46 #define MTYPE 12345
47
48 struct PeerContext
49 {
50   struct GNUNET_CONFIGURATION_Handle *cfg;
51   struct GNUNET_CORE_Handle *ch;
52   struct GNUNET_PeerIdentity id;   
53   struct GNUNET_TRANSPORT_Handle *th;
54   struct GNUNET_MessageHeader *hello;
55   int connect_status;
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 #if VERBOSE
82   fprintf(stderr, "ENDING WELL %u\n", ok);
83 #endif
84   GNUNET_CORE_disconnect (p1.ch);
85   GNUNET_CORE_disconnect (p2.ch);
86   GNUNET_TRANSPORT_disconnect (p1.th);
87   GNUNET_TRANSPORT_disconnect (p2.th);
88   GNUNET_ARM_stop_services (p1.cfg, sched, "core", NULL);
89   GNUNET_ARM_stop_services (p2.cfg, sched, "core", NULL);
90   ok = 0;
91 }
92
93
94 static void
95 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 {
97 #if VERBOSE
98   fprintf(stderr, "ENDING ANGRILY %u\n", ok);
99 #endif
100   GNUNET_break (0);
101   GNUNET_CORE_disconnect (p1.ch);
102   GNUNET_CORE_disconnect (p2.ch);
103   GNUNET_TRANSPORT_disconnect (p1.th);
104   GNUNET_TRANSPORT_disconnect (p2.th);
105   GNUNET_ARM_stop_services (p1.cfg, sched, "core", NULL);
106   GNUNET_ARM_stop_services (p2.cfg, sched, "core", NULL);
107   ok = 42;
108 }
109
110
111 static void
112 connect_notify (void *cls,
113                 const struct GNUNET_PeerIdentity *peer,
114                 struct GNUNET_TIME_Relative latency,
115                 uint32_t distance)
116 {
117   struct PeerContext *pc = cls;
118   GNUNET_assert (pc->connect_status == 0);
119   pc->connect_status = 1;
120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121               "Encrypted connection established to peer `%4s'\n",
122               GNUNET_i2s (peer));
123 }
124
125
126 static void
127 disconnect_notify (void *cls,
128                    const struct GNUNET_PeerIdentity *peer)
129 {
130   struct PeerContext *pc = cls;
131   GNUNET_assert (pc->connect_status == 1);
132   pc->connect_status = 0;
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
135 }
136
137
138 static int
139 inbound_notify (void *cls,
140                 const struct GNUNET_PeerIdentity *other,
141                 const struct GNUNET_MessageHeader *message,
142                 struct GNUNET_TIME_Relative latency,
143                 uint32_t distance)
144 {
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
147   return GNUNET_OK;
148 }
149
150
151 static int
152 outbound_notify (void *cls,
153                  const struct GNUNET_PeerIdentity *other,
154                  const struct GNUNET_MessageHeader *message,
155                  struct GNUNET_TIME_Relative latency,
156                  uint32_t distance)
157 {
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159               "Core notifies about outbound data for `%4s'.\n",
160               GNUNET_i2s (other));
161   return GNUNET_OK;
162 }
163
164
165 static GNUNET_SCHEDULER_TaskIdentifier err_task;
166
167
168 static int
169 process_mtype (void *cls,
170                const struct GNUNET_PeerIdentity *peer,
171                const struct GNUNET_MessageHeader *message,
172                struct GNUNET_TIME_Relative latency,
173                uint32_t distance)
174 {
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
177   GNUNET_assert (ok == 5);
178   OKPP;
179   GNUNET_SCHEDULER_cancel (sched, err_task);
180   GNUNET_SCHEDULER_add_now (sched, &terminate_task, NULL);
181   return GNUNET_OK;
182 }
183
184
185 static struct GNUNET_CORE_MessageHandler handlers[] = {
186   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
187   {NULL, 0, 0}
188 };
189
190
191 static size_t
192 transmit_ready (void *cls, size_t size, void *buf)
193 {
194   struct PeerContext *p = cls;
195   struct GNUNET_MessageHeader *m;
196
197   GNUNET_assert (ok == 4);
198   OKPP;
199   GNUNET_assert (p == &p1);
200   GNUNET_assert (buf != NULL);
201   m = (struct GNUNET_MessageHeader *) buf;
202   m->type = htons (MTYPE);
203   m->size = htons (sizeof (struct GNUNET_MessageHeader));
204   err_task = 
205     GNUNET_SCHEDULER_add_delayed (sched,
206         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &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                            NULL,
238                            &connect_notify,
239                            &disconnect_notify,
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
253       if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
254                                          0,
255                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
256                                          &p2.id,
257                                          sizeof (struct GNUNET_MessageHeader),
258                                          &transmit_ready, &p1))
259         {
260           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261                       "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
262                       GNUNET_i2s (&p2.id));
263         }
264
265     }
266 }
267
268
269 static void
270 process_hello (void *cls,
271                const struct GNUNET_MessageHeader *message)
272 {
273   struct PeerContext *p = cls;
274
275   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277               "Received (my) `%s' from transport service\n",
278               "HELLO");
279   GNUNET_assert (message != NULL);
280   p->hello = GNUNET_malloc (ntohs (message->size));
281   memcpy (p->hello, message, ntohs (message->size));
282   if ((p == &p1) && (p2.th != NULL))
283     GNUNET_TRANSPORT_offer_hello (p2.th, message);
284   if ((p == &p2) && (p1.th != NULL))
285     GNUNET_TRANSPORT_offer_hello (p1.th, message);
286
287   if ((p == &p1) && (p2.hello != NULL))
288     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
289   if ((p == &p2) && (p1.hello != NULL))
290     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
291 }
292
293
294
295 static void
296 setup_peer (struct PeerContext *p, const char *cfgname)
297 {
298   p->cfg = GNUNET_CONFIGURATION_create ();
299 #if START_ARM
300   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
301                                         "gnunet-service-arm",
302 #if VERBOSE
303                                         "-L", "DEBUG",
304 #endif
305                                         "-c", cfgname, NULL);
306 #endif
307   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
308   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
309   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
310   GNUNET_assert (p->th != NULL);
311   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
312 }
313
314
315 static void
316 run (void *cls,
317      struct GNUNET_SCHEDULER_Handle *s,
318      char *const *args,
319      const char *cfgfile,
320      const struct GNUNET_CONFIGURATION_Handle *cfg)
321 {
322   GNUNET_assert (ok == 1);
323   OKPP;
324   sched = s;
325   setup_peer (&p1, "test_core_api_peer1.conf");
326   setup_peer (&p2, "test_core_api_peer2.conf");
327   GNUNET_CORE_connect (sched,
328                        p1.cfg,
329                        TIMEOUT,
330                        &p1,
331                        &init_notify,
332                        NULL,
333                        &connect_notify,
334                        &disconnect_notify,
335                        &inbound_notify,
336                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
337 }
338
339
340 static void
341 stop_arm (struct PeerContext *p)
342 {
343 #if START_ARM
344   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
345     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
346   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
347     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "ARM process %u stopped\n", p->arm_pid);
350 #endif
351   GNUNET_CONFIGURATION_destroy (p->cfg);
352 }
353
354 static int
355 check ()
356 {
357   char *const argv[] = { "test-core-api",
358     "-c",
359     "test_core_api_data.conf",
360 #if VERBOSE
361     "-L", "DEBUG",
362 #endif
363     NULL
364   };
365   struct GNUNET_GETOPT_CommandLineOption options[] = {
366     GNUNET_GETOPT_OPTION_END
367   };
368   ok = 1;
369   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
370                       argv, "test-core-api", "nohelp", options, &run, &ok);
371   stop_arm (&p1);
372   stop_arm (&p2);
373   return ok;
374 }
375
376 int
377 main (int argc, char *argv[])
378 {
379   int ret;
380
381   GNUNET_log_setup ("test-core-api",
382 #if VERBOSE
383                     "DEBUG",
384 #else
385                     "WARNING",
386 #endif
387                     NULL);
388   ret = check ();
389   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
390   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
391
392   return ret;
393 }
394
395 /* end of test_core_api.c */