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