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