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