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_reliability.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp and http
25 * transport test cases to check that the transports
26 * achieve reliable message delivery.
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
39 #define VERBOSE GNUNET_NO
41 #define VERBOSE_ARM GNUNET_NO
43 #define START_ARM GNUNET_YES
46 * Note that this value must not significantly exceed
47 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48 * messages may be dropped even for a reliable transport.
50 #define TOTAL_MSGS (60000 * 2)
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
61 struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_TRANSPORT_Handle *th;
63 struct GNUNET_PeerIdentity id;
69 static struct PeerContext p1;
71 static struct PeerContext p2;
73 static struct GNUNET_SCHEDULER_Handle *sched;
79 static int is_tcp_nat;
87 static unsigned long long total_bytes;
89 static struct GNUNET_TIME_Absolute start_time;
91 static GNUNET_SCHEDULER_TaskIdentifier die_task;
94 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
96 #define OKPP do { ok++; } while (0)
103 unsigned long long delta;
105 GNUNET_SCHEDULER_cancel (sched, die_task);
106 die_task = GNUNET_SCHEDULER_NO_TASK;
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
110 GNUNET_TRANSPORT_disconnect (p1.th);
111 GNUNET_TRANSPORT_disconnect (p2.th);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Transports disconnected, returning success!\n");
116 delta = GNUNET_TIME_absolute_get_duration (start_time).value;
118 "\nThroughput was %llu kb/s\n",
119 total_bytes * 1000 / 1024 / delta);
125 stop_arm (struct PeerContext *p)
128 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
129 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
130 GNUNET_OS_process_wait (p->arm_pid);
132 GNUNET_CONFIGURATION_destroy (p->cfg);
137 end_badly (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc)
141 GNUNET_TRANSPORT_disconnect (p1.th);
142 GNUNET_TRANSPORT_disconnect (p2.th);
149 struct GNUNET_MessageHeader header;
155 get_size (unsigned int iter)
160 return iter + sizeof (struct TestMessage);
161 ret = (iter * iter * iter);
162 return sizeof (struct TestMessage) + (ret % 60000);
167 notify_receive (void *cls,
168 const struct GNUNET_PeerIdentity *peer,
169 const struct GNUNET_MessageHeader *message,
170 struct GNUNET_TIME_Relative latency,
175 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
176 const struct TestMessage *hdr;
178 hdr = (const struct TestMessage*) message;
180 if (MTYPE != ntohs (message->type))
182 if (ntohs (message->size) != s)
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Expected message %u of size %u, got %u bytes of message %u\n",
187 ntohs (message->size),
189 GNUNET_SCHEDULER_cancel (sched, die_task);
190 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
193 if (ntohl (hdr->num) != n)
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 "Expected message %u of size %u, got %u bytes of message %u\n",
198 ntohs (message->size),
200 GNUNET_SCHEDULER_cancel (sched, die_task);
201 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
204 memset (cbuf, n, s - sizeof (struct TestMessage));
205 if (0 != memcmp (cbuf,
207 s - sizeof (struct TestMessage)))
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Expected message %u with bits %u, but body did not match\n",
211 n, (unsigned char) n);
212 GNUNET_SCHEDULER_cancel (sched, die_task);
213 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
217 if (ntohl(hdr->num) % 5000 == 0)
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Got message %u of size %u\n",
222 ntohs (message->size));
226 if (0 == (n % (TOTAL_MSGS/100)))
228 fprintf (stderr, ".");
229 GNUNET_SCHEDULER_cancel (sched, die_task);
230 die_task = GNUNET_SCHEDULER_add_delayed (sched,
241 notify_ready (void *cls, size_t size, void *buf)
245 struct TestMessage hdr;
257 GNUNET_assert (size >= s);
258 GNUNET_assert (buf != NULL);
262 hdr.header.size = htons (s);
263 hdr.header.type = htons (MTYPE);
265 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
266 ret += sizeof (struct TestMessage);
267 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
268 ret += s - sizeof (struct TestMessage);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Sending message %u of size %u\n",
280 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
281 break; /* sometimes pack buffer full, sometimes not */
283 while (size - ret >= s);
285 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Returning total message block of size %u\n",
302 notify_connect (void *cls,
303 const struct GNUNET_PeerIdentity *peer,
304 struct GNUNET_TIME_Relative latency,
309 GNUNET_TRANSPORT_set_quota (p1.th,
311 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
312 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
313 GNUNET_TIME_UNIT_FOREVER_REL,
315 start_time = GNUNET_TIME_absolute_get ();
320 GNUNET_TRANSPORT_set_quota (p2.th,
322 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
323 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
324 GNUNET_TIME_UNIT_FOREVER_REL,
331 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
333 get_size (0), 0, TIMEOUT,
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
345 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Peer `%4s' disconnected (%p)!\n",
350 GNUNET_i2s (peer), cls);
356 setup_peer (struct PeerContext *p, const char *cfgname)
358 p->cfg = GNUNET_CONFIGURATION_create ();
360 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
361 "gnunet-service-arm",
362 "gnunet-service-arm",
366 "-c", cfgname, NULL);
368 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
369 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
374 GNUNET_assert (p->th != NULL);
379 exchange_hello_last (void *cls,
380 const struct GNUNET_MessageHeader *message)
382 struct PeerContext *me = cls;
384 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Exchanging HELLO with peer (%p)!\n", cls);
389 GNUNET_assert (ok >= 3);
391 GNUNET_assert (message != NULL);
392 GNUNET_assert (GNUNET_OK ==
393 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
395 /* both HELLOs exchanged, get ready to test transmission! */
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Finished exchanging HELLOs, now waiting for transmission!\n");
402 exchange_hello (void *cls,
403 const struct GNUNET_MessageHeader *message)
405 struct PeerContext *me = cls;
407 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "Exchanging HELLO with peer (%p)!\n", cls);
412 GNUNET_assert (ok >= 2);
414 GNUNET_assert (message != NULL);
415 GNUNET_assert (GNUNET_OK ==
416 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Received HELLO size %d\n",
422 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
424 GNUNET_TRANSPORT_offer_hello (p2.th, message);
425 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
431 struct GNUNET_SCHEDULER_Handle *s,
433 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
435 GNUNET_assert (ok == 1);
438 die_task = GNUNET_SCHEDULER_add_delayed (sched,
444 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
445 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
449 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
450 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
454 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
455 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
459 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
460 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
464 GNUNET_assert(p1.th != NULL);
465 GNUNET_assert(p2.th != NULL);
466 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
473 char *const argv[] = { "test-transport-api-reliability",
475 "test_transport_api_data.conf",
481 struct GNUNET_GETOPT_CommandLineOption options[] = {
482 GNUNET_GETOPT_OPTION_END
486 setTransportOptions("test_transport_api_data.conf");
489 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
490 argv, "test-transport-api-reliability", "nohelp",
499 main (int argc, char *argv[])
503 return GNUNET_SYSERR;
505 if (strstr(argv[0], "tcp_nat") != NULL)
507 is_tcp_nat = GNUNET_YES;
509 else if (strstr(argv[0], "tcp") != NULL)
513 else if (strstr(argv[0], "http") != NULL)
515 is_http = GNUNET_YES;
517 else if (strstr(argv[0], "udp") != NULL)
521 GNUNET_log_setup ("test-transport-api-reliability",
529 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
530 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
534 /* end of test_transport_api_reliability.c */