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_transport_service.h"
32 #include "transport-testing.h"
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
40 * How long until we give up on transmitting the message?
42 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44 static char *test_source;
46 static char *test_plugin;
48 static char *test_name;
52 static GNUNET_SCHEDULER_TaskIdentifier die_task;
54 struct PeerContext *p1;
56 struct PeerContext *p2;
58 struct GNUNET_TRANSPORT_TransmitHandle *th;
67 struct GNUNET_TRANSPORT_TESTING_handle *tth;
70 * Testcase specific declarations
74 * Note that this value must not significantly exceed
75 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
76 * messages may be dropped even for a reliable transport.
78 #define TOTAL_MSGS (1024 * 3)
82 GNUNET_NETWORK_STRUCT_BEGIN
86 struct GNUNET_MessageHeader header;
89 GNUNET_NETWORK_STRUCT_END
91 static char *test_name;
93 static int msg_scheduled;
95 static int msg_recv_expected;
98 static int test_connected;
99 static int test_sending;
100 static int test_send_timeout;
102 static unsigned long long total_bytes;
104 static struct GNUNET_TIME_Absolute start_time;
106 static char bitmap[TOTAL_MSGS / 8];
108 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
111 * END Testcase specific declarations
115 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
117 #define OKPP do { ok++; } while (0)
121 get_bit (const char *map, unsigned int bit);
126 unsigned long long delta;
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
132 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
133 FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
134 total_bytes * 1000 / 1024 / delta);
135 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
136 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
138 GNUNET_free (value_name);
140 if (die_task != GNUNET_SCHEDULER_NO_TASK)
141 GNUNET_SCHEDULER_cancel (die_task);
144 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
148 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
151 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
152 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
154 GNUNET_TRANSPORT_TESTING_done (tth);
160 for (i = 0; i < TOTAL_MSGS; i++)
162 if (get_bit (bitmap, i) == 0)
164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
173 die_task = GNUNET_SCHEDULER_NO_TASK;
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
176 if (test_connected == GNUNET_YES)
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
181 if (test_sending == GNUNET_NO)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Testcase did not send any messages before timeout\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
187 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
188 if (test_send_timeout == GNUNET_YES)
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "Test had timeout while waiting to send data\n");
194 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
198 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
202 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
204 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
206 GNUNET_TRANSPORT_TESTING_done (tth);
213 get_size (unsigned int iter)
217 ret = (iter * iter * iter);
220 /* FreeBSD/OSX etc. Unix DGRAMs do not work
221 * with large messages */
222 if (0 == strcmp ("unix", test_plugin))
223 return sizeof (struct TestMessage) + (ret % 1024);
225 return sizeof (struct TestMessage) + (ret % 60000);
230 * Sets a bit active in the bitmap.
232 * @param bitIdx which bit to set
235 set_bit (unsigned int bitIdx)
238 unsigned int targetBit;
240 if (bitIdx >= sizeof (bitmap) * 8)
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
243 bitIdx, sizeof (bitmap) * 8);
246 arraySlot = bitIdx / 8;
247 targetBit = (1L << (bitIdx % 8));
248 bitmap[arraySlot] |= targetBit;
252 * Obtain a bit from bitmap.
253 * @param map the bitmap
254 * @param bit index from bitmap
256 * @return Bit \a bit from hashcode \a code
259 get_bit (const char *map, unsigned int bit)
261 if (bit >= TOTAL_MSGS)
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
264 sizeof (bitmap) * 8);
267 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
272 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
273 const struct GNUNET_MessageHeader *message,
274 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
279 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
280 const struct TestMessage *hdr;
282 hdr = (const struct TestMessage *) message;
284 if (MTYPE != ntohs (message->type))
286 msg_recv_expected = n;
287 msg_recv = ntohl (hdr->num);
288 s = get_size (ntohl (hdr->num));
290 if (ntohs (message->size) != s)
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 "Expected message %u of size %u, got %u bytes of message %u\n",
294 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
295 if (GNUNET_SCHEDULER_NO_TASK != die_task)
296 GNUNET_SCHEDULER_cancel (die_task);
297 test_sending = GNUNET_YES;
298 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
302 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
303 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 "Expected message %u with bits %u, but body did not match\n",
307 ntohl (hdr->num), (unsigned char) n);
308 if (GNUNET_SCHEDULER_NO_TASK != die_task)
309 GNUNET_SCHEDULER_cancel (die_task);
310 test_sending = GNUNET_YES;
311 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
315 if (ntohl (hdr->num) % 5 == 0)
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
318 ntohl (hdr->num), ntohs (message->size));
322 set_bit (ntohl (hdr->num));
323 test_sending = GNUNET_YES;
324 if (0 == (n % (TOTAL_MSGS / 100)))
326 FPRINTF (stderr, "%s", ".");
327 if (GNUNET_SCHEDULER_NO_TASK != die_task)
328 GNUNET_SCHEDULER_cancel (die_task);
329 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
339 notify_ready (void *cls, size_t size, void *buf)
343 struct TestMessage hdr;
351 test_send_timeout = GNUNET_YES;
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
354 msg_scheduled, TOTAL_MSGS);
355 if (GNUNET_SCHEDULER_NO_TASK != die_task)
356 GNUNET_SCHEDULER_cancel (die_task);
357 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
363 GNUNET_assert (size >= s);
364 GNUNET_assert (buf != NULL);
368 hdr.header.size = htons (s);
369 hdr.header.type = htons (MTYPE);
372 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
373 ret += sizeof (struct TestMessage);
374 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
375 ret += s - sizeof (struct TestMessage);
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
386 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
387 break; /* sometimes pack buffer full, sometimes not */
389 while (size - ret >= s);
392 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
394 ¬ify_ready, NULL);
399 FPRINTF (stderr, "%s", "\n");
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent\n");
401 if (GNUNET_SCHEDULER_NO_TASK != die_task)
402 GNUNET_SCHEDULER_cancel (die_task);
403 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Returning total message block of size %u\n", ret);
416 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
417 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
421 GNUNET_i2s (peer), cls);
426 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
429 GNUNET_i2s (peer), cls);
431 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
438 start_time = GNUNET_TIME_absolute_get ();
439 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
440 TIMEOUT_TRANSMIT, ¬ify_ready,
445 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
447 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
450 GNUNET_i2s (&p2->id));
453 test_connected = GNUNET_YES;
456 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
460 start_cb (struct PeerContext *p, void *cls)
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
467 GNUNET_i2s (&p->id));
472 test_connected = GNUNET_NO;
473 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
479 run (void *cls, char *const *args, const char *cfgfile,
480 const struct GNUNET_CONFIGURATION_Handle *cfg)
482 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
483 test_send_timeout = GNUNET_NO;
486 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
487 ¬ify_receive, ¬ify_connect,
488 ¬ify_disconnect, &start_cb,
490 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
491 ¬ify_receive, ¬ify_connect,
492 ¬ify_disconnect, &start_cb,
496 if ((p1 == NULL) || (p2 == NULL))
498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
499 if (die_task != GNUNET_SCHEDULER_NO_TASK)
500 GNUNET_SCHEDULER_cancel (die_task);
501 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
511 static char *const argv[] = { "test-transport-api-unreliability",
513 "test_transport_api_data.conf",
516 static struct GNUNET_GETOPT_CommandLineOption options[] = {
517 GNUNET_GETOPT_OPTION_END
521 setTransportOptions ("test_transport_api_data.conf");
525 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
526 "nohelp", options, &run, &ok);
532 main (int argc, char *argv[])
536 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
538 GNUNET_log_setup (test_name,
542 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
543 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
546 tth = GNUNET_TRANSPORT_TESTING_init ();
548 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
549 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
553 GNUNET_free (cfg_file_p1);
554 GNUNET_free (cfg_file_p2);
556 GNUNET_free (test_source);
557 GNUNET_free (test_plugin);
558 GNUNET_free (test_name);
564 /* end of test_transport_api_unreliability.c */