2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/test_transport_api_reliability.c
22 * @brief base test case for transport implementations
24 * This test case serves ensures that messages are reliably sent between peers
26 * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
27 * and ensures that all message were received.
30 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
35 * Allow making the problem "bigger".
40 * Total number of messages to send
42 * Note that this value must not significantly exceed
43 * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
44 * messages may be dropped even for a reliable transport.
46 #define TOTAL_MSGS (1024 * 3 * FACTOR)
49 * Message type of test messages
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90 * FACTOR)
59 * How long until we give up on transmitting the message?
61 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60 * FACTOR)
65 * Struct for the test message
67 GNUNET_NETWORK_STRUCT_BEGIN
71 struct GNUNET_MessageHeader header;
74 GNUNET_NETWORK_STRUCT_END
78 * Name of the plugin to test
80 static char *test_plugin;
85 static char *test_name;
88 * Return value of the test
95 static struct PeerContext *p1;
98 * Configuration file of peer 1
100 static char *cfg_file_p1;
105 static struct PeerContext *p2;
108 * Configuration file of peer 1
110 static char *cfg_file_p2;
115 static struct GNUNET_SCHEDULER_Task * die_task;
118 * Transport transmit handle used
120 static struct GNUNET_TRANSPORT_TransmitHandle *th;
123 * Transport testing handle
125 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
128 * Total amount of bytes sent
130 static unsigned long long total_bytes;
135 static struct GNUNET_TIME_Absolute start_time;
138 * No. of message currently scheduled to be send
140 static int msg_scheduled;
143 * No. of last message sent
148 * No. of last message received
152 static int test_connected;
154 static int test_sending;
156 static int test_send_timeout;
160 * Bitmap storing which messages were received
162 static char bitmap[TOTAL_MSGS / 8];
164 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
167 * END Testcase specific declarations
171 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
173 #define OKPP do { ok++; } while (0)
178 get_bit (const char *map, unsigned int bit);
184 unsigned long long delta;
185 unsigned long long rate;
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 /* Calculcate statistics */
193 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
194 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
196 "\nThroughput was %llu KiBytes/s\n",
199 GNUNET_asprintf (&value_name,
206 GNUNET_free (value_name);
208 if (die_task != NULL)
209 GNUNET_SCHEDULER_cancel (die_task);
213 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
218 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
221 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
222 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
223 GNUNET_TRANSPORT_TESTING_done (tth);
225 for (i = 0; i < TOTAL_MSGS; i++)
227 if (get_bit (bitmap, i) == 0)
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Did not receive message %d\n",
244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245 "Fail! Stopping peers\n");
246 if (test_connected == GNUNET_YES)
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248 "Peers got connected\n");
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 "Peers got NOT connected\n");
253 if (test_sending == GNUNET_NO)
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "Testcase did not send any messages before timeout\n");
256 if (test_send_timeout == GNUNET_YES)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "Test had timeout while waiting to send data\n");
259 for (i = 0; i < TOTAL_MSGS; i++)
261 if (get_bit (bitmap, i) == 0)
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "Did not receive message %u\n",
272 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
277 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
281 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
283 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
284 GNUNET_TRANSPORT_TESTING_done (tth);
290 get_size (unsigned int iter)
294 ret = (iter * iter * iter);
297 /* FreeBSD/OSX etc. Unix DGRAMs do not work
298 * with large messages */
299 if (0 == strcmp ("unix", test_plugin))
300 return sizeof (struct TestMessage) + (ret % 1024);
302 return sizeof (struct TestMessage) + (ret % 60000);
307 * Sets a bit active in the bitmap.
309 * @param bitIdx which bit to set
310 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
313 set_bit (unsigned int bitIdx)
316 unsigned int targetBit;
318 if (bitIdx >= sizeof (bitmap) * 8)
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "tried to set bit %d of %d(!?!?)\n",
322 bitIdx, sizeof (bitmap) * 8);
323 return GNUNET_SYSERR;
325 arraySlot = bitIdx / 8;
326 targetBit = (1L << (bitIdx % 8));
327 bitmap[arraySlot] |= targetBit;
333 * Obtain a bit from bitmap.
334 * @param map the bitmap
335 * @param bit index from bitmap
337 * @return Bit \a bit from hashcode \a code
340 get_bit (const char *map, unsigned int bit)
342 if (bit > TOTAL_MSGS)
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
345 sizeof (bitmap) * 8);
348 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
353 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
354 const struct GNUNET_MessageHeader *message)
359 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
360 const struct TestMessage *hdr;
362 hdr = (const struct TestMessage *) message;
364 if (MTYPE != ntohs (message->type))
366 msg_recv = ntohl (hdr->num);
367 s = get_size (ntohl (hdr->num));
369 if (ntohs (message->size) != s)
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 "Expected message %u of size %u, got %u bytes of message %u\n",
373 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
374 if (NULL != die_task)
375 GNUNET_SCHEDULER_cancel (die_task);
376 test_sending = GNUNET_YES;
377 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
381 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
382 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 "Expected message %u with bits %u, but body did not match\n",
386 ntohl (hdr->num), (unsigned char) n);
387 if (NULL != die_task)
388 GNUNET_SCHEDULER_cancel (die_task);
389 test_sending = GNUNET_YES;
390 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
394 if (ntohl (hdr->num) % 5 == 0)
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
397 ntohl (hdr->num), ntohs (message->size));
401 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 "Message id %u is bigger than maxmimum number of messages %u expected\n",
408 test_sending = GNUNET_YES;
409 if (0 == (n % (TOTAL_MSGS / 100)))
411 FPRINTF (stderr, "%s", ".");
412 if (NULL != die_task)
413 GNUNET_SCHEDULER_cancel (die_task);
414 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
424 notify_ready (void *cls, size_t size, void *buf)
428 struct TestMessage hdr;
436 test_send_timeout = GNUNET_YES;
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
439 msg_scheduled, TOTAL_MSGS);
440 if (NULL != die_task)
441 GNUNET_SCHEDULER_cancel (die_task);
442 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
448 GNUNET_assert (size >= s);
449 GNUNET_assert (buf != NULL);
450 GNUNET_assert (n < TOTAL_MSGS);
454 GNUNET_assert (n < TOTAL_MSGS);
455 hdr.header.size = htons (s);
456 hdr.header.type = htons (MTYPE);
459 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
460 ret += sizeof (struct TestMessage);
461 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
462 ret += s - sizeof (struct TestMessage);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Sending message %u of size %u\n",
475 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
476 break; /* sometimes pack buffer full, sometimes not */
478 while ((size - ret >= s) && (n < TOTAL_MSGS));
481 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
483 ¬ify_ready, NULL);
488 FPRINTF (stderr, "%s", "\n");
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "All messages scheduled to be sent\n");
491 if (NULL != die_task)
492 GNUNET_SCHEDULER_cancel (die_task);
493 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Returning total message block of size %u\n", ret);
506 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
509 GNUNET_i2s (peer), cls);
514 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
517 GNUNET_i2s (peer), cls);
519 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
527 start_time = GNUNET_TIME_absolute_get ();
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Starting to send %u messages\n",
531 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
532 TIMEOUT_TRANSMIT, ¬ify_ready,
537 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
539 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Peers connected: %s <-> %s\n", p1_c,
543 GNUNET_i2s (&p2->id));
546 test_connected = GNUNET_YES;
549 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
554 start_cb (struct PeerContext *p, void *cls)
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Peer %u (`%s') started\n",
562 GNUNET_i2s (&p->id));
567 test_connected = GNUNET_NO;
568 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
578 const struct GNUNET_CONFIGURATION_Handle *cfg)
580 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
582 test_send_timeout = GNUNET_NO;
584 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
585 ¬ify_receive, ¬ify_connect,
586 ¬ify_disconnect, &start_cb,
588 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
589 ¬ify_receive, ¬ify_connect,
590 ¬ify_disconnect, &start_cb,
592 if ((p1 == NULL) || (p2 == NULL))
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
595 if (die_task != NULL)
596 GNUNET_SCHEDULER_cancel (die_task);
597 //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
604 main (int argc, char *argv[])
609 static char *const argv_new[] = {
610 "test-transport-api-reliability",
612 "test_transport_api_data.conf",
615 static struct GNUNET_GETOPT_CommandLineOption options[] = {
616 GNUNET_GETOPT_OPTION_END
619 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
621 GNUNET_log_setup (test_name,
624 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
626 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
630 tth = GNUNET_TRANSPORT_TESTING_init ();
632 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
633 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
636 setTransportOptions ("test_transport_api_data.conf");
640 ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
644 if (GNUNET_SYSERR == ret)
651 GNUNET_free (cfg_file_p1);
652 GNUNET_free (cfg_file_p2);
653 GNUNET_free (test_source);
654 GNUNET_free (test_plugin);
655 GNUNET_free (test_name);
660 /* end of test_transport_api_reliability.c */