-fix NPE
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 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 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, "Got message %u of size %u\n",
397                 ntohl (hdr->num), ntohs (message->size));
398   }
399 #endif
400   n++;
401   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
402   {
403       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404                   "Message id %u is bigger than maxmimum number of messages %u expected\n",
405                   ntohl (hdr->num),
406                   TOTAL_MSGS);
407   }
408   test_sending = GNUNET_YES;
409   if (0 == (n % (TOTAL_MSGS / 100)))
410   {
411     FPRINTF (stderr, "%s",  ".");
412     if (NULL != die_task)
413       GNUNET_SCHEDULER_cancel (die_task);
414     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
415   }
416   if (n == TOTAL_MSGS)
417   {
418     end ();
419   }
420 }
421
422
423 static size_t
424 notify_ready (void *cls, size_t size, void *buf)
425 {
426   static int n;
427   char *cbuf = buf;
428   struct TestMessage hdr;
429   unsigned int s;
430   unsigned int ret;
431
432   th = NULL;
433
434   if (buf == NULL)
435   {
436     test_send_timeout = GNUNET_YES;
437     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438                 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
439                 msg_scheduled, TOTAL_MSGS);
440     if (NULL != die_task)
441       GNUNET_SCHEDULER_cancel (die_task);
442     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
443     ok = 42;
444     return 0;
445   }
446   ret = 0;
447   s = get_size (n);
448   GNUNET_assert (size >= s);
449   GNUNET_assert (buf != NULL);
450   GNUNET_assert (n < TOTAL_MSGS);
451   cbuf = buf;
452   do
453   {
454     GNUNET_assert (n < TOTAL_MSGS);
455     hdr.header.size = htons (s);
456     hdr.header.type = htons (MTYPE);
457     hdr.num = htonl (n);
458     msg_sent = n;
459     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
460     ret += sizeof (struct TestMessage);
461     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
462     ret += s - sizeof (struct TestMessage);
463
464 #if VERBOSE
465     if (n % 5000 == 0)
466     {
467       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468                   "Sending message %u of size %u\n",
469                   n,
470                   s);
471     }
472 #endif
473     n++;
474     s = get_size (n);
475     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
476       break;                    /* sometimes pack buffer full, sometimes not */
477   }
478   while ((size - ret >= s) && (n < TOTAL_MSGS));
479   if (n < TOTAL_MSGS)
480   {
481     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
482                                                  TIMEOUT_TRANSMIT,
483                                                  &notify_ready, NULL);
484     msg_scheduled = n;
485   }
486   else
487   {
488     FPRINTF (stderr, "%s",  "\n");
489     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490                 "All messages scheduled to be sent\n");
491     if (NULL != die_task)
492       GNUNET_SCHEDULER_cancel (die_task);
493     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
494   }
495   if (n % 5000 == 0)
496   {
497     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498                 "Returning total message block of size %u\n", ret);
499   }
500   total_bytes += ret;
501   return ret;
502 }
503
504
505 static void
506 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
507 {
508   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
509               GNUNET_i2s (peer), cls);
510 }
511
512
513 static void
514 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
515 {
516   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
517               GNUNET_i2s (peer), cls);
518   if (th != NULL)
519     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
520   th = NULL;
521 }
522
523
524 static void
525 sendtask ()
526 {
527   start_time = GNUNET_TIME_absolute_get ();
528   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529               "Starting to send %u messages\n",
530               TOTAL_MSGS);
531   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
532                                                TIMEOUT_TRANSMIT, &notify_ready,
533                                                NULL);
534 }
535
536 static void
537 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
538 {
539   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
540
541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542               "Peers connected: %s <-> %s\n", p1_c,
543               GNUNET_i2s (&p2->id));
544   GNUNET_free (p1_c);
545
546   test_connected = GNUNET_YES;
547   cc = NULL;
548
549   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
550 }
551
552
553 static void
554 start_cb (struct PeerContext *p, void *cls)
555 {
556   static int started;
557   started++;
558
559   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560               "Peer %u (`%s') started\n",
561               p->no,
562               GNUNET_i2s (&p->id));
563
564   if (started != 2)
565     return;
566
567   test_connected = GNUNET_NO;
568   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
569                                                NULL);
570
571 }
572
573
574 static void
575 run (void *cls,
576      char *const *args,
577      const char *cfgfile,
578      const struct GNUNET_CONFIGURATION_Handle *cfg)
579 {
580   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
581                                            &end_badly, NULL);
582   test_send_timeout = GNUNET_NO;
583
584   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
585                                             &notify_receive, &notify_connect,
586                                             &notify_disconnect, &start_cb,
587                                             NULL);
588   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
589                                             &notify_receive, &notify_connect,
590                                             &notify_disconnect, &start_cb,
591                                             NULL);
592   if ((p1 == NULL) || (p2 == NULL))
593   {
594     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
595     if (die_task != NULL)
596       GNUNET_SCHEDULER_cancel (die_task);
597     //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
598     return;
599   }
600 }
601
602
603 int
604 main (int argc, char *argv[])
605 {
606   char *test_source;
607   int ret;
608
609   static char *const argv_new[] = {
610     "test-transport-api-reliability",
611     "-c",
612     "test_transport_api_data.conf",
613     NULL
614   };
615   static struct GNUNET_GETOPT_CommandLineOption options[] = {
616     GNUNET_GETOPT_OPTION_END
617   };
618
619   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
620                                           &test_name);
621   GNUNET_log_setup (test_name,
622                     "WARNING",
623                     NULL);
624   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
625                                                  &test_source);
626   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
627                                                  test_source,
628                                                  &test_plugin);
629
630   tth = GNUNET_TRANSPORT_TESTING_init ();
631
632   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
633   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
634
635 #if WRITECONFIG
636   setTransportOptions ("test_transport_api_data.conf");
637 #endif
638   ok = GNUNET_SYSERR;
639
640   ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
641                             argv_new, test_name,
642                             "nohelp", options,
643                             &run, &ok);
644   if (GNUNET_SYSERR == ret)
645   {
646     fprintf (stderr,
647              "Test failed: %d\n",
648              ok);
649     ok = -1;
650   }
651   GNUNET_free (cfg_file_p1);
652   GNUNET_free (cfg_file_p2);
653   GNUNET_free (test_source);
654   GNUNET_free (test_plugin);
655   GNUNET_free (test_name);
656
657   return ok;
658 }
659
660 /* end of test_transport_api_reliability.c */