missing function reference
[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, 60)
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 #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   GNUNET_assert ((ok == 5) || (ok == 6));
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119               "Encrypted connection established to peer `%4s'\n",
120               GNUNET_i2s (peer));
121 }
122
123
124 static void
125 disconnect_notify (void *cls,
126                    const struct GNUNET_PeerIdentity *peer)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
130 }
131
132
133 static int
134 inbound_notify (void *cls,
135                 const struct GNUNET_PeerIdentity *other,
136                 const struct GNUNET_MessageHeader *message,
137                 struct GNUNET_TIME_Relative latency,
138                 uint32_t distance)
139 {
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
142   return GNUNET_OK;
143 }
144
145
146 static int
147 outbound_notify (void *cls,
148                  const struct GNUNET_PeerIdentity *other,
149                  const struct GNUNET_MessageHeader *message,
150                  struct GNUNET_TIME_Relative latency,
151                  uint32_t distance)
152 {
153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154               "Core notifies about outbound data for `%4s'.\n",
155               GNUNET_i2s (other));
156   return GNUNET_OK;
157 }
158
159
160 static GNUNET_SCHEDULER_TaskIdentifier err_task;
161
162
163 static int
164 process_mtype (void *cls,
165                const struct GNUNET_PeerIdentity *peer,
166                const struct GNUNET_MessageHeader *message,
167                struct GNUNET_TIME_Relative latency,
168                uint32_t distance)
169 {
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
172   GNUNET_assert (ok == 5);
173   OKPP;
174   GNUNET_SCHEDULER_cancel (sched, err_task);
175   GNUNET_SCHEDULER_add_now (sched, &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_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &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                            NULL,
233                            &connect_notify,
234                            &disconnect_notify,
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
248       if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
249                                          0,
250                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
251                                          &p2.id,
252                                          sizeof (struct GNUNET_MessageHeader),
253                                          &transmit_ready, &p1))
254         {
255           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256                       "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
257                       GNUNET_i2s (&p2.id));
258         }
259
260     }
261 }
262
263
264 static void
265 process_hello (void *cls,
266                const struct GNUNET_MessageHeader *message)
267 {
268   struct PeerContext *p = cls;
269
270   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
271   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272               "Received (my) `%s' from transport service\n",
273               "HELLO");
274   GNUNET_assert (message != NULL);
275   p->hello = GNUNET_malloc (ntohs (message->size));
276   memcpy (p->hello, message, ntohs (message->size));
277   if ((p == &p1) && (p2.th != NULL))
278     GNUNET_TRANSPORT_offer_hello (p2.th, message);
279   if ((p == &p2) && (p1.th != NULL))
280     GNUNET_TRANSPORT_offer_hello (p1.th, message);
281
282   if ((p == &p1) && (p2.hello != NULL))
283     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
284   if ((p == &p2) && (p1.hello != NULL))
285     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
286 }
287
288
289
290 static void
291 setup_peer (struct PeerContext *p, const char *cfgname)
292 {
293   p->cfg = GNUNET_CONFIGURATION_create ();
294 #if START_ARM
295   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
296                                         "gnunet-service-arm",
297 #if VERBOSE
298                                         "-L", "DEBUG",
299 #endif
300                                         "-c", cfgname, NULL);
301 #endif
302   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
303   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
304   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
305   GNUNET_assert (p->th != NULL);
306   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
307 }
308
309
310 static void
311 run (void *cls,
312      struct GNUNET_SCHEDULER_Handle *s,
313      char *const *args,
314      const char *cfgfile,
315      const struct GNUNET_CONFIGURATION_Handle *cfg)
316 {
317   GNUNET_assert (ok == 1);
318   OKPP;
319   sched = s;
320   setup_peer (&p1, "test_core_api_peer1.conf");
321   setup_peer (&p2, "test_core_api_peer2.conf");
322   GNUNET_CORE_connect (sched,
323                        p1.cfg,
324                        TIMEOUT,
325                        &p1,
326                        &init_notify,
327                        NULL,
328                        &connect_notify,
329                        &disconnect_notify,
330                        &inbound_notify,
331                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
332 }
333
334
335 static void
336 stop_arm (struct PeerContext *p)
337 {
338 #if START_ARM
339   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
340     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
341   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
342     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
343   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344               "ARM process %u stopped\n", p->arm_pid);
345 #endif
346   GNUNET_CONFIGURATION_destroy (p->cfg);
347 }
348
349 static int
350 check ()
351 {
352   char *const argv[] = { "test-core-api",
353     "-c",
354     "test_core_api_data.conf",
355 #if VERBOSE
356     "-L", "DEBUG",
357 #endif
358     NULL
359   };
360   struct GNUNET_GETOPT_CommandLineOption options[] = {
361     GNUNET_GETOPT_OPTION_END
362   };
363   ok = 1;
364   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
365                       argv, "test-core-api", "nohelp", options, &run, &ok);
366   stop_arm (&p1);
367   stop_arm (&p2);
368   return ok;
369 }
370
371 int
372 main (int argc, char *argv[])
373 {
374   int ret;
375
376   GNUNET_log_setup ("test-core-api",
377 #if VERBOSE
378                     "DEBUG",
379 #else
380                     "WARNING",
381 #endif
382                     NULL);
383   ret = check ();
384   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
385   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
386
387   return ret;
388 }
389
390 /* end of test_core_api.c */