(no commit message)
[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 3, 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   ok = 0;
86 }
87
88
89 static void
90 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92 #if VERBOSE
93   fprintf(stderr, "ENDING ANGRILY %u\n", ok);
94 #endif
95   GNUNET_break (0);
96   GNUNET_CORE_disconnect (p1.ch);
97   GNUNET_CORE_disconnect (p2.ch);
98   GNUNET_TRANSPORT_disconnect (p1.th);
99   GNUNET_TRANSPORT_disconnect (p2.th);
100   ok = 42;
101 }
102
103
104 static void
105 connect_notify (void *cls,
106                 const struct GNUNET_PeerIdentity *peer,
107                 struct GNUNET_TIME_Relative latency,
108                 uint32_t distance)
109 {
110   struct PeerContext *pc = cls;
111   GNUNET_assert (pc->connect_status == 0);
112   pc->connect_status = 1;
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114               "Encrypted connection established to peer `%4s'\n",
115               GNUNET_i2s (peer));
116 }
117
118
119 static void
120 disconnect_notify (void *cls,
121                    const struct GNUNET_PeerIdentity *peer)
122 {
123   struct PeerContext *pc = cls;
124   pc->connect_status = 0;
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
127 }
128
129
130 static int
131 inbound_notify (void *cls,
132                 const struct GNUNET_PeerIdentity *other,
133                 const struct GNUNET_MessageHeader *message,
134                 struct GNUNET_TIME_Relative latency,
135                 uint32_t distance)
136 {
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
139   return GNUNET_OK;
140 }
141
142
143 static int
144 outbound_notify (void *cls,
145                  const struct GNUNET_PeerIdentity *other,
146                  const struct GNUNET_MessageHeader *message,
147                  struct GNUNET_TIME_Relative latency,
148                  uint32_t distance)
149 {
150   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151               "Core notifies about outbound data for `%4s'.\n",
152               GNUNET_i2s (other));
153   return GNUNET_OK;
154 }
155
156
157 static GNUNET_SCHEDULER_TaskIdentifier err_task;
158
159
160 static int
161 process_mtype (void *cls,
162                const struct GNUNET_PeerIdentity *peer,
163                const struct GNUNET_MessageHeader *message,
164                struct GNUNET_TIME_Relative latency,
165                uint32_t distance)
166 {
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
169   GNUNET_assert (ok == 5);
170   OKPP;
171   GNUNET_SCHEDULER_cancel (sched, err_task);
172   GNUNET_SCHEDULER_add_now (sched, &terminate_task, NULL);
173   return GNUNET_OK;
174 }
175
176
177 static struct GNUNET_CORE_MessageHandler handlers[] = {
178   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
179   {NULL, 0, 0}
180 };
181
182
183 static size_t
184 transmit_ready (void *cls, size_t size, void *buf)
185 {
186   struct PeerContext *p = cls;
187   struct GNUNET_MessageHeader *m;
188
189   GNUNET_assert (ok == 4);
190   OKPP;
191   GNUNET_assert (p == &p1);
192   GNUNET_assert (buf != NULL);
193   m = (struct GNUNET_MessageHeader *) buf;
194   m->type = htons (MTYPE);
195   m->size = htons (sizeof (struct GNUNET_MessageHeader));
196   err_task = 
197     GNUNET_SCHEDULER_add_delayed (sched,
198         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &terminate_task_error, NULL);
199
200   return sizeof (struct GNUNET_MessageHeader);
201 }
202
203
204
205 static void
206 init_notify (void *cls,
207              struct GNUNET_CORE_Handle *server,
208              const struct GNUNET_PeerIdentity *my_identity,
209              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
210 {
211   struct PeerContext *p = cls;
212
213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214               "Core connection to `%4s' established\n",
215               GNUNET_i2s (my_identity));
216   GNUNET_assert (server != NULL);
217   p->id = *my_identity;
218   p->ch = server;
219   if (cls == &p1)
220     {
221       GNUNET_assert (ok == 2);
222       OKPP;
223       /* connect p2 */
224       GNUNET_CORE_connect (sched,
225                            p2.cfg,
226                            TIMEOUT,
227                            &p2,
228                            &init_notify,                         
229                            &connect_notify,
230                            &disconnect_notify,
231                            &inbound_notify,
232                            GNUNET_YES,
233                            &outbound_notify, GNUNET_YES, handlers);
234     }
235   else
236     {
237       GNUNET_assert (ok == 3);
238       OKPP;
239       GNUNET_assert (cls == &p2);
240       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241                   "Asking core (1) for transmission to peer `%4s'\n",
242                   GNUNET_i2s (&p2.id));
243
244       if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
245                                          0,
246                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
247                                          &p2.id,
248                                          sizeof (struct GNUNET_MessageHeader),
249                                          &transmit_ready, &p1))
250         {
251           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                       "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
253                       GNUNET_i2s (&p2.id));
254         }
255
256     }
257 }
258
259
260 static void
261 process_hello (void *cls,
262                const struct GNUNET_MessageHeader *message)
263 {
264   struct PeerContext *p = cls;
265
266   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268               "Received (my) `%s' from transport service\n",
269               "HELLO");
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 (NULL, NULL, "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   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, &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                        &inbound_notify,
325                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
326 }
327
328
329 static void
330 stop_arm (struct PeerContext *p)
331 {
332 #if START_ARM
333   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
334     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
335   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
336     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
337   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338               "ARM process %u stopped\n", p->arm_pid);
339 #endif
340   GNUNET_CONFIGURATION_destroy (p->cfg);
341 }
342
343 static int
344 check ()
345 {
346   char *const argv[] = { "test-core-api",
347     "-c",
348     "test_core_api_data.conf",
349 #if VERBOSE
350     "-L", "DEBUG",
351 #endif
352     NULL
353   };
354   struct GNUNET_GETOPT_CommandLineOption options[] = {
355     GNUNET_GETOPT_OPTION_END
356   };
357   ok = 1;
358   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
359                       argv, "test-core-api", "nohelp", options, &run, &ok);
360   stop_arm (&p1);
361   stop_arm (&p2);
362   return ok;
363 }
364
365 int
366 main (int argc, char *argv[])
367 {
368   int ret;
369
370   GNUNET_log_setup ("test-core-api",
371 #if VERBOSE
372                     "DEBUG",
373 #else
374                     "WARNING",
375 #endif
376                     NULL);
377   ret = check ();
378   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
379   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
380
381   return ret;
382 }
383
384 /* end of test_core_api.c */