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;
296 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
300 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
301 GNUNET_free (receiver_s);
304 if (0 == messages_recv)
306 start_normal = GNUNET_TIME_absolute_get();
308 if (1 == messages_recv)
310 start_delayed = GNUNET_TIME_absolute_get();
313 s_sending = GNUNET_YES;
314 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
324 notify_connect (void *cls,
325 const struct GNUNET_PeerIdentity *peer)
330 struct PeerContext *p = cls;
331 struct PeerContext *t = NULL;
333 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
335 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
337 GNUNET_assert (t != NULL);
340 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
354 notify_disconnect (void *cls,
355 const struct GNUNET_PeerIdentity *peer)
357 struct PeerContext *p = cls;
360 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
372 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
379 testing_connect_cb (struct PeerContext *p1,
380 struct PeerContext *p2,
386 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Peers connected: %u (%s) <-> %u (%s)\n",
393 GNUNET_i2s (&p2->id));
397 s_connected = GNUNET_YES;
398 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
403 start_cb (struct PeerContext *p, void *cls)
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Peer %u (`%s') started\n",
411 GNUNET_i2s (&p->id));
416 s_started = GNUNET_YES;
418 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
425 GNUNET_i2s (&p2->id));
426 GNUNET_free (sender_c);
429 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
439 run (void *cls, char *const *args, const char *cfgfile,
440 const struct GNUNET_CONFIGURATION_Handle *cfg)
442 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
444 s_started = GNUNET_NO;
445 s_connected = GNUNET_NO;
446 s_sending = GNUNET_NO;
448 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
449 ¬ify_receive, ¬ify_connect,
450 ¬ify_disconnect, &start_cb,
453 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
454 ¬ify_receive, ¬ify_connect,
455 ¬ify_disconnect, &start_cb,
458 if ((p1 == NULL) || (p2 == NULL))
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
461 if (die_task != NULL)
462 GNUNET_SCHEDULER_cancel (die_task);
463 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
472 static char *const argv[] = { "test-transport-api-manipulation",
474 "test_transport_api_data.conf",
477 static struct GNUNET_GETOPT_CommandLineOption options[] = {
478 GNUNET_GETOPT_OPTION_END
484 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
485 "nohelp", options, &run, &ok);
491 main (int argc, char *argv[])
495 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
496 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
497 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
500 GNUNET_log_setup (test_name,
503 tth = GNUNET_TRANSPORT_TESTING_init ();
505 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
506 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
510 GNUNET_free (cfg_file_p1);
511 GNUNET_free (cfg_file_p2);
513 GNUNET_free (test_source);
514 GNUNET_free (test_plugin);
515 GNUNET_free (test_name);
517 GNUNET_TRANSPORT_TESTING_done (tth);
522 /* end of test_transport_api_manipulation_recv_tcp.c */