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