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_transport_service.h"
31 #include "transport-testing.h"
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
38 * How long until we give up on transmitting the message?
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43 static char *test_source;
45 static char *test_plugin;
47 static char *test_name;
51 static GNUNET_SCHEDULER_TaskIdentifier die_task;
53 struct PeerContext *p1;
55 struct PeerContext *p2;
57 struct PeerContext *sender;
59 struct PeerContext *receiver;
61 struct GNUNET_TRANSPORT_TransmitHandle *th;
67 struct GNUNET_TRANSPORT_TESTING_handle *tth;
69 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
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)
85 GNUNET_NETWORK_STRUCT_BEGIN
89 struct GNUNET_MessageHeader header;
92 GNUNET_NETWORK_STRUCT_END
94 static int msg_scheduled;
96 static int msg_recv_expected;
99 static int test_failed;
100 static int test_connected;
102 static unsigned long long total_bytes;
104 static struct GNUNET_TIME_Absolute start_time;
107 * END Testcase specific declarations
111 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
113 #define OKPP do { ok++; } while (0)
120 unsigned long long delta;
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
126 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
127 FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
128 total_bytes * 1000 / 1024 / delta);
129 GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
130 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
132 GNUNET_free (value_name);
134 if (die_task != GNUNET_SCHEDULER_NO_TASK)
135 GNUNET_SCHEDULER_cancel (die_task);
138 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
142 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
144 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
145 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
147 GNUNET_TRANSPORT_TESTING_done (tth);
153 die_task = GNUNET_SCHEDULER_NO_TASK;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
156 if (test_connected == GNUNET_YES)
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
162 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
166 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
169 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
171 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
173 GNUNET_TRANSPORT_TESTING_done (tth);
180 get_size (unsigned int iter)
184 ret = (iter * iter * iter);
185 return sizeof (struct TestMessage) + (ret % 60000);
190 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
191 const struct GNUNET_MessageHeader *message)
195 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
196 const struct TestMessage *hdr;
198 hdr = (const struct TestMessage *) message;
200 if (MTYPE != ntohs (message->type))
202 msg_recv_expected = n;
203 msg_recv = ntohl (hdr->num);
204 if (ntohs (message->size) != (s))
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 "Expected message %u of size %u, got %u bytes of message %u\n",
208 n, s, ntohs (message->size), ntohl (hdr->num));
209 if (die_task != GNUNET_SCHEDULER_NO_TASK)
210 GNUNET_SCHEDULER_cancel (die_task);
211 test_failed = GNUNET_YES;
212 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
215 if (ntohl (hdr->num) != n)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Expected message %u of size %u, got %u bytes of message %u\n",
219 n, s, ntohs (message->size), ntohl (hdr->num));
220 if (die_task != GNUNET_SCHEDULER_NO_TASK)
221 GNUNET_SCHEDULER_cancel (die_task);
222 test_failed = GNUNET_YES;
223 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
226 memset (cbuf, n, s - sizeof (struct TestMessage));
227 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Expected message %u with bits %u, but body did not match at position %u\n", n,
232 if (die_task != GNUNET_SCHEDULER_NO_TASK)
233 GNUNET_SCHEDULER_cancel (die_task);
234 test_failed = GNUNET_YES;
235 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 if (ntohl (hdr->num) % 5000 == 0)
241 struct PeerContext *p = cls;
242 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
246 p->no, ps, ntohl (hdr->num), ntohs (message->size),
252 if (0 == (n % (TOTAL_MSGS / 100)))
254 FPRINTF (stderr, "%s", ".");
255 if (die_task != GNUNET_SCHEDULER_NO_TASK)
256 GNUNET_SCHEDULER_cancel (die_task);
257 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nAll messages received\n");
269 notify_ready (void *cls, size_t size, void *buf)
273 struct TestMessage hdr;
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
282 msg_scheduled, TOTAL_MSGS);
283 if (GNUNET_SCHEDULER_NO_TASK != die_task)
284 GNUNET_SCHEDULER_cancel (die_task);
285 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
292 GNUNET_assert (size >= s);
293 GNUNET_assert (buf != NULL);
297 hdr.header.size = htons (s);
298 hdr.header.type = htons (MTYPE);
301 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
302 ret += sizeof (struct TestMessage);
303 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
304 ret += s - sizeof (struct TestMessage);
309 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
313 n, sender->no, GNUNET_i2s (&sender->id), receiver->no,
315 GNUNET_free (receiver_s);
320 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
321 break; /* sometimes pack buffer full, sometimes not */
323 while (size - ret >= s);
327 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
329 ¬ify_ready, NULL);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Returning total message block of size %u\n", ret);
340 FPRINTF (stderr, "%s", "\n");
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
348 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
351 struct PeerContext *p = cls;
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
354 p->no, GNUNET_i2s (peer));
359 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
361 struct PeerContext *p = cls;
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
366 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
374 start_time = GNUNET_TIME_absolute_get ();
375 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
376 TIMEOUT_TRANSMIT, ¬ify_ready,
381 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
383 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
386 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
389 test_connected = GNUNET_YES;
392 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
397 start_cb (struct PeerContext *p, void *cls)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
404 GNUNET_i2s (&p->id));
409 test_connected = GNUNET_NO;
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Peer %u: `%s' using configuration file `%s'\n", p1->no,
413 GNUNET_i2s (&p1->id), cfg_file_p1);
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Peer %u: `%s' using configuration file `%s'\n", p2->no,
416 GNUNET_i2s (&p2->id), cfg_file_p2);
421 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Test triest to send from %u (%s) -> peer %u (%s)\n", sender->no,
425 sender_c, receiver->no, GNUNET_i2s (&receiver->id));
426 GNUNET_free (sender_c);
428 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
435 run (void *cls, char *const *args, const char *cfgfile,
436 const struct GNUNET_CONFIGURATION_Handle *cfg)
438 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
440 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
441 ¬ify_receive, ¬ify_connect,
442 ¬ify_disconnect, &start_cb,
444 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
445 ¬ify_receive, ¬ify_connect,
446 ¬ify_disconnect, &start_cb,
449 if ((p1 == NULL) || (p2 == NULL))
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
452 if (die_task != GNUNET_SCHEDULER_NO_TASK)
453 GNUNET_SCHEDULER_cancel (die_task);
454 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
462 static char *argv[] = { "test_transport",
464 "test_transport_api_data.conf",
467 static struct GNUNET_GETOPT_CommandLineOption options[] = {
468 GNUNET_GETOPT_OPTION_END
472 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
473 "nohelp", options, &run, &ok);
479 main (int argc, char *argv[])
483 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
485 GNUNET_log_setup (test_name,
489 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
490 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
493 tth = GNUNET_TRANSPORT_TESTING_init ();
495 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
496 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
500 GNUNET_free (cfg_file_p1);
501 GNUNET_free (cfg_file_p2);
503 GNUNET_free (test_source);
504 GNUNET_free (test_plugin);
505 GNUNET_free (test_name);
511 /* end of test_transport_api_reliability.c */