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