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