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