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