- fix coverity
[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 %u of %u(!?!?)\n",
322                 bitIdx,
323                 (unsigned int) sizeof (bitmap) * 8);
324     return GNUNET_SYSERR;
325   }
326   arraySlot = bitIdx / 8;
327   targetBit = (1L << (bitIdx % 8));
328   bitmap[arraySlot] |= targetBit;
329   return GNUNET_OK;
330 }
331
332
333 /**
334  * Obtain a bit from bitmap.
335  * @param map the bitmap
336  * @param bit index from bitmap
337  *
338  * @return Bit \a bit from hashcode \a code
339  */
340 static int
341 get_bit (const char *map, unsigned int bit)
342 {
343   if (bit > TOTAL_MSGS)
344   {
345     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346                 "get bit %u of %u(!?!?)\n",
347                 bit,
348                 (unsigned int) sizeof (bitmap) * 8);
349     return 0;
350   }
351   return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
352 }
353
354
355 static void
356 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
357                 const struct GNUNET_MessageHeader *message)
358 {
359   static int n;
360
361   unsigned int s;
362   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
363   const struct TestMessage *hdr;
364
365   hdr = (const struct TestMessage *) message;
366
367   if (MTYPE != ntohs (message->type))
368     return;
369   msg_recv = ntohl (hdr->num);
370   s = get_size (ntohl (hdr->num));
371
372   if (ntohs (message->size) != s)
373   {
374     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375                 "Expected message %u of size %u, got %u bytes of message %u\n",
376                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
377     if (NULL != die_task)
378       GNUNET_SCHEDULER_cancel (die_task);
379     test_sending = GNUNET_YES;
380     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
381     return;
382   }
383
384   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
385   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
386   {
387     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
388                 "Expected message %u with bits %u, but body did not match\n",
389                 ntohl (hdr->num), (unsigned char) n);
390     if (NULL != die_task)
391       GNUNET_SCHEDULER_cancel (die_task);
392     test_sending = GNUNET_YES;
393     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
394     return;
395   }
396 #if VERBOSE
397   if (ntohl (hdr->num) % 5 == 0)
398   {
399     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400                 "Got message %u of size %u\n",
401                 ntohl (hdr->num),
402                 ntohs (message->size));
403   }
404 #endif
405   n++;
406   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
407   {
408       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409                   "Message id %u is bigger than maxmimum number of messages %u expected\n",
410                   ntohl (hdr->num),
411                   TOTAL_MSGS);
412   }
413   test_sending = GNUNET_YES;
414   if (0 == (n % (TOTAL_MSGS / 100)))
415   {
416     FPRINTF (stderr, "%s",  ".");
417     if (NULL != die_task)
418       GNUNET_SCHEDULER_cancel (die_task);
419     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
420   }
421   if (n == TOTAL_MSGS)
422   {
423     end ();
424   }
425 }
426
427
428 static size_t
429 notify_ready (void *cls, size_t size, void *buf)
430 {
431   static int n;
432   char *cbuf = buf;
433   struct TestMessage hdr;
434   unsigned int s;
435   unsigned int ret;
436
437   th = NULL;
438
439   if (buf == NULL)
440   {
441     test_send_timeout = GNUNET_YES;
442     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443                 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
444                 msg_scheduled, TOTAL_MSGS);
445     if (NULL != die_task)
446       GNUNET_SCHEDULER_cancel (die_task);
447     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
448     ok = 42;
449     return 0;
450   }
451   ret = 0;
452   s = get_size (n);
453   GNUNET_assert (size >= s);
454   GNUNET_assert (buf != NULL);
455   GNUNET_assert (n < TOTAL_MSGS);
456   cbuf = buf;
457   do
458   {
459     GNUNET_assert (n < TOTAL_MSGS);
460     hdr.header.size = htons (s);
461     hdr.header.type = htons (MTYPE);
462     hdr.num = htonl (n);
463     msg_sent = n;
464     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
465     ret += sizeof (struct TestMessage);
466     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
467     ret += s - sizeof (struct TestMessage);
468
469 #if VERBOSE
470     if (n % 5000 == 0)
471     {
472       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473                   "Sending message %u of size %u\n",
474                   n,
475                   s);
476     }
477 #endif
478     n++;
479     s = get_size (n);
480     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
481       break;                    /* sometimes pack buffer full, sometimes not */
482   }
483   while ((size - ret >= s) && (n < TOTAL_MSGS));
484   if (n < TOTAL_MSGS)
485   {
486     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
487                                                  TIMEOUT_TRANSMIT,
488                                                  &notify_ready, NULL);
489     msg_scheduled = n;
490   }
491   else
492   {
493     FPRINTF (stderr, "%s",  "\n");
494     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495                 "All messages scheduled to be sent\n");
496     if (NULL != die_task)
497       GNUNET_SCHEDULER_cancel (die_task);
498     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
499   }
500   if (n % 5000 == 0)
501   {
502     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503                 "Returning total message block of size %u\n", ret);
504   }
505   total_bytes += ret;
506   return ret;
507 }
508
509
510 static void
511 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
512 {
513   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514               "Peer `%4s' connected to us (%p)!\n",
515               GNUNET_i2s (peer), cls);
516 }
517
518
519 static void
520 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
521 {
522   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523               "Peer `%4s' disconnected (%p)!\n",
524               GNUNET_i2s (peer), cls);
525   if (th != NULL)
526     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
527   th = NULL;
528 }
529
530
531 static void
532 sendtask (void *cls)
533 {
534   start_time = GNUNET_TIME_absolute_get ();
535   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536               "Starting to send %u messages\n",
537               TOTAL_MSGS);
538   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
539                                                TIMEOUT_TRANSMIT, &notify_ready,
540                                                NULL);
541 }
542
543
544 static void
545 testing_connect_cb (struct PeerContext *p1,
546                     struct PeerContext *p2,
547                     void *cls)
548 {
549   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
550
551   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552               "Peers connected: %s <-> %s\n",
553               p1_c,
554               GNUNET_i2s (&p2->id));
555   GNUNET_free (p1_c);
556
557   test_connected = GNUNET_YES;
558   cc = NULL;
559
560   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
561 }
562
563
564 static void
565 start_cb (struct PeerContext *p, void *cls)
566 {
567   static int started;
568   started++;
569
570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571               "Peer %u (`%s') started\n",
572               p->no,
573               GNUNET_i2s (&p->id));
574
575   if (started != 2)
576     return;
577
578   test_connected = GNUNET_NO;
579   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
580                                                NULL);
581
582 }
583
584
585 static void
586 run (void *cls,
587      char *const *args,
588      const char *cfgfile,
589      const struct GNUNET_CONFIGURATION_Handle *cfg)
590 {
591   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
592                                            &end_badly, NULL);
593   test_send_timeout = GNUNET_NO;
594
595   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
596                                             &notify_receive, &notify_connect,
597                                             &notify_disconnect, &start_cb,
598                                             NULL);
599   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
600                                             &notify_receive, &notify_connect,
601                                             &notify_disconnect, &start_cb,
602                                             NULL);
603   if ((p1 == NULL) || (p2 == NULL))
604   {
605     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
606     if (die_task != NULL)
607       GNUNET_SCHEDULER_cancel (die_task);
608     //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
609     return;
610   }
611 }
612
613
614 int
615 main (int argc, char *argv[])
616 {
617   char *test_source;
618   int ret;
619
620   static char *const argv_new[] = {
621     "test-transport-api-reliability",
622     "-c",
623     "test_transport_api_data.conf",
624     NULL
625   };
626   static struct GNUNET_GETOPT_CommandLineOption options[] = {
627     GNUNET_GETOPT_OPTION_END
628   };
629
630   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
631                                           &test_name);
632   GNUNET_log_setup (test_name,
633                     "WARNING",
634                     NULL);
635   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
636                                                  &test_source);
637   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
638                                                  test_source,
639                                                  &test_plugin);
640
641   tth = GNUNET_TRANSPORT_TESTING_init ();
642
643   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
644   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
645
646 #if WRITECONFIG
647   setTransportOptions ("test_transport_api_data.conf");
648 #endif
649   ok = GNUNET_SYSERR;
650
651   ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
652                             argv_new, test_name,
653                             "nohelp", options,
654                             &run, &ok);
655   if (GNUNET_SYSERR == ret)
656   {
657     fprintf (stderr,
658              "Test failed: %d\n",
659              ok);
660     ok = -1;
661   }
662   GNUNET_free (cfg_file_p1);
663   GNUNET_free (cfg_file_p2);
664   GNUNET_free (test_source);
665   GNUNET_free (test_plugin);
666   GNUNET_free (test_name);
667
668   return ok;
669 }
670
671 /* end of test_transport_api_reliability.c */