2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
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 struct 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 %u of %u(!?!?)\n",
323 (unsigned int) sizeof (bitmap) * 8);
324 return GNUNET_SYSERR;
326 arraySlot = bitIdx / 8;
327 targetBit = (1L << (bitIdx % 8));
328 bitmap[arraySlot] |= targetBit;
334 * Obtain a bit from bitmap.
335 * @param map the bitmap
336 * @param bit index from bitmap
338 * @return Bit \a bit from hashcode \a code
341 get_bit (const char *map, unsigned int bit)
343 if (bit > TOTAL_MSGS)
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 "get bit %u of %u(!?!?)\n",
348 (unsigned int) sizeof (bitmap) * 8);
351 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
356 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
357 const struct GNUNET_MessageHeader *message)
362 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
363 const struct TestMessage *hdr;
365 hdr = (const struct TestMessage *) message;
367 if (MTYPE != ntohs (message->type))
369 msg_recv = ntohl (hdr->num);
370 s = get_size (ntohl (hdr->num));
372 if (ntohs (message->size) != s)
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 "Expected message %u of size %u, got %u bytes of message %u\n",
376 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
377 if (NULL != die_task)
378 GNUNET_SCHEDULER_cancel (die_task);
379 test_sending = GNUNET_YES;
380 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
384 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
385 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
388 "Expected message %u with bits %u, but body did not match\n",
389 ntohl (hdr->num), (unsigned char) n);
390 if (NULL != die_task)
391 GNUNET_SCHEDULER_cancel (die_task);
392 test_sending = GNUNET_YES;
393 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
397 if (ntohl (hdr->num) % 5 == 0)
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Got message %u of size %u\n",
402 ntohs (message->size));
406 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 "Message id %u is bigger than maxmimum number of messages %u expected\n",
413 test_sending = GNUNET_YES;
414 if (0 == (n % (TOTAL_MSGS / 100)))
416 FPRINTF (stderr, "%s", ".");
417 if (NULL != die_task)
418 GNUNET_SCHEDULER_cancel (die_task);
419 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
429 notify_ready (void *cls, size_t size, void *buf)
433 struct TestMessage hdr;
441 test_send_timeout = GNUNET_YES;
442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
443 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
444 msg_scheduled, TOTAL_MSGS);
445 if (NULL != die_task)
446 GNUNET_SCHEDULER_cancel (die_task);
447 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
453 GNUNET_assert (size >= s);
454 GNUNET_assert (buf != NULL);
455 GNUNET_assert (n < TOTAL_MSGS);
459 GNUNET_assert (n < TOTAL_MSGS);
460 hdr.header.size = htons (s);
461 hdr.header.type = htons (MTYPE);
464 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
465 ret += sizeof (struct TestMessage);
466 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
467 ret += s - sizeof (struct TestMessage);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Sending message %u of size %u\n",
480 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
481 break; /* sometimes pack buffer full, sometimes not */
483 while ((size - ret >= s) && (n < TOTAL_MSGS));
486 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
488 ¬ify_ready, NULL);
493 FPRINTF (stderr, "%s", "\n");
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "All messages scheduled to be sent\n");
496 if (NULL != die_task)
497 GNUNET_SCHEDULER_cancel (die_task);
498 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Returning total message block of size %u\n", ret);
511 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Peer `%4s' connected to us (%p)!\n",
515 GNUNET_i2s (peer), cls);
520 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Peer `%4s' disconnected (%p)!\n",
524 GNUNET_i2s (peer), cls);
526 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
534 start_time = GNUNET_TIME_absolute_get ();
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Starting to send %u messages\n",
538 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
539 TIMEOUT_TRANSMIT, ¬ify_ready,
545 testing_connect_cb (struct PeerContext *p1,
546 struct PeerContext *p2,
549 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Peers connected: %s <-> %s\n",
554 GNUNET_i2s (&p2->id));
557 test_connected = GNUNET_YES;
560 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
565 start_cb (struct PeerContext *p, void *cls)
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Peer %u (`%s') started\n",
573 GNUNET_i2s (&p->id));
578 test_connected = GNUNET_NO;
579 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
589 const struct GNUNET_CONFIGURATION_Handle *cfg)
591 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
593 test_send_timeout = GNUNET_NO;
595 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
596 ¬ify_receive, ¬ify_connect,
597 ¬ify_disconnect, &start_cb,
599 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
600 ¬ify_receive, ¬ify_connect,
601 ¬ify_disconnect, &start_cb,
603 if ((p1 == NULL) || (p2 == NULL))
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
606 if (die_task != NULL)
607 GNUNET_SCHEDULER_cancel (die_task);
608 //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
615 main (int argc, char *argv[])
620 static char *const argv_new[] = {
621 "test-transport-api-reliability",
623 "test_transport_api_data.conf",
626 static struct GNUNET_GETOPT_CommandLineOption options[] = {
627 GNUNET_GETOPT_OPTION_END
630 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
632 GNUNET_log_setup (test_name,
635 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
637 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
641 tth = GNUNET_TRANSPORT_TESTING_init ();
643 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
644 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
647 setTransportOptions ("test_transport_api_data.conf");
651 ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1,
655 if (GNUNET_SYSERR == ret)
662 GNUNET_free (cfg_file_p1);
663 GNUNET_free (cfg_file_p2);
664 GNUNET_free (test_source);
665 GNUNET_free (test_plugin);
666 GNUNET_free (test_name);
671 /* end of test_transport_api_reliability.c */