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.
16 * @file transport/test_transport_api_reliability.c
17 * @brief base test case for transport implementations
19 * This test case serves ensures that messages are reliably sent between peers
21 * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
22 * and ensures that all message were received.
25 #include "gnunet_transport_service.h"
27 #include "transport-testing.h"
30 * Allow making the problem "bigger".
35 * Total number of messages to send
37 * Note that this value must not significantly exceed
38 * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
39 * messages may be dropped even for a reliable transport.
41 #define TOTAL_MSGS (1024 * 3 * FACTOR)
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
49 * If we are in an "xhdr" test, the factor by which we divide
50 * #TOTAL_MSGS for a more sane test duration.
52 static unsigned int xhdr = 1;
54 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
57 * Total amount of bytes sent
59 static unsigned long long total_bytes;
64 static struct GNUNET_TIME_Absolute start_time;
67 * No. of last message received
69 static unsigned int msg_recv;
72 * Bitmap storing which messages were received
74 static char bitmap[TOTAL_MSGS / 8];
78 * Get the desired message size for message number @a iter.
81 get_size (unsigned int iter)
85 ret = (iter * iter * iter);
87 /* FreeBSD/OSX etc. Unix DGRAMs do not work
88 * with large messages */
89 if (0 == strcmp ("unix", ccc->test_plugin))
90 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
92 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
98 * Implementation of the callback for obtaining the
99 * size of messages for transmission. Counts the total
100 * number of bytes sent as a side-effect.
102 * @param cnt_down count down from `TOTAL_MSGS - 1`
103 * @return message size of the message
106 get_size_cnt (unsigned int cnt_down)
108 size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
116 * Sets a bit active in the bitmap.
118 * @param bitIdx which bit to set
119 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
122 set_bit (unsigned int bitIdx)
125 unsigned int targetBit;
127 if (bitIdx >= sizeof (bitmap) * 8)
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 "tried to set bit %u of %u(!?!?)\n",
132 (unsigned int) sizeof (bitmap) * 8);
133 return GNUNET_SYSERR;
135 arraySlot = bitIdx / 8;
136 targetBit = (1L << (bitIdx % 8));
137 bitmap[arraySlot] |= targetBit;
143 * Obtain a bit from bitmap.
144 * @param map the bitmap
145 * @param bit index from bitmap
147 * @return Bit @a bit from @a map
150 get_bit (const char *map,
153 if (bit > TOTAL_MSGS)
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "get bit %u of %u(!?!?)\n",
158 (unsigned int) sizeof (bitmap) * 8);
161 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
166 custom_shutdown (void *cls)
168 unsigned long long delta;
169 unsigned long long rate;
172 /* Calculcate statistics */
173 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
174 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
176 "\nThroughput was %llu KiBytes/s\n",
181 GNUNET_asprintf (&value_name,
188 GNUNET_free (value_name);
192 for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
194 if (get_bit (bitmap, i) == 0)
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 "Did not receive message %d\n",
203 ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
208 notify_receive (void *cls,
209 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
210 const struct GNUNET_PeerIdentity *sender,
211 const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
215 char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
217 if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
219 msg_recv = ntohl (hdr->num);
220 s = get_size (ntohl (hdr->num));
222 if (ntohs (hdr->header.size) != s)
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Expected message %u of size %u, got %u bytes of message %u\n",
226 (uint32_t) ntohl (hdr->num),
228 ntohs (hdr->header.size),
229 (uint32_t) ntohl (hdr->num));
230 ccc->global_ret = GNUNET_SYSERR;
231 GNUNET_SCHEDULER_shutdown ();
237 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
241 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 "Expected message %u with bits %u, but body did not match\n",
245 (uint32_t) ntohl (hdr->num),
246 (unsigned char) ntohl (hdr->num));
247 ccc->global_ret = GNUNET_SYSERR;
248 GNUNET_SCHEDULER_shutdown ();
252 if (0 == ntohl (hdr->num) % 5)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Got message %u of size %u\n",
256 (uint32_t) ntohl (hdr->num),
257 ntohs (hdr->header.size));
261 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "Message id %u is bigger than maxmimum number of messages %u expected\n",
265 (uint32_t) ntohl (hdr->num),
268 if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
270 FPRINTF (stderr, "%s", ".");
272 if (n == TOTAL_MSGS / xhdr)
274 /* end testcase with success */
275 ccc->global_ret = GNUNET_OK;
276 GNUNET_SCHEDULER_shutdown ();
282 main (int argc, char *argv[])
284 if (0 == strstr (argv[0], "xhdr"))
286 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
287 .num_messages = TOTAL_MSGS / xhdr,
288 .get_size_cb = &get_size_cnt
290 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
291 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
292 .connect_continuation_cls = &sc,
293 .config_file = "test_transport_api_data.conf",
294 .rec = ¬ify_receive,
295 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
296 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
297 .shutdown_task = &custom_shutdown,
299 .global_ret = GNUNET_SYSERR
304 start_time = GNUNET_TIME_absolute_get ();
306 GNUNET_TRANSPORT_TESTING_main (2,
307 &GNUNET_TRANSPORT_TESTING_connect_check,
314 /* end of test_transport_api_reliability.c */