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"
38 #include "transport.h"
39 #include "transport-testing.h"
41 #define VERBOSE GNUNET_YES
43 #define VERBOSE_ARM GNUNET_NO
45 #define START_ARM GNUNET_YES
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
52 static char *test_source;
54 static char *test_plugin;
56 static char *test_name;
60 static GNUNET_SCHEDULER_TaskIdentifier die_task;
62 struct PeerContext *p1;
64 struct PeerContext *p2;
66 struct GNUNET_TRANSPORT_TransmitHandle *th;
73 * Testcase specific declarations
77 * Note that this value must not significantly exceed
78 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
79 * messages may be dropped even for a reliable transport.
81 #define TOTAL_MSGS (1024 * 2)
87 struct GNUNET_MessageHeader header;
91 static int msg_scheduled;
93 static int msg_recv_expected;
96 static int test_failed;
98 static unsigned long long total_bytes;
100 static struct GNUNET_TIME_Absolute start_time;
103 * END Testcase specific declarations
107 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
109 #define OKPP do { ok++; } while (0)
116 unsigned long long delta;
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
122 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
123 fprintf (stderr, "\nThroughput was %llu kb/s\n",
124 total_bytes * 1000 / 1024 / delta);
125 GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
126 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
128 GNUNET_free (value_name);
130 if (die_task != GNUNET_SCHEDULER_NO_TASK)
131 GNUNET_SCHEDULER_cancel (die_task);
134 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
137 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
138 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
144 die_task = GNUNET_SCHEDULER_NO_TASK;
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
148 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
152 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
154 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
161 get_size (unsigned int iter)
165 ret = (iter * iter * iter);
166 return sizeof (struct TestMessage) + (ret % 60000);
171 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_MessageHeader *message,
173 const struct GNUNET_TRANSPORT_ATS_Information *ats,
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 msg_recv_expected = n;
186 msg_recv = ntohl (hdr->num);
187 if (ntohs (message->size) != (s))
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "Expected message %u of size %u, got %u bytes of message %u\n",
191 n, s, ntohs (message->size), ntohl (hdr->num));
192 if (die_task != GNUNET_SCHEDULER_NO_TASK)
193 GNUNET_SCHEDULER_cancel (die_task);
194 test_failed = GNUNET_YES;
195 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
198 if (ntohl (hdr->num) != n)
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 "Expected message %u of size %u, got %u bytes of message %u\n",
202 n, s, ntohs (message->size), ntohl (hdr->num));
203 if (die_task != GNUNET_SCHEDULER_NO_TASK)
204 GNUNET_SCHEDULER_cancel (die_task);
205 test_failed = GNUNET_YES;
206 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
209 memset (cbuf, n, s - sizeof (struct TestMessage));
210 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Expected message %u with bits %u, but body did not match\n", n,
215 if (die_task != GNUNET_SCHEDULER_NO_TASK)
216 GNUNET_SCHEDULER_cancel (die_task);
217 test_failed = GNUNET_YES;
218 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222 if (ntohl (hdr->num) % 5000 == 0)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
225 ntohl (hdr->num), ntohs (message->size));
229 if (0 == (n % (TOTAL_MSGS / 100)))
231 fprintf (stderr, ".");
232 if (die_task != GNUNET_SCHEDULER_NO_TASK)
233 GNUNET_SCHEDULER_cancel (die_task);
234 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages received\n");
246 notify_ready (void *cls, size_t size, void *buf)
250 struct TestMessage hdr;
263 GNUNET_assert (size >= s);
264 GNUNET_assert (buf != NULL);
268 hdr.header.size = htons (s);
269 hdr.header.type = htons (MTYPE);
272 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
273 ret += sizeof (struct TestMessage);
274 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
275 ret += s - sizeof (struct TestMessage);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
285 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
286 break; /* sometimes pack buffer full, sometimes not */
288 while (size - ret >= s);
292 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
293 TIMEOUT, ¬ify_ready,
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Returning total message block of size %u\n", ret);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
312 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
313 const struct GNUNET_TRANSPORT_ATS_Information *ats,
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
318 GNUNET_i2s (peer), cls);
322 GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
323 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
325 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
330 GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
331 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
333 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
340 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
343 GNUNET_i2s (peer), cls);
349 start_time = GNUNET_TIME_absolute_get ();
350 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
351 TIMEOUT, ¬ify_ready, NULL);
355 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
357 char *p1_c = strdup (GNUNET_i2s (&p1->id));
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
360 GNUNET_i2s (&p2->id));
363 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
364 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
368 run (void *cls, char *const *args, const char *cfgfile,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
371 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
373 p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, ¬ify_receive,
374 ¬ify_connect, ¬ify_disconnect,
376 p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, ¬ify_receive,
377 ¬ify_connect, ¬ify_disconnect,
380 if ((p1 == NULL) || (p2 == NULL))
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
383 if (die_task != GNUNET_SCHEDULER_NO_TASK)
384 GNUNET_SCHEDULER_cancel (die_task);
385 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
389 GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
395 static char *argv[] = { "test_transport",
397 "test_transport_api_data.conf",
403 static struct GNUNET_GETOPT_CommandLineOption options[] = {
404 GNUNET_GETOPT_OPTION_END
408 setTransportOptions ("test_transport_api_data.conf");
411 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
412 "nohelp", options, &run, &ok);
418 main (int argc, char *argv[])
423 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
424 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
426 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
428 GNUNET_log_setup (test_name,
436 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
437 (strcmp (test_plugin, "udp_nat") == 0))
439 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
440 if (GNUNET_NO == nat_res)
442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
443 "gnunet-nat-server", "SUID not set");
446 if (GNUNET_SYSERR == nat_res)
448 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
449 "gnunet-nat-server", "file not found");
454 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
455 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
459 GNUNET_free (cfg_file_p1);
460 GNUNET_free (cfg_file_p2);
462 GNUNET_free (test_source);
463 GNUNET_free (test_plugin);
464 GNUNET_free (test_name);
470 /* end of test_transport_api_reliability.c */