7b82236f9ac29ff6cbf14a38c52bc548a84f35c3
[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   const struct GNUNET_SCHEDULER_TaskContext *tc;
296
297   send_task = NULL;
298   tc = GNUNET_SCHEDULER_get_task_context ();
299   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
300     return;
301   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
302
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
305               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
306   GNUNET_free (receiver_s);
307
308
309   if (0 == messages_recv)
310   {
311     start_normal = GNUNET_TIME_absolute_get();
312   }
313   if (1 == messages_recv)
314   {
315     start_delayed = GNUNET_TIME_absolute_get();
316   }
317
318   s_sending = GNUNET_YES;
319   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
320                                                &p1->id,
321                                                TEST_MESSAGE_SIZE,
322                                                TIMEOUT_TRANSMIT,
323                                                &notify_ready,
324                                                p1);
325 }
326
327
328 static void
329 notify_connect (void *cls,
330                 const struct GNUNET_PeerIdentity *peer)
331 {
332   static int c;
333
334   c++;
335   struct PeerContext *p = cls;
336   struct PeerContext *t = NULL;
337
338   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
339     t = p1;
340   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
341     t = p2;
342   GNUNET_assert (t != NULL);
343
344   {
345     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
346
347     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
349                 p->no,
350                 ps,
351                 t->no,
352                 GNUNET_i2s (peer));
353     GNUNET_free (ps);
354   }
355 }
356
357
358 static void
359 notify_disconnect (void *cls,
360                    const struct GNUNET_PeerIdentity *peer)
361 {
362   struct PeerContext *p = cls;
363
364   {
365     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
366
367     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368                 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
369                 p->no,
370                 ps,
371                 GNUNET_i2s (peer));
372     GNUNET_free (ps);
373   }
374
375   if (th != NULL)
376   {
377     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
378     th = NULL;
379   }
380 }
381
382
383 static void
384 testing_connect_cb (struct PeerContext *p1,
385                     struct PeerContext *p2,
386                     void *cls)
387 {
388   cc = NULL;
389
390   {
391     char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
392
393     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394                 "Peers connected: %u (%s) <-> %u (%s)\n",
395                 p1->no,
396                 p1_c,
397                 p2->no,
398                 GNUNET_i2s (&p2->id));
399     GNUNET_free (p1_c);
400   }
401
402   s_connected = GNUNET_YES;
403   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
404 }
405
406
407 static void
408 start_cb (struct PeerContext *p, void *cls)
409 {
410   static int started;
411   started++;
412
413   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414               "Peer %u (`%s') started\n",
415               p->no,
416               GNUNET_i2s (&p->id));
417
418   if (started != 2)
419     return;
420   else
421     s_started = GNUNET_YES;
422   {
423     char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
424
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426                 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
427                 p1->no,
428                 sender_c,
429                 p2->no,
430                 GNUNET_i2s (&p2->id));
431     GNUNET_free (sender_c);
432   }
433
434   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
435                                                p1,
436                                                p2,
437                                                &testing_connect_cb,
438                                                NULL);
439
440 }
441
442
443 static void
444 run (void *cls, char *const *args, const char *cfgfile,
445      const struct GNUNET_CONFIGURATION_Handle *cfg)
446 {
447   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
448
449   s_started = GNUNET_NO;
450   s_connected = GNUNET_NO;
451   s_sending = GNUNET_NO;
452
453   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
454                                             &notify_receive, &notify_connect,
455                                             &notify_disconnect, &start_cb,
456                                             NULL);
457
458   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
459                                             &notify_receive, &notify_connect,
460                                             &notify_disconnect, &start_cb,
461                                             NULL);
462
463   if ((p1 == NULL) || (p2 == NULL))
464   {
465     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
466     if (die_task != NULL)
467       GNUNET_SCHEDULER_cancel (die_task);
468     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
469     return;
470   }
471 }
472
473
474 static int
475 check ()
476 {
477   static char *const argv[] = { "test-transport-api-manipulation",
478     "-c",
479     "test_transport_api_data.conf",
480     NULL
481   };
482   static struct GNUNET_GETOPT_CommandLineOption options[] = {
483     GNUNET_GETOPT_OPTION_END
484   };
485
486   send_task = NULL;
487
488   ok = 1;
489   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
490                       "nohelp", options, &run, &ok);
491
492   return ok;
493 }
494
495 int
496 main (int argc, char *argv[])
497 {
498   int ret;
499
500   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
501   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
502   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
503                                                  &test_plugin);
504
505   GNUNET_log_setup (test_name,
506                     "WARNING",
507                     NULL);
508   tth = GNUNET_TRANSPORT_TESTING_init ();
509
510   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
511   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
512
513   ret = check ();
514
515   GNUNET_free (cfg_file_p1);
516   GNUNET_free (cfg_file_p2);
517
518   GNUNET_free (test_source);
519   GNUNET_free (test_plugin);
520   GNUNET_free (test_name);
521
522   GNUNET_TRANSPORT_TESTING_done (tth);
523
524   return ret;
525 }
526
527 /* end of test_transport_api_manipulation_recv_tcp.c */