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