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