prevent assertion failure
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2016 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file transport/test_transport_api_reliability.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves ensures that messages are reliably sent between peers
25  *
26  * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
27  * and ensures that all message were received.
28  */
29 #include "platform.h"
30 #include "gnunet_transport_service.h"
31 #include "gauger.h"
32 #include "transport-testing.h"
33
34 /**
35  * Allow making the problem "bigger".
36  */
37 #define FACTOR 1
38
39 /**
40  * Total number of messages to send
41  *
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.
45  */
46 #define TOTAL_MSGS (1024 * 3 * FACTOR)
47
48 /**
49  * Testcase timeout
50  */
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
52
53 /**
54  * If we are in an "xhdr" test, the factor by which we divide
55  * #TOTAL_MSGS for a more sane test duration.
56  */
57 static unsigned int xhdr = 1;
58
59 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
60
61 /**
62  * Total amount of bytes sent
63  */
64 static unsigned long long total_bytes;
65
66 /**
67  * Time of start
68  */
69 static struct GNUNET_TIME_Absolute start_time;
70
71 /**
72  * No. of last message received
73  */
74 static unsigned int msg_recv;
75
76 /**
77  * Bitmap storing which messages were received
78  */
79 static char bitmap[TOTAL_MSGS / 8];
80
81
82 /**
83  * Get the desired message size for message number @a iter.
84  */
85 static size_t
86 get_size (unsigned int iter)
87 {
88   size_t ret;
89
90   ret = (iter * iter * iter);
91 #ifndef LINUX
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);
96 #endif
97   ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
98   return ret;
99 }
100
101
102 /**
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.
106  *
107  * @param cnt_down count down from `TOTAL_MSGS - 1`
108  * @return message size of the message
109  */
110 static size_t
111 get_size_cnt (unsigned int cnt_down)
112 {
113   size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
114
115   total_bytes += ret;
116   return ret;
117 }
118
119
120 /**
121  * Sets a bit active in the bitmap.
122  *
123  * @param bitIdx which bit to set
124  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
125  */
126 static int
127 set_bit (unsigned int bitIdx)
128 {
129   size_t arraySlot;
130   unsigned int targetBit;
131
132   if (bitIdx >= sizeof (bitmap) * 8)
133   {
134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
135                 "tried to set bit %u of %u(!?!?)\n",
136                 bitIdx,
137                 (unsigned int) sizeof (bitmap) * 8);
138     return GNUNET_SYSERR;
139   }
140   arraySlot = bitIdx / 8;
141   targetBit = (1L << (bitIdx % 8));
142   bitmap[arraySlot] |= targetBit;
143   return GNUNET_OK;
144 }
145
146
147 /**
148  * Obtain a bit from bitmap.
149  * @param map the bitmap
150  * @param bit index from bitmap
151  *
152  * @return Bit @a bit from @a map
153  */
154 static int
155 get_bit (const char *map,
156          unsigned int bit)
157 {
158   if (bit > TOTAL_MSGS)
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161                 "get bit %u of %u(!?!?)\n",
162                 bit,
163                 (unsigned int) sizeof (bitmap) * 8);
164     return 0;
165   }
166   return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
167 }
168
169
170 static void
171 custom_shutdown (void *cls)
172 {
173   unsigned long long delta;
174   unsigned long long rate;
175   int ok;
176
177   /* Calculcate statistics   */
178   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
179   rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
180   FPRINTF (stderr,
181            "\nThroughput was %llu KiBytes/s\n",
182            rate);
183   {
184     char *value_name;
185
186     GNUNET_asprintf (&value_name,
187                      "unreliable_%s",
188                      ccc->test_plugin);
189     GAUGER ("TRANSPORT",
190             value_name,
191             (int) rate,
192             "kb/s");
193     GNUNET_free (value_name);
194   }
195
196   ok = 0;
197   for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
198   {
199     if (get_bit (bitmap, i) == 0)
200     {
201       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202                   "Did not receive message %d\n",
203                   i);
204       ok = -1;
205     }
206   }
207   if (0 != ok)
208     ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
209 }
210
211
212 static void
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)
217 {
218   static int n;
219   unsigned int s;
220   char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
221
222   if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
223     return;
224   msg_recv = ntohl (hdr->num);
225   s = get_size (ntohl (hdr->num));
226
227   if (ntohs (hdr->header.size) != s)
228   {
229     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                 "Expected message %u of size %u, got %u bytes of message %u\n",
231                 (uint32_t) ntohl (hdr->num),
232                 s,
233                 ntohs (hdr->header.size),
234                 (uint32_t) ntohl (hdr->num));
235     ccc->global_ret = GNUNET_SYSERR;
236     GNUNET_SCHEDULER_shutdown ();
237     return;
238   }
239
240   memset (cbuf,
241           ntohl (hdr->num),
242           s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
243   if (0 !=
244       memcmp (cbuf,
245               &hdr[1],
246               s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
247   {
248     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249                 "Expected message %u with bits %u, but body did not match\n",
250                 (uint32_t) ntohl (hdr->num),
251                 (unsigned char) ntohl (hdr->num));
252     ccc->global_ret = GNUNET_SYSERR;
253     GNUNET_SCHEDULER_shutdown ();
254     return;
255   }
256 #if VERBOSE
257   if (0 == ntohl (hdr->num) % 5)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260                 "Got message %u of size %u\n",
261                 (uint32_t) ntohl (hdr->num),
262                 ntohs (hdr->header.size));
263   }
264 #endif
265   n++;
266   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
267   {
268       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269                   "Message id %u is bigger than maxmimum number of messages %u expected\n",
270                   (uint32_t) ntohl (hdr->num),
271                   TOTAL_MSGS / xhdr);
272   }
273   if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
274   {
275     FPRINTF (stderr, "%s",  ".");
276   }
277   if (n == TOTAL_MSGS / xhdr)
278   {
279     /* end testcase with success */
280     ccc->global_ret = GNUNET_OK;
281     GNUNET_SCHEDULER_shutdown ();
282   }
283 }
284
285
286 int
287 main (int argc, char *argv[])
288 {
289   if (0 == strstr (argv[0], "xhdr"))
290     xhdr = 30;
291   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
292     .num_messages = TOTAL_MSGS / xhdr,
293     .get_size_cb = &get_size_cnt
294   };
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 = &notify_receive,
300     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
301     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
302     .shutdown_task = &custom_shutdown,
303     .timeout = TIMEOUT,
304     .global_ret = GNUNET_SYSERR
305   };
306
307   ccc = &my_ccc;
308   sc.ccc = ccc;
309   start_time = GNUNET_TIME_absolute_get ();
310   if (GNUNET_OK !=
311       GNUNET_TRANSPORT_TESTING_main (2,
312                                      &GNUNET_TRANSPORT_TESTING_connect_check,
313                                      ccc))
314     return 1;
315   return 0;
316 }
317
318
319 /* end of test_transport_api_reliability.c */