-fix format warning
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_recv_tcp.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_manipulation_recv_tcp.c
22  * @brief base test case for transport traffic manipulation implementation
23  *
24  * This test case will setup 2 peers and connect them, the first message
25  * will be sent without manipulation, then a receive delay of 1 second will
26  * be configured and 2 more message will be sent. Time will be measured
27  *
28  * In addition the distance on receiver side will be manipulated to be 10
29  */
30 #include "platform.h"
31 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
33
34 /**
35  * How long until we give up on transmitting the message?
36  */
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
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, 30)
43
44 #define TEST_MESSAGE_SIZE 2600
45
46 #define TEST_MESSAGE_TYPE 12345
47
48 static char *test_source;
49
50 static char *test_plugin;
51
52 static char *test_name;
53
54 static int ok;
55
56 static int s_started;
57
58 static int s_connected;
59
60 static int s_sending;
61
62 static struct GNUNET_SCHEDULER_Task * die_task;
63
64 static struct GNUNET_SCHEDULER_Task * send_task;
65
66 static struct PeerContext *p1;
67
68 static struct PeerContext *p2;
69
70 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
71
72 static struct GNUNET_TRANSPORT_TransmitHandle *th;
73
74 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
75
76 static char *cfg_file_p1;
77
78 static char *cfg_file_p2;
79
80 static int messages_recv;
81
82 static struct GNUNET_TIME_Absolute start_normal;
83 static struct GNUNET_TIME_Relative dur_normal;
84
85 static struct GNUNET_TIME_Absolute start_delayed;
86 static struct GNUNET_TIME_Relative dur_delayed;
87
88 static void
89 end ()
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
92
93   if (send_task != NULL)
94     GNUNET_SCHEDULER_cancel (send_task);
95
96   if (die_task != NULL)
97     GNUNET_SCHEDULER_cancel (die_task);
98
99   if (th != NULL)
100     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
101   th = NULL;
102
103   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
104   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
105 }
106
107
108 static void
109 end_badly (void *cls)
110 {
111   die_task = NULL;
112
113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
114
115
116   if (send_task != NULL)
117     GNUNET_SCHEDULER_cancel (send_task);
118
119   if (cc != NULL)
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
122     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
123     cc = NULL;
124   }
125
126   if (th != NULL)
127     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
128   else
129     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
130
131   if (s_started == GNUNET_NO)
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
133   else
134     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
135
136   if (s_connected == GNUNET_NO)
137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
138   else
139     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
140
141   if (s_sending == GNUNET_NO)
142     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
143   else
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
145
146   th = NULL;
147
148   if (p1 != NULL)
149     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
150   else
151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
152   if (p2 != NULL)
153     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
154   else
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
156
157   ok = GNUNET_SYSERR;
158 }
159
160
161 static void
162 sendtask (void *cls);
163
164
165 static void
166 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
167                 const struct GNUNET_MessageHeader *message)
168 {
169   struct PeerContext *p = cls;
170   struct PeerContext *t = NULL;
171
172   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
173     t = p1;
174   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
175     t = p2;
176   GNUNET_assert (t != NULL);
177
178   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
179
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
182               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
183               GNUNET_i2s (&t->id));
184   GNUNET_free (ps);
185
186   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
187       (TEST_MESSAGE_SIZE == ntohs (message->size)))
188   {
189     ok = 0;
190
191   }
192   else
193   {
194     GNUNET_break (0);
195     ok = 1;
196     end ();
197     return;
198   }
199
200   if (messages_recv <= 1)
201   {
202     /* Received non-delayed message */
203     dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
204     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205                 "Received non-delayed message %u after %s\n",
206                 messages_recv,
207                 GNUNET_STRINGS_relative_time_to_string (dur_normal,
208                                                         GNUNET_YES));
209
210     struct GNUNET_ATS_Properties prop;
211     struct GNUNET_TIME_Relative delay;
212
213     delay.rel_value_us = 1000 * 1000LL;
214     memset (&prop, 0, sizeof (prop));
215     prop.distance = 10;
216     GNUNET_TRANSPORT_set_traffic_metric (p1->th,
217                                          &p2->id,
218                                          &prop,
219                                          delay,
220                                          GNUNET_TIME_UNIT_ZERO);
221     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
222   }
223   if (2 == messages_recv)
224   {
225         /* Received manipulated message */
226         dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
227       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228                   "Received delayed message %u after %s\n",
229                   messages_recv,
230                   GNUNET_STRINGS_relative_time_to_string (dur_delayed,
231                                                           GNUNET_YES));
232       if (dur_delayed.rel_value_us < 1000 * 1000LL)
233       {
234         GNUNET_break (0);
235         ok += 1;
236         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                     "Delayed message was not delayed correctly: took only %s\n",
238                     GNUNET_STRINGS_relative_time_to_string (dur_delayed,
239                                                             GNUNET_YES));
240       }
241       /* shutdown */
242       end ();
243   }
244
245   messages_recv ++;
246 }
247
248
249 static size_t
250 notify_ready (void *cls, size_t size, void *buf)
251 {
252   struct PeerContext *p = cls;
253   struct GNUNET_MessageHeader *hdr;
254
255   th = NULL;
256
257   if (buf == NULL)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                 "Timeout occurred while waiting for transmit_ready\n");
261     if (NULL != die_task)
262       GNUNET_SCHEDULER_cancel (die_task);
263     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
264     ok = 42;
265     return 0;
266   }
267
268   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
269   memset (buf, '\0', TEST_MESSAGE_SIZE);
270   hdr = buf;
271   hdr->size = htons (TEST_MESSAGE_SIZE);
272   hdr->type = htons (TEST_MESSAGE_TYPE);
273
274   {
275     char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
276
277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
279                 p2->no,
280                 ps,
281                 ntohs (hdr->type),
282                 ntohs (hdr->size),
283                 p->no,
284                 GNUNET_i2s (&p->id));
285     GNUNET_free (ps);
286   }
287
288   return TEST_MESSAGE_SIZE;
289 }
290
291
292 static void
293 sendtask (void *cls)
294 {
295   send_task = NULL;
296   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
297
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
300               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
301   GNUNET_free (receiver_s);
302
303
304   if (0 == messages_recv)
305   {
306     start_normal = GNUNET_TIME_absolute_get();
307   }
308   if (1 == messages_recv)
309   {
310     start_delayed = GNUNET_TIME_absolute_get();
311   }
312
313   s_sending = GNUNET_YES;
314   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
315                                                &p1->id,
316                                                TEST_MESSAGE_SIZE,
317                                                TIMEOUT_TRANSMIT,
318                                                &notify_ready,
319                                                p1);
320 }
321
322
323 static void
324 notify_connect (void *cls,
325                 const struct GNUNET_PeerIdentity *peer)
326 {
327   static int c;
328
329   c++;
330   struct PeerContext *p = cls;
331   struct PeerContext *t = NULL;
332
333   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
334     t = p1;
335   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
336     t = p2;
337   GNUNET_assert (t != NULL);
338
339   {
340     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
341
342     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
344                 p->no,
345                 ps,
346                 t->no,
347                 GNUNET_i2s (peer));
348     GNUNET_free (ps);
349   }
350 }
351
352
353 static void
354 notify_disconnect (void *cls,
355                    const struct GNUNET_PeerIdentity *peer)
356 {
357   struct PeerContext *p = cls;
358
359   {
360     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
361
362     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363                 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
364                 p->no,
365                 ps,
366                 GNUNET_i2s (peer));
367     GNUNET_free (ps);
368   }
369
370   if (th != NULL)
371   {
372     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
373     th = NULL;
374   }
375 }
376
377
378 static void
379 testing_connect_cb (struct PeerContext *p1,
380                     struct PeerContext *p2,
381                     void *cls)
382 {
383   cc = NULL;
384
385   {
386     char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
387
388     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389                 "Peers connected: %u (%s) <-> %u (%s)\n",
390                 p1->no,
391                 p1_c,
392                 p2->no,
393                 GNUNET_i2s (&p2->id));
394     GNUNET_free (p1_c);
395   }
396
397   s_connected = GNUNET_YES;
398   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
399 }
400
401
402 static void
403 start_cb (struct PeerContext *p, void *cls)
404 {
405   static int started;
406   started++;
407
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409               "Peer %u (`%s') started\n",
410               p->no,
411               GNUNET_i2s (&p->id));
412
413   if (started != 2)
414     return;
415   else
416     s_started = GNUNET_YES;
417   {
418     char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
419
420     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421                 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
422                 p1->no,
423                 sender_c,
424                 p2->no,
425                 GNUNET_i2s (&p2->id));
426     GNUNET_free (sender_c);
427   }
428
429   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
430                                                p1,
431                                                p2,
432                                                &testing_connect_cb,
433                                                NULL);
434
435 }
436
437
438 static void
439 run (void *cls, char *const *args, const char *cfgfile,
440      const struct GNUNET_CONFIGURATION_Handle *cfg)
441 {
442   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
443
444   s_started = GNUNET_NO;
445   s_connected = GNUNET_NO;
446   s_sending = GNUNET_NO;
447
448   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
449                                             &notify_receive, &notify_connect,
450                                             &notify_disconnect, &start_cb,
451                                             NULL);
452
453   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
454                                             &notify_receive, &notify_connect,
455                                             &notify_disconnect, &start_cb,
456                                             NULL);
457
458   if ((p1 == NULL) || (p2 == NULL))
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
461     if (die_task != NULL)
462       GNUNET_SCHEDULER_cancel (die_task);
463     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
464     return;
465   }
466 }
467
468
469 static int
470 check ()
471 {
472   static char *const argv[] = { "test-transport-api-manipulation",
473     "-c",
474     "test_transport_api_data.conf",
475     NULL
476   };
477   static struct GNUNET_GETOPT_CommandLineOption options[] = {
478     GNUNET_GETOPT_OPTION_END
479   };
480
481   send_task = NULL;
482
483   ok = 1;
484   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
485                       "nohelp", options, &run, &ok);
486
487   return ok;
488 }
489
490 int
491 main (int argc, char *argv[])
492 {
493   int ret;
494
495   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
496   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
497   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
498                                                  &test_plugin);
499
500   GNUNET_log_setup (test_name,
501                     "WARNING",
502                     NULL);
503   tth = GNUNET_TRANSPORT_TESTING_init ();
504
505   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
506   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
507
508   ret = check ();
509
510   GNUNET_free (cfg_file_p1);
511   GNUNET_free (cfg_file_p2);
512
513   GNUNET_free (test_source);
514   GNUNET_free (test_plugin);
515   GNUNET_free (test_name);
516
517   GNUNET_TRANSPORT_TESTING_done (tth);
518
519   return ret;
520 }
521
522 /* end of test_transport_api_manipulation_recv_tcp.c */