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