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