error msg
[oweals/gnunet.git] / src / transport / test_transport_api_unreliability_constant.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier 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 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
126   char *value_name;
127
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
129
130   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
131   FPRINTF (stderr, "\nThroughput was %llu kb/s\n",
132            total_bytes * 1000 / 1024 / delta);
133   GNUNET_asprintf (&value_name, "unreliable_%s", test_plugin);
134   GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
135           "kb/s");
136   GNUNET_free (value_name);
137
138   if (die_task != GNUNET_SCHEDULER_NO_TASK)
139     GNUNET_SCHEDULER_cancel (die_task);
140
141   if (th != NULL)
142     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
143   th = NULL;
144
145   if (cc != NULL)
146     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
147   cc = NULL;
148
149   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
150   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
151
152   GNUNET_TRANSPORT_TESTING_done (tth);
153
154   ok = 0;
155   if (test_failed == GNUNET_NO)
156     ok = GNUNET_SYSERR;
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GOT %u of %u messages\n", msg_recv,
158               TOTAL_MSGS);
159 }
160
161 static void
162 end_badly ()
163 {
164   die_task = GNUNET_SCHEDULER_NO_TASK;
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
166
167   if (test_failed == GNUNET_NO)
168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
169   else
170     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171                 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
172                 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
173
174   if (th != NULL)
175     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
176   th = NULL;
177
178   if (cc != NULL)
179     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
180   cc = NULL;
181
182   if (p1 != NULL)
183     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
184   if (p2 != NULL)
185     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
186
187   GNUNET_TRANSPORT_TESTING_done (tth);
188
189   ok = GNUNET_SYSERR;
190 }
191
192
193 static unsigned int
194 get_size (unsigned int iter)
195 {
196 /*
197   unsigned int ret;
198   ret = (iter * iter * iter);
199   return sizeof (struct TestMessage) + (ret % 60000);
200   */
201   return MSG_SIZE;
202 }
203
204
205
206
207 static void
208 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
209                 const struct GNUNET_MessageHeader *message,
210                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
211 {
212   static int n;
213
214   unsigned int s;
215   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
216   const struct TestMessage *hdr;
217
218   hdr = (const struct TestMessage *) message;
219
220   if (MTYPE != ntohs (message->type))
221     return;
222   msg_recv_expected = n;
223   msg_recv = ntohl (hdr->num);
224   if (msg_recv_expected != msg_recv)
225   {
226     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected message no %u, got %u\n",
227                 msg_recv_expected, msg_recv);
228     if (GNUNET_SCHEDULER_NO_TASK != die_task)
229       GNUNET_SCHEDULER_cancel (die_task);
230     test_failed = GNUNET_YES;
231     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
232     return;
233   }
234
235   s = get_size (ntohl (hdr->num));
236   if (ntohs (message->size) != s)
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239                 "Expected message %u of size %u, got %u bytes of message %u\n",
240                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
241     if (GNUNET_SCHEDULER_NO_TASK != die_task)
242       GNUNET_SCHEDULER_cancel (die_task);
243     test_failed = GNUNET_YES;
244     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
245     return;
246   }
247
248   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
249   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
250   {
251     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252                 "Expected message %u with bits %u, but body did not match\n",
253                 ntohl (hdr->num), (unsigned char) n);
254     if (GNUNET_SCHEDULER_NO_TASK != die_task)
255       GNUNET_SCHEDULER_cancel (die_task);
256     test_failed = GNUNET_YES;
257     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
258     return;
259   }
260 #if VERBOSE
261   if (ntohl (hdr->num) % 5 == 0)
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
264                 ntohl (hdr->num), ntohs (message->size));
265   }
266 #endif
267   n++;
268   if (0 == (n % (TOTAL_MSGS / 100)))
269   {
270     FPRINTF (stderr, "%s",  ".");
271     if (GNUNET_SCHEDULER_NO_TASK != die_task)
272       GNUNET_SCHEDULER_cancel (die_task);
273     test_failed = GNUNET_YES;
274     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
275   }
276   if (n == TOTAL_MSGS)
277   {
278     /* because of starting with 0 */
279     msg_recv++;
280     FPRINTF (stderr, "%s",  "\n");
281     end ();
282   }
283 }
284
285
286 static size_t
287 notify_ready (void *cls, size_t size, void *buf)
288 {
289   static int n;
290   char *cbuf = buf;
291   struct TestMessage hdr;
292   unsigned int s;
293   unsigned int ret;
294
295   th = NULL;
296
297   if (buf == NULL)
298   {
299     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300                 "Timeout occurred while waiting for transmit_ready\n");
301     if (GNUNET_SCHEDULER_NO_TASK != die_task)
302       GNUNET_SCHEDULER_cancel (die_task);
303     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
304     ok = 42;
305     return 0;
306   }
307   ret = 0;
308   s = get_size (n);
309   GNUNET_assert (size >= s);
310   GNUNET_assert (buf != NULL);
311   cbuf = buf;
312   do
313   {
314     hdr.header.size = htons (s);
315     hdr.header.type = htons (MTYPE);
316     hdr.num = htonl (n);
317     msg_sent = n;
318     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
319     ret += sizeof (struct TestMessage);
320     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
321     ret += s - sizeof (struct TestMessage);
322 #if VERBOSE
323     if (n % 1 == 0)
324     {
325       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
326                   s);
327     }
328
329 #endif
330     n++;
331     s = get_size (n);
332     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
333       break;                    /* sometimes pack buffer full, sometimes not */
334   }
335   while (size - ret >= s);
336   if (n < TOTAL_MSGS)
337   {
338     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
339                                                  TIMEOUT_TRANSMIT,
340                                                  &notify_ready, NULL);
341     msg_scheduled = n;
342   }
343   else
344   {
345     FPRINTF (stderr, "%s",  "\n");
346     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347                 "All messages scheduled to be sent!!\n");
348     if (GNUNET_SCHEDULER_NO_TASK != die_task)
349       GNUNET_SCHEDULER_cancel (die_task);
350     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
351   }
352   if (n % 5000 == 0)
353   {
354     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355                 "Returning total message block of size %u\n", ret);
356   }
357   total_bytes += ret;
358   return ret;
359 }
360
361
362 static void
363 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
364                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
365 {
366
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
368               GNUNET_i2s (peer), cls);
369 }
370
371
372 static void
373 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
374 {
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
376               GNUNET_i2s (peer), cls);
377   if (th != NULL)
378     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
379   th = NULL;
380 }
381
382 static void
383 sendtask ()
384 {
385   start_time = GNUNET_TIME_absolute_get ();
386   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
387                                                TIMEOUT_TRANSMIT, &notify_ready,
388                                                NULL);
389 }
390
391 static void
392 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
393 {
394   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
395
396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
397               GNUNET_i2s (&p2->id));
398   GNUNET_free (p1_c);
399
400   cc = NULL;
401
402   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
403 }
404
405 void
406 start_cb (struct PeerContext *p, void *cls)
407 {
408   static int started;
409
410   started++;
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
413               GNUNET_i2s (&p->id));
414
415   if (started != 2)
416     return;
417
418   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
419                                                NULL);
420 }
421
422 static void
423 run (void *cls, char *const *args, const char *cfgfile,
424      const struct GNUNET_CONFIGURATION_Handle *cfg)
425 {
426   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
427
428   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
429                                             &notify_receive, &notify_connect,
430                                             &notify_disconnect, &start_cb,
431                                             NULL);
432   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
433                                             &notify_receive, &notify_connect,
434                                             &notify_disconnect, &start_cb,
435                                             NULL);
436
437   if ((p1 == NULL) || (p2 == NULL))
438   {
439     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
440     if (die_task != GNUNET_SCHEDULER_NO_TASK)
441       GNUNET_SCHEDULER_cancel (die_task);
442     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
443     return;
444   }
445 }
446
447 static int
448 check ()
449 {
450   static char *const argv[] = { "test-transport-api-unreliability-constant",
451     "-c",
452     "test_transport_api_data.conf",
453     NULL
454   };
455   static struct GNUNET_GETOPT_CommandLineOption options[] = {
456     GNUNET_GETOPT_OPTION_END
457   };
458
459   ok = GNUNET_SYSERR;
460
461   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
462                       "nohelp", options, &run, &ok);
463
464   return ok;
465 }
466
467 int
468 main (int argc, char *argv[])
469 {
470   int ret;
471
472   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
473
474   GNUNET_log_setup (test_name,
475                     "WARNING",
476                     NULL);
477
478   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
479   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
480                                                  &test_plugin);
481
482   tth = GNUNET_TRANSPORT_TESTING_init ();
483
484   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
485   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
486
487   ret = check ();
488
489   GNUNET_free (cfg_file_p1);
490   GNUNET_free (cfg_file_p2);
491
492   GNUNET_free (test_source);
493   GNUNET_free (test_plugin);
494   GNUNET_free (test_name);
495
496   return ret;
497 }
498
499 /* end of test_transport_api_unreliability_constant.c */