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