18971a986218950e7dc1984edec24c767c73134f
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.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_reliability.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves as a base for tcp and http
25  * transport test cases to check that the transports
26  * achieve reliable message delivery.
27  */
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "gauger.h"
38 #include "transport.h"
39 #include "transport-testing.h"
40
41 #define VERBOSE GNUNET_NO
42
43 #define VERBOSE_ARM GNUNET_NO
44
45 #define START_ARM GNUNET_YES
46
47 /**
48  * Testcase timeout
49  */
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51
52 /**
53  * How long until we give up on transmitting the message?
54  */
55 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
56
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 /*
79  * Testcase specific declarations
80  */
81
82 /**
83  * Note that this value must not significantly exceed
84  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
85  * messages may be dropped even for a reliable transport.
86  */
87 #define TOTAL_MSGS (1024 * 2)
88
89 #define MTYPE 12345
90
91 struct TestMessage
92 {
93   struct GNUNET_MessageHeader header;
94   uint32_t num;
95 };
96
97 static int msg_scheduled;
98 static int msg_sent;
99 static int msg_recv_expected;
100 static int msg_recv;
101
102 static int test_failed;
103
104 static unsigned long long total_bytes;
105
106 static struct GNUNET_TIME_Absolute start_time;
107
108 /*
109  * END Testcase specific declarations
110  */
111
112 #if VERBOSE
113 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
114 #else
115 #define OKPP do { ok++; } while (0)
116 #endif
117
118
119 static void
120 end ()
121 {
122   unsigned long long delta;
123
124   char *value_name;
125
126   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
127
128   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
129   fprintf (stderr, "\nThroughput was %llu kb/s\n",
130            total_bytes * 1000 / 1024 / delta);
131   GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
132   GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
133           "kb/s");
134   GNUNET_free (value_name);
135
136   if (die_task != GNUNET_SCHEDULER_NO_TASK)
137     GNUNET_SCHEDULER_cancel (die_task);
138
139   if (th != NULL)
140     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
141   th = NULL;
142
143   GNUNET_TRANSPORT_TESTING_stop_peer (p1);
144   GNUNET_TRANSPORT_TESTING_stop_peer (p2);
145 }
146
147 static void
148 end_badly ()
149 {
150   die_task = GNUNET_SCHEDULER_NO_TASK;
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
152
153   if (th != NULL)
154     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
155   th = NULL;
156
157   if (p1 != NULL)
158     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
159   if (p2 != NULL)
160     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
161
162   ok = GNUNET_SYSERR;
163 }
164
165
166 static unsigned int
167 get_size (unsigned int iter)
168 {
169   unsigned int ret;
170
171   ret = (iter * iter * iter);
172   return sizeof (struct TestMessage) + (ret % 60000);
173 }
174
175
176 static void
177 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
178                 const struct GNUNET_MessageHeader *message,
179                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
180                 uint32_t ats_count)
181 {
182   static int n;
183   unsigned int s;
184   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
185   const struct TestMessage *hdr;
186
187   hdr = (const struct TestMessage *) message;
188   s = get_size (n);
189   if (MTYPE != ntohs (message->type))
190     return;
191   msg_recv_expected = n;
192   msg_recv = ntohl (hdr->num);
193   if (ntohs (message->size) != (s))
194   {
195     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196                 "Expected message %u of size %u, got %u bytes of message %u\n",
197                 n, s, ntohs (message->size), ntohl (hdr->num));
198     if (die_task != GNUNET_SCHEDULER_NO_TASK)
199       GNUNET_SCHEDULER_cancel (die_task);
200     test_failed = GNUNET_YES;
201     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
202     return;
203   }
204   if (ntohl (hdr->num) != n)
205   {
206     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207                 "Expected message %u of size %u, got %u bytes of message %u\n",
208                 n, s, ntohs (message->size), ntohl (hdr->num));
209     if (die_task != GNUNET_SCHEDULER_NO_TASK)
210       GNUNET_SCHEDULER_cancel (die_task);
211     test_failed = GNUNET_YES;
212     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
213     return;
214   }
215   memset (cbuf, n, s - sizeof (struct TestMessage));
216   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
217   {
218     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219                 "Expected message %u with bits %u, but body did not match\n", n,
220                 (unsigned char) n);
221     if (die_task != GNUNET_SCHEDULER_NO_TASK)
222       GNUNET_SCHEDULER_cancel (die_task);
223     test_failed = GNUNET_YES;
224     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
225     return;
226   }
227 #if VERBOSE
228   if (ntohl (hdr->num) % 5000 == 0)
229   {
230     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
231                 ntohl (hdr->num), ntohs (message->size));
232   }
233 #endif
234   n++;
235   if (0 == (n % (TOTAL_MSGS / 100)))
236   {
237     fprintf (stderr, ".");
238     if (die_task != GNUNET_SCHEDULER_NO_TASK)
239       GNUNET_SCHEDULER_cancel (die_task);
240     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
241   }
242   if (n == TOTAL_MSGS)
243   {
244     ok = 0;
245     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages received\n");
246     end ();
247   }
248 }
249
250
251 static size_t
252 notify_ready (void *cls, size_t size, void *buf)
253 {
254   static int n;
255   char *cbuf = buf;
256   struct TestMessage hdr;
257   unsigned int s;
258   unsigned int ret;
259
260   th = NULL;
261   if (buf == NULL)
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264                 "Timeout occurred while waiting for transmit_ready\n");
265     if (GNUNET_SCHEDULER_NO_TASK != die_task)
266       GNUNET_SCHEDULER_cancel (die_task);
267     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
268     ok = 42;
269     return 0;
270   }
271
272   ret = 0;
273   s = get_size (n);
274   GNUNET_assert (size >= s);
275   GNUNET_assert (buf != NULL);
276   cbuf = buf;
277   do
278   {
279     hdr.header.size = htons (s);
280     hdr.header.type = htons (MTYPE);
281     hdr.num = htonl (n);
282     msg_sent = n;
283     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
284     ret += sizeof (struct TestMessage);
285     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
286     ret += s - sizeof (struct TestMessage);
287 #if VERBOSE
288     if (n % 5000 == 0)
289     {
290       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
291                   s);
292     }
293 #endif
294     n++;
295     s = get_size (n);
296     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
297       break;                    /* sometimes pack buffer full, sometimes not */
298   }
299   while (size - ret >= s);
300   if (n < TOTAL_MSGS)
301   {
302     if (th == NULL)
303       th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
304                                                    TIMEOUT_TRANSMIT,
305                                                    &notify_ready, NULL);
306     msg_scheduled = n;
307   }
308   if (n % 5000 == 0)
309   {
310     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311                 "Returning total message block of size %u\n", ret);
312   }
313   total_bytes += ret;
314   if (n == TOTAL_MSGS)
315   {
316     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
317   }
318   return ret;
319 }
320
321
322 static void
323 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
324                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
325                 uint32_t ats_count)
326 {
327
328   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
329               GNUNET_i2s (peer), cls);
330
331   if (cls == p1)
332   {
333     GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
334                                 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
335                                                              1024),
336                                 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
337                                                              1024));
338   }
339   else if (cls == p2)
340   {
341     GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
342                                 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
343                                                              1024),
344                                 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
345                                                              1024));
346   }
347 }
348
349
350 static void
351 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
352 {
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
354               GNUNET_i2s (peer), cls);
355 }
356
357 static void
358 sendtask ()
359 {
360   start_time = GNUNET_TIME_absolute_get ();
361   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
362                                                TIMEOUT_TRANSMIT, &notify_ready,
363                                                NULL);
364 }
365
366 static void
367 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
368 {
369   char *p1_c = strdup (GNUNET_i2s (&p1->id));
370
371   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
372               GNUNET_i2s (&p2->id));
373   GNUNET_free (p1_c);
374
375   // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
376   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
377 }
378
379 static void
380 run (void *cls, char *const *args, const char *cfgfile,
381      const struct GNUNET_CONFIGURATION_Handle *cfg)
382 {
383   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
384
385   p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, &notify_receive,
386                                             &notify_connect, &notify_disconnect,
387                                             NULL);
388   p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, &notify_receive,
389                                             &notify_connect, &notify_disconnect,
390                                             NULL);
391
392   if ((p1 == NULL) || (p2 == NULL))
393   {
394     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
395     if (die_task != GNUNET_SCHEDULER_NO_TASK)
396       GNUNET_SCHEDULER_cancel (die_task);
397     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
398     return;
399   }
400
401   GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
402 }
403
404 static int
405 check ()
406 {
407   static char *argv[] = { "test_transport",
408     "-c",
409     "test_transport_api_data.conf",
410 #if VERBOSE
411     "-L", "DEBUG",
412 #endif
413     NULL
414   };
415   static struct GNUNET_GETOPT_CommandLineOption options[] = {
416     GNUNET_GETOPT_OPTION_END
417   };
418
419 #if WRITECONFIG
420   setTransportOptions ("test_transport_api_data.conf");
421 #endif
422   ok = 1;
423   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
424                       "nohelp", options, &run, &ok);
425
426   return ok;
427 }
428
429 int
430 main (int argc, char *argv[])
431 {
432   int ret;
433   int nat_res;
434
435   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
436   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
437                                                  &test_plugin);
438   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
439
440   GNUNET_log_setup (test_name,
441 #if VERBOSE
442                     "DEBUG",
443 #else
444                     "WARNING",
445 #endif
446                     NULL);
447
448   if ((strcmp (test_plugin, "tcp_nat") == 0) ||
449       (strcmp (test_plugin, "udp_nat") == 0))
450   {
451     nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
452     if (GNUNET_NO == nat_res)
453     {
454       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
455                   "gnunet-nat-server", "SUID not set");
456       return 0;
457     }
458     if (GNUNET_SYSERR == nat_res)
459     {
460       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
461                   "gnunet-nat-server", "file not found");
462       return 0;
463     }
464   }
465
466   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
467   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
468
469   ret = check ();
470
471   GNUNET_free (cfg_file_p1);
472   GNUNET_free (cfg_file_p2);
473
474   GNUNET_free (test_source);
475   GNUNET_free (test_plugin);
476   GNUNET_free (test_name);
477
478   return ret;
479 }
480
481
482 /* end of test_transport_api_reliability.c */