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