(no commit message)
[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 "transport.h"
38
39 #define VERBOSE GNUNET_NO
40
41 #define VERBOSE_ARM GNUNET_NO
42
43 #define START_ARM GNUNET_YES
44
45 /**
46  * Note that this value must not significantly exceed
47  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48  * messages may be dropped even for a reliable transport.
49  */
50 #define TOTAL_MSGS (60000 * 2)
51
52 /**
53  * How long until we give up on transmitting the message?
54  */
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
56
57 #define MTYPE 12345
58
59 struct PeerContext
60 {
61   struct GNUNET_CONFIGURATION_Handle *cfg;
62   struct GNUNET_TRANSPORT_Handle *th;
63   struct GNUNET_PeerIdentity id;
64 #if START_ARM
65   pid_t arm_pid;
66 #endif
67 };
68
69 static struct PeerContext p1;
70
71 static struct PeerContext p2;
72
73 static struct GNUNET_SCHEDULER_Handle *sched;
74
75 static int ok;
76
77 static int is_tcp;
78
79 static int is_tcp_nat;
80
81 static int is_http;
82
83 static int is_udp;
84
85 static int connected;
86
87 static unsigned long long total_bytes;
88
89 static struct GNUNET_TIME_Absolute start_time;
90
91 static GNUNET_SCHEDULER_TaskIdentifier die_task;
92
93 #if VERBOSE
94 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
95 #else
96 #define OKPP do { ok++; } while (0)
97 #endif
98
99
100 static void
101 end ()
102 {
103   unsigned long long delta;
104
105   GNUNET_SCHEDULER_cancel (sched, die_task);
106   die_task = GNUNET_SCHEDULER_NO_TASK;
107 #if VERBOSE
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
109 #endif
110   GNUNET_TRANSPORT_disconnect (p1.th);
111   GNUNET_TRANSPORT_disconnect (p2.th);
112 #if VERBOSE
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
114               "Transports disconnected, returning success!\n");
115 #endif
116   delta = GNUNET_TIME_absolute_get_duration (start_time).value;
117   fprintf (stderr,
118            "\nThroughput was %llu kb/s\n",
119            total_bytes * 1000 / 1024 / delta);
120   ok = 0;
121 }
122
123
124 static void
125 stop_arm (struct PeerContext *p)
126 {
127 #if START_ARM
128   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
129     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
130   GNUNET_OS_process_wait (p->arm_pid);
131 #endif
132   GNUNET_CONFIGURATION_destroy (p->cfg);
133 }
134
135
136 static void
137 end_badly (void *cls,
138            const struct GNUNET_SCHEDULER_TaskContext *tc)
139 {
140   GNUNET_break (0);
141   GNUNET_TRANSPORT_disconnect (p1.th);
142   GNUNET_TRANSPORT_disconnect (p2.th);
143   ok = 1;
144 }
145
146
147 struct TestMessage 
148 {
149   struct GNUNET_MessageHeader header;
150   uint32_t num;
151 };
152
153
154 static unsigned int
155 get_size (unsigned int iter)
156 {
157   unsigned int ret;
158
159   if (iter < 60000)
160     return iter + sizeof (struct TestMessage);
161   ret = (iter * iter * iter);
162   return sizeof (struct TestMessage) + (ret % 60000);
163 }
164
165
166 static void
167 notify_receive (void *cls,
168                 const struct GNUNET_PeerIdentity *peer,
169                 const struct GNUNET_MessageHeader *message,
170                 struct GNUNET_TIME_Relative latency,
171                 uint32_t distance)
172 {
173   static int n;
174   unsigned int s;
175   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
176   const struct TestMessage *hdr;
177
178   hdr = (const struct TestMessage*) message;
179   s = get_size (n);
180   if (MTYPE != ntohs (message->type))
181     return;
182   if (ntohs (message->size) != s)
183     {
184       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185                   "Expected message %u of size %u, got %u bytes of message %u\n",
186                   n, s,
187                   ntohs (message->size),
188                   ntohl (hdr->num));
189       GNUNET_SCHEDULER_cancel (sched, die_task);
190       die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
191       return;
192     }
193   if (ntohl (hdr->num) != n)
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,
198                   ntohs (message->size),
199                   ntohl (hdr->num));
200       GNUNET_SCHEDULER_cancel (sched, die_task);
201       die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
202       return;
203     }
204   memset (cbuf, n, s - sizeof (struct TestMessage));
205   if (0 != memcmp (cbuf,
206                    &hdr[1],
207                    s - sizeof (struct TestMessage)))
208     {
209       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210                   "Expected message %u with bits %u, but body did not match\n",
211                   n, (unsigned char) n);
212       GNUNET_SCHEDULER_cancel (sched, die_task);
213       die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
214       return;
215     }
216 #if VERBOSE
217   if (ntohl(hdr->num) % 5000 == 0)
218     {
219       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                   "Got message %u of size %u\n",
221                   ntohl (hdr->num),
222                   ntohs (message->size));
223     }
224 #endif
225   n++;
226   if (0 == (n % (TOTAL_MSGS/100)))
227     {
228       fprintf (stderr, ".");
229       GNUNET_SCHEDULER_cancel (sched, die_task);
230       die_task = GNUNET_SCHEDULER_add_delayed (sched,
231                                                TIMEOUT,
232                                                &end_badly,
233                                                NULL);    
234     }
235   if (n == TOTAL_MSGS)
236     end ();
237 }
238
239
240 static size_t
241 notify_ready (void *cls, size_t size, void *buf)
242 {
243   static int n;
244   char *cbuf = buf;
245   struct TestMessage hdr;
246   unsigned int s;
247   unsigned int ret;
248
249   if (buf == NULL)
250     {
251       GNUNET_break (0);
252       ok = 42;
253       return 0;
254     }
255   ret = 0;
256   s = get_size (n);
257   GNUNET_assert (size >= s);
258   GNUNET_assert (buf != NULL);
259   cbuf = buf;
260   do
261     {
262       hdr.header.size = htons (s);
263       hdr.header.type = htons (MTYPE);
264       hdr.num = htonl (n);
265       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
266       ret += sizeof (struct TestMessage);
267       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
268       ret += s - sizeof (struct TestMessage);
269 #if VERBOSE
270       if (n % 5000 == 0)
271         {
272           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273                       "Sending message %u of size %u\n",
274                       n,
275                       s);
276         }
277 #endif
278       n++;
279       s = get_size (n);
280       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
281         break; /* sometimes pack buffer full, sometimes not */
282     }
283   while (size - ret >= s);
284   if (n < TOTAL_MSGS)
285     GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
286                                             &p2.id,
287                                             s, 0, TIMEOUT, 
288                                             &notify_ready,
289                                             NULL);
290   if (n % 5000 == 0)
291     {
292       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293                   "Returning total message block of size %u\n",
294                   ret);
295     }
296   total_bytes += ret;
297   return ret;
298 }
299
300
301 static void
302 notify_connect (void *cls,
303                 const struct GNUNET_PeerIdentity *peer,
304                 struct GNUNET_TIME_Relative latency,
305                 uint32_t distance)
306 {
307   if (cls == &p1)
308     {
309       GNUNET_TRANSPORT_set_quota (p1.th,
310                                   &p2.id,
311                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
312                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
313                                   GNUNET_TIME_UNIT_FOREVER_REL,
314                                   NULL, NULL);
315       start_time = GNUNET_TIME_absolute_get ();
316       connected++;
317     }
318   else
319     {
320       GNUNET_TRANSPORT_set_quota (p2.th,
321                                   &p1.id,
322                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
323                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
324                                   GNUNET_TIME_UNIT_FOREVER_REL,
325                                   NULL, NULL);
326       connected++;
327     }
328
329   if (connected == 2)
330     {
331       GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
332                                               &p2.id,
333                                               get_size (0), 0, TIMEOUT,
334                                               &notify_ready,
335                                               NULL);
336     }
337 #if VERBOSE
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
340 #endif
341 }
342
343
344 static void
345 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
346 {
347 #if VERBOSE
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "Peer `%4s' disconnected (%p)!\n",
350               GNUNET_i2s (peer), cls);
351 #endif
352 }
353
354
355 static void
356 setup_peer (struct PeerContext *p, const char *cfgname)
357 {
358   p->cfg = GNUNET_CONFIGURATION_create ();
359 #if START_ARM
360   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, 
361                                         "gnunet-service-arm",
362                                         "gnunet-service-arm",
363 #if VERBOSE_ARM
364                                         "-L", "DEBUG",
365 #endif
366                                         "-c", cfgname, NULL);
367 #endif
368   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
369   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
370                                     p,
371                                     &notify_receive,
372                                     &notify_connect, 
373                                     &notify_disconnect);
374   GNUNET_assert (p->th != NULL);
375 }
376
377
378 static void
379 exchange_hello_last (void *cls,
380                      const struct GNUNET_MessageHeader *message)
381 {
382   struct PeerContext *me = cls;
383
384   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
385 #if VERBOSE
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387               "Exchanging HELLO with peer (%p)!\n", cls);
388 #endif
389   GNUNET_assert (ok >= 3);
390   OKPP;
391   GNUNET_assert (message != NULL);
392   GNUNET_assert (GNUNET_OK ==
393                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
394                                       message, &me->id));
395   /* both HELLOs exchanged, get ready to test transmission! */
396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397               "Finished exchanging HELLOs, now waiting for transmission!\n");
398 }
399
400
401 static void
402 exchange_hello (void *cls,
403                 const struct GNUNET_MessageHeader *message)
404 {
405   struct PeerContext *me = cls;
406
407   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
408 #if VERBOSE
409   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410               "Exchanging HELLO with peer (%p)!\n", cls);
411 #endif
412   GNUNET_assert (ok >= 2);
413   OKPP;
414   GNUNET_assert (message != NULL);
415   GNUNET_assert (GNUNET_OK ==
416                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
417                                       message, &me->id));
418
419 #if VERBOSE
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421               "Received HELLO size %d\n", 
422               GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
423 #endif
424   GNUNET_TRANSPORT_offer_hello (p2.th, message);
425   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
426 }
427
428
429 static void
430 run (void *cls,
431      struct GNUNET_SCHEDULER_Handle *s,
432      char *const *args,
433      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
434 {
435   GNUNET_assert (ok == 1);
436   OKPP;
437   sched = s;
438   die_task = GNUNET_SCHEDULER_add_delayed (sched,
439                                            TIMEOUT,
440                                            &end_badly,
441                                            NULL);
442   if (is_tcp)
443     {
444       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
445       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
446     }
447   else if (is_http)
448     {
449       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
450       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
451     }
452   else if (is_udp)
453     {
454       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
455       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
456     }
457   else if (is_tcp_nat)
458     {
459       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
460       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
461     }
462   else
463     GNUNET_assert (0);
464   GNUNET_assert(p1.th != NULL);
465   GNUNET_assert(p2.th != NULL);
466   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
467 }
468
469
470 static int
471 check ()
472 {
473   char *const argv[] = { "test-transport-api-reliability",
474     "-c",
475     "test_transport_api_data.conf",
476 #if VERBOSE
477     "-L", "DEBUG",
478 #endif
479     NULL
480   };
481   struct GNUNET_GETOPT_CommandLineOption options[] = {
482     GNUNET_GETOPT_OPTION_END
483   };
484
485 #if WRITECONFIG
486   setTransportOptions("test_transport_api_data.conf");
487 #endif
488   ok = 1;
489   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
490                       argv, "test-transport-api-reliability", "nohelp",
491                       options, &run, &ok);
492   stop_arm (&p1);
493   stop_arm (&p2);
494   return ok;
495 }
496
497
498 int
499 main (int argc, char *argv[])
500 {
501   int ret;
502 #ifdef MINGW
503   return GNUNET_SYSERR;
504 #endif
505   if (strstr(argv[0], "tcp_nat") != NULL)
506     {
507       is_tcp_nat = GNUNET_YES;
508     }
509   else if (strstr(argv[0], "tcp") != NULL)
510     {
511       is_tcp = GNUNET_YES;
512     }
513   else if (strstr(argv[0], "http") != NULL)
514     {
515       is_http = GNUNET_YES;
516     }
517   else if (strstr(argv[0], "udp") != NULL)
518     {
519       is_udp = GNUNET_YES;
520     }
521   GNUNET_log_setup ("test-transport-api-reliability",
522 #if VERBOSE
523                     "DEBUG",
524 #else
525                     "WARNING",
526 #endif
527                     NULL);
528   ret = check ();
529   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
530   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
531   return ret;
532 }
533
534 /* end of test_transport_api_reliability.c */