docu
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_send_tcp.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_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 GNUNET_SCHEDULER_TaskIdentifier die_task;
63
64 static GNUNET_SCHEDULER_TaskIdentifier send_task;
65
66 static struct PeerContext *p1;
67
68 static struct PeerContext *p2;
69
70 static 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 != GNUNET_SCHEDULER_NO_TASK)
94     GNUNET_SCHEDULER_cancel (send_task);
95
96   if (die_task != GNUNET_SCHEDULER_NO_TASK)
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 static void
108 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   die_task = GNUNET_SCHEDULER_NO_TASK;
111
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
113
114
115   if (send_task != GNUNET_SCHEDULER_NO_TASK)
116     GNUNET_SCHEDULER_cancel (send_task);
117
118   if (cc != NULL)
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
121     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
122     cc = NULL;
123   }
124
125   if (th != NULL)
126     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
127   else
128     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
129
130   if (s_started == GNUNET_NO)
131     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
132   else
133     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
134
135   if (s_connected == GNUNET_NO)
136     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
137   else
138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
139
140   if (s_sending == GNUNET_NO)
141     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
142   else
143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
144
145   th = NULL;
146
147   if (p1 != NULL)
148     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
149   else
150     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
151   if (p2 != NULL)
152     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
153   else
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
155
156   ok = GNUNET_SYSERR;
157 }
158
159
160 static void
161 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
162
163 static void
164 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
165                 const struct GNUNET_MessageHeader *message,
166                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
167 {
168   struct PeerContext *p = cls;
169   struct PeerContext *t = NULL;
170   int c;
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 (0 == messages_recv)
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 %llu\n",
206                 messages_recv,
207                 (long long unsigned int) dur_normal.rel_value);
208     send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
209   }
210   if (1 == messages_recv)
211   {
212         /* Received manipulated message */
213         dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
214       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215                   "Received delayed message %u after %llu\n",
216                   messages_recv,
217                   (long long unsigned int) dur_delayed.rel_value);
218       if (dur_delayed.rel_value < 1000)
219       {
220                                 GNUNET_break (0);
221                                 ok += 1;
222         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223                     "Delayed message was not delayed correctly: took only %llu\n",
224                     (long long unsigned int) dur_delayed.rel_value);
225       }
226       for (c = 0; c < ats_count; c++)
227       {
228         if (ntohl (ats[c].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
229         {
230                         if (ntohl (ats[c].value) == 10)
231                                 ok += 0;
232                         else
233                         {
234                                         GNUNET_break (0);
235                                         ok += 1;
236                         }
237         }
238       }
239       /* shutdown */
240       end ();
241   }
242   messages_recv ++;
243 }
244
245
246 static size_t
247 notify_ready (void *cls, size_t size, void *buf)
248 {
249   struct PeerContext *p = cls;
250   struct GNUNET_MessageHeader *hdr;
251
252   th = NULL;
253
254   if (buf == NULL)
255   {
256     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257                 "Timeout occurred while waiting for transmit_ready\n");
258     if (GNUNET_SCHEDULER_NO_TASK != die_task)
259       GNUNET_SCHEDULER_cancel (die_task);
260     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
261     ok = 42;
262     return 0;
263   }
264
265   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
266   if (buf != NULL)
267   {
268     memset (buf, '\0', TEST_MESSAGE_SIZE);
269     hdr = buf;
270     hdr->size = htons (TEST_MESSAGE_SIZE);
271     hdr->type = htons (TEST_MESSAGE_TYPE);
272   }
273
274   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
275   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
277               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
278               GNUNET_i2s (&p->id));
279   GNUNET_free (ps);
280
281   return TEST_MESSAGE_SIZE;
282 }
283
284
285 static void
286 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287 {
288   struct GNUNET_ATS_Information ats[1];
289   send_task = GNUNET_SCHEDULER_NO_TASK;
290
291   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
292     return;
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, GNUNET_i2s (&p2->id), p1->no, receiver_s);
298   GNUNET_free (receiver_s);
299
300
301   if (0 == messages_recv)
302   {
303         start_normal = GNUNET_TIME_absolute_get();
304   }
305   if (1 == messages_recv)
306   {
307                 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
308                 ats[0].value = htonl (1000);
309                 GNUNET_TRANSPORT_set_traffic_metric (p2->th, &p1->id, TM_SEND, ats, 1);
310                 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
311                 ats[0].value = htonl (10);
312                 GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_BOTH, ats, 1);
313
314                 start_delayed = GNUNET_TIME_absolute_get();
315   }
316
317   s_sending = GNUNET_YES;
318   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
319                                                TIMEOUT_TRANSMIT, &notify_ready,
320                                                p1);
321 }
322
323
324 static void
325 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
326                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
327 {
328   static int c;
329
330   c++;
331   struct PeerContext *p = cls;
332   struct PeerContext *t = NULL;
333
334   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
335     t = p1;
336   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
337     t = p2;
338   GNUNET_assert (t != NULL);
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", p->no, ps,
344               t->no, GNUNET_i2s (peer));
345   GNUNET_free (ps);
346 }
347
348
349 static void
350 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
351 {
352   struct PeerContext *p = cls;
353   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
354
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
357               GNUNET_i2s (peer));
358
359   GNUNET_free (ps);
360
361   if (th != NULL)
362     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
363   th = NULL;
364 }
365
366
367 static void
368 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
369 {
370   cc = NULL;
371   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
372
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
374               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
375   GNUNET_free (p1_c);
376
377   s_connected = GNUNET_YES;
378   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
379 }
380
381
382 static void
383 start_cb (struct PeerContext *p, void *cls)
384 {
385   static int started;
386   started++;
387
388   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
389               GNUNET_i2s (&p->id));
390
391   if (started != 2)
392     return;
393   else
394     s_started = GNUNET_YES;
395   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
396
397   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
399               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
400   GNUNET_free (sender_c);
401   /*
402   struct GNUNET_ATS_Information ats[2];
403         ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
404         ats[0].value = htonl (1000);
405         ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
406         ats[1].value = htonl (10);
407
408   GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_RECEIVE, ats, 2);
409 */
410   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
411                                                NULL);
412
413 }
414
415
416 static void
417 run (void *cls, char *const *args, const char *cfgfile,
418      const struct GNUNET_CONFIGURATION_Handle *cfg)
419 {
420   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
421
422   s_started = GNUNET_NO;
423   s_connected = GNUNET_NO;
424   s_sending = GNUNET_NO;
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
431   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
432                                             &notify_receive, &notify_connect,
433                                             &notify_disconnect, &start_cb,
434                                             NULL);
435
436   if ((p1 == NULL) || (p2 == NULL))
437   {
438     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
439     if (die_task != GNUNET_SCHEDULER_NO_TASK)
440       GNUNET_SCHEDULER_cancel (die_task);
441     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
442     return;
443   }
444 }
445
446
447 static int
448 check ()
449 {
450   static char *const argv[] = { "test-transport-api-manipulation",
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   send_task = GNUNET_SCHEDULER_NO_TASK;
460
461   ok = 1;
462   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
463                       "nohelp", options, &run, &ok);
464
465   return ok;
466 }
467
468 int
469 main (int argc, char *argv[])
470 {
471   int ret;
472
473   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
474   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
475   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
476                                                  &test_plugin);
477
478   GNUNET_log_setup (test_name,
479                     "WARNING",
480                     NULL);
481   tth = GNUNET_TRANSPORT_TESTING_init ();
482
483   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
484   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
485
486   ret = check ();
487
488   GNUNET_free (cfg_file_p1);
489   GNUNET_free (cfg_file_p2);
490
491   GNUNET_free (test_source);
492   GNUNET_free (test_plugin);
493   GNUNET_free (test_name);
494
495   GNUNET_TRANSPORT_TESTING_done (tth);
496
497   return ret;
498 }
499
500 /* end of test_transport_api.c */