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_unreliability_constant.c
22 * @brief test case for transports; ensures messages get
23 * through, regardless of order, constant packet size
25 * This test case serves as a base for unreliable
26 * transport test cases to check that the transports
27 * achieve reliable message delivery.
30 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
40 * How long until we give up on transmitting the message?
42 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44 static char *test_source;
46 static char *test_plugin;
48 static char *test_name;
52 static GNUNET_SCHEDULER_TaskIdentifier die_task;
54 struct PeerContext *p1;
56 struct PeerContext *p2;
58 struct GNUNET_TRANSPORT_TransmitHandle *th;
60 struct GNUNET_TRANSPORT_TESTING_handle *tth;
69 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
72 * Testcase specific declarations
76 * Note that this value must not significantly exceed
77 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
78 * messages may be dropped even for a reliable transport.
80 #define TOTAL_MSGS (1024 * 3)
84 #define MSG_SIZE 10000
86 GNUNET_NETWORK_STRUCT_BEGIN
90 struct GNUNET_MessageHeader header;
93 GNUNET_NETWORK_STRUCT_END
95 static char *test_name;
97 static int msg_scheduled;
99 static int msg_recv_expected;
102 static int test_failed;
104 static unsigned long long total_bytes;
106 static struct GNUNET_TIME_Absolute start_time;
109 * END Testcase specific declarations
113 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
115 #define OKPP do { ok++; } while (0)
119 get_bit (const char *map, unsigned int bit);
124 unsigned long long delta;
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
129 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
130 FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
131 total_bytes / 1024 / delta);
132 GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
133 GAUGER ("TRANSPORT", value_name, (int) (total_bytes / 1024 / delta),
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);
148 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
149 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
151 GNUNET_TRANSPORT_TESTING_done (tth);
154 if (test_failed == GNUNET_NO)
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GOT %u of %u messages\n", msg_recv,
163 die_task = GNUNET_SCHEDULER_NO_TASK;
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
166 if (test_failed == GNUNET_NO)
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
171 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
174 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
178 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
182 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
184 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
186 GNUNET_TRANSPORT_TESTING_done (tth);
193 get_size (unsigned int iter)
197 ret = (iter * iter * iter);
198 return sizeof (struct TestMessage) + (ret % 60000);
207 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
208 const struct GNUNET_MessageHeader *message)
213 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
214 const struct TestMessage *hdr;
216 hdr = (const struct TestMessage *) message;
218 if (MTYPE != ntohs (message->type))
220 msg_recv_expected = n;
221 msg_recv = ntohl (hdr->num);
222 if (msg_recv_expected != msg_recv)
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected message no %u, got %u\n",
225 msg_recv_expected, msg_recv);
226 if (GNUNET_SCHEDULER_NO_TASK != die_task)
227 GNUNET_SCHEDULER_cancel (die_task);
228 test_failed = GNUNET_YES;
229 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
233 s = get_size (ntohl (hdr->num));
234 if (ntohs (message->size) != s)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Expected message %u of size %u, got %u bytes of message %u\n",
238 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
239 if (GNUNET_SCHEDULER_NO_TASK != die_task)
240 GNUNET_SCHEDULER_cancel (die_task);
241 test_failed = GNUNET_YES;
242 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
246 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
247 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 "Expected message %u with bits %u, but body did not match\n",
251 ntohl (hdr->num), (unsigned char) n);
252 if (GNUNET_SCHEDULER_NO_TASK != die_task)
253 GNUNET_SCHEDULER_cancel (die_task);
254 test_failed = GNUNET_YES;
255 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
259 if (ntohl (hdr->num) % 5 == 0)
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
262 ntohl (hdr->num), ntohs (message->size));
266 if (0 == (n % (TOTAL_MSGS / 100)))
268 FPRINTF (stderr, "%s", ".");
269 if (GNUNET_SCHEDULER_NO_TASK != die_task)
270 GNUNET_SCHEDULER_cancel (die_task);
271 test_failed = GNUNET_YES;
272 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
276 /* because of starting with 0 */
278 FPRINTF (stderr, "%s", "\n");
285 notify_ready (void *cls, size_t size, void *buf)
289 struct TestMessage hdr;
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298 "Timeout occurred while waiting for transmit_ready\n");
299 if (GNUNET_SCHEDULER_NO_TASK != die_task)
300 GNUNET_SCHEDULER_cancel (die_task);
301 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
307 GNUNET_assert (size >= s);
308 GNUNET_assert (buf != NULL);
312 hdr.header.size = htons (s);
313 hdr.header.type = htons (MTYPE);
316 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
317 ret += sizeof (struct TestMessage);
318 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
319 ret += s - sizeof (struct TestMessage);
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
330 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
331 break; /* sometimes pack buffer full, sometimes not */
333 while (size - ret >= s);
336 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
338 ¬ify_ready, NULL);
343 FPRINTF (stderr, "%s", "\n");
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "All messages scheduled to be sent!!\n");
346 if (GNUNET_SCHEDULER_NO_TASK != die_task)
347 GNUNET_SCHEDULER_cancel (die_task);
348 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Returning total message block of size %u\n", ret);
361 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
365 GNUNET_i2s (peer), cls);
370 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
373 GNUNET_i2s (peer), cls);
375 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
382 start_time = GNUNET_TIME_absolute_get ();
383 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
384 TIMEOUT_TRANSMIT, ¬ify_ready,
389 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
391 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
394 GNUNET_i2s (&p2->id));
399 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
403 start_cb (struct PeerContext *p, void *cls)
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
410 GNUNET_i2s (&p->id));
415 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
420 run (void *cls, char *const *args, const char *cfgfile,
421 const struct GNUNET_CONFIGURATION_Handle *cfg)
423 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
425 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
426 ¬ify_receive, ¬ify_connect,
427 ¬ify_disconnect, &start_cb,
429 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
430 ¬ify_receive, ¬ify_connect,
431 ¬ify_disconnect, &start_cb,
434 if ((p1 == NULL) || (p2 == NULL))
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
437 if (die_task != GNUNET_SCHEDULER_NO_TASK)
438 GNUNET_SCHEDULER_cancel (die_task);
439 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
447 static char *const argv[] = { "test-transport-api-unreliability-constant",
449 "test_transport_api_data.conf",
452 static struct GNUNET_GETOPT_CommandLineOption options[] = {
453 GNUNET_GETOPT_OPTION_END
458 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
459 "nohelp", options, &run, &ok);
465 main (int argc, char *argv[])
469 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
471 GNUNET_log_setup (test_name,
475 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
476 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
479 tth = GNUNET_TRANSPORT_TESTING_init ();
481 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
482 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
486 GNUNET_free (cfg_file_p1);
487 GNUNET_free (cfg_file_p2);
489 GNUNET_free (test_source);
490 GNUNET_free (test_plugin);
491 GNUNET_free (test_name);
496 /* end of test_transport_api_unreliability_constant.c */