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