-fix (C) notices
[oweals/gnunet.git] / src / transport / test_transport_api_unreliability_constant.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 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_unreliability_constant.c
22  * @brief test case for transports; ensures messages get
23  *        through, regardless of order, constant packet size
24  *
25  * This test case serves as a base for unreliable
26  * transport test cases to check that the transports
27  * achieve reliable message delivery.
28  */
29 #include "platform.h"
30 #include "gnunet_transport_service.h"
31 #include "gauger.h"
32 #include "transport-testing.h"
33
34 /**
35  * Testcase timeout
36  */
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 900)
38
39 /**
40  * How long until we give up on transmitting the message?
41  */
42 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
43
44 static char *test_source;
45
46 static char *test_plugin;
47
48 static char *test_name;
49
50 static int ok;
51
52 static struct GNUNET_SCHEDULER_Task * die_task;
53
54 struct PeerContext *p1;
55
56 struct PeerContext *p2;
57
58 struct GNUNET_TRANSPORT_TransmitHandle *th;
59
60 struct GNUNET_TRANSPORT_TESTING_handle *tth;
61
62 char *cfg_file_p1;
63
64 char *cfg_file_p2;
65
66 uint32_t max_bps_p1;
67 uint32_t max_bps_p2;
68
69 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
70
71 /*
72  * Testcase specific declarations
73  */
74
75 /**
76  * Note that this value must not significantly exceed
77  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
78  * messages may be dropped even for a reliable transport.
79  */
80 #define TOTAL_MSGS (1024 * 3)
81
82 #define MTYPE 12345
83
84 #define MSG_SIZE 10000
85
86 GNUNET_NETWORK_STRUCT_BEGIN
87
88 struct TestMessage
89 {
90   struct GNUNET_MessageHeader header;
91   uint32_t num;
92 };
93 GNUNET_NETWORK_STRUCT_END
94
95 static char *test_name;
96
97 static int msg_scheduled;
98 static int msg_sent;
99 static int msg_recv_expected;
100 static int msg_recv;
101
102 static int test_failed;
103
104 static unsigned long long total_bytes;
105
106 static struct GNUNET_TIME_Absolute start_time;
107
108 /*
109  * END Testcase specific declarations
110  */
111
112 #if VERBOSE
113 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
114 #else
115 #define OKPP do { ok++; } while (0)
116 #endif
117
118 int
119 get_bit (const char *map, unsigned int bit);
120
121 static void
122 end ()
123 {
124   unsigned long long delta;
125   char *value_name;
126
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
128
129   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us / (1000 * 1000);
130   FPRINTF (stderr, "\nThroughput was %llu kb/s total_bytes\n",
131            (total_bytes / 1024) / delta);
132   GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
133   GAUGER ("TRANSPORT", value_name, (int) (total_bytes / 1024 / delta),
134           "kb/s");
135   GNUNET_free (value_name);
136
137   if (die_task != NULL)
138     GNUNET_SCHEDULER_cancel (die_task);
139
140   if (th != NULL)
141     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
142   th = NULL;
143
144   if (cc != NULL)
145     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
146   cc = NULL;
147
148   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
149   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
150
151   GNUNET_TRANSPORT_TESTING_done (tth);
152
153   ok = 0;
154   if (test_failed == GNUNET_NO)
155     ok = GNUNET_SYSERR;
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GOT %u of %u messages\n", msg_recv,
157               TOTAL_MSGS);
158 }
159
160 static void
161 end_badly ()
162 {
163   die_task = NULL;
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
165
166   if (test_failed == GNUNET_NO)
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
168   else
169     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170                 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
171                 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
172
173   if (th != NULL)
174     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
175   th = NULL;
176
177   if (cc != NULL)
178     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
179   cc = NULL;
180
181   if (p1 != NULL)
182     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
183   if (p2 != NULL)
184     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
185
186   GNUNET_TRANSPORT_TESTING_done (tth);
187
188   ok = GNUNET_SYSERR;
189 }
190
191
192 static unsigned int
193 get_size (unsigned int iter)
194 {
195 /*
196   unsigned int ret;
197   ret = (iter * iter * iter);
198   return sizeof (struct TestMessage) + (ret % 60000);
199   */
200   return MSG_SIZE;
201 }
202
203
204
205
206 static void
207 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
208                 const struct GNUNET_MessageHeader *message)
209 {
210   static int n;
211
212   unsigned int s;
213   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
214   const struct TestMessage *hdr;
215
216   hdr = (const struct TestMessage *) message;
217
218   if (MTYPE != ntohs (message->type))
219     return;
220   msg_recv_expected = n;
221   msg_recv = ntohl (hdr->num);
222   if (msg_recv_expected != msg_recv)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected message no %u, got %u\n",
225                 msg_recv_expected, msg_recv);
226     if (NULL != die_task)
227       GNUNET_SCHEDULER_cancel (die_task);
228     test_failed = GNUNET_YES;
229     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
230     return;
231   }
232
233   s = get_size (ntohl (hdr->num));
234   if (ntohs (message->size) != s)
235   {
236     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                 "Expected message %u of size %u, got %u bytes of message %u\n",
238                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
239     if (NULL != die_task)
240       GNUNET_SCHEDULER_cancel (die_task);
241     test_failed = GNUNET_YES;
242     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
243     return;
244   }
245
246   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
247   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250                 "Expected message %u with bits %u, but body did not match\n",
251                 ntohl (hdr->num), (unsigned char) n);
252     if (NULL != die_task)
253       GNUNET_SCHEDULER_cancel (die_task);
254     test_failed = GNUNET_YES;
255     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
256     return;
257   }
258 #if VERBOSE
259   if (ntohl (hdr->num) % 5 == 0)
260   {
261     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
262                 ntohl (hdr->num), ntohs (message->size));
263   }
264 #endif
265   n++;
266   if (0 == (n % (TOTAL_MSGS / 100)))
267   {
268     FPRINTF (stderr, "%s",  ".");
269     if (NULL != die_task)
270       GNUNET_SCHEDULER_cancel (die_task);
271     test_failed = GNUNET_YES;
272     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
273   }
274   if (n == TOTAL_MSGS)
275   {
276     /* because of starting with 0 */
277     msg_recv++;
278     FPRINTF (stderr, "%s",  "\n");
279     end ();
280   }
281 }
282
283
284 static size_t
285 notify_ready (void *cls, size_t size, void *buf)
286 {
287   static int n;
288   char *cbuf = buf;
289   struct TestMessage hdr;
290   unsigned int s;
291   unsigned int ret;
292
293   th = NULL;
294
295   if (buf == NULL)
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298                 "Timeout occurred while waiting for transmit_ready\n");
299     if (NULL != die_task)
300       GNUNET_SCHEDULER_cancel (die_task);
301     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
302     ok = 42;
303     return 0;
304   }
305   ret = 0;
306   s = get_size (n);
307   GNUNET_assert (size >= s);
308   GNUNET_assert (buf != NULL);
309   cbuf = buf;
310   do
311   {
312     hdr.header.size = htons (s);
313     hdr.header.type = htons (MTYPE);
314     hdr.num = htonl (n);
315     msg_sent = n;
316     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
317     ret += sizeof (struct TestMessage);
318     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
319     ret += s - sizeof (struct TestMessage);
320 #if VERBOSE
321     if (n % 1 == 0)
322     {
323       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
324                   s);
325     }
326
327 #endif
328     n++;
329     s = get_size (n);
330     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
331       break;                    /* sometimes pack buffer full, sometimes not */
332   }
333   while (size - ret >= s);
334   if (n < TOTAL_MSGS)
335   {
336     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
337                                                  TIMEOUT_TRANSMIT,
338                                                  &notify_ready, NULL);
339     msg_scheduled = n;
340   }
341   else
342   {
343     FPRINTF (stderr, "%s",  "\n");
344     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345                 "All messages scheduled to be sent!!\n");
346     if (NULL != die_task)
347       GNUNET_SCHEDULER_cancel (die_task);
348     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
349   }
350   if (n % 5000 == 0)
351   {
352     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353                 "Returning total message block of size %u\n", ret);
354   }
355   total_bytes += ret;
356   return ret;
357 }
358
359
360 static void
361 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
362 {
363
364   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer `%4s' connected to us (%p)!\n",
365               GNUNET_i2s (peer), cls);
366 }
367
368
369 static void
370 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
371 {
372   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer `%4s' disconnected (%p)!\n",
373               GNUNET_i2s (peer), cls);
374   if (th != NULL)
375     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
376   th = NULL;
377 }
378
379 static void
380 sendtask ()
381 {
382   start_time = GNUNET_TIME_absolute_get ();
383   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
384                                                TIMEOUT_TRANSMIT, &notify_ready,
385                                                NULL);
386 }
387
388 static void
389 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
390 {
391   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
392
393   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %s <-> %s\n", p1_c,
394               GNUNET_i2s (&p2->id));
395   GNUNET_free (p1_c);
396
397   cc = NULL;
398
399   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
400 }
401
402 void
403 start_cb (struct PeerContext *p, void *cls)
404 {
405   static int started;
406
407   started++;
408
409   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
410               GNUNET_i2s (&p->id));
411
412   if (started != 2)
413     return;
414
415   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
416                                                NULL);
417 }
418
419 static void
420 run (void *cls, char *const *args, const char *cfgfile,
421      const struct GNUNET_CONFIGURATION_Handle *cfg)
422 {
423   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
424
425   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
426                                             &notify_receive, &notify_connect,
427                                             &notify_disconnect, &start_cb,
428                                             NULL);
429   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
430                                             &notify_receive, &notify_connect,
431                                             &notify_disconnect, &start_cb,
432                                             NULL);
433
434   if ((p1 == NULL) || (p2 == NULL))
435   {
436     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
437     if (die_task != NULL)
438       GNUNET_SCHEDULER_cancel (die_task);
439     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
440     return;
441   }
442 }
443
444 static int
445 check ()
446 {
447   static char *const argv[] = { "test-transport-api-unreliability-constant",
448     "-c",
449     "test_transport_api_data.conf",
450     NULL
451   };
452   static struct GNUNET_GETOPT_CommandLineOption options[] = {
453     GNUNET_GETOPT_OPTION_END
454   };
455
456   ok = GNUNET_SYSERR;
457
458   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
459                       "nohelp", options, &run, &ok);
460
461   return ok;
462 }
463
464 int
465 main (int argc, char *argv[])
466 {
467   int ret;
468
469   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
470
471   GNUNET_log_setup (test_name,
472                     "WARNING",
473                     NULL);
474
475   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
476   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
477                                                  &test_plugin);
478
479   tth = GNUNET_TRANSPORT_TESTING_init ();
480
481   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
482   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
483
484   ret = check ();
485
486   GNUNET_free (cfg_file_p1);
487   GNUNET_free (cfg_file_p2);
488
489   GNUNET_free (test_source);
490   GNUNET_free (test_plugin);
491   GNUNET_free (test_name);
492
493   return ret;
494 }
495
496 /* end of test_transport_api_unreliability_constant.c */