less debug output
[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, 15)
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   GNUNET_CORE_disconnect (p1.ch);
82   GNUNET_CORE_disconnect (p2.ch);
83   GNUNET_TRANSPORT_disconnect (p1.th);
84   GNUNET_TRANSPORT_disconnect (p2.th);
85   GNUNET_ARM_stop_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
86   GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, NULL);
87   ok = 0;
88 }
89
90
91 static void
92 connect_notify (void *cls,
93                 const struct GNUNET_PeerIdentity *peer)
94 {
95   GNUNET_assert ((ok == 5) || (ok == 6));
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97               "Encrypted connection established to peer `%4s'\n",
98               GNUNET_i2s (peer));
99 }
100
101
102 static void
103 disconnect_notify (void *cls,
104                    const struct GNUNET_PeerIdentity *peer)
105 {
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
108 }
109
110
111 static unsigned int
112 bfc_callback (void *cls,
113               const struct GNUNET_PeerIdentity *receiver,
114               void *position, unsigned int padding)
115 {
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117               "Core requests data for `%4s', I have none.\n",
118               GNUNET_i2s (receiver));
119   return 0;
120 }
121
122
123 static int
124 inbound_notify (void *cls,
125                 const struct GNUNET_PeerIdentity *other,
126                 const struct GNUNET_MessageHeader *message)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
130   return GNUNET_OK;
131 }
132
133
134 static int
135 outbound_notify (void *cls,
136                  const struct GNUNET_PeerIdentity *other,
137                  const struct GNUNET_MessageHeader *message)
138 {
139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140               "Core notifies about outbound data for `%4s'.\n",
141               GNUNET_i2s (other));
142   return GNUNET_OK;
143 }
144
145
146 static int
147 process_mtype (void *cls,
148                const struct GNUNET_PeerIdentity *peer,
149                const struct GNUNET_MessageHeader *message)
150 {
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
153   GNUNET_assert (ok == 5);
154   OKPP;
155   GNUNET_SCHEDULER_add_delayed (sched,
156                                 GNUNET_NO,
157                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
158                                 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
159                                 GNUNET_TIME_UNIT_ZERO, &terminate_task, NULL);
160   return GNUNET_OK;
161 }
162
163
164 static struct GNUNET_CORE_MessageHandler handlers[] = {
165   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
166   {NULL, 0, 0}
167 };
168
169
170 static size_t
171 transmit_ready (void *cls, size_t size, void *buf)
172 {
173   struct PeerContext *p = cls;
174   struct GNUNET_MessageHeader *m;
175
176   GNUNET_assert (ok == 4);
177   OKPP;
178   GNUNET_assert (p == &p1);
179   GNUNET_assert (buf != NULL);
180   m = (struct GNUNET_MessageHeader *) buf;
181   m->type = htons (MTYPE);
182   m->size = htons (sizeof (struct GNUNET_MessageHeader));
183   return sizeof (struct GNUNET_MessageHeader);
184 }
185
186
187
188 static void
189 init_notify (void *cls,
190              struct GNUNET_CORE_Handle *server,
191              const struct GNUNET_PeerIdentity *my_identity,
192              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
193 {
194   struct PeerContext *p = cls;
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Core connection to `%4s' established\n",
198               GNUNET_i2s (my_identity));
199   GNUNET_assert (server != NULL);
200   p->id = *my_identity;
201   p->ch = server;
202   if (cls == &p1)
203     {
204       GNUNET_assert (ok == 2);
205       OKPP;
206       /* connect p2 */
207       GNUNET_CORE_connect (sched,
208                            p2.cfg,
209                            TIMEOUT,
210                            &p2,
211                            &init_notify,
212                            &connect_notify,
213                            &disconnect_notify,
214                            &bfc_callback,
215                            &inbound_notify,
216                            GNUNET_YES,
217                            &outbound_notify, GNUNET_YES, handlers);
218     }
219   else
220     {
221       GNUNET_assert (ok == 3);
222       OKPP;
223       GNUNET_assert (cls == &p2);
224       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                   "Asking core (1) for transmission to peer `%4s'\n",
226                   GNUNET_i2s (&p2.id));
227       GNUNET_CORE_notify_transmit_ready (p1.ch,
228                                          0,
229                                          TIMEOUT,
230                                          &p2.id,
231                                          sizeof (struct GNUNET_MessageHeader),
232                                          &transmit_ready, &p1);
233
234     }
235 }
236
237
238 static void
239 process_hello (void *cls,
240                struct GNUNET_TIME_Relative latency,
241                const struct GNUNET_PeerIdentity *peer,
242                const struct GNUNET_MessageHeader *message)
243 {
244   struct PeerContext *p = cls;
245
246   GNUNET_assert (peer != NULL);
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248               "Received (my) `%s' from transport service of `%4s'\n",
249               "HELLO", GNUNET_i2s (peer));
250   GNUNET_assert (message != NULL);
251   p->hello = GNUNET_malloc (ntohs (message->size));
252   memcpy (p->hello, message, ntohs (message->size));
253   if ((p == &p1) && (p2.th != NULL))
254     GNUNET_TRANSPORT_offer_hello (p2.th, message);
255   if ((p == &p2) && (p1.th != NULL))
256     GNUNET_TRANSPORT_offer_hello (p1.th, message);
257
258   if ((p == &p1) && (p2.hello != NULL))
259     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
260   if ((p == &p2) && (p1.hello != NULL))
261     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
262 }
263
264
265
266 static void
267 setup_peer (struct PeerContext *p, const char *cfgname)
268 {
269   p->cfg = GNUNET_CONFIGURATION_create ();
270 #if START_ARM
271   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
272                                         "gnunet-service-arm",
273 #if VERBOSE
274                                         "-L", "DEBUG",
275 #endif
276                                         "-c", cfgname, NULL);
277   sleep (1);                    /* allow ARM to start */
278 #endif
279   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
280   GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
281   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
282   GNUNET_assert (p->th != NULL);
283   GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
284 }
285
286
287 static void
288 run (void *cls,
289      struct GNUNET_SCHEDULER_Handle *s,
290      char *const *args,
291      const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
292 {
293   GNUNET_assert (ok == 1);
294   OKPP;
295   sched = s;
296   setup_peer (&p1, "test_core_api_peer1.conf");
297   setup_peer (&p2, "test_core_api_peer2.conf");
298   GNUNET_CORE_connect (sched,
299                        p1.cfg,
300                        TIMEOUT,
301                        &p1,
302                        &init_notify,
303                        &connect_notify,
304                        &disconnect_notify,
305                        &bfc_callback,
306                        &inbound_notify,
307                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
308 }
309
310
311 static void
312 stop_arm (struct PeerContext *p)
313 {
314 #if START_ARM
315   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
316     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
317   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
318     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320               "ARM process %u stopped\n", p->arm_pid);
321 #endif
322   GNUNET_CONFIGURATION_destroy (p->cfg);
323 }
324
325 static int
326 check ()
327 {
328   char *const argv[] = { "test-core-api",
329     "-c",
330     "test_core_api_data.conf",
331 #if VERBOSE
332     "-L", "DEBUG",
333 #endif
334     NULL
335   };
336   struct GNUNET_GETOPT_CommandLineOption options[] = {
337     GNUNET_GETOPT_OPTION_END
338   };
339   // sleep (1); /* for 'make check': allow previous processes to fully terminate */
340   ok = 1;
341   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
342                       argv, "test-core-api", "nohelp", options, &run, &ok);
343   stop_arm (&p1);
344   stop_arm (&p2);
345   return ok;
346 }
347
348 int
349 main (int argc, char *argv[])
350 {
351   int ret;
352
353   GNUNET_log_setup ("test-core-api",
354 #if VERBOSE
355                     "DEBUG",
356 #else
357                     "WARNING",
358 #endif
359                     NULL);
360   ret = check ();
361
362   return ret;
363 }
364
365 /* end of test_core_api.c */