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