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