add -w option to gnunet-config
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file transport/test_transport_api_reliability.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves ensures that messages are reliably sent between peers
25  *
26  * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
27  * and ensures that all message were received.
28  */
29 #include "platform.h"
30 #include "gnunet_transport_service.h"
31 #include "gauger.h"
32 #include "transport-testing.h"
33
34 /**
35  * Allow making the problem "bigger".
36  */
37 #define FACTOR 1
38
39 /**
40  * Total number of messages to send
41  *
42  * Note that this value must not significantly exceed
43  * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
44  * messages may be dropped even for a reliable transport.
45  */
46 #define TOTAL_MSGS (1024 * 3 * FACTOR)
47
48 /**
49  * Message type of test messages
50  */
51 #define MTYPE 12345
52
53 /**
54  * Testcase timeout
55  */
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90 * FACTOR)
57
58 /**
59  * How long until we give up on transmitting the message?
60  */
61 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60 * FACTOR)
62
63
64 /**
65  * Struct for the test message
66  */
67 GNUNET_NETWORK_STRUCT_BEGIN
68
69 struct TestMessage
70 {
71   struct GNUNET_MessageHeader header;
72   uint32_t num;
73 };
74 GNUNET_NETWORK_STRUCT_END
75
76
77 /**
78  * Name of the plugin to test
79  */
80 static char *test_plugin;
81
82 /**
83  * Name of the test
84  */
85 static char *test_name;
86
87 /**
88  * Return value of the test
89  */
90 static int ok;
91
92 /**
93  * Context of peer 1
94  */
95 static struct PeerContext *p1;
96
97 /**
98  * Configuration file of peer 1
99  */
100 static char *cfg_file_p1;
101
102 /**
103  * Context of peer 2
104  */
105 static struct PeerContext *p2;
106
107 /**
108  * Configuration file of peer 1
109  */
110 static char *cfg_file_p2;
111
112 /**
113  * Timeout task
114  */
115 static struct GNUNET_SCHEDULER_Task * die_task;
116
117 /**
118  * Transport transmit handle used
119  */
120 static struct GNUNET_TRANSPORT_TransmitHandle *th;
121
122 /**
123  * Transport testing handle
124  */
125 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
126
127 /*
128  * Total amount of bytes sent
129  */
130 static unsigned long long total_bytes;
131
132 /**
133  * Time of start
134  */
135 static struct GNUNET_TIME_Absolute start_time;
136
137 /**
138  * No. of message currently scheduled to be send
139  */
140 static int msg_scheduled;
141
142 /**
143  * No. of last message sent
144  */
145 static int msg_sent;
146
147 /**
148  * No. of last message received
149  */
150 static int msg_recv;
151
152 static int test_connected;
153
154 static int test_sending;
155
156 static int test_send_timeout;
157
158
159 /**
160  * Bitmap storing which messages were received
161  */
162 static char bitmap[TOTAL_MSGS / 8];
163
164 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
165
166 /*
167  * END Testcase specific declarations
168  */
169
170 #if VERBOSE
171 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
172 #else
173 #define OKPP do { ok++; } while (0)
174 #endif
175
176
177 static int
178 get_bit (const char *map, unsigned int bit);
179
180
181 static void
182 end ()
183 {
184   unsigned long long delta;
185   unsigned long long rate;
186   char *value_name;
187   unsigned int i;
188
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "Stopping peers\n");
191
192   /* Calculcate statistics   */
193   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
194   rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
195   FPRINTF (stderr,
196            "\nThroughput was %llu KiBytes/s\n",
197            rate);
198
199   GNUNET_asprintf (&value_name,
200                    "unreliable_%s",
201                    test_plugin);
202   GAUGER ("TRANSPORT",
203           value_name,
204           (int) rate,
205           "kb/s");
206   GNUNET_free (value_name);
207
208   if (die_task != NULL)
209     GNUNET_SCHEDULER_cancel (die_task);
210
211   if (th != NULL)
212   {
213     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
214     th = NULL;
215   }
216   if (cc != NULL)
217   {
218     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
219     cc = NULL;
220   }
221   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
222   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
223   GNUNET_TRANSPORT_TESTING_done (tth);
224   ok = 0;
225   for (i = 0; i < TOTAL_MSGS; i++)
226   {
227     if (get_bit (bitmap, i) == 0)
228     {
229       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                   "Did not receive message %d\n",
231                   i);
232       ok = -1;
233     }
234   }
235 }
236
237
238 static void
239 end_badly ()
240 {
241   unsigned int i;
242
243   die_task = NULL;
244   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245               "Fail! Stopping peers\n");
246   if (test_connected == GNUNET_YES)
247     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248                 "Peers got connected\n");
249   else
250     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251                 "Peers got NOT connected\n");
252
253   if (test_sending == GNUNET_NO)
254     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255                 "Testcase did not send any messages before timeout\n");
256   if (test_send_timeout == GNUNET_YES)
257     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258                 "Test had timeout while waiting to send data\n");
259   for (i = 0; i < TOTAL_MSGS; i++)
260   {
261     if (get_bit (bitmap, i) == 0)
262     {
263       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264                   "Did not receive message %u\n",
265                   i);
266       ok = -1;
267     }
268   }
269
270   if (th != NULL)
271   {
272     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
273     th = NULL;
274   }
275   if (cc != NULL)
276   {
277     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
278     cc = NULL;
279   }
280   if (p1 != NULL)
281     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
282   if (p2 != NULL)
283     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
284   GNUNET_TRANSPORT_TESTING_done (tth);
285   ok = GNUNET_SYSERR;
286 }
287
288
289 static unsigned int
290 get_size (unsigned int iter)
291 {
292   unsigned int ret;
293
294   ret = (iter * iter * iter);
295
296 #ifndef LINUX
297   /* FreeBSD/OSX etc. Unix DGRAMs do not work
298    * with large messages */
299   if (0 == strcmp ("unix", test_plugin))
300     return sizeof (struct TestMessage) + (ret % 1024);
301 #endif
302   return sizeof (struct TestMessage) + (ret % 60000);
303 }
304
305
306 /**
307  * Sets a bit active in the bitmap.
308  *
309  * @param bitIdx which bit to set
310  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
311  */
312 static int
313 set_bit (unsigned int bitIdx)
314 {
315   size_t arraySlot;
316   unsigned int targetBit;
317
318   if (bitIdx >= sizeof (bitmap) * 8)
319   {
320     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321                 "tried to set bit %d of %d(!?!?)\n",
322                 bitIdx, sizeof (bitmap) * 8);
323     return GNUNET_SYSERR;
324   }
325   arraySlot = bitIdx / 8;
326   targetBit = (1L << (bitIdx % 8));
327   bitmap[arraySlot] |= targetBit;
328   return GNUNET_OK;
329 }
330
331
332 /**
333  * Obtain a bit from bitmap.
334  * @param map the bitmap
335  * @param bit index from bitmap
336  *
337  * @return Bit \a bit from hashcode \a code
338  */
339 static int
340 get_bit (const char *map, unsigned int bit)
341 {
342   if (bit > TOTAL_MSGS)
343   {
344     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
345                 sizeof (bitmap) * 8);
346     return 0;
347   }
348   return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
349 }
350
351
352 static void
353 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
354                 const struct GNUNET_MessageHeader *message)
355 {
356   static int n;
357
358   unsigned int s;
359   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
360   const struct TestMessage *hdr;
361
362   hdr = (const struct TestMessage *) message;
363
364   if (MTYPE != ntohs (message->type))
365     return;
366   msg_recv = ntohl (hdr->num);
367   s = get_size (ntohl (hdr->num));
368
369   if (ntohs (message->size) != s)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372                 "Expected message %u of size %u, got %u bytes of message %u\n",
373                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
374     if (NULL != die_task)
375       GNUNET_SCHEDULER_cancel (die_task);
376     test_sending = GNUNET_YES;
377     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
378     return;
379   }
380
381   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
382   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
383   {
384     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385                 "Expected message %u with bits %u, but body did not match\n",
386                 ntohl (hdr->num), (unsigned char) n);
387     if (NULL != die_task)
388       GNUNET_SCHEDULER_cancel (die_task);
389     test_sending = GNUNET_YES;
390     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
391     return;
392   }
393 #if VERBOSE
394   if (ntohl (hdr->num) % 5 == 0)
395   {
396     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397                 "Got message %u of size %u\n",
398                 ntohl (hdr->num),
399                 ntohs (message->size));
400   }
401 #endif
402   n++;
403   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
404   {
405       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406                   "Message id %u is bigger than maxmimum number of messages %u expected\n",
407                   ntohl (hdr->num),
408                   TOTAL_MSGS);
409   }
410   test_sending = GNUNET_YES;
411   if (0 == (n % (TOTAL_MSGS / 100)))
412   {
413     FPRINTF (stderr, "%s",  ".");
414     if (NULL != die_task)
415       GNUNET_SCHEDULER_cancel (die_task);
416     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
417   }
418   if (n == TOTAL_MSGS)
419   {
420     end ();
421   }
422 }
423
424
425 static size_t
426 notify_ready (void *cls, size_t size, void *buf)
427 {
428   static int n;
429   char *cbuf = buf;
430   struct TestMessage hdr;
431   unsigned int s;
432   unsigned int ret;
433
434   th = NULL;
435
436   if (buf == NULL)
437   {
438     test_send_timeout = GNUNET_YES;
439     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440                 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
441                 msg_scheduled, TOTAL_MSGS);
442     if (NULL != die_task)
443       GNUNET_SCHEDULER_cancel (die_task);
444     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
445     ok = 42;
446     return 0;
447   }
448   ret = 0;
449   s = get_size (n);
450   GNUNET_assert (size >= s);
451   GNUNET_assert (buf != NULL);
452   GNUNET_assert (n < TOTAL_MSGS);
453   cbuf = buf;
454   do
455   {
456     GNUNET_assert (n < TOTAL_MSGS);
457     hdr.header.size = htons (s);
458     hdr.header.type = htons (MTYPE);
459     hdr.num = htonl (n);
460     msg_sent = n;
461     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
462     ret += sizeof (struct TestMessage);
463     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
464     ret += s - sizeof (struct TestMessage);
465
466 #if VERBOSE
467     if (n % 5000 == 0)
468     {
469       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470                   "Sending message %u of size %u\n",
471                   n,
472                   s);
473     }
474 #endif
475     n++;
476     s = get_size (n);
477     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
478       break;                    /* sometimes pack buffer full, sometimes not */
479   }
480   while ((size - ret >= s) && (n < TOTAL_MSGS));
481   if (n < TOTAL_MSGS)
482   {
483     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
484                                                  TIMEOUT_TRANSMIT,
485                                                  &notify_ready, NULL);
486     msg_scheduled = n;
487   }
488   else
489   {
490     FPRINTF (stderr, "%s",  "\n");
491     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492                 "All messages scheduled to be sent\n");
493     if (NULL != die_task)
494       GNUNET_SCHEDULER_cancel (die_task);
495     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
496   }
497   if (n % 5000 == 0)
498   {
499     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500                 "Returning total message block of size %u\n", ret);
501   }
502   total_bytes += ret;
503   return ret;
504 }
505
506
507 static void
508 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
509 {
510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511               "Peer `%4s' connected to us (%p)!\n",
512               GNUNET_i2s (peer), cls);
513 }
514
515
516 static void
517 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
518 {
519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520               "Peer `%4s' disconnected (%p)!\n",
521               GNUNET_i2s (peer), cls);
522   if (th != NULL)
523     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
524   th = NULL;
525 }
526
527
528 static void
529 sendtask (void *cls)
530 {
531   start_time = GNUNET_TIME_absolute_get ();
532   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533               "Starting to send %u messages\n",
534               TOTAL_MSGS);
535   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
536                                                TIMEOUT_TRANSMIT, &notify_ready,
537                                                NULL);
538 }
539
540
541 static void
542 testing_connect_cb (struct PeerContext *p1,
543                     struct PeerContext *p2,
544                     void *cls)
545 {
546   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
547
548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549               "Peers connected: %s <-> %s\n",
550               p1_c,
551               GNUNET_i2s (&p2->id));
552   GNUNET_free (p1_c);
553
554   test_connected = GNUNET_YES;
555   cc = NULL;
556
557   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
558 }
559
560
561 static void
562 start_cb (struct PeerContext *p, void *cls)
563 {
564   static int started;
565   started++;
566
567   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568               "Peer %u (`%s') started\n",
569               p->no,
570               GNUNET_i2s (&p->id));
571
572   if (started != 2)
573     return;
574
575   test_connected = GNUNET_NO;
576   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
577                                                NULL);
578
579 }
580
581
582 static void
583 run (void *cls,
584      char *const *args,
585      const char *cfgfile,
586      const struct GNUNET_CONFIGURATION_Handle *cfg)
587 {
588   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
589                                            &end_badly, NULL);
590   test_send_timeout = GNUNET_NO;
591
592   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
593                                             &notify_receive, &notify_connect,
594                                             &notify_disconnect, &start_cb,
595                                             NULL);
596   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
597                                             &notify_receive, &notify_connect,
598                                             &notify_disconnect, &start_cb,
599                                             NULL);
600   if ((p1 == NULL) || (p2 == NULL))
601   {
602     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
603     if (die_task != NULL)
604       GNUNET_SCHEDULER_cancel (die_task);
605     //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
606     return;
607   }
608 }
609
610
611 int
612 main (int argc, char *argv[])
613 {
614   char *test_source;
615   int ret;
616
617   static char *const argv_new[] = {
618     "test-transport-api-reliability",
619     "-c",
620     "test_transport_api_data.conf",
621     NULL
622   };
623   static struct GNUNET_GETOPT_CommandLineOption options[] = {
624     GNUNET_GETOPT_OPTION_END
625   };
626
627   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
628                                           &test_name);
629   GNUNET_log_setup (test_name,
630                     "WARNING",
631                     NULL);
632   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
633                                                  &test_source);
634   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
635                                                  test_source,
636                                                  &test_plugin);
637
638   tth = GNUNET_TRANSPORT_TESTING_init ();
639
640   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
641   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
642
643 #if WRITECONFIG
644   setTransportOptions ("test_transport_api_data.conf");
645 #endif
646   ok = GNUNET_SYSERR;
647
648   ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
649                             argv_new, test_name,
650                             "nohelp", options,
651                             &run, &ok);
652   if (GNUNET_SYSERR == ret)
653   {
654     fprintf (stderr,
655              "Test failed: %d\n",
656              ok);
657     ok = -1;
658   }
659   GNUNET_free (cfg_file_p1);
660   GNUNET_free (cfg_file_p2);
661   GNUNET_free (test_source);
662   GNUNET_free (test_plugin);
663   GNUNET_free (test_name);
664
665   return ok;
666 }
667
668 /* end of test_transport_api_reliability.c */