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_send_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 send delay of 1 second will
26 * be configured and 1 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;
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
94 if (send_task != NULL)
95 GNUNET_SCHEDULER_cancel (send_task);
98 GNUNET_SCHEDULER_cancel (die_task);
101 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
105 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
110 end_badly (void *cls)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
117 if (send_task != NULL)
118 GNUNET_SCHEDULER_cancel (send_task);
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
123 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
128 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
132 if (s_started == GNUNET_NO)
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
137 if (s_connected == GNUNET_NO)
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
142 if (s_sending == GNUNET_NO)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
150 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
154 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
163 sendtask (void *cls);
167 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
168 const struct GNUNET_MessageHeader *message)
170 struct PeerContext *p = cls;
171 struct PeerContext *t = NULL;
173 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
175 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
177 GNUNET_assert (t != NULL);
180 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
186 ntohs (message->type),
187 ntohs (message->size),
189 GNUNET_i2s (&t->id));
193 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
194 (TEST_MESSAGE_SIZE == ntohs (message->size)))
207 if (0 == messages_recv)
209 /* Received non-delayed message */
210 dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Received non-delayed message %u after %s\n",
214 GNUNET_STRINGS_relative_time_to_string (dur_normal,
216 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
218 if (1 == messages_recv)
220 /* Received manipulated message */
221 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Received delayed message %u after %s\n",
225 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
227 if (dur_delayed.rel_value_us < 1000 * 1000LL)
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Delayed message was not delayed correctly: took only %s\n",
233 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
245 notify_ready (void *cls, size_t size, void *buf)
247 struct PeerContext *p = cls;
248 struct GNUNET_MessageHeader *hdr;
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "Timeout occurred while waiting for transmit_ready\n");
256 if (NULL != die_task)
257 GNUNET_SCHEDULER_cancel (die_task);
258 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
263 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
264 memset (buf, '\0', TEST_MESSAGE_SIZE);
266 hdr->size = htons (TEST_MESSAGE_SIZE);
267 hdr->type = htons (TEST_MESSAGE_TYPE);
270 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
278 GNUNET_i2s (&p->id));
282 return TEST_MESSAGE_SIZE;
289 struct GNUNET_TIME_Relative delay;
290 struct GNUNET_ATS_Properties prop;
293 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
298 GNUNET_i2s (&p2->id),
301 GNUNET_free (receiver_s);
303 if (0 == messages_recv)
305 start_normal = GNUNET_TIME_absolute_get ();
307 if (1 == messages_recv)
309 memset (&prop, 0, sizeof (prop));
310 delay.rel_value_us = 1000LL * 1000LL;
311 GNUNET_TRANSPORT_set_traffic_metric (p2->th,
314 GNUNET_TIME_UNIT_ZERO,
317 GNUNET_TRANSPORT_set_traffic_metric (p1->th,
322 start_delayed = GNUNET_TIME_absolute_get();
325 s_sending = GNUNET_YES;
326 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
327 TIMEOUT_TRANSMIT, ¬ify_ready,
333 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
336 struct PeerContext *p = cls;
337 struct PeerContext *t = NULL;
340 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
342 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
344 GNUNET_assert (t != NULL);
347 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
361 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
363 struct PeerContext *p = cls;
366 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
378 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
385 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
390 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Peers connected: %u (%s) <-> %u (%s)\n",
397 GNUNET_i2s (&p2->id));
401 s_connected = GNUNET_YES;
402 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
407 start_cb (struct PeerContext *p, void *cls)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Peer %u (`%s') started\n",
415 GNUNET_i2s (&p->id));
420 s_started = GNUNET_YES;
421 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
425 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
426 GNUNET_free (sender_c);
427 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2,
435 run (void *cls, char *const *args, const char *cfgfile,
436 const struct GNUNET_CONFIGURATION_Handle *cfg)
438 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
440 s_started = GNUNET_NO;
441 s_connected = GNUNET_NO;
442 s_sending = GNUNET_NO;
444 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
445 ¬ify_receive, ¬ify_connect,
446 ¬ify_disconnect, &start_cb,
449 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
450 ¬ify_receive, ¬ify_connect,
451 ¬ify_disconnect, &start_cb,
454 if ((p1 == NULL) || (p2 == NULL))
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
457 if (die_task != NULL)
458 GNUNET_SCHEDULER_cancel (die_task);
459 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
468 static char *const argv[] = { "test-transport-api-manipulation",
470 "test_transport_api_data.conf",
473 static struct GNUNET_GETOPT_CommandLineOption options[] = {
474 GNUNET_GETOPT_OPTION_END
480 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
481 "nohelp", options, &run, &ok);
487 main (int argc, char *argv[])
491 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
492 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
493 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
496 GNUNET_log_setup (test_name,
499 tth = GNUNET_TRANSPORT_TESTING_init ();
501 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
502 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
506 GNUNET_free (cfg_file_p1);
507 GNUNET_free (cfg_file_p2);
509 GNUNET_free (test_source);
510 GNUNET_free (test_plugin);
511 GNUNET_free (test_name);
513 GNUNET_TRANSPORT_TESTING_done (tth);
518 /* end of test_transport_api_manipulation_send_tcp.c */