2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
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.
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.
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.
21 * @file transport/test_transport_api_manipulation_recv_tcp.c
22 * @brief base test case for transport traffic manipulation implementation
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
28 * In addition the distance on receiver side will be manipulated to be 10
31 #include "gnunet_transport_service.h"
32 #include "transport-testing.h"
35 * How long until we give up on transmitting the message?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
40 * How long until we give up on transmitting the message?
42 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 #define TEST_MESSAGE_SIZE 2600
46 #define TEST_MESSAGE_TYPE 12345
48 static char *test_source;
50 static char *test_plugin;
52 static char *test_name;
58 static int s_connected;
62 static struct GNUNET_SCHEDULER_Task * die_task;
64 static struct GNUNET_SCHEDULER_Task * send_task;
66 static struct PeerContext *p1;
68 static struct PeerContext *p2;
70 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
72 static struct GNUNET_TRANSPORT_TransmitHandle *th;
74 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
76 static char *cfg_file_p1;
78 static char *cfg_file_p2;
80 static int messages_recv;
82 static struct GNUNET_TIME_Absolute start_normal;
83 static struct GNUNET_TIME_Relative dur_normal;
85 static struct GNUNET_TIME_Absolute start_delayed;
86 static struct GNUNET_TIME_Relative dur_delayed;
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
93 if (send_task != NULL)
94 GNUNET_SCHEDULER_cancel (send_task);
97 GNUNET_SCHEDULER_cancel (die_task);
100 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
103 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
104 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
115 if (send_task != NULL)
116 GNUNET_SCHEDULER_cancel (send_task);
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
121 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
126 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
130 if (s_started == GNUNET_NO)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
135 if (s_connected == GNUNET_NO)
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
140 if (s_sending == GNUNET_NO)
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
148 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
152 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
161 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
164 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
165 const struct GNUNET_MessageHeader *message)
167 struct PeerContext *p = cls;
168 struct PeerContext *t = NULL;
170 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
172 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
174 GNUNET_assert (t != NULL);
176 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
180 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
181 GNUNET_i2s (&t->id));
184 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
185 (TEST_MESSAGE_SIZE == ntohs (message->size)))
198 if (messages_recv <= 1)
200 /* Received non-delayed message */
201 dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Received non-delayed message %u after %s\n",
205 GNUNET_STRINGS_relative_time_to_string (dur_normal,
208 struct GNUNET_ATS_Properties prop;
209 struct GNUNET_TIME_Relative delay;
211 delay.rel_value_us = 1000 * 1000LL;
212 memset (&prop, 0, sizeof (prop));
214 GNUNET_TRANSPORT_set_traffic_metric (p1->th,
218 GNUNET_TIME_UNIT_ZERO);
219 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
221 if (2 == messages_recv)
223 /* Received manipulated message */
224 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Received delayed message %u after %s\n",
228 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
230 if (dur_delayed.rel_value_us < 1000 * 1000LL)
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Delayed message was not delayed correctly: took only %s\n",
236 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
248 notify_ready (void *cls, size_t size, void *buf)
250 struct PeerContext *p = cls;
251 struct GNUNET_MessageHeader *hdr;
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "Timeout occurred while waiting for transmit_ready\n");
259 if (NULL != die_task)
260 GNUNET_SCHEDULER_cancel (die_task);
261 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
266 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
267 memset (buf, '\0', TEST_MESSAGE_SIZE);
269 hdr->size = htons (TEST_MESSAGE_SIZE);
270 hdr->type = htons (TEST_MESSAGE_TYPE);
273 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",
282 GNUNET_i2s (&p->id));
286 return TEST_MESSAGE_SIZE;
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
296 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
298 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
302 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
303 GNUNET_free (receiver_s);
306 if (0 == messages_recv)
308 start_normal = GNUNET_TIME_absolute_get();
310 if (1 == messages_recv)
312 start_delayed = GNUNET_TIME_absolute_get();
315 s_sending = GNUNET_YES;
316 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
326 notify_connect (void *cls,
327 const struct GNUNET_PeerIdentity *peer)
332 struct PeerContext *p = cls;
333 struct PeerContext *t = NULL;
335 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
337 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
339 GNUNET_assert (t != NULL);
342 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
356 notify_disconnect (void *cls,
357 const struct GNUNET_PeerIdentity *peer)
359 struct PeerContext *p = cls;
362 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
374 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
381 testing_connect_cb (struct PeerContext *p1,
382 struct PeerContext *p2,
388 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Peers connected: %u (%s) <-> %u (%s)\n",
395 GNUNET_i2s (&p2->id));
399 s_connected = GNUNET_YES;
400 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
405 start_cb (struct PeerContext *p, void *cls)
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Peer %u (`%s') started\n",
413 GNUNET_i2s (&p->id));
418 s_started = GNUNET_YES;
420 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
427 GNUNET_i2s (&p2->id));
428 GNUNET_free (sender_c);
431 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
441 run (void *cls, char *const *args, const char *cfgfile,
442 const struct GNUNET_CONFIGURATION_Handle *cfg)
444 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
446 s_started = GNUNET_NO;
447 s_connected = GNUNET_NO;
448 s_sending = GNUNET_NO;
450 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
451 ¬ify_receive, ¬ify_connect,
452 ¬ify_disconnect, &start_cb,
455 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
456 ¬ify_receive, ¬ify_connect,
457 ¬ify_disconnect, &start_cb,
460 if ((p1 == NULL) || (p2 == NULL))
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
463 if (die_task != NULL)
464 GNUNET_SCHEDULER_cancel (die_task);
465 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
474 static char *const argv[] = { "test-transport-api-manipulation",
476 "test_transport_api_data.conf",
479 static struct GNUNET_GETOPT_CommandLineOption options[] = {
480 GNUNET_GETOPT_OPTION_END
486 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
487 "nohelp", options, &run, &ok);
493 main (int argc, char *argv[])
497 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
498 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
499 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
502 GNUNET_log_setup (test_name,
505 tth = GNUNET_TRANSPORT_TESTING_init ();
507 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
508 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
512 GNUNET_free (cfg_file_p1);
513 GNUNET_free (cfg_file_p2);
515 GNUNET_free (test_source);
516 GNUNET_free (test_plugin);
517 GNUNET_free (test_name);
519 GNUNET_TRANSPORT_TESTING_done (tth);
524 /* end of test_transport_api_manipulation_recv_tcp.c */