2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/test_transport_api_reliability.c
22 * @brief base test case for transport implementations
24 * This test case serves ensures that messages are reliably sent between peers
26 * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
27 * and ensures that all message were received.
30 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
35 * Allow making the problem "bigger".
40 * Total number of messages to send
42 * Note that this value must not significantly exceed
43 * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
44 * messages may be dropped even for a reliable transport.
46 #define TOTAL_MSGS (1024 * 3 * FACTOR)
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
54 * If we are in an "xhdr" test, the factor by which we divide
55 * #TOTAL_MSGS for a more sane test duration.
57 static unsigned int xhdr = 1;
59 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
62 * Total amount of bytes sent
64 static unsigned long long total_bytes;
69 static struct GNUNET_TIME_Absolute start_time;
72 * No. of last message received
74 static unsigned int msg_recv;
77 * Bitmap storing which messages were received
79 static char bitmap[TOTAL_MSGS / 8];
83 * Get the desired message size for message number @a iter.
86 get_size (unsigned int iter)
90 ret = (iter * iter * iter);
92 /* FreeBSD/OSX etc. Unix DGRAMs do not work
93 * with large messages */
94 if (0 == strcmp ("unix", test_plugin))
95 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
97 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
103 * Implementation of the callback for obtaining the
104 * size of messages for transmission. Counts the total
105 * number of bytes sent as a side-effect.
107 * @param cnt_down count down from `TOTAL_MSGS - 1`
108 * @return message size of the message
111 get_size_cnt (unsigned int cnt_down)
113 size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
121 * Sets a bit active in the bitmap.
123 * @param bitIdx which bit to set
124 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
127 set_bit (unsigned int bitIdx)
130 unsigned int targetBit;
132 if (bitIdx >= sizeof (bitmap) * 8)
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135 "tried to set bit %u of %u(!?!?)\n",
137 (unsigned int) sizeof (bitmap) * 8);
138 return GNUNET_SYSERR;
140 arraySlot = bitIdx / 8;
141 targetBit = (1L << (bitIdx % 8));
142 bitmap[arraySlot] |= targetBit;
148 * Obtain a bit from bitmap.
149 * @param map the bitmap
150 * @param bit index from bitmap
152 * @return Bit @a bit from @a map
155 get_bit (const char *map,
158 if (bit > TOTAL_MSGS)
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 "get bit %u of %u(!?!?)\n",
163 (unsigned int) sizeof (bitmap) * 8);
166 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
171 custom_shutdown (void *cls)
173 unsigned long long delta;
174 unsigned long long rate;
177 /* Calculcate statistics */
178 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
179 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
181 "\nThroughput was %llu KiBytes/s\n",
186 GNUNET_asprintf (&value_name,
193 GNUNET_free (value_name);
197 for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
199 if (get_bit (bitmap, i) == 0)
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Did not receive message %d\n",
208 ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
213 notify_receive (void *cls,
214 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
215 const struct GNUNET_PeerIdentity *sender,
216 const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
220 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
222 if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
224 msg_recv = ntohl (hdr->num);
225 s = get_size (ntohl (hdr->num));
227 if (ntohs (hdr->header.size) != s)
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Expected message %u of size %u, got %u bytes of message %u\n",
233 ntohs (hdr->header.size),
235 ccc->global_ret = GNUNET_SYSERR;
236 GNUNET_SCHEDULER_shutdown ();
242 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
246 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 "Expected message %u with bits %u, but body did not match\n",
251 (unsigned char) ntohl (hdr->num));
252 ccc->global_ret = GNUNET_SYSERR;
253 GNUNET_SCHEDULER_shutdown ();
257 if (0 == ntohl (hdr->num) % 5)
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Got message %u of size %u\n",
262 ntohs (hdr->header.size));
266 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "Message id %u is bigger than maxmimum number of messages %u expected\n",
273 if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
275 FPRINTF (stderr, "%s", ".");
277 if (n == TOTAL_MSGS / xhdr)
279 /* end testcase with success */
280 ccc->global_ret = GNUNET_OK;
281 GNUNET_SCHEDULER_shutdown ();
287 main (int argc, char *argv[])
289 if (0 == strstr (argv[0], "xhdr"))
291 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
292 .num_messages = TOTAL_MSGS / xhdr,
293 .get_size_cb = &get_size_cnt
295 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
296 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
297 .connect_continuation_cls = &sc,
298 .config_file = "test_transport_api_data.conf",
299 .rec = ¬ify_receive,
300 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
301 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
302 .shutdown_task = &custom_shutdown,
304 .global_ret = GNUNET_SYSERR
309 start_time = GNUNET_TIME_absolute_get ();
311 GNUNET_TRANSPORT_TESTING_main (2,
312 &GNUNET_TRANSPORT_TESTING_connect_check,
319 /* end of test_transport_api_reliability.c */