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