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