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 2, 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_transport_service.h"
36 #include "transport.h"
38 #define VERBOSE GNUNET_NO
40 #define VERBOSE_ARM GNUNET_NO
42 #define START_ARM GNUNET_YES
45 * Note that this value must not significantly exceed
46 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
47 * messages may be dropped even for a reliable transport.
49 #define TOTAL_MSGS (60000 * 20)
52 * How long until we give up on transmitting the message?
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
60 struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct GNUNET_TRANSPORT_Handle *th;
62 struct GNUNET_PeerIdentity id;
68 static struct PeerContext p1;
70 static struct PeerContext p2;
72 static struct GNUNET_SCHEDULER_Handle *sched;
78 static int is_tcp_nat;
84 static unsigned long long total_bytes;
86 static struct GNUNET_TIME_Absolute start_time;
88 static GNUNET_SCHEDULER_TaskIdentifier die_task;
91 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
93 #define OKPP do { ok++; } while (0)
100 unsigned long long delta;
102 GNUNET_SCHEDULER_cancel (sched, die_task);
103 die_task = GNUNET_SCHEDULER_NO_TASK;
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
107 GNUNET_TRANSPORT_disconnect (p1.th);
108 GNUNET_TRANSPORT_disconnect (p2.th);
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Transports disconnected, returning success!\n");
113 delta = GNUNET_TIME_absolute_get_duration (start_time).value;
115 "\nThroughput was %llu kb/s\n",
116 total_bytes * 1000 / 1024 / delta);
122 stop_arm (struct PeerContext *p)
125 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
127 GNUNET_OS_process_wait (p->arm_pid);
129 GNUNET_CONFIGURATION_destroy (p->cfg);
134 end_badly (void *cls,
135 const struct GNUNET_SCHEDULER_TaskContext *tc)
138 GNUNET_TRANSPORT_disconnect (p1.th);
139 GNUNET_TRANSPORT_disconnect (p2.th);
146 struct GNUNET_MessageHeader header;
152 get_size (unsigned int iter)
157 return iter + sizeof (struct TestMessage);
158 ret = (iter * iter * iter);
159 return sizeof (struct TestMessage) + (ret % 60000);
164 notify_receive (void *cls,
165 const struct GNUNET_PeerIdentity *peer,
166 const struct GNUNET_MessageHeader *message,
167 struct GNUNET_TIME_Relative latency,
172 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
173 const struct TestMessage *hdr;
175 hdr = (const struct TestMessage*) message;
177 if (MTYPE != ntohs (message->type))
179 if (ntohs (message->size) != s)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Expected message %u of size %u, got %u bytes of message %u\n",
184 ntohs (message->size),
186 GNUNET_SCHEDULER_cancel (sched, die_task);
187 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
190 if (ntohl (hdr->num) != n)
192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 "Expected message %u of size %u, got %u bytes of message %u\n",
195 ntohs (message->size),
197 GNUNET_SCHEDULER_cancel (sched, die_task);
198 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
201 memset (cbuf, n, s - sizeof (struct TestMessage));
202 if (0 != memcmp (cbuf,
204 s - sizeof (struct TestMessage)))
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 "Expected message %u with bits %u, but body did not match\n",
208 n, (unsigned char) n);
209 GNUNET_SCHEDULER_cancel (sched, die_task);
210 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
214 if (ntohl(hdr->num) % 5000 == 0)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Got message %u of size %u\n",
219 ntohs (message->size));
223 if (0 == (n % (TOTAL_MSGS/100)))
225 fprintf (stderr, ".");
226 GNUNET_SCHEDULER_cancel (sched, die_task);
227 die_task = GNUNET_SCHEDULER_add_delayed (sched,
238 notify_ready (void *cls, size_t size, void *buf)
242 struct TestMessage hdr;
254 GNUNET_assert (size >= s);
255 GNUNET_assert (buf != NULL);
259 hdr.header.size = htons (s);
260 hdr.header.type = htons (MTYPE);
262 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
263 ret += sizeof (struct TestMessage);
264 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
265 ret += s - sizeof (struct TestMessage);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Sending message %u of size %u\n",
277 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
278 break; /* sometimes pack buffer full, sometimes not */
280 while (size - ret >= s);
282 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Returning total message block of size %u\n",
299 notify_connect (void *cls,
300 const struct GNUNET_PeerIdentity *peer,
301 struct GNUNET_TIME_Relative latency,
306 GNUNET_TRANSPORT_set_quota (p1.th,
308 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
309 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
310 GNUNET_TIME_UNIT_FOREVER_REL,
312 start_time = GNUNET_TIME_absolute_get ();
317 GNUNET_TRANSPORT_set_quota (p2.th,
319 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
320 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
321 GNUNET_TIME_UNIT_FOREVER_REL,
328 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
330 get_size (0), 0, TIMEOUT,
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
342 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Peer `%4s' disconnected (%p)!\n",
347 GNUNET_i2s (peer), cls);
353 setup_peer (struct PeerContext *p, const char *cfgname)
355 p->cfg = GNUNET_CONFIGURATION_create ();
357 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
358 "gnunet-service-arm",
359 "gnunet-service-arm",
363 "-c", cfgname, NULL);
365 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
366 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
371 GNUNET_assert (p->th != NULL);
376 exchange_hello_last (void *cls,
377 const struct GNUNET_MessageHeader *message)
379 struct PeerContext *me = cls;
381 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Exchanging HELLO with peer (%p)!\n", cls);
386 GNUNET_assert (ok >= 3);
388 GNUNET_assert (message != NULL);
389 GNUNET_assert (GNUNET_OK ==
390 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
392 /* both HELLOs exchanged, get ready to test transmission! */
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Finished exchanging HELLOs, now waiting for transmission!\n");
399 exchange_hello (void *cls,
400 const struct GNUNET_MessageHeader *message)
402 struct PeerContext *me = cls;
404 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Exchanging HELLO with peer (%p)!\n", cls);
409 GNUNET_assert (ok >= 2);
411 GNUNET_assert (message != NULL);
412 GNUNET_assert (GNUNET_OK ==
413 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Received HELLO size %d\n",
419 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
421 GNUNET_TRANSPORT_offer_hello (p2.th, message);
422 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
428 struct GNUNET_SCHEDULER_Handle *s,
430 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
432 GNUNET_assert (ok == 1);
435 die_task = GNUNET_SCHEDULER_add_delayed (sched,
441 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
442 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
446 setup_peer (&p1, "test_transport_api_http_peer1.conf");
447 setup_peer (&p2, "test_transport_api_http_peer2.conf");
451 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
452 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
456 GNUNET_assert(p1.th != NULL);
457 GNUNET_assert(p2.th != NULL);
458 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
465 char *const argv[] = { "test-transport-api-reliability",
467 "test_transport_api_data.conf",
473 struct GNUNET_GETOPT_CommandLineOption options[] = {
474 GNUNET_GETOPT_OPTION_END
478 setTransportOptions("test_transport_api_data.conf");
481 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
482 argv, "test-transport-api-reliability", "nohelp",
491 main (int argc, char *argv[])
495 return GNUNET_SYSERR;
497 if (strstr(argv[0], "tcp_nat") != NULL)
499 is_tcp_nat = GNUNET_YES;
501 else if (strstr(argv[0], "tcp") != NULL)
505 else if (strstr(argv[0], "http") != NULL)
507 is_http = GNUNET_YES;
509 GNUNET_log_setup ("test-transport-api-reliability",
517 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
518 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
522 /* end of test_transport_api_reliability.c */