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