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