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