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;
121 unsigned long long rate;
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
127 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
128 rate = (1000LL * 1000LL * total_bytes) / (1024 * delta);
129 FPRINTF (stderr, "\nThroughput was %llu KiBytes/s\n",
132 GNUNET_asprintf (&value_name, "reliable_%s", test_plugin);
133 GAUGER ("TRANSPORT", value_name, (int) rate,
135 GNUNET_free (value_name);
137 if (die_task != GNUNET_SCHEDULER_NO_TASK)
138 GNUNET_SCHEDULER_cancel (die_task);
141 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
145 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
147 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
148 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
150 GNUNET_TRANSPORT_TESTING_done (tth);
156 die_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
159 if (test_connected == GNUNET_YES)
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
165 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
169 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
172 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
174 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
176 GNUNET_TRANSPORT_TESTING_done (tth);
183 get_size (unsigned int iter)
187 ret = (iter * iter * iter);
188 return sizeof (struct TestMessage) + (ret % 60000);
193 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
194 const struct GNUNET_MessageHeader *message)
198 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
199 const struct TestMessage *hdr;
201 hdr = (const struct TestMessage *) message;
203 if (MTYPE != ntohs (message->type))
205 msg_recv_expected = n;
206 msg_recv = ntohl (hdr->num);
207 if (ntohs (message->size) != (s))
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Expected message %u of size %u, got %u bytes of message %u\n",
211 n, s, ntohs (message->size), ntohl (hdr->num));
212 if (die_task != GNUNET_SCHEDULER_NO_TASK)
213 GNUNET_SCHEDULER_cancel (die_task);
214 test_failed = GNUNET_YES;
215 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
218 if (ntohl (hdr->num) != n)
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221 "Expected message %u of size %u, got %u bytes of message %u\n",
222 n, s, ntohs (message->size), ntohl (hdr->num));
223 if (die_task != GNUNET_SCHEDULER_NO_TASK)
224 GNUNET_SCHEDULER_cancel (die_task);
225 test_failed = GNUNET_YES;
226 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
229 memset (cbuf, n, s - sizeof (struct TestMessage));
230 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 "Expected message %u with bits %u, but body did not match at position %u\n", n,
235 if (die_task != GNUNET_SCHEDULER_NO_TASK)
236 GNUNET_SCHEDULER_cancel (die_task);
237 test_failed = GNUNET_YES;
238 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
242 if (ntohl (hdr->num) % 5000 == 0)
244 struct PeerContext *p = cls;
245 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
249 p->no, ps, ntohl (hdr->num), ntohs (message->size),
255 if (0 == (n % (TOTAL_MSGS / 100)))
257 FPRINTF (stderr, "%s", ".");
258 if (die_task != GNUNET_SCHEDULER_NO_TASK)
259 GNUNET_SCHEDULER_cancel (die_task);
260 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nAll messages received\n");
272 notify_ready (void *cls, size_t size, void *buf)
276 struct TestMessage hdr;
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
285 msg_scheduled, TOTAL_MSGS);
286 if (GNUNET_SCHEDULER_NO_TASK != die_task)
287 GNUNET_SCHEDULER_cancel (die_task);
288 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
295 GNUNET_assert (size >= s);
296 GNUNET_assert (buf != NULL);
300 hdr.header.size = htons (s);
301 hdr.header.type = htons (MTYPE);
304 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
305 ret += sizeof (struct TestMessage);
306 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
307 ret += s - sizeof (struct TestMessage);
312 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
316 n, sender->no, GNUNET_i2s (&sender->id), receiver->no,
318 GNUNET_free (receiver_s);
323 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
324 break; /* sometimes pack buffer full, sometimes not */
326 while (size - ret >= s);
330 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
332 ¬ify_ready, NULL);
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Returning total message block of size %u\n", ret);
343 FPRINTF (stderr, "%s", "\n");
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
351 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
354 struct PeerContext *p = cls;
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
357 p->no, GNUNET_i2s (peer));
362 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
364 struct PeerContext *p = cls;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
369 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
377 start_time = GNUNET_TIME_absolute_get ();
378 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
379 TIMEOUT_TRANSMIT, ¬ify_ready,
384 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
386 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
389 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
392 test_connected = GNUNET_YES;
395 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
400 start_cb (struct PeerContext *p, void *cls)
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
407 GNUNET_i2s (&p->id));
412 test_connected = GNUNET_NO;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Peer %u: `%s' using configuration file `%s'\n", p1->no,
416 GNUNET_i2s (&p1->id), cfg_file_p1);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Peer %u: `%s' using configuration file `%s'\n", p2->no,
419 GNUNET_i2s (&p2->id), cfg_file_p2);
424 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Test triest to send from %u (%s) -> peer %u (%s)\n", sender->no,
428 sender_c, receiver->no, GNUNET_i2s (&receiver->id));
429 GNUNET_free (sender_c);
431 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
438 run (void *cls, char *const *args, const char *cfgfile,
439 const struct GNUNET_CONFIGURATION_Handle *cfg)
441 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
443 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
444 ¬ify_receive, ¬ify_connect,
445 ¬ify_disconnect, &start_cb,
447 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
448 ¬ify_receive, ¬ify_connect,
449 ¬ify_disconnect, &start_cb,
452 if ((p1 == NULL) || (p2 == NULL))
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
455 if (die_task != GNUNET_SCHEDULER_NO_TASK)
456 GNUNET_SCHEDULER_cancel (die_task);
457 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
465 static char *argv[] = { "test_transport",
467 "test_transport_api_data.conf",
470 static struct GNUNET_GETOPT_CommandLineOption options[] = {
471 GNUNET_GETOPT_OPTION_END
475 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
476 "nohelp", options, &run, &ok);
482 main (int argc, char *argv[])
486 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
488 GNUNET_log_setup (test_name,
492 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
493 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
496 tth = GNUNET_TRANSPORT_TESTING_init ();
498 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
499 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
503 GNUNET_free (cfg_file_p1);
504 GNUNET_free (cfg_file_p2);
506 GNUNET_free (test_source);
507 GNUNET_free (test_plugin);
508 GNUNET_free (test_name);
514 /* end of test_transport_api_reliability.c */