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