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