some diagnoses
[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     end ();
316 }
317
318
319 static size_t
320 notify_ready (void *cls, size_t size, void *buf)
321 {
322   static int n;
323   char *cbuf = buf;
324   struct TestMessage hdr;
325   unsigned int s;
326   unsigned int ret;
327
328   th = NULL;
329
330   if (buf == NULL)
331   {
332     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333                 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
334                 msg_scheduled, TOTAL_MSGS);
335     if (GNUNET_SCHEDULER_NO_TASK != die_task)
336       GNUNET_SCHEDULER_cancel (die_task);
337     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
338     ok = 42;
339     return 0;
340   }
341   ret = 0;
342   s = get_size (n);
343   GNUNET_assert (size >= s);
344   GNUNET_assert (buf != NULL);
345   cbuf = buf;
346   do
347   {
348     hdr.header.size = htons (s);
349     hdr.header.type = htons (MTYPE);
350     hdr.num = htonl (n);
351     msg_sent = n;
352     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
353     ret += sizeof (struct TestMessage);
354     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
355     ret += s - sizeof (struct TestMessage);
356 #if VERBOSE
357     if (n % 5000 == 0)
358     {
359       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
360                   s);
361     }
362
363 #endif
364     n++;
365     s = get_size (n);
366     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
367       break;                    /* sometimes pack buffer full, sometimes not */
368   }
369   while (size - ret >= s);
370   if (n < TOTAL_MSGS)
371   {
372     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
373                                                  TIMEOUT_TRANSMIT,
374                                                  &notify_ready, NULL);
375     msg_scheduled = n;
376   }
377   else
378   {
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380                 "All messages scheduled to be sent!!\n");
381     if (GNUNET_SCHEDULER_NO_TASK != die_task)
382       GNUNET_SCHEDULER_cancel (die_task);
383     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
384   }
385   if (n % 5000 == 0)
386   {
387     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388                 "Returning total message block of size %u\n", ret);
389   }
390   total_bytes += ret;
391   return ret;
392 }
393
394
395 static void
396 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
397                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
398                 uint32_t ats_count)
399 {
400
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
402               GNUNET_i2s (peer), cls);
403
404   if (cls == p1)
405   {
406     max_bps_p1 = 1024 * 1024 * 1024;
407     GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
408                                 GNUNET_BANDWIDTH_value_init (max_bps_p1),
409                                 GNUNET_BANDWIDTH_value_init (max_bps_p1));
410   }
411   else if (cls == p2)
412   {
413     max_bps_p2 = 1024 * 1024 * 1024;
414     GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
415                                 GNUNET_BANDWIDTH_value_init (max_bps_p2),
416                                 GNUNET_BANDWIDTH_value_init (max_bps_p2));
417   }
418 }
419
420
421 static void
422 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
423 {
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
425               GNUNET_i2s (peer), cls);
426   if (th != NULL)
427     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
428   th = NULL;
429 }
430
431 static void
432 sendtask ()
433 {
434   start_time = GNUNET_TIME_absolute_get ();
435   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
436                                                TIMEOUT_TRANSMIT, &notify_ready,
437                                                NULL);
438 }
439
440 static void
441 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
442 {
443   char *p1_c = strdup (GNUNET_i2s (&p1->id));
444
445   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
446               GNUNET_i2s (&p2->id));
447   GNUNET_free (p1_c);
448
449   test_connected = GNUNET_YES;
450
451   // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
452   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
453 }
454
455 static void
456 run (void *cls, char *const *args, const char *cfgfile,
457      const struct GNUNET_CONFIGURATION_Handle *cfg)
458 {
459   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
460
461   p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, &notify_receive,
462                                             &notify_connect, &notify_disconnect,
463                                             NULL);
464   p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, &notify_receive,
465                                             &notify_connect, &notify_disconnect,
466                                             NULL);
467
468   if ((p1 == NULL) || (p2 == NULL))
469   {
470     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
471     if (die_task != GNUNET_SCHEDULER_NO_TASK)
472       GNUNET_SCHEDULER_cancel (die_task);
473     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
474     return;
475   }
476
477   test_connected = GNUNET_NO;
478   GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
479 }
480
481 static int
482 check ()
483 {
484   static char *const argv[] = { "test-transport-api-unreliability",
485     "-c",
486     "test_transport_api_data.conf",
487 #if VERBOSE
488     "-L", "DEBUG",
489 #endif
490     NULL
491   };
492   static struct GNUNET_GETOPT_CommandLineOption options[] = {
493     GNUNET_GETOPT_OPTION_END
494   };
495
496 #if WRITECONFIG
497   setTransportOptions ("test_transport_api_data.conf");
498 #endif
499   ok = GNUNET_SYSERR;
500
501   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
502                       "nohelp", options, &run, &ok);
503
504   return ok;
505 }
506
507 int
508 main (int argc, char *argv[])
509 {
510   int ret;
511   int nat_res;
512
513   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
514   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
515                                                  &test_plugin);
516   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
517
518   GNUNET_log_setup (test_name,
519 #if VERBOSE
520                     "DEBUG",
521 #else
522                     "WARNING",
523 #endif
524                     NULL);
525
526   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
527       (strcmp (test_plugin, "udp_nat") == 0))
528   {
529     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
530     if (GNUNET_NO == nat_res)
531     {
532       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
533                   "gnunet-nat-server", "SUID not set");
534       return 0;
535     }
536     if (GNUNET_SYSERR == nat_res)
537     {
538       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
539                   "gnunet-nat-server", "file not found");
540       return 0;
541     }
542   }
543
544   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
545   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
546
547   ret = check ();
548
549   GNUNET_free (cfg_file_p1);
550   GNUNET_free (cfg_file_p2);
551
552   GNUNET_free (test_source);
553   GNUNET_free (test_plugin);
554   GNUNET_free (test_name);
555
556   return ret;
557 }
558
559 /* end of test_transport_api_unreliability.c */