allow transmission to self via core API
[oweals/gnunet.git] / src / core / test_core_api_reliability.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_reliability.c
22  * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
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_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37
38 #define VERBOSE GNUNET_NO
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * Note that this value must not significantly exceed
44  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
45  * messages may be dropped even for a reliable transport.
46  */
47 #define TOTAL_MSGS (600 * 10)
48
49 /**
50  * How long until we give up on transmitting the message?
51  */
52 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 6000)
53
54 /**
55  * What delay do we request from the core service for transmission?
56  * Any value smaller than the CORK delay will disable CORKing, which
57  * is what we want here.
58  */
59 #define FAST_TIMEOUT GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2)
60
61 #define MTYPE 12345
62
63
64 static unsigned long long total_bytes;
65
66 static struct GNUNET_TIME_Absolute start_time;
67
68 static GNUNET_SCHEDULER_TaskIdentifier err_task;
69
70
71 struct PeerContext
72 {
73   struct GNUNET_CONFIGURATION_Handle *cfg;
74   struct GNUNET_CORE_Handle *ch;
75   struct GNUNET_PeerIdentity id;   
76   struct GNUNET_TRANSPORT_Handle *th;
77   struct GNUNET_MessageHeader *hello;
78   int connect_status;
79 #if START_ARM
80   struct GNUNET_OS_Process *arm_proc;
81 #endif
82 };
83
84 static struct PeerContext p1;
85
86 static struct PeerContext p2;
87
88 static int ok;
89
90 static int32_t tr_n;
91
92
93 #if VERBOSE
94 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
95 #else
96 #define OKPP do { ok++; } while (0)
97 #endif
98
99 struct TestMessage 
100 {
101   struct GNUNET_MessageHeader header;
102   uint32_t num;
103 };
104
105
106 static unsigned int
107 get_size (unsigned int iter)
108 {
109   unsigned int ret;
110   if (iter < 60000)
111     return iter + sizeof (struct TestMessage);
112   ret = (iter * iter * iter);
113   return sizeof (struct TestMessage) + (ret % 60000);
114 }
115
116
117 static void
118 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 {
120   unsigned long long delta;
121
122   GNUNET_CORE_disconnect (p1.ch);
123   p1.ch = NULL;
124   GNUNET_CORE_disconnect (p2.ch);
125   p2.ch = NULL;
126   GNUNET_TRANSPORT_disconnect (p1.th);
127   p1.th = NULL;
128   GNUNET_TRANSPORT_disconnect (p2.th);
129   p2.th = NULL;
130   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
131   fprintf (stderr,
132            "\nThroughput was %llu kb/s\n",
133            total_bytes * 1000 / 1024 / delta);
134   ok = 0;
135 }
136
137
138 static void
139 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 {
141   GNUNET_break (0);
142   GNUNET_CORE_disconnect (p1.ch);
143   p1.ch = NULL;
144   GNUNET_CORE_disconnect (p2.ch);
145   p2.ch = NULL;
146   GNUNET_TRANSPORT_disconnect (p1.th);
147   p1.th = NULL;
148   GNUNET_TRANSPORT_disconnect (p2.th);
149   p2.th = NULL;
150   ok = 42;
151 }
152
153
154 static size_t
155 transmit_ready (void *cls, size_t size, void *buf)
156 {
157   char *cbuf = buf;
158   struct TestMessage hdr;
159   unsigned int s;
160   unsigned int ret;
161
162   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE); 
163   if (buf == NULL)
164     {
165       if (p1.ch != NULL)
166         GNUNET_break (NULL != 
167                       GNUNET_CORE_notify_transmit_ready (p1.ch,
168                                                          0,
169                                                          FAST_TIMEOUT,
170                                                          &p2.id,
171                                                          get_size(tr_n),
172                                                          &transmit_ready, &p1));
173       return 0;
174     }
175   GNUNET_assert (tr_n < TOTAL_MSGS);
176   ret = 0;
177   s = get_size (tr_n);
178   GNUNET_assert (size >= s);
179   GNUNET_assert (buf != NULL);
180   cbuf = buf;
181   do
182     {
183 #if VERBOSE
184       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185                   "Sending message %u of size %u at offset %u\n",
186                   tr_n,
187                   s,
188                   ret);
189 #endif
190       hdr.header.size = htons (s);
191       hdr.header.type = htons (MTYPE);
192       hdr.num = htonl (tr_n);
193       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
194       ret += sizeof (struct TestMessage);
195       memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
196       ret += s - sizeof (struct TestMessage);
197       tr_n++;
198       s = get_size (tr_n);
199       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
200         break; /* sometimes pack buffer full, sometimes not */
201     }
202   while (size - ret >= s);
203   GNUNET_SCHEDULER_cancel (err_task);
204   err_task = 
205     GNUNET_SCHEDULER_add_delayed (TIMEOUT,
206                                   &terminate_task_error, 
207                                   NULL);
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209               "Returning total message block of size %u\n",
210               ret);
211   total_bytes += ret;
212   return ret;
213 }
214
215
216
217 static void
218 connect_notify (void *cls,
219                 const struct GNUNET_PeerIdentity *peer,
220                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
221 {
222   struct PeerContext *pc = cls;
223
224   if (0 == memcmp (&pc->id,
225                    peer,
226                    sizeof (struct GNUNET_PeerIdentity)))
227     return;
228   GNUNET_assert (pc->connect_status == 0);
229   pc->connect_status = 1;
230   if (pc == &p1)
231     {
232       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233                   "Encrypted connection established to peer `%4s'\n",
234                   GNUNET_i2s (peer));
235       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236                   "Asking core (1) for transmission to peer `%4s'\n",
237                   GNUNET_i2s (&p2.id));
238       err_task = 
239         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
240                                       &terminate_task_error, 
241                                       NULL);
242       start_time = GNUNET_TIME_absolute_get ();
243       GNUNET_break (NULL != 
244                     GNUNET_CORE_notify_transmit_ready (p1.ch,
245                                                        0,
246                                                        TIMEOUT,
247                                                        &p2.id,
248                                                        get_size (0),
249                                                        &transmit_ready, &p1));
250     }
251 }
252
253
254 static void
255 disconnect_notify (void *cls,
256                    const struct GNUNET_PeerIdentity *peer)
257 {
258   struct PeerContext *pc = cls;
259
260   if (0 == memcmp (&pc->id,
261                    peer,
262                    sizeof (struct GNUNET_PeerIdentity)))
263     return;
264   pc->connect_status = 0;
265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
267 }
268
269
270 static int
271 inbound_notify (void *cls,
272                 const struct GNUNET_PeerIdentity *other,
273                 const struct GNUNET_MessageHeader *message,
274                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
275 {
276 #if VERBOSE
277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
279 #endif
280   return GNUNET_OK;
281 }
282
283
284 static int
285 outbound_notify (void *cls,
286                  const struct GNUNET_PeerIdentity *other,
287                  const struct GNUNET_MessageHeader *message,
288                  const struct GNUNET_TRANSPORT_ATS_Information *atsi)
289 {
290 #if VERBOSE
291   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292               "Core notifies about outbound data for `%4s'.\n",
293               GNUNET_i2s (other));
294 #endif
295   return GNUNET_OK;
296 }
297
298
299 static size_t
300 transmit_ready (void *cls, size_t size, void *buf);
301
302 static int
303 process_mtype (void *cls,
304                const struct GNUNET_PeerIdentity *peer,
305                const struct GNUNET_MessageHeader *message,
306                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
307 {
308   static int n;
309   unsigned int s;
310   const struct TestMessage *hdr;
311
312   hdr = (const struct TestMessage*) message;
313   s = get_size (n);
314   if (MTYPE != ntohs (message->type))
315     return GNUNET_SYSERR;
316   if (ntohs (message->size) != s)
317     {
318       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319                   "Expected message %u of size %u, got %u bytes of message %u\n",
320                   n, s,
321                   ntohs (message->size),
322                   ntohl (hdr->num));
323       GNUNET_SCHEDULER_cancel (err_task);
324       err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
325       return GNUNET_SYSERR;
326     }
327   if (ntohl (hdr->num) != n)
328     {
329       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330                   "Expected message %u of size %u, got %u bytes of message %u\n",
331                   n, s,
332                   ntohs (message->size),
333                   ntohl (hdr->num));
334       GNUNET_SCHEDULER_cancel (err_task);
335       err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
336       return GNUNET_SYSERR;
337     }
338 #if VERBOSE
339   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340               "Got message %u of size %u\n",
341               ntohl (hdr->num),
342               ntohs (message->size));         
343 #endif
344   n++;
345   if (0 == (n % (TOTAL_MSGS/100)))
346     fprintf (stderr, ".");
347   if (n == TOTAL_MSGS)
348     {
349       GNUNET_SCHEDULER_cancel (err_task);
350       GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
351     }
352   else
353     {
354       if (n == tr_n)
355         GNUNET_break (NULL != 
356                       GNUNET_CORE_notify_transmit_ready (p1.ch,
357                                                          0,
358                                                          FAST_TIMEOUT,
359                                                          &p2.id,
360                                                          get_size(tr_n),
361                                                          &transmit_ready, &p1));
362     }
363   return GNUNET_OK;
364 }
365
366
367 static struct GNUNET_CORE_MessageHandler handlers[] = {
368   {&process_mtype, MTYPE, 0},
369   {NULL, 0, 0}
370 };
371
372
373
374 static void
375 init_notify (void *cls,
376              struct GNUNET_CORE_Handle *server,
377              const struct GNUNET_PeerIdentity *my_identity,
378              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
379 {
380   struct PeerContext *p = cls;
381
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383               "Connection to CORE service of `%4s' established\n",
384               GNUNET_i2s (my_identity));
385   GNUNET_assert (server != NULL);
386   p->id = *my_identity;
387   p->ch = server;
388   if (cls == &p1)
389     {
390       GNUNET_assert (ok == 2);
391       OKPP;
392       /* connect p2 */
393       GNUNET_CORE_connect (p2.cfg, 1,
394                            &p2,
395                            &init_notify,                         
396                            &connect_notify,
397                            &disconnect_notify,
398                            NULL,
399                            &inbound_notify,
400                            GNUNET_YES,
401                            &outbound_notify, GNUNET_YES, handlers);
402     }
403   else
404     {
405       GNUNET_assert (ok == 3);
406       OKPP;
407       GNUNET_assert (cls == &p2);
408       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409                   "Asking core (1) to connect to peer `%4s'\n",
410                   GNUNET_i2s (&p2.id));
411       GNUNET_CORE_peer_request_connect (p1.ch,
412                                         GNUNET_TIME_UNIT_SECONDS,
413                                         &p2.id,
414                                         NULL, NULL);
415     }
416 }
417
418
419 static void
420 process_hello (void *cls,
421                const struct GNUNET_MessageHeader *message)
422 {
423   struct PeerContext *p = cls;
424
425   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
426   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427               "Received (my) `%s' from transport service\n",
428               "HELLO");
429   GNUNET_assert (message != NULL);
430   p->hello = GNUNET_malloc (ntohs (message->size));
431   memcpy (p->hello, message, ntohs (message->size));
432   if ((p == &p1) && (p2.th != NULL))
433     GNUNET_TRANSPORT_offer_hello (p2.th, message);
434   if ((p == &p2) && (p1.th != NULL))
435     GNUNET_TRANSPORT_offer_hello (p1.th, message);
436
437   if ((p == &p1) && (p2.hello != NULL))
438     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
439   if ((p == &p2) && (p1.hello != NULL))
440     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
441 }
442
443
444
445 static void
446 setup_peer (struct PeerContext *p, const char *cfgname)
447 {
448   p->cfg = GNUNET_CONFIGURATION_create ();
449 #if START_ARM
450   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
451                                         "gnunet-service-arm",
452 #if VERBOSE
453                                         "-L", "DEBUG",
454 #endif
455                                         "-c", cfgname, NULL);
456 #endif
457   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
458   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
459   GNUNET_assert (p->th != NULL);
460   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
461 }
462
463
464 static void
465 run (void *cls,
466      char *const *args,
467      const char *cfgfile,
468      const struct GNUNET_CONFIGURATION_Handle *cfg)
469 {
470   GNUNET_assert (ok == 1);
471   OKPP;
472   setup_peer (&p1, "test_core_api_peer1.conf");
473   setup_peer (&p2, "test_core_api_peer2.conf");
474   GNUNET_CORE_connect (p1.cfg, 1,
475                        &p1,
476                        &init_notify,
477                        &connect_notify,
478                        &disconnect_notify,
479                        NULL,
480                        &inbound_notify,
481                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
482 }
483
484
485 static void
486 stop_arm (struct PeerContext *p)
487 {
488 #if START_ARM
489   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
490     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
491   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
492     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
493   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
495   GNUNET_OS_process_close (p->arm_proc);
496   p->arm_proc = NULL;
497 #endif
498   GNUNET_CONFIGURATION_destroy (p->cfg);
499 }
500
501 static int
502 check ()
503 {
504   char *const argv[] = { "test-core-api-reliability",
505     "-c",
506     "test_core_api_data.conf",
507 #if VERBOSE
508     "-L", "DEBUG",
509 #endif
510     NULL
511   };
512   struct GNUNET_GETOPT_CommandLineOption options[] = {
513     GNUNET_GETOPT_OPTION_END
514   };
515   ok = 1;
516   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
517                       argv, "test-core-api-reliability", "nohelp", options, &run, &ok);
518   stop_arm (&p1);
519   stop_arm (&p2);
520   return ok;
521 }
522
523 int
524 main (int argc, char *argv[])
525 {
526   int ret;
527
528   GNUNET_log_setup ("test-core-api",
529 #if VERBOSE
530                     "DEBUG",
531 #else
532                     "WARNING",
533 #endif
534                     NULL);
535   ret = check ();
536   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 
537   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
538
539   return ret;
540 }
541
542 /* end of test_core_api_reliability.c */