fix div by zero
[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 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.
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      Affero General Public License for more details.
14
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/>.
17 */
18 /**
19  * @file transport/test_transport_api_reliability.c
20  * @brief base test case for transport implementations
21  *
22  * This test case serves ensures that messages are reliably sent between peers
23  *
24  * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
25  * and ensures that all message were received.
26  */
27 #include "platform.h"
28 #include "gnunet_transport_service.h"
29 #include "gauger.h"
30 #include "transport-testing.h"
31
32 /**
33  * Allow making the problem "bigger".
34  */
35 #define FACTOR 1
36
37 /**
38  * Total number of messages to send
39  *
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.
43  */
44 #define TOTAL_MSGS (1024 * 3 * FACTOR)
45
46 /**
47  * Testcase timeout
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
50
51 /**
52  * If we are in an "xhdr" test, the factor by which we divide
53  * #TOTAL_MSGS for a more sane test duration.
54  */
55 static unsigned int xhdr = 1;
56
57 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
58
59 /**
60  * Total amount of bytes sent
61  */
62 static unsigned long long total_bytes;
63
64 /**
65  * Time of start
66  */
67 static struct GNUNET_TIME_Absolute start_time;
68
69 /**
70  * No. of last message received
71  */
72 static unsigned int msg_recv;
73
74 /**
75  * Bitmap storing which messages were received
76  */
77 static char bitmap[TOTAL_MSGS / 8];
78
79
80 /**
81  * Get the desired message size for message number @a iter.
82  */
83 static size_t
84 get_size (unsigned int iter)
85 {
86   size_t ret;
87
88   ret = (iter * iter * iter);
89 #ifndef LINUX
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);
94 #endif
95   ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
96   return ret;
97 }
98
99
100 /**
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.
104  *
105  * @param cnt_down count down from `TOTAL_MSGS - 1`
106  * @return message size of the message
107  */
108 static size_t
109 get_size_cnt (unsigned int cnt_down)
110 {
111   size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
112
113   total_bytes += ret;
114   return ret;
115 }
116
117
118 /**
119  * Sets a bit active in the bitmap.
120  *
121  * @param bitIdx which bit to set
122  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
123  */
124 static int
125 set_bit (unsigned int bitIdx)
126 {
127   size_t arraySlot;
128   unsigned int targetBit;
129
130   if (bitIdx >= sizeof (bitmap) * 8)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133                 "tried to set bit %u of %u(!?!?)\n",
134                 bitIdx,
135                 (unsigned int) sizeof (bitmap) * 8);
136     return GNUNET_SYSERR;
137   }
138   arraySlot = bitIdx / 8;
139   targetBit = (1L << (bitIdx % 8));
140   bitmap[arraySlot] |= targetBit;
141   return GNUNET_OK;
142 }
143
144
145 /**
146  * Obtain a bit from bitmap.
147  * @param map the bitmap
148  * @param bit index from bitmap
149  *
150  * @return Bit @a bit from @a map
151  */
152 static int
153 get_bit (const char *map,
154          unsigned int bit)
155 {
156   if (bit > TOTAL_MSGS)
157   {
158     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159                 "get bit %u of %u(!?!?)\n",
160                 bit,
161                 (unsigned int) sizeof (bitmap) * 8);
162     return 0;
163   }
164   return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
165 }
166
167
168 static void
169 custom_shutdown (void *cls)
170 {
171   unsigned long long delta;
172   unsigned long long rate;
173   int ok;
174
175   /* Calculcate statistics   */
176   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
177   if (0 == delta)
178     delta = 1;
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 */