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