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_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 * Total number of messages to send
37 * Note that this value must not significantly exceed
38 * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
39 * messages may be dropped even for a reliable transport.
41 #define TOTAL_MSGS (1024 * 3)
44 * Message type of test messages
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)
60 * Struct for the test message
62 GNUNET_NETWORK_STRUCT_BEGIN
66 struct GNUNET_MessageHeader header;
69 GNUNET_NETWORK_STRUCT_END
73 * Name of the plugin to test
75 static char *test_plugin;
80 static char *test_name;
83 * Return value of the test
90 struct PeerContext *p1;
93 * Configuration file of peer 1
100 struct PeerContext *p2;
103 * Configuration file of peer 1
110 static struct GNUNET_SCHEDULER_Task * die_task;
113 * Transport transmit handle used
115 struct GNUNET_TRANSPORT_TransmitHandle *th;
118 * Transport testing handle
120 struct GNUNET_TRANSPORT_TESTING_handle *tth;
123 * Total amount of bytes sent
125 static unsigned long long total_bytes;
130 static struct GNUNET_TIME_Absolute start_time;
133 * No. of message currently scheduled to be send
135 static int msg_scheduled;
138 * No. of last message sent
143 * No. of last message received
147 static int test_connected;
148 static int test_sending;
149 static int test_send_timeout;
153 * Bitmap storing which messages were received
156 static char bitmap[TOTAL_MSGS / 8];
158 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
161 * END Testcase specific declarations
165 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
167 #define OKPP do { ok++; } while (0)
171 get_bit (const char *map, unsigned int bit);
176 unsigned long long delta;
177 unsigned long long rate;
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
182 /* Calculcate statistics */
183 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
184 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
185 FPRINTF (stderr, "\nThroughput was %llu KiBytes/s\n",
188 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
189 GAUGER ("TRANSPORT", value_name, (int) rate,
191 GNUNET_free (value_name);
193 if (die_task != NULL)
194 GNUNET_SCHEDULER_cancel (die_task);
197 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
201 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
204 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
205 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
207 GNUNET_TRANSPORT_TESTING_done (tth);
213 for (i = 0; i < TOTAL_MSGS; i++)
215 if (get_bit (bitmap, i) == 0)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not receive message %d\n", i);
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
230 if (test_connected == GNUNET_YES)
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
235 if (test_sending == GNUNET_NO)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Testcase did not send any messages before timeout\n");
238 if (test_send_timeout == GNUNET_YES)
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 "Test had timeout while waiting to send data\n");
241 for (i = 0; i < TOTAL_MSGS; i++)
243 if (get_bit (bitmap, i) == 0)
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Did not receive message %d\n", i);
251 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
255 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
259 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
261 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
262 GNUNET_TRANSPORT_TESTING_done (tth);
268 get_size (unsigned int iter)
272 ret = (iter * iter * iter);
275 /* FreeBSD/OSX etc. Unix DGRAMs do not work
276 * with large messages */
277 if (0 == strcmp ("unix", test_plugin))
278 return sizeof (struct TestMessage) + (ret % 1024);
280 return sizeof (struct TestMessage) + (ret % 60000);
285 * Sets a bit active in the bitmap.
287 * @param bitIdx which bit to set
288 * @return GNUNET_SYSERR on error, GNUNET_OK on success
291 set_bit (unsigned int bitIdx)
294 unsigned int targetBit;
296 if (bitIdx >= sizeof (bitmap) * 8)
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "tried to set bit %d of %d(!?!?)\n",
299 bitIdx, sizeof (bitmap) * 8);
300 return GNUNET_SYSERR;
302 arraySlot = bitIdx / 8;
303 targetBit = (1L << (bitIdx % 8));
304 bitmap[arraySlot] |= targetBit;
309 * Obtain a bit from bitmap.
310 * @param map the bitmap
311 * @param bit index from bitmap
313 * @return Bit \a bit from hashcode \a code
316 get_bit (const char *map, unsigned int bit)
318 if (bit > TOTAL_MSGS)
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
321 sizeof (bitmap) * 8);
324 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
329 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
330 const struct GNUNET_MessageHeader *message)
335 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
336 const struct TestMessage *hdr;
338 hdr = (const struct TestMessage *) message;
340 if (MTYPE != ntohs (message->type))
342 msg_recv = ntohl (hdr->num);
343 s = get_size (ntohl (hdr->num));
345 if (ntohs (message->size) != s)
347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 "Expected message %u of size %u, got %u bytes of message %u\n",
349 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
350 if (NULL != die_task)
351 GNUNET_SCHEDULER_cancel (die_task);
352 test_sending = GNUNET_YES;
353 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
357 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
358 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
361 "Expected message %u with bits %u, but body did not match\n",
362 ntohl (hdr->num), (unsigned char) n);
363 if (NULL != die_task)
364 GNUNET_SCHEDULER_cancel (die_task);
365 test_sending = GNUNET_YES;
366 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
370 if (ntohl (hdr->num) % 5 == 0)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
373 ntohl (hdr->num), ntohs (message->size));
377 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380 _("Message id %u is bigger than maxmimum number of messages %u expected\n"),
381 ntohl (hdr->num), TOTAL_MSGS);
383 test_sending = GNUNET_YES;
384 if (0 == (n % (TOTAL_MSGS / 100)))
386 FPRINTF (stderr, "%s", ".");
387 if (NULL != die_task)
388 GNUNET_SCHEDULER_cancel (die_task);
389 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
399 notify_ready (void *cls, size_t size, void *buf)
403 struct TestMessage hdr;
411 test_send_timeout = GNUNET_YES;
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
413 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
414 msg_scheduled, TOTAL_MSGS);
415 if (NULL != die_task)
416 GNUNET_SCHEDULER_cancel (die_task);
417 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
423 GNUNET_assert (size >= s);
424 GNUNET_assert (buf != NULL);
425 GNUNET_assert (n < TOTAL_MSGS);
429 GNUNET_assert (n < TOTAL_MSGS);
430 hdr.header.size = htons (s);
431 hdr.header.type = htons (MTYPE);
434 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
435 ret += sizeof (struct TestMessage);
436 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
437 ret += s - sizeof (struct TestMessage);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
448 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
449 break; /* sometimes pack buffer full, sometimes not */
451 while ((size - ret >= s) && (n < TOTAL_MSGS));
454 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
456 ¬ify_ready, NULL);
461 FPRINTF (stderr, "%s", "\n");
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent\n");
463 if (NULL != die_task)
464 GNUNET_SCHEDULER_cancel (die_task);
465 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Returning total message block of size %u\n", ret);
478 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
481 GNUNET_i2s (peer), cls);
486 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
489 GNUNET_i2s (peer), cls);
491 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
498 start_time = GNUNET_TIME_absolute_get ();
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting to send %u messages\n",
501 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
502 TIMEOUT_TRANSMIT, ¬ify_ready,
507 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
509 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
512 GNUNET_i2s (&p2->id));
515 test_connected = GNUNET_YES;
518 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
522 start_cb (struct PeerContext *p, void *cls)
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
528 GNUNET_i2s (&p->id));
533 test_connected = GNUNET_NO;
534 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
540 run (void *cls, char *const *args, const char *cfgfile,
541 const struct GNUNET_CONFIGURATION_Handle *cfg)
543 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
544 test_send_timeout = GNUNET_NO;
546 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
547 ¬ify_receive, ¬ify_connect,
548 ¬ify_disconnect, &start_cb,
550 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
551 ¬ify_receive, ¬ify_connect,
552 ¬ify_disconnect, &start_cb,
554 if ((p1 == NULL) || (p2 == NULL))
556 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
557 if (die_task != NULL)
558 GNUNET_SCHEDULER_cancel (die_task);
559 //die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
566 main (int argc, char *argv[])
571 static char *const argv_new[] = { "test-transport-api-reliability",
573 "test_transport_api_data.conf",
576 static struct GNUNET_GETOPT_CommandLineOption options[] = {
577 GNUNET_GETOPT_OPTION_END
580 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
582 GNUNET_log_setup (test_name,
586 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
587 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
590 tth = GNUNET_TRANSPORT_TESTING_init ();
592 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
593 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
598 setTransportOptions ("test_transport_api_data.conf");
602 ret = GNUNET_PROGRAM_run ((sizeof (argv_new) / sizeof (char *)) - 1, argv_new, test_name,
603 "nohelp", options, &run, &ok);
604 if (GNUNET_SYSERR == ret)
607 GNUNET_free (cfg_file_p1);
608 GNUNET_free (cfg_file_p2);
610 GNUNET_free (test_source);
611 GNUNET_free (test_plugin);
612 GNUNET_free (test_name);
617 /* end of test_transport_api_reliability.c */