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