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