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_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;
85 static unsigned long long total_bytes;
87 static struct GNUNET_TIME_Absolute start_time;
89 static GNUNET_SCHEDULER_TaskIdentifier die_task;
92 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
94 #define OKPP do { ok++; } while (0)
101 unsigned long long delta;
103 GNUNET_SCHEDULER_cancel (sched, die_task);
104 die_task = GNUNET_SCHEDULER_NO_TASK;
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
108 GNUNET_TRANSPORT_disconnect (p1.th);
109 GNUNET_TRANSPORT_disconnect (p2.th);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Transports disconnected, returning success!\n");
114 delta = GNUNET_TIME_absolute_get_duration (start_time).value;
116 "\nThroughput was %llu kb/s\n",
117 total_bytes * 1000 / 1024 / delta);
123 stop_arm (struct PeerContext *p)
126 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
127 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
128 GNUNET_OS_process_wait (p->arm_pid);
130 GNUNET_CONFIGURATION_destroy (p->cfg);
135 end_badly (void *cls,
136 const struct GNUNET_SCHEDULER_TaskContext *tc)
139 GNUNET_TRANSPORT_disconnect (p1.th);
140 GNUNET_TRANSPORT_disconnect (p2.th);
147 struct GNUNET_MessageHeader header;
153 get_size (unsigned int iter)
158 return iter + sizeof (struct TestMessage);
159 ret = (iter * iter * iter);
160 return sizeof (struct TestMessage) + (ret % 60000);
165 notify_receive (void *cls,
166 const struct GNUNET_PeerIdentity *peer,
167 const struct GNUNET_MessageHeader *message,
168 struct GNUNET_TIME_Relative latency,
173 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
174 const struct TestMessage *hdr;
176 hdr = (const struct TestMessage*) message;
178 if (MTYPE != ntohs (message->type))
180 if (ntohs (message->size) != s)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Expected message %u of size %u, got %u bytes of message %u\n",
185 ntohs (message->size),
187 GNUNET_SCHEDULER_cancel (sched, die_task);
188 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
191 if (ntohl (hdr->num) != n)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 "Expected message %u of size %u, got %u bytes of message %u\n",
196 ntohs (message->size),
198 GNUNET_SCHEDULER_cancel (sched, die_task);
199 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
202 memset (cbuf, n, s - sizeof (struct TestMessage));
203 if (0 != memcmp (cbuf,
205 s - sizeof (struct TestMessage)))
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 "Expected message %u with bits %u, but body did not match\n",
209 n, (unsigned char) n);
210 GNUNET_SCHEDULER_cancel (sched, die_task);
211 die_task = GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
215 if (ntohl(hdr->num) % 5000 == 0)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Got message %u of size %u\n",
220 ntohs (message->size));
224 if (0 == (n % (TOTAL_MSGS/100)))
226 fprintf (stderr, ".");
227 GNUNET_SCHEDULER_cancel (sched, die_task);
228 die_task = GNUNET_SCHEDULER_add_delayed (sched,
239 notify_ready (void *cls, size_t size, void *buf)
243 struct TestMessage hdr;
255 GNUNET_assert (size >= s);
256 GNUNET_assert (buf != NULL);
260 hdr.header.size = htons (s);
261 hdr.header.type = htons (MTYPE);
263 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
264 ret += sizeof (struct TestMessage);
265 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
266 ret += s - sizeof (struct TestMessage);
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Sending message %u of size %u\n",
278 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
279 break; /* sometimes pack buffer full, sometimes not */
281 while (size - ret >= s);
283 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Returning total message block of size %u\n",
300 notify_connect (void *cls,
301 const struct GNUNET_PeerIdentity *peer,
302 struct GNUNET_TIME_Relative latency,
307 GNUNET_TRANSPORT_set_quota (p1.th,
309 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
310 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
311 GNUNET_TIME_UNIT_FOREVER_REL,
313 start_time = GNUNET_TIME_absolute_get ();
318 GNUNET_TRANSPORT_set_quota (p2.th,
320 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
321 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
322 GNUNET_TIME_UNIT_FOREVER_REL,
329 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
331 get_size (0), 0, TIMEOUT,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
343 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Peer `%4s' disconnected (%p)!\n",
348 GNUNET_i2s (peer), cls);
354 setup_peer (struct PeerContext *p, const char *cfgname)
356 p->cfg = GNUNET_CONFIGURATION_create ();
358 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
359 "gnunet-service-arm",
360 "gnunet-service-arm",
364 "-c", cfgname, NULL);
366 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
367 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
372 GNUNET_assert (p->th != NULL);
377 exchange_hello_last (void *cls,
378 const struct GNUNET_MessageHeader *message)
380 struct PeerContext *me = cls;
382 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Exchanging HELLO with peer (%p)!\n", cls);
387 GNUNET_assert (ok >= 3);
389 GNUNET_assert (message != NULL);
390 GNUNET_assert (GNUNET_OK ==
391 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
393 /* both HELLOs exchanged, get ready to test transmission! */
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Finished exchanging HELLOs, now waiting for transmission!\n");
400 exchange_hello (void *cls,
401 const struct GNUNET_MessageHeader *message)
403 struct PeerContext *me = cls;
405 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Exchanging HELLO with peer (%p)!\n", cls);
410 GNUNET_assert (ok >= 2);
412 GNUNET_assert (message != NULL);
413 GNUNET_assert (GNUNET_OK ==
414 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Received HELLO size %d\n",
420 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
422 GNUNET_TRANSPORT_offer_hello (p2.th, message);
423 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
429 struct GNUNET_SCHEDULER_Handle *s,
431 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
433 GNUNET_assert (ok == 1);
436 die_task = GNUNET_SCHEDULER_add_delayed (sched,
442 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
443 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
447 setup_peer (&p1, "test_transport_api_http_peer1.conf");
448 setup_peer (&p2, "test_transport_api_http_peer2.conf");
452 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
453 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
457 GNUNET_assert(p1.th != NULL);
458 GNUNET_assert(p2.th != NULL);
459 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
466 char *const argv[] = { "test-transport-api-reliability",
468 "test_transport_api_data.conf",
474 struct GNUNET_GETOPT_CommandLineOption options[] = {
475 GNUNET_GETOPT_OPTION_END
479 setTransportOptions("test_transport_api_data.conf");
482 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
483 argv, "test-transport-api-reliability", "nohelp",
492 main (int argc, char *argv[])
496 return GNUNET_SYSERR;
498 if (strstr(argv[0], "tcp_nat") != NULL)
500 is_tcp_nat = GNUNET_YES;
502 else if (strstr(argv[0], "tcp") != NULL)
506 else if (strstr(argv[0], "http") != NULL)
508 is_http = GNUNET_YES;
510 GNUNET_log_setup ("test-transport-api-reliability",
518 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
519 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
523 /* end of test_transport_api_reliability.c */