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