2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_transport_api_unreliability.c
22 * @brief test case for transports; ensures messages get
23 * through, regardless of order
25 * This test case serves as a base for unreliable
26 * transport test cases to check that the transports
27 * achieve reliable message delivery.
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"
39 #include "transport.h"
40 #include "transport-testing.h"
42 #define VERBOSE GNUNET_NO
44 #define VERBOSE_ARM GNUNET_NO
46 #define START_ARM GNUNET_YES
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
54 * How long until we give up on transmitting the message?
56 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
58 static char *test_source;
60 static char *test_plugin;
62 static char *test_name;
66 static GNUNET_SCHEDULER_TaskIdentifier die_task;
68 struct PeerContext *p1;
70 struct PeerContext *p2;
72 struct GNUNET_TRANSPORT_TransmitHandle *th;
81 struct GNUNET_TRANSPORT_TESTING_handle *tth;
84 * Testcase specific declarations
88 * Note that this value must not significantly exceed
89 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
90 * messages may be dropped even for a reliable transport.
92 #define TOTAL_MSGS (1024 * 3)
96 GNUNET_NETWORK_STRUCT_BEGIN
100 struct GNUNET_MessageHeader header;
103 GNUNET_NETWORK_STRUCT_END
105 static char *test_name;
107 static int msg_scheduled;
109 static int msg_recv_expected;
112 static int test_connected;
113 static int test_sending;
114 static int test_send_timeout;
116 static unsigned long long total_bytes;
118 static struct GNUNET_TIME_Absolute start_time;
120 static char bitmap[TOTAL_MSGS / 8];
122 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
125 * END Testcase specific declarations
129 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
131 #define OKPP do { ok++; } while (0)
135 get_bit (const char *map, unsigned int bit);
140 unsigned long long delta;
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
146 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
147 FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
148 total_bytes * 1000 / 1024 / delta);
149 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
150 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
152 GNUNET_free (value_name);
154 if (die_task != GNUNET_SCHEDULER_NO_TASK)
155 GNUNET_SCHEDULER_cancel (die_task);
158 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
162 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
165 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
166 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
168 GNUNET_TRANSPORT_TESTING_done (tth);
174 for (i = 0; i < TOTAL_MSGS; i++)
176 if (get_bit (bitmap, i) == 0)
178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
187 die_task = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
190 if (test_connected == GNUNET_YES)
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
195 if (test_sending == GNUNET_NO)
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 "Testcase did not send any messages before timeout\n");
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
201 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
202 if (test_send_timeout == GNUNET_YES)
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 "Test had timeout while waiting to send data\n");
208 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
212 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
216 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
218 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
220 GNUNET_TRANSPORT_TESTING_done (tth);
227 get_size (unsigned int iter)
231 ret = (iter * iter * iter);
234 /* FreeBSD/OSX etc. Unix DGRAMs do not work
235 * with large messages */
236 if (0 == strcmp ("unix", test_plugin))
237 return sizeof (struct TestMessage) + (ret % 1024);
239 return sizeof (struct TestMessage) + (ret % 60000);
244 * Sets a bit active in the bitmap.
246 * @param bitIdx which bit to set
249 set_bit (unsigned int bitIdx)
252 unsigned int targetBit;
254 if (bitIdx >= sizeof (bitmap) * 8)
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
257 bitIdx, sizeof (bitmap) * 8);
260 arraySlot = bitIdx / 8;
261 targetBit = (1L << (bitIdx % 8));
262 bitmap[arraySlot] |= targetBit;
266 * Obtain a bit from bitmap.
267 * @param map the bitmap
268 * @param bit index from bitmap
270 * @return Bit \a bit from hashcode \a code
273 get_bit (const char *map, unsigned int bit)
275 if (bit >= TOTAL_MSGS)
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
278 sizeof (bitmap) * 8);
281 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
286 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
287 const struct GNUNET_MessageHeader *message,
288 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
293 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
294 const struct TestMessage *hdr;
296 hdr = (const struct TestMessage *) message;
298 if (MTYPE != ntohs (message->type))
300 msg_recv_expected = n;
301 msg_recv = ntohl (hdr->num);
302 s = get_size (ntohl (hdr->num));
304 if (ntohs (message->size) != s)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 "Expected message %u of size %u, got %u bytes of message %u\n",
308 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
309 if (GNUNET_SCHEDULER_NO_TASK != die_task)
310 GNUNET_SCHEDULER_cancel (die_task);
311 test_sending = GNUNET_YES;
312 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
316 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
317 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "Expected message %u with bits %u, but body did not match\n",
321 ntohl (hdr->num), (unsigned char) n);
322 if (GNUNET_SCHEDULER_NO_TASK != die_task)
323 GNUNET_SCHEDULER_cancel (die_task);
324 test_sending = GNUNET_YES;
325 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
329 if (ntohl (hdr->num) % 5 == 0)
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
332 ntohl (hdr->num), ntohs (message->size));
336 set_bit (ntohl (hdr->num));
337 test_sending = GNUNET_YES;
338 if (0 == (n % (TOTAL_MSGS / 100)))
340 FPRINTF (stderr, "%s", ".");
341 if (GNUNET_SCHEDULER_NO_TASK != die_task)
342 GNUNET_SCHEDULER_cancel (die_task);
343 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
353 notify_ready (void *cls, size_t size, void *buf)
357 struct TestMessage hdr;
365 test_send_timeout = GNUNET_YES;
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
368 msg_scheduled, TOTAL_MSGS);
369 if (GNUNET_SCHEDULER_NO_TASK != die_task)
370 GNUNET_SCHEDULER_cancel (die_task);
371 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
377 GNUNET_assert (size >= s);
378 GNUNET_assert (buf != NULL);
382 hdr.header.size = htons (s);
383 hdr.header.type = htons (MTYPE);
386 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
387 ret += sizeof (struct TestMessage);
388 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
389 ret += s - sizeof (struct TestMessage);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
400 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
401 break; /* sometimes pack buffer full, sometimes not */
403 while (size - ret >= s);
406 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
408 ¬ify_ready, NULL);
413 FPRINTF (stderr, "%s", "\n");
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent\n");
415 if (GNUNET_SCHEDULER_NO_TASK != die_task)
416 GNUNET_SCHEDULER_cancel (die_task);
417 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Returning total message block of size %u\n", ret);
430 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
431 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
435 GNUNET_i2s (peer), cls);
440 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
443 GNUNET_i2s (peer), cls);
445 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
452 start_time = GNUNET_TIME_absolute_get ();
453 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
454 TIMEOUT_TRANSMIT, ¬ify_ready,
459 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
461 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
464 GNUNET_i2s (&p2->id));
467 test_connected = GNUNET_YES;
470 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
474 start_cb (struct PeerContext *p, void *cls)
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
481 GNUNET_i2s (&p->id));
486 test_connected = GNUNET_NO;
487 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
493 run (void *cls, char *const *args, const char *cfgfile,
494 const struct GNUNET_CONFIGURATION_Handle *cfg)
496 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
497 test_send_timeout = GNUNET_NO;
500 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
501 ¬ify_receive, ¬ify_connect,
502 ¬ify_disconnect, &start_cb,
504 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
505 ¬ify_receive, ¬ify_connect,
506 ¬ify_disconnect, &start_cb,
510 if ((p1 == NULL) || (p2 == NULL))
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
513 if (die_task != GNUNET_SCHEDULER_NO_TASK)
514 GNUNET_SCHEDULER_cancel (die_task);
515 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
525 static char *const argv[] = { "test-transport-api-unreliability",
527 "test_transport_api_data.conf",
533 static struct GNUNET_GETOPT_CommandLineOption options[] = {
534 GNUNET_GETOPT_OPTION_END
538 setTransportOptions ("test_transport_api_data.conf");
542 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
543 "nohelp", options, &run, &ok);
549 main (int argc, char *argv[])
553 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
555 GNUNET_log_setup (test_name,
563 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
564 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
567 tth = GNUNET_TRANSPORT_TESTING_init ();
569 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
570 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
574 GNUNET_free (cfg_file_p1);
575 GNUNET_free (cfg_file_p2);
577 GNUNET_free (test_source);
578 GNUNET_free (test_plugin);
579 GNUNET_free (test_name);
585 /* end of test_transport_api_unreliability.c */