f2f1038d6dd51fafd65bab2f2df376a44fb8917f
[oweals/gnunet.git] / src / transport / test_transport_api_unreliability_constant.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_constant.c
22  * @brief test case for transports; ensures messages get
23  *        through, regardless of order, constant packet size
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  * 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 /*
82  * Testcase specific declarations
83  */
84
85 /**
86  * Note that this value must not significantly exceed
87  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
88  * messages may be dropped even for a reliable transport.
89  */
90 #define TOTAL_MSGS (1024 * 3)
91
92 #define MTYPE 12345
93
94 #define MSG_SIZE 10000
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_failed;
110
111 static unsigned long long total_bytes;
112
113 static struct GNUNET_TIME_Absolute start_time;
114
115 /*
116  * END Testcase specific declarations
117  */
118
119 #if VERBOSE
120 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
121 #else
122 #define OKPP do { ok++; } while (0)
123 #endif
124
125 int
126 get_bit (const char *map, unsigned int bit);
127
128 static void
129 end ()
130 {
131   unsigned long long delta;
132
133   char *value_name;
134
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
136
137   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
138   fprintf (stderr, "\nThroughput was %llu kb/s\n",
139            total_bytes * 1000 / 1024 / delta);
140   GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
141   GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
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   GNUNET_TRANSPORT_TESTING_stop_peer (p1);
153   GNUNET_TRANSPORT_TESTING_stop_peer (p2);
154
155   ok = 0;
156   if (test_failed == GNUNET_NO)
157     ok = GNUNET_SYSERR;
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GOT %u of %u messages\n", msg_recv, TOTAL_MSGS);
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 /*
191   unsigned int ret;
192   ret = (iter * iter * iter);
193   return sizeof (struct TestMessage) + (ret % 60000);
194   */
195   return MSG_SIZE;
196 }
197
198
199
200
201 static void
202 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
203                 const struct GNUNET_MessageHeader *message,
204                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
205                 uint32_t ats_count)
206 {
207   static int n;
208
209   unsigned int s;
210   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
211   const struct TestMessage *hdr;
212
213   hdr = (const struct TestMessage *) message;
214
215   if (MTYPE != ntohs (message->type))
216     return;
217   msg_recv_expected = n;
218   msg_recv = ntohl (hdr->num);
219   if (msg_recv_expected != msg_recv)
220   {
221     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222                 "Expected message no %u, got %u\n",
223                 msg_recv_expected, msg_recv);
224     if (GNUNET_SCHEDULER_NO_TASK != die_task)
225       GNUNET_SCHEDULER_cancel (die_task);
226     test_failed = GNUNET_YES;
227     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
228     return;
229   }
230
231   s = get_size (ntohl (hdr->num));
232   if (ntohs (message->size) != s)
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                 "Expected message %u of size %u, got %u bytes of message %u\n",
236                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
237     if (GNUNET_SCHEDULER_NO_TASK != die_task)
238       GNUNET_SCHEDULER_cancel (die_task);
239     test_failed = GNUNET_YES;
240     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
241     return;
242   }
243
244   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
245   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
246   {
247     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248                 "Expected message %u with bits %u, but body did not match\n",
249                 ntohl (hdr->num), (unsigned char) n);
250     if (GNUNET_SCHEDULER_NO_TASK != die_task)
251       GNUNET_SCHEDULER_cancel (die_task);
252     test_failed = GNUNET_YES;
253     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
254     return;
255   }
256 #if VERBOSE
257   if (ntohl (hdr->num) % 5 == 0)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
260                 ntohl (hdr->num), ntohs (message->size));
261   }
262 #endif
263   n++;
264   if (0 == (n % (TOTAL_MSGS / 100)))
265   {
266     fprintf (stderr, ".");
267     if (GNUNET_SCHEDULER_NO_TASK != die_task)
268       GNUNET_SCHEDULER_cancel (die_task);
269     test_failed = GNUNET_YES;
270     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
271   }
272   if (n == TOTAL_MSGS)
273   {
274     /* because of starting with 0 */
275     msg_recv++;
276     end ();
277   }
278 }
279
280
281 static size_t
282 notify_ready (void *cls, size_t size, void *buf)
283 {
284   static int n;
285   char *cbuf = buf;
286   struct TestMessage hdr;
287   unsigned int s;
288   unsigned int ret;
289
290   th = NULL;
291
292   if (buf == NULL)
293   {
294     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295                 "Timeout occurred while waiting for transmit_ready\n");
296     if (GNUNET_SCHEDULER_NO_TASK != die_task)
297       GNUNET_SCHEDULER_cancel (die_task);
298     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
299     ok = 42;
300     return 0;
301   }
302   ret = 0;
303   s = get_size (n);
304   GNUNET_assert (size >= s);
305   GNUNET_assert (buf != NULL);
306   cbuf = buf;
307   do
308   {
309     hdr.header.size = htons (s);
310     hdr.header.type = htons (MTYPE);
311     hdr.num = htonl (n);
312     msg_sent = n;
313     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
314     ret += sizeof (struct TestMessage);
315     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
316     ret += s - sizeof (struct TestMessage);
317 #if VERBOSE
318     if (n % 1 == 0)
319     {
320       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
321                   s);
322     }
323
324 #endif
325     n++;
326     s = get_size (n);
327     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
328       break;                    /* sometimes pack buffer full, sometimes not */
329   }
330   while (size - ret >= s);
331   if (n < TOTAL_MSGS)
332   {
333     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
334                                                  TIMEOUT_TRANSMIT,
335                                                  &notify_ready, NULL);
336     msg_scheduled = n;
337   }
338   else
339   {
340     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                 "All messages scheduled to be sent!!\n");
342     if (GNUNET_SCHEDULER_NO_TASK != die_task)
343       GNUNET_SCHEDULER_cancel (die_task);
344     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
345   }
346   if (n % 5000 == 0)
347   {
348     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349                 "Returning total message block of size %u\n", ret);
350   }
351   total_bytes += ret;
352   return ret;
353 }
354
355
356 static void
357 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
358                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
359                 uint32_t ats_count)
360 {
361
362   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
363               GNUNET_i2s (peer), cls);
364
365   if (cls == p1)
366   {
367     char * sec;
368     long long unsigned int l_bps;
369
370     GNUNET_asprintf(&sec, "transport-%s", test_plugin);
371     if (GNUNET_CONFIGURATION_have_value (p1->cfg, sec, "MAX_BPS"))
372     {
373       GNUNET_CONFIGURATION_get_value_number (p1->cfg, sec, "MAX_BPS",
374                                              &l_bps);
375       max_bps_p1 = l_bps;
376     }
377     else
378       max_bps_p1 = 1024 * 1024 * 1024;
379     GNUNET_free (sec);
380
381     GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
382                                 GNUNET_BANDWIDTH_value_init (max_bps_p1),
383                                 GNUNET_BANDWIDTH_value_init (max_bps_p1));
384   }
385   else if (cls == p2)
386   {
387     char * sec;
388     long long unsigned int l_bps;
389
390     GNUNET_asprintf(&sec, "transport-%s", test_plugin);
391     if (GNUNET_CONFIGURATION_have_value (p2->cfg, sec, "MAX_BPS"))
392     {
393       GNUNET_CONFIGURATION_get_value_number (p2->cfg, sec, "MAX_BPS",
394                                              &l_bps);
395       max_bps_p2 = l_bps;
396     }
397     else
398       max_bps_p2 = 1024 * 1024 * 1024;
399     GNUNET_free (sec);
400
401     GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
402                                 GNUNET_BANDWIDTH_value_init (max_bps_p2),
403                                 GNUNET_BANDWIDTH_value_init (max_bps_p2));
404   }
405 }
406
407
408 static void
409 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
410 {
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
412               GNUNET_i2s (peer), cls);
413 }
414
415 static void
416 sendtask ()
417 {
418   start_time = GNUNET_TIME_absolute_get ();
419   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
420                                                TIMEOUT_TRANSMIT, &notify_ready,
421                                                NULL);
422 }
423
424 static void
425 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
426 {
427   char *p1_c = strdup (GNUNET_i2s (&p1->id));
428
429   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
430               GNUNET_i2s (&p2->id));
431   GNUNET_free (p1_c);
432
433   // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
434   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
435 }
436
437 static void
438 run (void *cls, char *const *args, const char *cfgfile,
439      const struct GNUNET_CONFIGURATION_Handle *cfg)
440 {
441   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
442
443   p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, &notify_receive,
444                                             &notify_connect, &notify_disconnect,
445                                             NULL);
446   p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, &notify_receive,
447                                             &notify_connect, &notify_disconnect,
448                                             NULL);
449
450   if ((p1 == NULL) || (p2 == NULL))
451   {
452     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
453     if (die_task != GNUNET_SCHEDULER_NO_TASK)
454       GNUNET_SCHEDULER_cancel (die_task);
455     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
456     return;
457   }
458
459   GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
460 }
461
462 static int
463 check ()
464 {
465   static char *const argv[] = { "test-transport-api-unreliability-constant",
466     "-c",
467     "test_transport_api_data.conf",
468 #if VERBOSE
469     "-L", "DEBUG",
470 #endif
471     NULL
472   };
473   static struct GNUNET_GETOPT_CommandLineOption options[] = {
474     GNUNET_GETOPT_OPTION_END
475   };
476
477 #if WRITECONFIG
478   setTransportOptions ("test_transport_api_data.conf");
479 #endif
480   ok = GNUNET_SYSERR;
481
482   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
483                       "nohelp", options, &run, &ok);
484
485   return ok;
486 }
487
488 int
489 main (int argc, char *argv[])
490 {
491   int ret;
492   int nat_res;
493
494   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
495   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
496                                                  &test_plugin);
497   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
498
499   GNUNET_log_setup (test_name,
500 #if VERBOSE
501                     "DEBUG",
502 #else
503                     "WARNING",
504 #endif
505                     NULL);
506
507   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
508       (strcmp (test_plugin, "udp_nat") == 0))
509   {
510     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
511     if (GNUNET_NO == nat_res)
512     {
513       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
514                   "gnunet-nat-server", "SUID not set");
515       return 0;
516     }
517     if (GNUNET_SYSERR == nat_res)
518     {
519       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
520                   "gnunet-nat-server", "file not found");
521       return 0;
522     }
523   }
524
525   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
526   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
527
528   ret = check ();
529
530   GNUNET_free (cfg_file_p1);
531   GNUNET_free (cfg_file_p2);
532
533   GNUNET_free (test_source);
534   GNUNET_free (test_plugin);
535   GNUNET_free (test_name);
536
537   return ret;
538 }
539
540 /* end of test_transport_api_unreliability_constant.c */