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_NO
46 #define START_ARM GNUNET_YES
49 * How long until we give up on transmitting the message?
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
55 static GNUNET_SCHEDULER_TaskIdentifier die_task;
57 struct PeerContext *p1;
59 struct PeerContext *p2;
61 struct GNUNET_TRANSPORT_TransmitHandle *th;
68 * Testcase specific declarations
72 * Note that this value must not significantly exceed
73 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
74 * messages may be dropped even for a reliable transport.
76 #define TOTAL_MSGS (1024 * 3)
82 struct GNUNET_MessageHeader header;
86 static char *test_name;
88 static int msg_scheduled;
90 static int msg_recv_expected;
93 static int test_failed;
95 static unsigned long long total_bytes;
97 static struct GNUNET_TIME_Absolute start_time;
99 static char bitmap[TOTAL_MSGS / 8];
102 * END Testcase specific declarations
106 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
108 #define OKPP do { ok++; } while (0)
112 get_bit (const char *map, unsigned int bit);
117 unsigned long long delta;
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
123 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
124 fprintf (stderr, "\nThroughput was %llu kb/s\n",
125 total_bytes * 1000 / 1024 / delta);
126 GNUNET_asprintf (&value_name, "unreliable_%s", test_name);
127 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
129 GNUNET_free (value_name);
131 if (die_task != GNUNET_SCHEDULER_NO_TASK)
132 GNUNET_SCHEDULER_cancel (die_task);
135 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
138 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
139 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
145 for (i = 0; i < TOTAL_MSGS; i++)
147 if (get_bit (bitmap, i) == 0)
149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
158 die_task = GNUNET_SCHEDULER_NO_TASK;
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
161 if (test_failed == GNUNET_NO)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
166 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
169 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
173 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
175 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
182 get_size (unsigned int iter)
186 ret = (iter * iter * iter);
187 return sizeof (struct TestMessage) + (ret % 60000);
192 * Sets a bit active in the bitmap.
194 * @param bitIdx which bit to set
197 set_bit (unsigned int bitIdx)
200 unsigned int targetBit;
202 if (bitIdx >= sizeof (bitmap) * 8)
204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
205 bitIdx, sizeof (bitmap) * 8);
208 arraySlot = bitIdx / 8;
209 targetBit = (1L << (bitIdx % 8));
210 bitmap[arraySlot] |= targetBit;
214 * Obtain a bit from bitmap.
215 * @param map the bitmap
216 * @param bit index from bitmap
218 * @return Bit \a bit from hashcode \a code
221 get_bit (const char *map, unsigned int bit)
223 if (bit >= TOTAL_MSGS)
225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
226 sizeof (bitmap) * 8);
229 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
234 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
235 const struct GNUNET_MessageHeader *message,
236 const struct GNUNET_TRANSPORT_ATS_Information *ats,
242 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
243 const struct TestMessage *hdr;
245 hdr = (const struct TestMessage *) message;
247 if (MTYPE != ntohs (message->type))
249 msg_recv_expected = n;
250 msg_recv = ntohl (hdr->num);
251 s = get_size (ntohl (hdr->num));
253 if (ntohs (message->size) != s)
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "Expected message %u of size %u, got %u bytes of message %u\n",
257 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
258 if (GNUNET_SCHEDULER_NO_TASK != die_task)
259 GNUNET_SCHEDULER_cancel (die_task);
260 test_failed = GNUNET_YES;
261 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
265 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
266 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "Expected message %u with bits %u, but body did not match\n",
270 ntohl (hdr->num), (unsigned char) n);
271 if (GNUNET_SCHEDULER_NO_TASK != die_task)
272 GNUNET_SCHEDULER_cancel (die_task);
273 test_failed = GNUNET_YES;
274 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
278 if (ntohl (hdr->num) % 5 == 0)
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
281 ntohl (hdr->num), ntohs (message->size));
285 set_bit (ntohl (hdr->num));
286 if (0 == (n % (TOTAL_MSGS / 100)))
288 fprintf (stderr, ".");
289 if (GNUNET_SCHEDULER_NO_TASK != die_task)
290 GNUNET_SCHEDULER_cancel (die_task);
291 test_failed = GNUNET_YES;
292 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
300 notify_ready (void *cls, size_t size, void *buf)
304 struct TestMessage hdr;
318 GNUNET_assert (size >= s);
319 GNUNET_assert (buf != NULL);
323 hdr.header.size = htons (s);
324 hdr.header.type = htons (MTYPE);
327 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
328 ret += sizeof (struct TestMessage);
329 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
330 ret += s - sizeof (struct TestMessage);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
341 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
342 break; /* sometimes pack buffer full, sometimes not */
344 while (size - ret >= s);
347 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0, TIMEOUT,
348 ¬ify_ready, NULL);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "All messages scheduled to be sent!!\n");
355 if (GNUNET_SCHEDULER_NO_TASK != die_task)
356 GNUNET_SCHEDULER_cancel (die_task);
357 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Returning total message block of size %u\n", ret);
370 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
371 const struct GNUNET_TRANSPORT_ATS_Information *ats,
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
376 GNUNET_i2s (peer), cls);
380 GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
381 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
383 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
388 GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
389 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
391 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
398 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
401 GNUNET_i2s (peer), cls);
407 start_time = GNUNET_TIME_absolute_get ();
408 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
409 TIMEOUT, ¬ify_ready, NULL);
413 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
415 char *p1_c = strdup (GNUNET_i2s (&p1->id));
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
418 GNUNET_i2s (&p2->id));
421 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
422 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
426 run (void *cls, char *const *args, const char *cfgfile,
427 const struct GNUNET_CONFIGURATION_Handle *cfg)
429 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
431 p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, ¬ify_receive,
432 ¬ify_connect, ¬ify_disconnect,
434 p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, ¬ify_receive,
435 ¬ify_connect, ¬ify_disconnect,
438 GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
444 static char *const argv[] = { "test-transport-api-unreliability",
446 "test_transport_api_data.conf",
452 static struct GNUNET_GETOPT_CommandLineOption options[] = {
453 GNUNET_GETOPT_OPTION_END
457 setTransportOptions ("test_transport_api_data.conf");
461 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
462 "test-transport-api-unreliability", "nohelp", options,
469 main (int argc, char *argv[])
474 GNUNET_log_setup ("test-transport-api-unreliability",
482 char *pch = strdup (argv[0]);
484 char *filename = NULL;
486 char *src_name = strdup (__FILE__);
489 /* get executable filename */
490 pch = strtok (pch, "/");
493 pch = strtok (NULL, "/");
498 filename = strstr (filename, "tes");
499 if (NULL != (dotexe = strstr (filename, ".exe")))
502 /* create cfg filename */
503 GNUNET_asprintf (&cfg_file_p1, "%s_peer1.conf", filename);
504 GNUNET_asprintf (&cfg_file_p2, "%s_peer2.conf", filename);
506 split = strstr (src_name, ".");
510 test_name = strdup (&filename[strlen (src_name) + 1]);
515 GNUNET_free (src_name);
516 GNUNET_free (backup);
518 if ((strstr (argv[0], "tcp_nat") != NULL) ||
519 (strstr (argv[0], "udp_nat") != NULL))
521 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
522 if (GNUNET_NO == nat_res)
524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
525 "gnunet-nat-server", "SUID not set");
528 if (GNUNET_SYSERR == nat_res)
530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
531 "gnunet-nat-server", "file not found");
537 GNUNET_free (cfg_file_p1);
538 GNUNET_free (cfg_file_p2);
539 GNUNET_free_non_null (test_name);
544 /* end of test_transport_api_unreliability.c */