1ae9c74e8c639a2b971b60f66adadb422e9e43c6
[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_NO
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   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 #if VERBOSE
95   fprintf(stderr, "ENDING ANGRILY %u\n", ok);
96 #endif
97   GNUNET_break (0);
98   GNUNET_CORE_disconnect (p1.ch);
99   GNUNET_CORE_disconnect (p2.ch);
100   GNUNET_TRANSPORT_disconnect (p1.th);
101   GNUNET_TRANSPORT_disconnect (p2.th);
102   GNUNET_ARM_stop_services (p1.cfg, sched, "core", NULL);
103   GNUNET_ARM_stop_services (p2.cfg, sched, "core", NULL);
104   ok = 42;
105 }
106
107
108 static void
109 connect_notify (void *cls,
110                 const struct GNUNET_PeerIdentity *peer,
111                 struct GNUNET_TIME_Relative latency,
112                 uint32_t distance)
113 {
114   struct PeerContext *pc = cls;
115   GNUNET_assert (pc->connect_status == 0);
116   pc->connect_status = 1;
117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118               "Encrypted connection established to peer `%4s'\n",
119               GNUNET_i2s (peer));
120 }
121
122
123 static void
124 disconnect_notify (void *cls,
125                    const struct GNUNET_PeerIdentity *peer)
126 {
127   struct PeerContext *pc = cls;
128   pc->connect_status = 0;
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
131 }
132
133
134 static int
135 inbound_notify (void *cls,
136                 const struct GNUNET_PeerIdentity *other,
137                 const struct GNUNET_MessageHeader *message,
138                 struct GNUNET_TIME_Relative latency,
139                 uint32_t distance)
140 {
141   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
143   return GNUNET_OK;
144 }
145
146
147 static int
148 outbound_notify (void *cls,
149                  const struct GNUNET_PeerIdentity *other,
150                  const struct GNUNET_MessageHeader *message,
151                  struct GNUNET_TIME_Relative latency,
152                  uint32_t distance)
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                struct GNUNET_TIME_Relative latency,
169                uint32_t distance)
170 {
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
173   GNUNET_assert (ok == 5);
174   OKPP;
175   GNUNET_SCHEDULER_cancel (sched, err_task);
176   GNUNET_SCHEDULER_add_now (sched, &terminate_task, NULL);
177   return GNUNET_OK;
178 }
179
180
181 static struct GNUNET_CORE_MessageHandler handlers[] = {
182   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
183   {NULL, 0, 0}
184 };
185
186
187 static size_t
188 transmit_ready (void *cls, size_t size, void *buf)
189 {
190   struct PeerContext *p = cls;
191   struct GNUNET_MessageHeader *m;
192
193   GNUNET_assert (ok == 4);
194   OKPP;
195   GNUNET_assert (p == &p1);
196   GNUNET_assert (buf != NULL);
197   m = (struct GNUNET_MessageHeader *) buf;
198   m->type = htons (MTYPE);
199   m->size = htons (sizeof (struct GNUNET_MessageHeader));
200   err_task = 
201     GNUNET_SCHEDULER_add_delayed (sched,
202         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &terminate_task_error, NULL);
203
204   return sizeof (struct GNUNET_MessageHeader);
205 }
206
207
208
209 static void
210 init_notify (void *cls,
211              struct GNUNET_CORE_Handle *server,
212              const struct GNUNET_PeerIdentity *my_identity,
213              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
214 {
215   struct PeerContext *p = cls;
216
217   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218               "Core connection to `%4s' established\n",
219               GNUNET_i2s (my_identity));
220   GNUNET_assert (server != NULL);
221   p->id = *my_identity;
222   p->ch = server;
223   if (cls == &p1)
224     {
225       GNUNET_assert (ok == 2);
226       OKPP;
227       /* connect p2 */
228       GNUNET_CORE_connect (sched,
229                            p2.cfg,
230                            TIMEOUT,
231                            &p2,
232                            &init_notify,
233                            NULL,
234                            &connect_notify,
235                            &disconnect_notify,
236                            &inbound_notify,
237                            GNUNET_YES,
238                            &outbound_notify, GNUNET_YES, handlers);
239     }
240   else
241     {
242       GNUNET_assert (ok == 3);
243       OKPP;
244       GNUNET_assert (cls == &p2);
245       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246                   "Asking core (1) for transmission to peer `%4s'\n",
247                   GNUNET_i2s (&p2.id));
248
249       if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
250                                          0,
251                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
252                                          &p2.id,
253                                          sizeof (struct GNUNET_MessageHeader),
254                                          &transmit_ready, &p1))
255         {
256           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257                       "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
258                       GNUNET_i2s (&p2.id));
259         }
260
261     }
262 }
263
264
265 static void
266 process_hello (void *cls,
267                const struct GNUNET_MessageHeader *message)
268 {
269   struct PeerContext *p = cls;
270
271   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273               "Received (my) `%s' from transport service\n",
274               "HELLO");
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 #endif
303   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
304   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
305   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
306   GNUNET_assert (p->th != NULL);
307   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
308 }
309
310
311 static void
312 run (void *cls,
313      struct GNUNET_SCHEDULER_Handle *s,
314      char *const *args,
315      const char *cfgfile,
316      const 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                        NULL,
329                        &connect_notify,
330                        &disconnect_notify,
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   ok = 1;
365   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
366                       argv, "test-core-api", "nohelp", options, &run, &ok);
367   stop_arm (&p1);
368   stop_arm (&p2);
369   return ok;
370 }
371
372 int
373 main (int argc, char *argv[])
374 {
375   int ret;
376
377   GNUNET_log_setup ("test-core-api",
378 #if VERBOSE
379                     "DEBUG",
380 #else
381                     "WARNING",
382 #endif
383                     NULL);
384   ret = check ();
385   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
386   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
387
388   return ret;
389 }
390
391 /* end of test_core_api.c */