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_constant.c
22 * @brief test case for transports; ensures messages get
23 * through, regardless of order, constant packet size
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;
74 struct GNUNET_TRANSPORT_TESTING_handle * tth;
83 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
86 * Testcase specific declarations
90 * Note that this value must not significantly exceed
91 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
92 * messages may be dropped even for a reliable transport.
94 #define TOTAL_MSGS (1024 * 3)
98 #define MSG_SIZE 10000
102 struct GNUNET_MessageHeader header;
106 static char *test_name;
108 static int msg_scheduled;
110 static int msg_recv_expected;
113 static int test_failed;
115 static unsigned long long total_bytes;
117 static struct GNUNET_TIME_Absolute start_time;
120 * END Testcase specific declarations
124 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
126 #define OKPP do { ok++; } while (0)
130 get_bit (const char *map, unsigned int bit);
135 unsigned long long delta;
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
141 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
142 fprintf (stderr, "\nThroughput was %llu kb/s\n",
143 total_bytes * 1000 / 1024 / delta);
144 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
145 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
147 GNUNET_free (value_name);
149 if (die_task != GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_cancel (die_task);
153 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
157 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
160 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
161 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
163 GNUNET_TRANSPORT_TESTING_done (tth);
166 if (test_failed == GNUNET_NO)
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GOT %u of %u messages\n", msg_recv, TOTAL_MSGS);
174 die_task = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
177 if (test_failed == GNUNET_NO)
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
181 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
182 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
185 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
189 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
193 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
195 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
197 GNUNET_TRANSPORT_TESTING_done (tth);
204 get_size (unsigned int iter)
208 ret = (iter * iter * iter);
209 return sizeof (struct TestMessage) + (ret % 60000);
218 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
219 const struct GNUNET_MessageHeader *message,
220 const struct GNUNET_ATS_Information *ats,
226 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
227 const struct TestMessage *hdr;
229 hdr = (const struct TestMessage *) message;
231 if (MTYPE != ntohs (message->type))
233 msg_recv_expected = n;
234 msg_recv = ntohl (hdr->num);
235 if (msg_recv_expected != msg_recv)
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Expected message no %u, got %u\n",
239 msg_recv_expected, msg_recv);
240 if (GNUNET_SCHEDULER_NO_TASK != die_task)
241 GNUNET_SCHEDULER_cancel (die_task);
242 test_failed = GNUNET_YES;
243 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
247 s = get_size (ntohl (hdr->num));
248 if (ntohs (message->size) != s)
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 "Expected message %u of size %u, got %u bytes of message %u\n",
252 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
253 if (GNUNET_SCHEDULER_NO_TASK != die_task)
254 GNUNET_SCHEDULER_cancel (die_task);
255 test_failed = GNUNET_YES;
256 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
260 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
261 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "Expected message %u with bits %u, but body did not match\n",
265 ntohl (hdr->num), (unsigned char) n);
266 if (GNUNET_SCHEDULER_NO_TASK != die_task)
267 GNUNET_SCHEDULER_cancel (die_task);
268 test_failed = GNUNET_YES;
269 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
273 if (ntohl (hdr->num) % 5 == 0)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
276 ntohl (hdr->num), ntohs (message->size));
280 if (0 == (n % (TOTAL_MSGS / 100)))
282 fprintf (stderr, ".");
283 if (GNUNET_SCHEDULER_NO_TASK != die_task)
284 GNUNET_SCHEDULER_cancel (die_task);
285 test_failed = GNUNET_YES;
286 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
290 /* because of starting with 0 */
292 fprintf (stderr, "\n");
299 notify_ready (void *cls, size_t size, void *buf)
303 struct TestMessage hdr;
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 "Timeout occurred while waiting for transmit_ready\n");
313 if (GNUNET_SCHEDULER_NO_TASK != die_task)
314 GNUNET_SCHEDULER_cancel (die_task);
315 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
321 GNUNET_assert (size >= s);
322 GNUNET_assert (buf != NULL);
326 hdr.header.size = htons (s);
327 hdr.header.type = htons (MTYPE);
330 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
331 ret += sizeof (struct TestMessage);
332 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
333 ret += s - sizeof (struct TestMessage);
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
344 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
345 break; /* sometimes pack buffer full, sometimes not */
347 while (size - ret >= s);
350 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
352 ¬ify_ready, NULL);
357 fprintf (stderr, "\n");
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "All messages scheduled to be sent!!\n");
360 if (GNUNET_SCHEDULER_NO_TASK != die_task)
361 GNUNET_SCHEDULER_cancel (die_task);
362 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Returning total message block of size %u\n", ret);
375 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
376 const struct GNUNET_ATS_Information *ats,
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
381 GNUNET_i2s (peer), cls);
386 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
389 GNUNET_i2s (peer), cls);
391 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
398 start_time = GNUNET_TIME_absolute_get ();
399 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
400 TIMEOUT_TRANSMIT, ¬ify_ready,
405 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
407 char *p1_c = strdup (GNUNET_i2s (&p1->id));
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
410 GNUNET_i2s (&p2->id));
415 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
418 void start_cb (struct PeerContext * p,
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
426 GNUNET_i2s (&p->id));
431 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, NULL);
435 run (void *cls, char *const *args, const char *cfgfile,
436 const struct GNUNET_CONFIGURATION_Handle *cfg)
438 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
440 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
442 ¬ify_connect, ¬ify_disconnect,
445 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
447 ¬ify_connect, ¬ify_disconnect,
451 if ((p1 == NULL) || (p2 == NULL))
453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
454 if (die_task != GNUNET_SCHEDULER_NO_TASK)
455 GNUNET_SCHEDULER_cancel (die_task);
456 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
464 static char *const argv[] = { "test-transport-api-unreliability-constant",
466 "test_transport_api_data.conf",
472 static struct GNUNET_GETOPT_CommandLineOption options[] = {
473 GNUNET_GETOPT_OPTION_END
477 setTransportOptions ("test_transport_api_data.conf");
481 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
482 "nohelp", options, &run, &ok);
488 main (int argc, char *argv[])
493 tth = GNUNET_TRANSPORT_TESTING_init ();
495 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
496 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
498 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
500 GNUNET_log_setup (test_name,
508 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
509 (strcmp (test_plugin, "udp_nat") == 0))
511 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
512 if (GNUNET_NO == nat_res)
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
515 "gnunet-nat-server", "SUID not set");
518 if (GNUNET_SYSERR == nat_res)
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
521 "gnunet-nat-server", "file not found");
526 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
527 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
531 GNUNET_free (cfg_file_p1);
532 GNUNET_free (cfg_file_p2);
534 GNUNET_free (test_source);
535 GNUNET_free (test_plugin);
536 GNUNET_free (test_name);
541 /* end of test_transport_api_unreliability_constant.c */