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