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_sending;
111 static int test_send_timeout;
113 static unsigned long long total_bytes;
115 static struct GNUNET_TIME_Absolute start_time;
117 static char bitmap[TOTAL_MSGS / 8];
119 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
122 * END Testcase specific declarations
126 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
128 #define OKPP do { ok++; } while (0)
132 get_bit (const char *map, unsigned int bit);
137 unsigned long long delta;
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
143 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
144 fprintf (stderr, "\nThroughput was %llu kb/s\n",
145 total_bytes * 1000 / 1024 / delta);
146 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
147 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
149 GNUNET_free (value_name);
151 if (die_task != GNUNET_SCHEDULER_NO_TASK)
152 GNUNET_SCHEDULER_cancel (die_task);
155 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
159 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
162 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
163 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
165 GNUNET_TRANSPORT_TESTING_done (tth);
171 for (i = 0; i < TOTAL_MSGS; i++)
173 if (get_bit (bitmap, i) == 0)
175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
184 die_task = GNUNET_SCHEDULER_NO_TASK;
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
187 if (test_connected == GNUNET_YES)
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
192 if (test_sending == GNUNET_NO)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase did not send any messages timeout\n");
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
197 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
198 if (test_send_timeout == GNUNET_YES)
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test had timeout while waiting to send data\n");
203 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
207 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
211 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
213 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
215 GNUNET_TRANSPORT_TESTING_done (tth);
222 get_size (unsigned int iter)
226 ret = (iter * iter * iter);
227 return sizeof (struct TestMessage) + (ret % 60000);
232 * Sets a bit active in the bitmap.
234 * @param bitIdx which bit to set
237 set_bit (unsigned int bitIdx)
240 unsigned int targetBit;
242 if (bitIdx >= sizeof (bitmap) * 8)
244 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
245 bitIdx, sizeof (bitmap) * 8);
248 arraySlot = bitIdx / 8;
249 targetBit = (1L << (bitIdx % 8));
250 bitmap[arraySlot] |= targetBit;
254 * Obtain a bit from bitmap.
255 * @param map the bitmap
256 * @param bit index from bitmap
258 * @return Bit \a bit from hashcode \a code
261 get_bit (const char *map, unsigned int bit)
263 if (bit >= TOTAL_MSGS)
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
266 sizeof (bitmap) * 8);
269 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
274 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
275 const struct GNUNET_MessageHeader *message,
276 const struct GNUNET_ATS_Information *ats,
282 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
283 const struct TestMessage *hdr;
285 hdr = (const struct TestMessage *) message;
287 if (MTYPE != ntohs (message->type))
289 msg_recv_expected = n;
290 msg_recv = ntohl (hdr->num);
291 s = get_size (ntohl (hdr->num));
293 if (ntohs (message->size) != s)
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 "Expected message %u of size %u, got %u bytes of message %u\n",
297 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
298 if (GNUNET_SCHEDULER_NO_TASK != die_task)
299 GNUNET_SCHEDULER_cancel (die_task);
300 test_sending = GNUNET_YES;
301 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
305 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
306 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "Expected message %u with bits %u, but body did not match\n",
310 ntohl (hdr->num), (unsigned char) n);
311 if (GNUNET_SCHEDULER_NO_TASK != die_task)
312 GNUNET_SCHEDULER_cancel (die_task);
313 test_sending = GNUNET_YES;
314 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
318 if (ntohl (hdr->num) % 5 == 0)
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
321 ntohl (hdr->num), ntohs (message->size));
325 set_bit (ntohl (hdr->num));
326 if (0 == (n % (TOTAL_MSGS / 100)))
328 fprintf (stderr, ".");
329 if (GNUNET_SCHEDULER_NO_TASK != die_task)
330 GNUNET_SCHEDULER_cancel (die_task);
331 test_sending = GNUNET_YES;
332 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
342 notify_ready (void *cls, size_t size, void *buf)
346 struct TestMessage hdr;
354 test_send_timeout = GNUNET_YES;
355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
356 "Timeout occurred while waiting for transmit_ready for msg %u of %u\n",
357 msg_scheduled, TOTAL_MSGS);
358 if (GNUNET_SCHEDULER_NO_TASK != die_task)
359 GNUNET_SCHEDULER_cancel (die_task);
360 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
366 GNUNET_assert (size >= s);
367 GNUNET_assert (buf != NULL);
371 hdr.header.size = htons (s);
372 hdr.header.type = htons (MTYPE);
375 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
376 ret += sizeof (struct TestMessage);
377 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
378 ret += s - sizeof (struct TestMessage);
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
389 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
390 break; /* sometimes pack buffer full, sometimes not */
392 while (size - ret >= s);
395 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
397 ¬ify_ready, NULL);
402 fprintf (stderr, "\n");
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "All messages scheduled to be sent\n");
405 if (GNUNET_SCHEDULER_NO_TASK != die_task)
406 GNUNET_SCHEDULER_cancel (die_task);
407 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Returning total message block of size %u\n", ret);
420 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
421 const struct GNUNET_ATS_Information *ats,
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
426 GNUNET_i2s (peer), cls);
431 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
434 GNUNET_i2s (peer), cls);
436 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
443 start_time = GNUNET_TIME_absolute_get ();
444 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
445 TIMEOUT_TRANSMIT, ¬ify_ready,
450 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
452 char *p1_c = strdup (GNUNET_i2s (&p1->id));
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
454 GNUNET_i2s (&p2->id));
457 test_connected = GNUNET_YES;
460 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
463 void start_cb (struct PeerContext * p,
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
471 GNUNET_i2s (&p->id));
476 test_connected = GNUNET_NO;
477 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, NULL);
482 run (void *cls, char *const *args, const char *cfgfile,
483 const struct GNUNET_CONFIGURATION_Handle *cfg)
485 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
486 test_send_timeout = GNUNET_NO;
489 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
491 ¬ify_connect, ¬ify_disconnect,
494 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
496 ¬ify_connect, ¬ify_disconnect,
501 if ((p1 == NULL) || (p2 == NULL))
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
504 if (die_task != GNUNET_SCHEDULER_NO_TASK)
505 GNUNET_SCHEDULER_cancel (die_task);
506 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
516 static char *const argv[] = { "test-transport-api-unreliability",
518 "test_transport_api_data.conf",
524 static struct GNUNET_GETOPT_CommandLineOption options[] = {
525 GNUNET_GETOPT_OPTION_END
529 setTransportOptions ("test_transport_api_data.conf");
533 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
534 "nohelp", options, &run, &ok);
540 main (int argc, char *argv[])
545 tth = GNUNET_TRANSPORT_TESTING_init ();
547 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
548 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
550 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
552 GNUNET_log_setup (test_name,
560 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
561 (strcmp (test_plugin, "udp_nat") == 0))
563 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
564 if (GNUNET_NO == nat_res)
566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
567 "gnunet-nat-server", "SUID not set");
570 if (GNUNET_SYSERR == nat_res)
572 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
573 "gnunet-nat-server", "file not found");
578 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
579 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
583 GNUNET_free (cfg_file_p1);
584 GNUNET_free (cfg_file_p2);
586 GNUNET_free (test_source);
587 GNUNET_free (test_plugin);
588 GNUNET_free (test_name);
594 /* end of test_transport_api_unreliability.c */