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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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", ccc->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;
181 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
183 "\nThroughput was %llu KiBytes/s\n",
188 GNUNET_asprintf (&value_name,
195 GNUNET_free (value_name);
199 for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
201 if (get_bit (bitmap, i) == 0)
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 "Did not receive message %d\n",
210 ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
215 notify_receive (void *cls,
216 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
217 const struct GNUNET_PeerIdentity *sender,
218 const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
222 char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
224 if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
226 msg_recv = ntohl (hdr->num);
227 s = get_size (ntohl (hdr->num));
229 if (ntohs (hdr->header.size) != s)
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Expected message %u of size %u, got %u bytes of message %u\n",
233 (uint32_t) ntohl (hdr->num),
235 ntohs (hdr->header.size),
236 (uint32_t) ntohl (hdr->num));
237 ccc->global_ret = GNUNET_SYSERR;
238 GNUNET_SCHEDULER_shutdown ();
244 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
248 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 "Expected message %u with bits %u, but body did not match\n",
252 (uint32_t) ntohl (hdr->num),
253 (unsigned char) ntohl (hdr->num));
254 ccc->global_ret = GNUNET_SYSERR;
255 GNUNET_SCHEDULER_shutdown ();
259 if (0 == ntohl (hdr->num) % 5)
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Got message %u of size %u\n",
263 (uint32_t) ntohl (hdr->num),
264 ntohs (hdr->header.size));
268 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Message id %u is bigger than maxmimum number of messages %u expected\n",
272 (uint32_t) ntohl (hdr->num),
275 if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
277 FPRINTF (stderr, "%s", ".");
279 if (n == TOTAL_MSGS / xhdr)
281 /* end testcase with success */
282 ccc->global_ret = GNUNET_OK;
283 GNUNET_SCHEDULER_shutdown ();
289 main (int argc, char *argv[])
291 if (0 == strstr (argv[0], "xhdr"))
293 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
294 .num_messages = TOTAL_MSGS / xhdr,
295 .get_size_cb = &get_size_cnt
297 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
298 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
299 .connect_continuation_cls = &sc,
300 .config_file = "test_transport_api_data.conf",
301 .rec = ¬ify_receive,
302 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
303 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
304 .shutdown_task = &custom_shutdown,
306 .global_ret = GNUNET_SYSERR
311 start_time = GNUNET_TIME_absolute_get ();
313 GNUNET_TRANSPORT_TESTING_main (2,
314 &GNUNET_TRANSPORT_TESTING_connect_check,
321 /* end of test_transport_api_reliability.c */