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_NO
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;
81 struct GNUNET_TRANSPORT_TESTING_handle * tth;
84 * Testcase specific declarations
88 * Note that this value must not significantly exceed
89 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
90 * messages may be dropped even for a reliable transport.
92 #define TOTAL_MSGS (1024 * 3)
98 struct GNUNET_MessageHeader header;
102 static char *test_name;
104 static int msg_scheduled;
106 static int msg_recv_expected;
109 static int test_connected;
110 static int test_failed;
112 static unsigned long long total_bytes;
114 static struct GNUNET_TIME_Absolute start_time;
116 static char bitmap[TOTAL_MSGS / 8];
118 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
121 * END Testcase specific declarations
125 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
127 #define OKPP do { ok++; } while (0)
131 get_bit (const char *map, unsigned int bit);
136 unsigned long long delta;
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
142 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
143 fprintf (stderr, "\nThroughput was %llu kb/s\n",
144 total_bytes * 1000 / 1024 / delta);
145 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
146 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
148 GNUNET_free (value_name);
150 if (die_task != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel (die_task);
154 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
158 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
161 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
162 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
164 GNUNET_TRANSPORT_TESTING_done (tth);
170 for (i = 0; i < TOTAL_MSGS; i++)
172 if (get_bit (bitmap, i) == 0)
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
183 die_task = GNUNET_SCHEDULER_NO_TASK;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
186 if (test_connected == GNUNET_NO)
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
191 if (test_failed == GNUNET_NO)
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
196 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
199 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
203 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
207 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
209 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
211 GNUNET_TRANSPORT_TESTING_done (tth);
218 get_size (unsigned int iter)
222 ret = (iter * iter * iter);
223 return sizeof (struct TestMessage) + (ret % 60000);
228 * Sets a bit active in the bitmap.
230 * @param bitIdx which bit to set
233 set_bit (unsigned int bitIdx)
236 unsigned int targetBit;
238 if (bitIdx >= sizeof (bitmap) * 8)
240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
241 bitIdx, sizeof (bitmap) * 8);
244 arraySlot = bitIdx / 8;
245 targetBit = (1L << (bitIdx % 8));
246 bitmap[arraySlot] |= targetBit;
250 * Obtain a bit from bitmap.
251 * @param map the bitmap
252 * @param bit index from bitmap
254 * @return Bit \a bit from hashcode \a code
257 get_bit (const char *map, unsigned int bit)
259 if (bit >= TOTAL_MSGS)
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
262 sizeof (bitmap) * 8);
265 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
270 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
271 const struct GNUNET_MessageHeader *message,
272 const struct GNUNET_ATS_Information *ats,
278 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
279 const struct TestMessage *hdr;
281 hdr = (const struct TestMessage *) message;
283 if (MTYPE != ntohs (message->type))
285 msg_recv_expected = n;
286 msg_recv = ntohl (hdr->num);
287 s = get_size (ntohl (hdr->num));
289 if (ntohs (message->size) != s)
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "Expected message %u of size %u, got %u bytes of message %u\n",
293 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
294 if (GNUNET_SCHEDULER_NO_TASK != die_task)
295 GNUNET_SCHEDULER_cancel (die_task);
296 test_failed = GNUNET_YES;
297 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
301 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
302 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 "Expected message %u with bits %u, but body did not match\n",
306 ntohl (hdr->num), (unsigned char) n);
307 if (GNUNET_SCHEDULER_NO_TASK != die_task)
308 GNUNET_SCHEDULER_cancel (die_task);
309 test_failed = GNUNET_YES;
310 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
314 if (ntohl (hdr->num) % 5 == 0)
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
317 ntohl (hdr->num), ntohs (message->size));
321 set_bit (ntohl (hdr->num));
322 if (0 == (n % (TOTAL_MSGS / 100)))
324 fprintf (stderr, ".");
325 if (GNUNET_SCHEDULER_NO_TASK != die_task)
326 GNUNET_SCHEDULER_cancel (die_task);
327 test_failed = GNUNET_YES;
328 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
338 notify_ready (void *cls, size_t size, void *buf)
342 struct TestMessage hdr;
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
352 msg_scheduled, TOTAL_MSGS);
353 if (GNUNET_SCHEDULER_NO_TASK != die_task)
354 GNUNET_SCHEDULER_cancel (die_task);
355 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
361 GNUNET_assert (size >= s);
362 GNUNET_assert (buf != NULL);
366 hdr.header.size = htons (s);
367 hdr.header.type = htons (MTYPE);
370 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
371 ret += sizeof (struct TestMessage);
372 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
373 ret += s - sizeof (struct TestMessage);
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
384 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
385 break; /* sometimes pack buffer full, sometimes not */
387 while (size - ret >= s);
390 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
392 ¬ify_ready, NULL);
397 fprintf (stderr, "\n");
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "All messages scheduled to be sent\n");
400 if (GNUNET_SCHEDULER_NO_TASK != die_task)
401 GNUNET_SCHEDULER_cancel (die_task);
402 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Returning total message block of size %u\n", ret);
415 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
416 const struct GNUNET_ATS_Information *ats,
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 = strdup (GNUNET_i2s (&p1->id));
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
449 GNUNET_i2s (&p2->id));
452 test_connected = GNUNET_YES;
455 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
458 void start_cb (struct PeerContext * p,
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
466 GNUNET_i2s (&p->id));
471 test_connected = GNUNET_NO;
472 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, NULL);
477 run (void *cls, char *const *args, const char *cfgfile,
478 const struct GNUNET_CONFIGURATION_Handle *cfg)
480 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
482 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
484 ¬ify_connect, ¬ify_disconnect,
487 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
489 ¬ify_connect, ¬ify_disconnect,
494 if ((p1 == NULL) || (p2 == NULL))
496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
497 if (die_task != GNUNET_SCHEDULER_NO_TASK)
498 GNUNET_SCHEDULER_cancel (die_task);
499 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
509 static char *const argv[] = { "test-transport-api-unreliability",
511 "test_transport_api_data.conf",
517 static struct GNUNET_GETOPT_CommandLineOption options[] = {
518 GNUNET_GETOPT_OPTION_END
522 setTransportOptions ("test_transport_api_data.conf");
526 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
527 "nohelp", options, &run, &ok);
533 main (int argc, char *argv[])
538 tth = GNUNET_TRANSPORT_TESTING_init ();
540 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
541 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
543 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
545 GNUNET_log_setup (test_name,
553 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
554 (strcmp (test_plugin, "udp_nat") == 0))
556 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
557 if (GNUNET_NO == nat_res)
559 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
560 "gnunet-nat-server", "SUID not set");
563 if (GNUNET_SYSERR == nat_res)
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
566 "gnunet-nat-server", "file not found");
571 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
572 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
576 GNUNET_free (cfg_file_p1);
577 GNUNET_free (cfg_file_p2);
579 GNUNET_free (test_source);
580 GNUNET_free (test_plugin);
581 GNUNET_free (test_name);
587 /* end of test_transport_api_unreliability.c */