uncrustify as demanded.
[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      SPDX-License-Identifier: AGPL3.0-or-later
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   if (0 == delta)
180     delta = 1;
181   rate = (1000LL * 1000ll * total_bytes) / (1024 * delta);
182   fprintf(stderr,
183           "\nThroughput was %llu KiBytes/s\n",
184           rate);
185   {
186     char *value_name;
187
188     GNUNET_asprintf(&value_name,
189                     "unreliable_%s",
190                     ccc->test_plugin);
191     GAUGER("TRANSPORT",
192            value_name,
193            (int)rate,
194            "kb/s");
195     GNUNET_free(value_name);
196   }
197
198   ok = 0;
199   for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
200     {
201       if (get_bit(bitmap, i) == 0)
202         {
203           GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
204                      "Did not receive message %d\n",
205                      i);
206           ok = -1;
207         }
208     }
209   if (0 != ok)
210     ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
211 }
212
213
214 static void
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)
219 {
220   static int n;
221   unsigned int s;
222   char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
223
224   if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs(hdr->header.type))
225     return;
226   msg_recv = ntohl(hdr->num);
227   s = get_size(ntohl(hdr->num));
228
229   if (ntohs(hdr->header.size) != s)
230     {
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),
234                  s,
235                  ntohs(hdr->header.size),
236                  (uint32_t)ntohl(hdr->num));
237       ccc->global_ret = GNUNET_SYSERR;
238       GNUNET_SCHEDULER_shutdown();
239       return;
240     }
241
242   memset(cbuf,
243          ntohl(hdr->num),
244          s - sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage));
245   if (0 !=
246       memcmp(cbuf,
247              &hdr[1],
248              s - sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage)))
249     {
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();
256       return;
257     }
258 #if VERBOSE
259   if (0 == ntohl(hdr->num) % 5)
260     {
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));
265     }
266 #endif
267   n++;
268   if (GNUNET_SYSERR == set_bit(ntohl(hdr->num)))
269     {
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),
273                  TOTAL_MSGS / xhdr);
274     }
275   if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
276     {
277       fprintf(stderr, "%s", ".");
278     }
279   if (n == TOTAL_MSGS / xhdr)
280     {
281       /* end testcase with success */
282       ccc->global_ret = GNUNET_OK;
283       GNUNET_SCHEDULER_shutdown();
284     }
285 }
286
287
288 int
289 main(int argc, char *argv[])
290 {
291   if (0 == strstr(argv[0], "xhdr"))
292     xhdr = 30;
293   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
294     .num_messages = TOTAL_MSGS / xhdr,
295     .get_size_cb = &get_size_cnt
296   };
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 = &notify_receive,
302     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
303     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
304     .shutdown_task = &custom_shutdown,
305     .timeout = TIMEOUT,
306     .global_ret = GNUNET_SYSERR
307   };
308
309   ccc = &my_ccc;
310   sc.ccc = ccc;
311   start_time = GNUNET_TIME_absolute_get();
312   if (GNUNET_OK !=
313       GNUNET_TRANSPORT_TESTING_main(2,
314                                     &GNUNET_TRANSPORT_TESTING_connect_check,
315                                     ccc))
316     return 1;
317   return 0;
318 }
319
320
321 /* end of test_transport_api_reliability.c */