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);
109 end_badly (void *cls)
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
116 if (send_task != NULL)
117 GNUNET_SCHEDULER_cancel (send_task);
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
122 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
127 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
131 if (s_started == GNUNET_NO)
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
136 if (s_connected == GNUNET_NO)
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
141 if (s_sending == GNUNET_NO)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
149 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
153 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
162 sendtask (void *cls);
166 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
167 const struct GNUNET_MessageHeader *message)
169 struct PeerContext *p = cls;
170 struct PeerContext *t = NULL;
172 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
174 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
176 GNUNET_assert (t != NULL);
178 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
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));
186 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
187 (TEST_MESSAGE_SIZE == ntohs (message->size)))
200 if (messages_recv <= 1)
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",
207 GNUNET_STRINGS_relative_time_to_string (dur_normal,
210 struct GNUNET_ATS_Properties prop;
211 struct GNUNET_TIME_Relative delay;
213 delay.rel_value_us = 1000 * 1000LL;
214 memset (&prop, 0, sizeof (prop));
216 GNUNET_TRANSPORT_set_traffic_metric (p1->th,
220 GNUNET_TIME_UNIT_ZERO);
221 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
223 if (2 == messages_recv)
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",
230 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
232 if (dur_delayed.rel_value_us < 1000 * 1000LL)
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,
250 notify_ready (void *cls, size_t size, void *buf)
252 struct PeerContext *p = cls;
253 struct GNUNET_MessageHeader *hdr;
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);
268 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
269 memset (buf, '\0', TEST_MESSAGE_SIZE);
271 hdr->size = htons (TEST_MESSAGE_SIZE);
272 hdr->type = htons (TEST_MESSAGE_TYPE);
275 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
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",
284 GNUNET_i2s (&p->id));
288 return TEST_MESSAGE_SIZE;
295 const struct GNUNET_SCHEDULER_TaskContext *tc;
298 tc = GNUNET_SCHEDULER_get_task_context ();
299 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
301 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
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);
309 if (0 == messages_recv)
311 start_normal = GNUNET_TIME_absolute_get();
313 if (1 == messages_recv)
315 start_delayed = GNUNET_TIME_absolute_get();
318 s_sending = GNUNET_YES;
319 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
329 notify_connect (void *cls,
330 const struct GNUNET_PeerIdentity *peer)
335 struct PeerContext *p = cls;
336 struct PeerContext *t = NULL;
338 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
340 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
342 GNUNET_assert (t != NULL);
345 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
359 notify_disconnect (void *cls,
360 const struct GNUNET_PeerIdentity *peer)
362 struct PeerContext *p = cls;
365 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
377 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
384 testing_connect_cb (struct PeerContext *p1,
385 struct PeerContext *p2,
391 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Peers connected: %u (%s) <-> %u (%s)\n",
398 GNUNET_i2s (&p2->id));
402 s_connected = GNUNET_YES;
403 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
408 start_cb (struct PeerContext *p, void *cls)
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Peer %u (`%s') started\n",
416 GNUNET_i2s (&p->id));
421 s_started = GNUNET_YES;
423 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
430 GNUNET_i2s (&p2->id));
431 GNUNET_free (sender_c);
434 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
444 run (void *cls, char *const *args, const char *cfgfile,
445 const struct GNUNET_CONFIGURATION_Handle *cfg)
447 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
449 s_started = GNUNET_NO;
450 s_connected = GNUNET_NO;
451 s_sending = GNUNET_NO;
453 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
454 ¬ify_receive, ¬ify_connect,
455 ¬ify_disconnect, &start_cb,
458 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
459 ¬ify_receive, ¬ify_connect,
460 ¬ify_disconnect, &start_cb,
463 if ((p1 == NULL) || (p2 == NULL))
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);
477 static char *const argv[] = { "test-transport-api-manipulation",
479 "test_transport_api_data.conf",
482 static struct GNUNET_GETOPT_CommandLineOption options[] = {
483 GNUNET_GETOPT_OPTION_END
489 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
490 "nohelp", options, &run, &ok);
496 main (int argc, char *argv[])
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,
505 GNUNET_log_setup (test_name,
508 tth = GNUNET_TRANSPORT_TESTING_init ();
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);
515 GNUNET_free (cfg_file_p1);
516 GNUNET_free (cfg_file_p2);
518 GNUNET_free (test_source);
519 GNUNET_free (test_plugin);
520 GNUNET_free (test_name);
522 GNUNET_TRANSPORT_TESTING_done (tth);
527 /* end of test_transport_api_manipulation_recv_tcp.c */