the big core API/protocol change, breaks all testcases using core, since the code...
[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
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 void
99 connect_notify (void *cls,
100                 const struct GNUNET_PeerIdentity *peer,
101                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
102 {
103   struct PeerContext *pc = cls;
104   GNUNET_assert (pc->connect_status == 0);
105   pc->connect_status = 1;
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107               "Encrypted connection established to peer `%4s'\n",
108               GNUNET_i2s (peer));
109 }
110
111
112 static void
113 disconnect_notify (void *cls,
114                    const struct GNUNET_PeerIdentity *peer)
115 {
116   struct PeerContext *pc = cls;
117   pc->connect_status = 0;
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
120 }
121
122
123 static int
124 inbound_notify (void *cls,
125                 const struct GNUNET_PeerIdentity *other,
126                 const struct GNUNET_MessageHeader *message,
127                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
128 {
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
131   return GNUNET_OK;
132 }
133
134
135 static int
136 outbound_notify (void *cls,
137                  const struct GNUNET_PeerIdentity *other,
138                  const struct GNUNET_MessageHeader *message,
139                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
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                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
156 {
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
159   GNUNET_assert (ok == 5);
160   OKPP;
161   GNUNET_SCHEDULER_cancel (err_task);
162   GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
163   return GNUNET_OK;
164 }
165
166
167 static struct GNUNET_CORE_MessageHandler handlers[] = {
168   {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
169   {NULL, 0, 0}
170 };
171
172
173 static size_t
174 transmit_ready (void *cls, size_t size, void *buf)
175 {
176   struct PeerContext *p = cls;
177   struct GNUNET_MessageHeader *m;
178
179   GNUNET_assert (ok == 4);
180   OKPP;
181   GNUNET_assert (p == &p1);
182   GNUNET_assert (buf != NULL);
183   m = (struct GNUNET_MessageHeader *) buf;
184   m->type = htons (MTYPE);
185   m->size = htons (sizeof (struct GNUNET_MessageHeader));
186   err_task = 
187     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &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 (p2.cfg, 1,
214                            &p2,
215                            &init_notify,                         
216                            &connect_notify,
217                            &disconnect_notify,
218                            NULL,
219                            &inbound_notify,
220                            GNUNET_YES,
221                            &outbound_notify, GNUNET_YES, handlers);
222     }
223   else
224     {
225       GNUNET_assert (ok == 3);
226       OKPP;
227       GNUNET_assert (cls == &p2);
228       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229                   "Asking core (1) for transmission to peer `%4s'\n",
230                   GNUNET_i2s (&p2.id));
231
232       if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
233                                          0,
234                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
235                                          &p2.id,
236                                          sizeof (struct GNUNET_MessageHeader),
237                                          &transmit_ready, &p1))
238         {
239           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240                       "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
241                       GNUNET_i2s (&p2.id));
242         }
243
244     }
245 }
246
247
248 static void
249 process_hello (void *cls,
250                const struct GNUNET_MessageHeader *message)
251 {
252   struct PeerContext *p = cls;
253
254   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "Received (my) `%s' from transport service\n",
257               "HELLO");
258   GNUNET_assert (message != NULL);
259   p->hello = GNUNET_malloc (ntohs (message->size));
260   memcpy (p->hello, message, ntohs (message->size));
261   if ((p == &p1) && (p2.th != NULL))
262     GNUNET_TRANSPORT_offer_hello (p2.th, message);
263   if ((p == &p2) && (p1.th != NULL))
264     GNUNET_TRANSPORT_offer_hello (p1.th, message);
265
266   if ((p == &p1) && (p2.hello != NULL))
267     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
268   if ((p == &p2) && (p1.hello != NULL))
269     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
270 }
271
272
273
274 static void
275 setup_peer (struct PeerContext *p, const char *cfgname)
276 {
277   p->cfg = GNUNET_CONFIGURATION_create ();
278 #if START_ARM
279   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
280                                         "gnunet-service-arm",
281 #if VERBOSE
282                                         "-L", "DEBUG",
283 #endif
284                                         "-c", cfgname, NULL);
285 #endif
286   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
287   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
288   GNUNET_assert (p->th != NULL);
289   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
290 }
291
292
293 static void
294 run (void *cls,
295      char *const *args,
296      const char *cfgfile,
297      const struct GNUNET_CONFIGURATION_Handle *cfg)
298 {
299   GNUNET_assert (ok == 1);
300   OKPP;
301   setup_peer (&p1, "test_core_api_peer1.conf");
302   setup_peer (&p2, "test_core_api_peer2.conf");
303   GNUNET_CORE_connect (p1.cfg, 1,
304                        &p1,
305                        &init_notify,
306                        &connect_notify,
307                        &disconnect_notify,
308                        NULL,
309                        &inbound_notify,
310                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
311 }
312
313
314 static void
315 stop_arm (struct PeerContext *p)
316 {
317 #if START_ARM
318   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
319     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
320   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
321     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
324   GNUNET_OS_process_close (p->arm_proc);
325   p->arm_proc = NULL;
326 #endif
327   GNUNET_CONFIGURATION_destroy (p->cfg);
328 }
329
330 static int
331 check ()
332 {
333   char *const argv[] = { "test-core-api",
334     "-c",
335     "test_core_api_data.conf",
336 #if VERBOSE
337     "-L", "DEBUG",
338 #endif
339     NULL
340   };
341   struct GNUNET_GETOPT_CommandLineOption options[] = {
342     GNUNET_GETOPT_OPTION_END
343   };
344   ok = 1;
345   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
346                       argv, "test-core-api", "nohelp", options, &run, &ok);
347   stop_arm (&p1);
348   stop_arm (&p2);
349   return ok;
350 }
351
352 int
353 main (int argc, char *argv[])
354 {
355   int ret;
356
357   GNUNET_log_setup ("test-core-api",
358 #if VERBOSE
359                     "DEBUG",
360 #else
361                     "WARNING",
362 #endif
363                     NULL);
364   ret = check ();
365   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
366   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
367
368   return ret;
369 }
370
371 /* end of test_core_api.c */