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