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