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;
89 static unsigned long long total_bytes;
91 static struct GNUNET_TIME_Absolute start_time;
93 static GNUNET_SCHEDULER_TaskIdentifier die_task;
96 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
98 #define OKPP do { ok++; } while (0)
105 unsigned long long delta;
107 GNUNET_SCHEDULER_cancel (sched, die_task);
108 die_task = GNUNET_SCHEDULER_NO_TASK;
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
112 GNUNET_TRANSPORT_disconnect (p1.th);
113 GNUNET_TRANSPORT_disconnect (p2.th);
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Transports disconnected, returning success!\n");
118 delta = GNUNET_TIME_absolute_get_duration (start_time).value;
120 "\nThroughput was %llu kb/s\n",
121 total_bytes * 1000 / 1024 / delta);
128 stop_arm (struct PeerContext *p)
131 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
132 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
133 GNUNET_OS_process_wait (p->arm_pid);
135 GNUNET_CONFIGURATION_destroy (p->cfg);
140 end_badly (void *cls,
141 const struct GNUNET_SCHEDULER_TaskContext *tc)
144 GNUNET_TRANSPORT_disconnect (p1.th);
145 GNUNET_TRANSPORT_disconnect (p2.th);
152 struct GNUNET_MessageHeader header;
158 get_size (unsigned int iter)
163 return iter + sizeof (struct TestMessage);
164 ret = (iter * iter * iter);
165 return sizeof (struct TestMessage) + (ret % 60000);
170 notify_receive (void *cls,
171 const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_MessageHeader *message,
173 struct GNUNET_TIME_Relative latency,
178 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
179 const struct TestMessage *hdr;
181 hdr = (const struct TestMessage*) message;
183 if (MTYPE != ntohs (message->type))
185 if (ntohs (message->size) != s)
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 "Expected message %u of size %u, got %u bytes of message %u\n",
190 ntohs (message->size),
192 GNUNET_SCHEDULER_cancel (sched, die_task);
193 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
196 if (ntohl (hdr->num) != n)
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199 "Expected message %u of size %u, got %u bytes of message %u\n",
201 ntohs (message->size),
203 GNUNET_SCHEDULER_cancel (sched, die_task);
204 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
207 memset (cbuf, n, s - sizeof (struct TestMessage));
208 if (0 != memcmp (cbuf,
210 s - sizeof (struct TestMessage)))
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Expected message %u with bits %u, but body did not match\n",
214 n, (unsigned char) n);
215 GNUNET_SCHEDULER_cancel (sched, die_task);
216 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
220 if (ntohl(hdr->num) % 5000 == 0)
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Got message %u of size %u\n",
225 ntohs (message->size));
229 if (0 == (n % (TOTAL_MSGS/100)))
231 fprintf (stderr, ".");
232 GNUNET_SCHEDULER_cancel (sched, die_task);
233 die_task = GNUNET_SCHEDULER_add_delayed (sched,
244 notify_ready (void *cls, size_t size, void *buf)
248 struct TestMessage hdr;
260 GNUNET_assert (size >= s);
261 GNUNET_assert (buf != NULL);
265 hdr.header.size = htons (s);
266 hdr.header.type = htons (MTYPE);
268 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
269 ret += sizeof (struct TestMessage);
270 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
271 ret += s - sizeof (struct TestMessage);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Sending message %u of size %u\n",
283 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
284 break; /* sometimes pack buffer full, sometimes not */
286 while (size - ret >= s);
288 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Returning total message block of size %u\n",
305 notify_connect (void *cls,
306 const struct GNUNET_PeerIdentity *peer,
307 struct GNUNET_TIME_Relative latency,
312 GNUNET_TRANSPORT_set_quota (p1.th,
314 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
315 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
316 GNUNET_TIME_UNIT_FOREVER_REL,
318 start_time = GNUNET_TIME_absolute_get ();
323 GNUNET_TRANSPORT_set_quota (p2.th,
325 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
326 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
327 GNUNET_TIME_UNIT_FOREVER_REL,
334 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
336 get_size (0), 0, TIMEOUT,
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
348 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Peer `%4s' disconnected (%p)!\n",
353 GNUNET_i2s (peer), cls);
359 setup_peer (struct PeerContext *p, const char *cfgname)
361 p->cfg = GNUNET_CONFIGURATION_create ();
363 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
364 "gnunet-service-arm",
365 "gnunet-service-arm",
369 "-c", cfgname, NULL);
371 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
372 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
377 GNUNET_assert (p->th != NULL);
382 exchange_hello_last (void *cls,
383 const struct GNUNET_MessageHeader *message)
385 struct PeerContext *me = cls;
387 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Exchanging HELLO with peer (%p)!\n", cls);
392 GNUNET_assert (ok >= 3);
394 GNUNET_assert (message != NULL);
395 GNUNET_assert (GNUNET_OK ==
396 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
398 /* both HELLOs exchanged, get ready to test transmission! */
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Finished exchanging HELLOs, now waiting for transmission!\n");
405 exchange_hello (void *cls,
406 const struct GNUNET_MessageHeader *message)
408 struct PeerContext *me = cls;
410 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Exchanging HELLO with peer (%p)!\n", cls);
415 GNUNET_assert (ok >= 2);
417 GNUNET_assert (message != NULL);
418 GNUNET_assert (GNUNET_OK ==
419 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Received HELLO size %d\n",
425 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
427 GNUNET_TRANSPORT_offer_hello (p2.th, message);
428 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
434 struct GNUNET_SCHEDULER_Handle *s,
436 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
438 GNUNET_assert (ok == 1);
441 die_task = GNUNET_SCHEDULER_add_delayed (sched,
447 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
448 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
452 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
453 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
457 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
458 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
462 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
463 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
467 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
468 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
472 GNUNET_assert(p1.th != NULL);
473 GNUNET_assert(p2.th != NULL);
474 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
481 char *const argv[] = { "test-transport-api-reliability",
483 "test_transport_api_data.conf",
489 struct GNUNET_GETOPT_CommandLineOption options[] = {
490 GNUNET_GETOPT_OPTION_END
494 setTransportOptions("test_transport_api_data.conf");
497 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
498 argv, "test-transport-api-reliability", "nohelp",
507 main (int argc, char *argv[])
511 return GNUNET_SYSERR;
513 if (strstr(argv[0], "tcp_nat") != NULL)
515 is_tcp_nat = GNUNET_YES;
517 else if (strstr(argv[0], "tcp") != NULL)
521 else if (strstr(argv[0], "https") != NULL)
523 is_https = GNUNET_YES;
525 else if (strstr(argv[0], "http") != NULL)
527 is_http = GNUNET_YES;
529 else if (strstr(argv[0], "udp") != NULL)
533 GNUNET_log_setup ("test-transport-api-reliability",
541 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
542 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
546 /* end of test_transport_api_reliability.c */