Extending the testcases to use bluetooth
[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 {
211   static int n;
212
213   unsigned int s;
214   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
215   const struct TestMessage *hdr;
216
217   hdr = (const struct TestMessage *) message;
218
219   if (MTYPE != ntohs (message->type))
220     return;
221   msg_recv_expected = n;
222   msg_recv = ntohl (hdr->num);
223   if (msg_recv_expected != msg_recv)
224   {
225     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected message no %u, got %u\n",
226                 msg_recv_expected, msg_recv);
227     if (GNUNET_SCHEDULER_NO_TASK != die_task)
228       GNUNET_SCHEDULER_cancel (die_task);
229     test_failed = GNUNET_YES;
230     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231     return;
232   }
233
234   s = get_size (ntohl (hdr->num));
235   if (ntohs (message->size) != s)
236   {
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238                 "Expected message %u of size %u, got %u bytes of message %u\n",
239                 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
240     if (GNUNET_SCHEDULER_NO_TASK != die_task)
241       GNUNET_SCHEDULER_cancel (die_task);
242     test_failed = GNUNET_YES;
243     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
244     return;
245   }
246
247   memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
248   if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
249   {
250     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251                 "Expected message %u with bits %u, but body did not match\n",
252                 ntohl (hdr->num), (unsigned char) n);
253     if (GNUNET_SCHEDULER_NO_TASK != die_task)
254       GNUNET_SCHEDULER_cancel (die_task);
255     test_failed = GNUNET_YES;
256     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
257     return;
258   }
259 #if VERBOSE
260   if (ntohl (hdr->num) % 5 == 0)
261   {
262     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
263                 ntohl (hdr->num), ntohs (message->size));
264   }
265 #endif
266   n++;
267   if (0 == (n % (TOTAL_MSGS / 100)))
268   {
269     FPRINTF (stderr, "%s",  ".");
270     if (GNUNET_SCHEDULER_NO_TASK != die_task)
271       GNUNET_SCHEDULER_cancel (die_task);
272     test_failed = GNUNET_YES;
273     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
274   }
275   if (n == TOTAL_MSGS)
276   {
277     /* because of starting with 0 */
278     msg_recv++;
279     FPRINTF (stderr, "%s",  "\n");
280     end ();
281   }
282 }
283
284
285 static size_t
286 notify_ready (void *cls, size_t size, void *buf)
287 {
288   static int n;
289   char *cbuf = buf;
290   struct TestMessage hdr;
291   unsigned int s;
292   unsigned int ret;
293
294   th = NULL;
295
296   if (buf == NULL)
297   {
298     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299                 "Timeout occurred while waiting for transmit_ready\n");
300     if (GNUNET_SCHEDULER_NO_TASK != die_task)
301       GNUNET_SCHEDULER_cancel (die_task);
302     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
303     ok = 42;
304     return 0;
305   }
306   ret = 0;
307   s = get_size (n);
308   GNUNET_assert (size >= s);
309   GNUNET_assert (buf != NULL);
310   cbuf = buf;
311   do
312   {
313     hdr.header.size = htons (s);
314     hdr.header.type = htons (MTYPE);
315     hdr.num = htonl (n);
316     msg_sent = n;
317     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
318     ret += sizeof (struct TestMessage);
319     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
320     ret += s - sizeof (struct TestMessage);
321 #if VERBOSE
322     if (n % 1 == 0)
323     {
324       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
325                   s);
326     }
327
328 #endif
329     n++;
330     s = get_size (n);
331     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
332       break;                    /* sometimes pack buffer full, sometimes not */
333   }
334   while (size - ret >= s);
335   if (n < TOTAL_MSGS)
336   {
337     th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
338                                                  TIMEOUT_TRANSMIT,
339                                                  &notify_ready, NULL);
340     msg_scheduled = n;
341   }
342   else
343   {
344     FPRINTF (stderr, "%s",  "\n");
345     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346                 "All messages scheduled to be sent!!\n");
347     if (GNUNET_SCHEDULER_NO_TASK != die_task)
348       GNUNET_SCHEDULER_cancel (die_task);
349     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
350   }
351   if (n % 5000 == 0)
352   {
353     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354                 "Returning total message block of size %u\n", ret);
355   }
356   total_bytes += ret;
357   return ret;
358 }
359
360
361 static void
362 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
363 {
364
365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
366               GNUNET_i2s (peer), cls);
367 }
368
369
370 static void
371 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
372 {
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
374               GNUNET_i2s (peer), cls);
375   if (th != NULL)
376     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
377   th = NULL;
378 }
379
380 static void
381 sendtask ()
382 {
383   start_time = GNUNET_TIME_absolute_get ();
384   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
385                                                TIMEOUT_TRANSMIT, &notify_ready,
386                                                NULL);
387 }
388
389 static void
390 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
391 {
392   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
393
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
395               GNUNET_i2s (&p2->id));
396   GNUNET_free (p1_c);
397
398   cc = NULL;
399
400   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
401 }
402
403 void
404 start_cb (struct PeerContext *p, void *cls)
405 {
406   static int started;
407
408   started++;
409
410   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
411               GNUNET_i2s (&p->id));
412
413   if (started != 2)
414     return;
415
416   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
417                                                NULL);
418 }
419
420 static void
421 run (void *cls, char *const *args, const char *cfgfile,
422      const struct GNUNET_CONFIGURATION_Handle *cfg)
423 {
424   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
425
426   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
427                                             &notify_receive, &notify_connect,
428                                             &notify_disconnect, &start_cb,
429                                             NULL);
430   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
431                                             &notify_receive, &notify_connect,
432                                             &notify_disconnect, &start_cb,
433                                             NULL);
434
435   if ((p1 == NULL) || (p2 == NULL))
436   {
437     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
438     if (die_task != GNUNET_SCHEDULER_NO_TASK)
439       GNUNET_SCHEDULER_cancel (die_task);
440     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
441     return;
442   }
443 }
444
445 static int
446 check ()
447 {
448   static char *const argv[] = { "test-transport-api-unreliability-constant",
449     "-c",
450     "test_transport_api_data.conf",
451     NULL
452   };
453   static struct GNUNET_GETOPT_CommandLineOption options[] = {
454     GNUNET_GETOPT_OPTION_END
455   };
456
457   ok = GNUNET_SYSERR;
458
459   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
460                       "nohelp", options, &run, &ok);
461
462   return ok;
463 }
464
465 int
466 main (int argc, char *argv[])
467 {
468   int ret;
469
470   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
471
472   GNUNET_log_setup (test_name,
473                     "WARNING",
474                     NULL);
475
476   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
477   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
478                                                  &test_plugin);
479
480   tth = GNUNET_TRANSPORT_TESTING_init ();
481
482   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
483   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
484
485   ret = check ();
486
487   GNUNET_free (cfg_file_p1);
488   GNUNET_free (cfg_file_p2);
489
490   GNUNET_free (test_source);
491   GNUNET_free (test_plugin);
492   GNUNET_free (test_name);
493
494   return ret;
495 }
496
497 /* end of test_transport_api_unreliability_constant.c */