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_EXTRA_LOGGING
44 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
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;
82 * Testcase specific declarations
86 * Note that this value must not significantly exceed
87 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
88 * messages may be dropped even for a reliable transport.
90 #define TOTAL_MSGS (1024 * 3)
96 struct GNUNET_MessageHeader header;
100 static char *test_name;
102 static int msg_scheduled;
104 static int msg_recv_expected;
107 static int test_connected;
108 static int test_failed;
110 static unsigned long long total_bytes;
112 static struct GNUNET_TIME_Absolute start_time;
114 static char bitmap[TOTAL_MSGS / 8];
117 * END Testcase specific declarations
121 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
123 #define OKPP do { ok++; } while (0)
127 get_bit (const char *map, unsigned int bit);
132 unsigned long long delta;
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
138 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
139 fprintf (stderr, "\nThroughput was %llu kb/s\n",
140 total_bytes * 1000 / 1024 / delta);
141 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
142 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
144 GNUNET_free (value_name);
146 if (die_task != GNUNET_SCHEDULER_NO_TASK)
147 GNUNET_SCHEDULER_cancel (die_task);
150 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
153 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
154 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
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_NO)
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_failed == GNUNET_NO)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
186 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
189 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
193 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
195 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
202 get_size (unsigned int iter)
206 ret = (iter * iter * iter);
207 return sizeof (struct TestMessage) + (ret % 60000);
212 * Sets a bit active in the bitmap.
214 * @param bitIdx which bit to set
217 set_bit (unsigned int bitIdx)
220 unsigned int targetBit;
222 if (bitIdx >= sizeof (bitmap) * 8)
224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
225 bitIdx, sizeof (bitmap) * 8);
228 arraySlot = bitIdx / 8;
229 targetBit = (1L << (bitIdx % 8));
230 bitmap[arraySlot] |= targetBit;
234 * Obtain a bit from bitmap.
235 * @param map the bitmap
236 * @param bit index from bitmap
238 * @return Bit \a bit from hashcode \a code
241 get_bit (const char *map, unsigned int bit)
243 if (bit >= TOTAL_MSGS)
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
246 sizeof (bitmap) * 8);
249 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
254 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
255 const struct GNUNET_MessageHeader *message,
256 const struct GNUNET_TRANSPORT_ATS_Information *ats,
262 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
263 const struct TestMessage *hdr;
265 hdr = (const struct TestMessage *) message;
267 if (MTYPE != ntohs (message->type))
269 msg_recv_expected = n;
270 msg_recv = ntohl (hdr->num);
271 s = get_size (ntohl (hdr->num));
273 if (ntohs (message->size) != s)
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 "Expected message %u of size %u, got %u bytes of message %u\n",
277 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
278 if (GNUNET_SCHEDULER_NO_TASK != die_task)
279 GNUNET_SCHEDULER_cancel (die_task);
280 test_failed = GNUNET_YES;
281 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
285 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
286 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 "Expected message %u with bits %u, but body did not match\n",
290 ntohl (hdr->num), (unsigned char) n);
291 if (GNUNET_SCHEDULER_NO_TASK != die_task)
292 GNUNET_SCHEDULER_cancel (die_task);
293 test_failed = GNUNET_YES;
294 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
298 if (ntohl (hdr->num) % 5 == 0)
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
301 ntohl (hdr->num), ntohs (message->size));
305 set_bit (ntohl (hdr->num));
306 if (0 == (n % (TOTAL_MSGS / 100)))
308 fprintf (stderr, ".");
309 if (GNUNET_SCHEDULER_NO_TASK != die_task)
310 GNUNET_SCHEDULER_cancel (die_task);
311 test_failed = GNUNET_YES;
312 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
316 fprintf (stderr, "\n");
323 notify_ready (void *cls, size_t size, void *buf)
327 struct TestMessage hdr;
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
337 msg_scheduled, TOTAL_MSGS);
338 if (GNUNET_SCHEDULER_NO_TASK != die_task)
339 GNUNET_SCHEDULER_cancel (die_task);
340 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
346 GNUNET_assert (size >= s);
347 GNUNET_assert (buf != NULL);
351 hdr.header.size = htons (s);
352 hdr.header.type = htons (MTYPE);
355 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
356 ret += sizeof (struct TestMessage);
357 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
358 ret += s - sizeof (struct TestMessage);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
369 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
370 break; /* sometimes pack buffer full, sometimes not */
372 while (size - ret >= s);
375 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
377 ¬ify_ready, NULL);
382 fprintf (stderr, "\n");
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "All messages scheduled to be sent!!\n");
385 if (GNUNET_SCHEDULER_NO_TASK != die_task)
386 GNUNET_SCHEDULER_cancel (die_task);
387 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Returning total message block of size %u\n", ret);
400 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
401 const struct GNUNET_TRANSPORT_ATS_Information *ats,
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
406 GNUNET_i2s (peer), cls);
410 max_bps_p1 = 1024 * 1024 * 1024;
411 GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
412 GNUNET_BANDWIDTH_value_init (max_bps_p1),
413 GNUNET_BANDWIDTH_value_init (max_bps_p1));
417 max_bps_p2 = 1024 * 1024 * 1024;
418 GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
419 GNUNET_BANDWIDTH_value_init (max_bps_p2),
420 GNUNET_BANDWIDTH_value_init (max_bps_p2));
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 = 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;
455 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
456 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
460 run (void *cls, char *const *args, const char *cfgfile,
461 const struct GNUNET_CONFIGURATION_Handle *cfg)
463 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
465 p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, ¬ify_receive,
466 ¬ify_connect, ¬ify_disconnect,
468 p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, ¬ify_receive,
469 ¬ify_connect, ¬ify_disconnect,
472 if ((p1 == NULL) || (p2 == NULL))
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
475 if (die_task != GNUNET_SCHEDULER_NO_TASK)
476 GNUNET_SCHEDULER_cancel (die_task);
477 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
481 test_connected = GNUNET_NO;
482 GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
488 static char *const argv[] = { "test-transport-api-unreliability",
490 "test_transport_api_data.conf",
496 static struct GNUNET_GETOPT_CommandLineOption options[] = {
497 GNUNET_GETOPT_OPTION_END
501 setTransportOptions ("test_transport_api_data.conf");
505 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
506 "nohelp", options, &run, &ok);
512 main (int argc, char *argv[])
517 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
518 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
520 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
522 GNUNET_log_setup (test_name,
530 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
531 (strcmp (test_plugin, "udp_nat") == 0))
533 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
534 if (GNUNET_NO == nat_res)
536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
537 "gnunet-nat-server", "SUID not set");
540 if (GNUNET_SYSERR == nat_res)
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
543 "gnunet-nat-server", "file not found");
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);
563 /* end of test_transport_api_unreliability.c */