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/>.
19 * @file transport/test_transport_api_reliability.c
20 * @brief base test case for transport implementations
22 * This test case serves ensures that messages are reliably sent between peers
24 * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
25 * and ensures that all message were received.
28 #include "gnunet_transport_service.h"
30 #include "transport-testing.h"
33 * Allow making the problem "bigger".
38 * Total number of messages to send
40 * Note that this value must not significantly exceed
41 * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
42 * messages may be dropped even for a reliable transport.
44 #define TOTAL_MSGS (1024 * 3 * FACTOR)
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
52 * If we are in an "xhdr" test, the factor by which we divide
53 * #TOTAL_MSGS for a more sane test duration.
55 static unsigned int xhdr = 1;
57 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
60 * Total amount of bytes sent
62 static unsigned long long total_bytes;
67 static struct GNUNET_TIME_Absolute start_time;
70 * No. of last message received
72 static unsigned int msg_recv;
75 * Bitmap storing which messages were received
77 static char bitmap[TOTAL_MSGS / 8];
81 * Get the desired message size for message number @a iter.
84 get_size (unsigned int iter)
88 ret = (iter * iter * iter);
90 /* FreeBSD/OSX etc. Unix DGRAMs do not work
91 * with large messages */
92 if (0 == strcmp ("unix", ccc->test_plugin))
93 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
95 ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
101 * Implementation of the callback for obtaining the
102 * size of messages for transmission. Counts the total
103 * number of bytes sent as a side-effect.
105 * @param cnt_down count down from `TOTAL_MSGS - 1`
106 * @return message size of the message
109 get_size_cnt (unsigned int cnt_down)
111 size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
119 * Sets a bit active in the bitmap.
121 * @param bitIdx which bit to set
122 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
125 set_bit (unsigned int bitIdx)
128 unsigned int targetBit;
130 if (bitIdx >= sizeof (bitmap) * 8)
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 "tried to set bit %u of %u(!?!?)\n",
135 (unsigned int) sizeof (bitmap) * 8);
136 return GNUNET_SYSERR;
138 arraySlot = bitIdx / 8;
139 targetBit = (1L << (bitIdx % 8));
140 bitmap[arraySlot] |= targetBit;
146 * Obtain a bit from bitmap.
147 * @param map the bitmap
148 * @param bit index from bitmap
150 * @return Bit @a bit from @a map
153 get_bit (const char *map,
156 if (bit > TOTAL_MSGS)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "get bit %u of %u(!?!?)\n",
161 (unsigned int) sizeof (bitmap) * 8);
164 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
169 custom_shutdown (void *cls)
171 unsigned long long delta;
172 unsigned long long rate;
175 /* Calculcate statistics */
176 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
177 rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
179 "\nThroughput was %llu KiBytes/s\n",
184 GNUNET_asprintf (&value_name,
191 GNUNET_free (value_name);
195 for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
197 if (get_bit (bitmap, i) == 0)
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Did not receive message %d\n",
206 ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
211 notify_receive (void *cls,
212 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
213 const struct GNUNET_PeerIdentity *sender,
214 const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
218 char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
220 if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
222 msg_recv = ntohl (hdr->num);
223 s = get_size (ntohl (hdr->num));
225 if (ntohs (hdr->header.size) != s)
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 "Expected message %u of size %u, got %u bytes of message %u\n",
229 (uint32_t) ntohl (hdr->num),
231 ntohs (hdr->header.size),
232 (uint32_t) ntohl (hdr->num));
233 ccc->global_ret = GNUNET_SYSERR;
234 GNUNET_SCHEDULER_shutdown ();
240 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
244 s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Expected message %u with bits %u, but body did not match\n",
248 (uint32_t) ntohl (hdr->num),
249 (unsigned char) ntohl (hdr->num));
250 ccc->global_ret = GNUNET_SYSERR;
251 GNUNET_SCHEDULER_shutdown ();
255 if (0 == ntohl (hdr->num) % 5)
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Got message %u of size %u\n",
259 (uint32_t) ntohl (hdr->num),
260 ntohs (hdr->header.size));
264 if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 "Message id %u is bigger than maxmimum number of messages %u expected\n",
268 (uint32_t) ntohl (hdr->num),
271 if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
273 FPRINTF (stderr, "%s", ".");
275 if (n == TOTAL_MSGS / xhdr)
277 /* end testcase with success */
278 ccc->global_ret = GNUNET_OK;
279 GNUNET_SCHEDULER_shutdown ();
285 main (int argc, char *argv[])
287 if (0 == strstr (argv[0], "xhdr"))
289 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
290 .num_messages = TOTAL_MSGS / xhdr,
291 .get_size_cb = &get_size_cnt
293 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
294 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
295 .connect_continuation_cls = &sc,
296 .config_file = "test_transport_api_data.conf",
297 .rec = ¬ify_receive,
298 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
299 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
300 .shutdown_task = &custom_shutdown,
302 .global_ret = GNUNET_SYSERR
307 start_time = GNUNET_TIME_absolute_get ();
309 GNUNET_TRANSPORT_TESTING_main (2,
310 &GNUNET_TRANSPORT_TESTING_connect_check,
317 /* end of test_transport_api_reliability.c */