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_EXTRA_LOGGING
43 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
45 #define START_ARM GNUNET_YES
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
58 static char *test_source;
60 static char *test_plugin;
62 static char *test_name;
66 static GNUNET_SCHEDULER_TaskIdentifier die_task;
68 struct PeerContext *p1;
70 struct PeerContext *p2;
72 struct PeerContext *sender;
74 struct PeerContext *receiver;
76 struct GNUNET_TRANSPORT_TransmitHandle *th;
82 struct GNUNET_TRANSPORT_TESTING_handle *tth;
84 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
88 * Testcase specific declarations
92 * Note that this value must not significantly exceed
93 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
94 * messages may be dropped even for a reliable transport.
96 #define TOTAL_MSGS (1024 * 2)
100 GNUNET_NETWORK_STRUCT_BEGIN
104 struct GNUNET_MessageHeader header;
107 GNUNET_NETWORK_STRUCT_END
109 static int msg_scheduled;
111 static int msg_recv_expected;
114 static int test_failed;
115 static int test_connected;
117 static unsigned long long total_bytes;
119 static struct GNUNET_TIME_Absolute start_time;
122 * END Testcase specific declarations
126 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
128 #define OKPP do { ok++; } while (0)
135 unsigned long long delta;
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
141 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
142 FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
143 total_bytes * 1000 / 1024 / delta);
144 GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
145 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
147 GNUNET_free (value_name);
149 if (die_task != GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_cancel (die_task);
153 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
157 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
159 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
160 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
162 GNUNET_TRANSPORT_TESTING_done (tth);
168 die_task = GNUNET_SCHEDULER_NO_TASK;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
171 if (test_connected == GNUNET_YES)
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
177 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
181 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
184 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
186 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
188 GNUNET_TRANSPORT_TESTING_done (tth);
195 get_size (unsigned int iter)
199 ret = (iter * iter * iter);
200 return sizeof (struct TestMessage) + (ret % 60000);
205 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
206 const struct GNUNET_MessageHeader *message,
207 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
211 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
212 const struct TestMessage *hdr;
214 hdr = (const struct TestMessage *) message;
216 if (MTYPE != ntohs (message->type))
218 msg_recv_expected = n;
219 msg_recv = ntohl (hdr->num);
220 if (ntohs (message->size) != (s))
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223 "Expected message %u of size %u, got %u bytes of message %u\n",
224 n, s, ntohs (message->size), ntohl (hdr->num));
225 if (die_task != GNUNET_SCHEDULER_NO_TASK)
226 GNUNET_SCHEDULER_cancel (die_task);
227 test_failed = GNUNET_YES;
228 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231 if (ntohl (hdr->num) != n)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 "Expected message %u of size %u, got %u bytes of message %u\n",
235 n, s, ntohs (message->size), ntohl (hdr->num));
236 if (die_task != GNUNET_SCHEDULER_NO_TASK)
237 GNUNET_SCHEDULER_cancel (die_task);
238 test_failed = GNUNET_YES;
239 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
242 memset (cbuf, n, s - sizeof (struct TestMessage));
243 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Expected message %u with bits %u, but body did not match\n", n,
248 if (die_task != GNUNET_SCHEDULER_NO_TASK)
249 GNUNET_SCHEDULER_cancel (die_task);
250 test_failed = GNUNET_YES;
251 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
255 if (ntohl (hdr->num) % 5000 == 0)
257 struct PeerContext *p = cls;
258 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
262 p->no, ps, ntohl (hdr->num), ntohs (message->size),
268 if (0 == (n % (TOTAL_MSGS / 100)))
270 FPRINTF (stderr, "%s", ".");
271 if (die_task != GNUNET_SCHEDULER_NO_TASK)
272 GNUNET_SCHEDULER_cancel (die_task);
273 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nAll messages received\n");
285 notify_ready (void *cls, size_t size, void *buf)
289 struct TestMessage hdr;
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
298 msg_scheduled, TOTAL_MSGS);
299 if (GNUNET_SCHEDULER_NO_TASK != die_task)
300 GNUNET_SCHEDULER_cancel (die_task);
301 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
308 GNUNET_assert (size >= s);
309 GNUNET_assert (buf != NULL);
313 hdr.header.size = htons (s);
314 hdr.header.type = htons (MTYPE);
317 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
318 ret += sizeof (struct TestMessage);
319 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
320 ret += s - sizeof (struct TestMessage);
325 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
329 n, sender->no, GNUNET_i2s (&sender->id), receiver->no,
331 GNUNET_free (receiver_s);
336 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
337 break; /* sometimes pack buffer full, sometimes not */
339 while (size - ret >= s);
343 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
345 ¬ify_ready, NULL);
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Returning total message block of size %u\n", ret);
356 FPRINTF (stderr, "%s", "\n");
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
364 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
365 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
368 struct PeerContext *p = cls;
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
371 p->no, GNUNET_i2s (peer));
376 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
378 struct PeerContext *p = cls;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
383 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
391 start_time = GNUNET_TIME_absolute_get ();
392 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
393 TIMEOUT_TRANSMIT, ¬ify_ready,
398 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
400 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
403 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
406 test_connected = GNUNET_YES;
409 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
414 start_cb (struct PeerContext *p, void *cls)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
421 GNUNET_i2s (&p->id));
426 test_connected = GNUNET_NO;
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Peer %u: `%s' using configuration file `%s'\n", p1->no,
430 GNUNET_i2s (&p1->id), cfg_file_p1);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Peer %u: `%s' using configuration file `%s'\n", p2->no,
433 GNUNET_i2s (&p2->id), cfg_file_p2);
438 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Test triest to send from %u (%s) -> peer %u (%s)\n", sender->no,
442 sender_c, receiver->no, GNUNET_i2s (&receiver->id));
443 GNUNET_free (sender_c);
445 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
452 run (void *cls, char *const *args, const char *cfgfile,
453 const struct GNUNET_CONFIGURATION_Handle *cfg)
455 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
457 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
458 ¬ify_receive, ¬ify_connect,
459 ¬ify_disconnect, &start_cb,
461 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
462 ¬ify_receive, ¬ify_connect,
463 ¬ify_disconnect, &start_cb,
466 if ((p1 == NULL) || (p2 == NULL))
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
469 if (die_task != GNUNET_SCHEDULER_NO_TASK)
470 GNUNET_SCHEDULER_cancel (die_task);
471 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
479 static char *argv[] = { "test_transport",
481 "test_transport_api_data.conf",
487 static struct GNUNET_GETOPT_CommandLineOption options[] = {
488 GNUNET_GETOPT_OPTION_END
492 setTransportOptions ("test_transport_api_data.conf");
495 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
496 "nohelp", options, &run, &ok);
502 main (int argc, char *argv[])
507 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
509 GNUNET_log_setup (test_name,
517 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
518 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
521 tth = GNUNET_TRANSPORT_TESTING_init ();
523 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
524 (strcmp (test_plugin, "udp_nat") == 0))
526 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
527 if (GNUNET_NO == nat_res)
529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
530 "gnunet-nat-server", "SUID not set");
533 if (GNUNET_SYSERR == nat_res)
535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
536 "gnunet-nat-server", "file not found");
541 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
542 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
546 GNUNET_free (cfg_file_p1);
547 GNUNET_free (cfg_file_p2);
549 GNUNET_free (test_source);
550 GNUNET_free (test_plugin);
551 GNUNET_free (test_name);
557 /* end of test_transport_api_reliability.c */