-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[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   rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
178   FPRINTF (stderr,
179            "\nThroughput was %llu KiBytes/s\n",
180            rate);
181   {
182     char *value_name;
183
184     GNUNET_asprintf (&value_name,
185                      "unreliable_%s",
186                      ccc->test_plugin);
187     GAUGER ("TRANSPORT",
188             value_name,
189             (int) rate,
190             "kb/s");
191     GNUNET_free (value_name);
192   }
193
194   ok = 0;
195   for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
196   {
197     if (get_bit (bitmap, i) == 0)
198     {
199       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                   "Did not receive message %d\n",
201                   i);
202       ok = -1;
203     }
204   }
205   if (0 != ok)
206     ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
207 }
208
209
210 static void
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)
215 {
216   static int n;
217   unsigned int s;
218   char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
219
220   if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
221     return;
222   msg_recv = ntohl (hdr->num);
223   s = get_size (ntohl (hdr->num));
224
225   if (ntohs (hdr->header.size) != s)
226   {
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),
230                 s,
231                 ntohs (hdr->header.size),
232                 (uint32_t) ntohl (hdr->num));
233     ccc->global_ret = GNUNET_SYSERR;
234     GNUNET_SCHEDULER_shutdown ();
235     return;
236   }
237
238   memset (cbuf,
239           ntohl (hdr->num),
240           s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
241   if (0 !=
242       memcmp (cbuf,
243               &hdr[1],
244               s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
245   {
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 ();
252     return;
253   }
254 #if VERBOSE
255   if (0 == ntohl (hdr->num) % 5)
256   {
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));
261   }
262 #endif
263   n++;
264   if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
265   {
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),
269                   TOTAL_MSGS / xhdr);
270   }
271   if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
272   {
273     FPRINTF (stderr, "%s",  ".");
274   }
275   if (n == TOTAL_MSGS / xhdr)
276   {
277     /* end testcase with success */
278     ccc->global_ret = GNUNET_OK;
279     GNUNET_SCHEDULER_shutdown ();
280   }
281 }
282
283
284 int
285 main (int argc, char *argv[])
286 {
287   if (0 == strstr (argv[0], "xhdr"))
288     xhdr = 30;
289   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
290     .num_messages = TOTAL_MSGS / xhdr,
291     .get_size_cb = &get_size_cnt
292   };
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 = &notify_receive,
298     .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
299     .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
300     .shutdown_task = &custom_shutdown,
301     .timeout = TIMEOUT,
302     .global_ret = GNUNET_SYSERR
303   };
304
305   ccc = &my_ccc;
306   sc.ccc = ccc;
307   start_time = GNUNET_TIME_absolute_get ();
308   if (GNUNET_OK !=
309       GNUNET_TRANSPORT_TESTING_main (2,
310                                      &GNUNET_TRANSPORT_TESTING_connect_check,
311                                      ccc))
312     return 1;
313   return 0;
314 }
315
316
317 /* end of test_transport_api_reliability.c */