how did this test ever work without assigning core handles?
[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 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_transport_service.h"
33
34 #define MTYPE 12345
35
36 struct PeerContext
37 {
38   struct GNUNET_CONFIGURATION_Handle *cfg;
39   struct GNUNET_CORE_Handle *ch;
40   struct GNUNET_PeerIdentity id;
41   struct GNUNET_TRANSPORT_Handle *th;
42   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
43   struct GNUNET_MessageHeader *hello;
44   int connect_status;
45   struct GNUNET_OS_Process *arm_proc;
46 };
47
48 static struct PeerContext p1;
49
50 static struct PeerContext p2;
51
52 static GNUNET_SCHEDULER_TaskIdentifier err_task;
53
54 static GNUNET_SCHEDULER_TaskIdentifier con_task;
55
56 static int ok;
57
58 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
59
60
61 static void
62 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
63 {
64   struct PeerContext *p = cls;
65
66   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67               "Received (my) `%s' from transport service\n", "HELLO");
68   GNUNET_assert (message != NULL);
69   if ((p == &p1) && (p2.th != NULL))
70     GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
71   if ((p == &p2) && (p1.th != NULL))
72     GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
73 }
74
75
76 static void
77 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   GNUNET_assert (ok == 6);
80   GNUNET_CORE_disconnect (p1.ch);
81   p1.ch = NULL;
82   GNUNET_CORE_disconnect (p2.ch);
83   p2.ch = NULL;
84   GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
85   p1.ghh = NULL;
86   GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
87   p2.ghh = NULL;
88   GNUNET_TRANSPORT_disconnect (p1.th);
89   p1.th = NULL;
90   GNUNET_TRANSPORT_disconnect (p2.th);
91   p2.th = NULL;
92   if (GNUNET_SCHEDULER_NO_TASK != con_task)
93   {
94     GNUNET_SCHEDULER_cancel (con_task);
95     con_task = GNUNET_SCHEDULER_NO_TASK;
96   }
97   ok = 0;
98 }
99
100
101 static void
102 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105               "ENDING ANGRILY %u\n", ok);
106   GNUNET_break (0);
107   if (NULL != p1.ch)
108   {
109     GNUNET_CORE_disconnect (p1.ch);
110     p1.ch = NULL;
111   }
112   if (NULL != p2.ch)
113   {
114     GNUNET_CORE_disconnect (p2.ch);
115     p2.ch = NULL;
116   }
117   if (p1.th != NULL)
118   {
119     GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
120     GNUNET_TRANSPORT_disconnect (p1.th);
121     p1.th = NULL;
122   }
123   if (p2.th != NULL)
124   {
125     GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
126     GNUNET_TRANSPORT_disconnect (p2.th);
127     p2.th = NULL;
128   }
129   if (GNUNET_SCHEDULER_NO_TASK != con_task)
130   {
131     GNUNET_SCHEDULER_cancel (con_task);
132     con_task = GNUNET_SCHEDULER_NO_TASK;
133   }
134   ok = 42;
135 }
136
137
138 static size_t
139 transmit_ready (void *cls, size_t size, void *buf)
140 {
141   struct PeerContext *p = cls;
142   struct GNUNET_MessageHeader *m;
143
144   GNUNET_assert (ok == 4);
145   OKPP;
146   GNUNET_assert (p == &p1);
147   GNUNET_assert (buf != NULL);
148   m = (struct GNUNET_MessageHeader *) buf;
149   m->type = htons (MTYPE);
150   m->size = htons (sizeof (struct GNUNET_MessageHeader));
151   return sizeof (struct GNUNET_MessageHeader);
152 }
153
154
155 static void
156 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
157 {
158   struct PeerContext *pc = cls;
159
160   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
161     return;
162   GNUNET_assert (pc->connect_status == 0);
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Encrypted connection established to peer `%4s'\n",
165               GNUNET_i2s (peer));
166   if (GNUNET_SCHEDULER_NO_TASK != con_task)
167   {
168     GNUNET_SCHEDULER_cancel (con_task);
169     con_task = GNUNET_SCHEDULER_NO_TASK;
170   }
171   pc->connect_status = 1;
172   if (pc == &p1)
173   {
174     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175                 "Asking core (1) for transmission to peer `%4s'\n",
176                 GNUNET_i2s (&p2.id));
177     if (NULL ==
178         GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES, 0,
179                                            GNUNET_TIME_relative_multiply
180                                            (GNUNET_TIME_UNIT_SECONDS, 145),
181                                            &p2.id,
182                                            sizeof (struct GNUNET_MessageHeader),
183                                            &transmit_ready, &p1))
184     {
185       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
187                   GNUNET_i2s (&p2.id));
188     }
189   }
190 }
191
192
193 static void
194 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
195 {
196   struct PeerContext *pc = cls;
197
198   if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
199     return;
200   pc->connect_status = 0;
201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
202               GNUNET_i2s (peer));
203 }
204
205
206 static int
207 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
208                 const struct GNUNET_MessageHeader *message)
209 {
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
212   return GNUNET_OK;
213 }
214
215
216 static int
217 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
218                  const struct GNUNET_MessageHeader *message)
219 {
220   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221               "Core notifies about outbound data for `%4s'.\n",
222               GNUNET_i2s (other));
223   return GNUNET_OK;
224 }
225
226
227
228 static int
229 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
230                const struct GNUNET_MessageHeader *message)
231 {
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving message from `%4s'.\n",
233               GNUNET_i2s (peer));
234   GNUNET_assert (ok == 5);
235   OKPP;
236   GNUNET_SCHEDULER_cancel (err_task);
237   err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
238   return GNUNET_OK;
239 }
240
241
242 static struct GNUNET_CORE_MessageHandler handlers[] = {
243   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
244   {NULL, 0, 0}
245 };
246
247
248 static void
249 connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
252   {
253     con_task = GNUNET_SCHEDULER_NO_TASK;
254     return;
255   }
256   con_task =
257       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &connect_task,
258                                     NULL);
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Asking transport (1) to connect to peer `%4s'\n",
261               GNUNET_i2s (&p2.id));
262   GNUNET_TRANSPORT_try_connect (p1.th, &p2.id, NULL, NULL); /*FIXME TRY_CONNECT change */
263 }
264
265
266 static void
267 init_notify (void *cls, 
268              const struct GNUNET_PeerIdentity *my_identity)
269 {
270   struct PeerContext *p = cls;
271
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core connection to `%4s' established\n",
273               GNUNET_i2s (my_identity));
274   p->id = *my_identity;
275   if (cls == &p1)
276   {
277     GNUNET_assert (ok == 2);
278     OKPP;
279     /* connect p2 */
280     p2.ch =
281         GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
282                              &disconnect_notify, &inbound_notify, GNUNET_YES,
283                              &outbound_notify, GNUNET_YES, handlers);
284   }
285   else
286   {
287     GNUNET_assert (ok == 3);
288     OKPP;
289     GNUNET_assert (cls == &p2);
290     con_task = GNUNET_SCHEDULER_add_now (&connect_task, NULL);
291   }
292 }
293
294
295 static void
296 setup_peer (struct PeerContext *p, const char *cfgname)
297 {
298   char *binary;
299
300   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
301   p->cfg = GNUNET_CONFIGURATION_create ();
302   p->arm_proc =
303     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 
304                              NULL, NULL, 
305                              binary,
306                              "gnunet-service-arm",
307                                "-c", cfgname, NULL);
308   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
309   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
310   GNUNET_assert (p->th != NULL);
311   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
312   GNUNET_free (binary);
313 }
314
315
316 static void
317 run (void *cls, char *const *args, const char *cfgfile,
318      const struct GNUNET_CONFIGURATION_Handle *cfg)
319 {
320   GNUNET_assert (ok == 1);
321   OKPP;
322   setup_peer (&p1, "test_core_api_peer1.conf");
323   setup_peer (&p2, "test_core_api_peer2.conf");
324   err_task =
325       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
326                                     (GNUNET_TIME_UNIT_SECONDS, 300),
327                                     &terminate_task_error, NULL);
328   p1.ch =
329       GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
330                            &disconnect_notify, &inbound_notify, GNUNET_YES,
331                            &outbound_notify, GNUNET_YES, handlers);
332 }
333
334
335 static void
336 stop_arm (struct PeerContext *p)
337 {
338   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
339     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
340   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
341     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
342   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
343               GNUNET_OS_process_get_pid (p->arm_proc));
344   GNUNET_OS_process_destroy (p->arm_proc);
345   p->arm_proc = NULL;
346   GNUNET_CONFIGURATION_destroy (p->cfg);
347 }
348
349
350 int
351 main (int argc, char *argv1[])
352 {
353   char *const argv[] = { "test-core-api",
354     "-c",
355     "test_core_api_data.conf",
356     NULL
357   };
358   struct GNUNET_GETOPT_CommandLineOption options[] = {
359     GNUNET_GETOPT_OPTION_END
360   };
361   ok = 1;
362   GNUNET_log_setup ("test-core-api",
363                     "WARNING",
364                     NULL);
365   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
366                       "test-core-api", "nohelp", options, &run, &ok);
367   stop_arm (&p1);
368   stop_arm (&p2);
369   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
370   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
371
372   return ok;
373 }
374
375 /* end of test_core_api.c */