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