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