2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier die_task;
64 static GNUNET_SCHEDULER_TaskIdentifier send_task;
66 static struct PeerContext *p1;
68 static struct PeerContext *p2;
70 static 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 != GNUNET_SCHEDULER_NO_TASK)
94 GNUNET_SCHEDULER_cancel (send_task);
96 if (die_task != GNUNET_SCHEDULER_NO_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)
110 die_task = GNUNET_SCHEDULER_NO_TASK;
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
115 if (send_task != GNUNET_SCHEDULER_NO_TASK)
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,
166 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
168 struct PeerContext *p = cls;
169 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 %llu\n",
207 (long long unsigned int) dur_normal.rel_value);
209 struct GNUNET_ATS_Information ats[2];
210 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
211 ats[0].value = htonl (1000);
212 ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
213 ats[1].value = htonl (10);
215 GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_RECEIVE, ats, 2);
216 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
218 if (2 == 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 %llu\n",
225 (long long unsigned int) dur_delayed.rel_value);
226 if (dur_delayed.rel_value < 1000)
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Delayed message was not delayed correctly: took only %llu\n",
232 (long long unsigned int) dur_delayed.rel_value);
234 for (c = 0; c < ats_count; c++)
236 if (ntohl (ats[c].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
238 if (ntohl (ats[c].value) == 10)
256 notify_ready (void *cls, size_t size, void *buf)
258 struct PeerContext *p = cls;
259 struct GNUNET_MessageHeader *hdr;
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "Timeout occurred while waiting for transmit_ready\n");
267 if (GNUNET_SCHEDULER_NO_TASK != die_task)
268 GNUNET_SCHEDULER_cancel (die_task);
269 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
274 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
277 memset (buf, '\0', TEST_MESSAGE_SIZE);
279 hdr->size = htons (TEST_MESSAGE_SIZE);
280 hdr->type = htons (TEST_MESSAGE_TYPE);
283 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
286 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
287 GNUNET_i2s (&p->id));
290 return TEST_MESSAGE_SIZE;
295 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
297 send_task = GNUNET_SCHEDULER_NO_TASK;
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, &p1->id, TEST_MESSAGE_SIZE, 0,
320 TIMEOUT_TRANSMIT, ¬ify_ready,
326 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
327 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
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);
341 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
345 t->no, GNUNET_i2s (peer));
351 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
353 struct PeerContext *p = cls;
354 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
363 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
369 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
372 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
375 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
378 s_connected = GNUNET_YES;
379 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
384 start_cb (struct PeerContext *p, void *cls)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
390 GNUNET_i2s (&p->id));
395 s_started = GNUNET_YES;
396 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
400 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
401 GNUNET_free (sender_c);
403 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
410 run (void *cls, char *const *args, const char *cfgfile,
411 const struct GNUNET_CONFIGURATION_Handle *cfg)
413 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
415 s_started = GNUNET_NO;
416 s_connected = GNUNET_NO;
417 s_sending = GNUNET_NO;
419 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
420 ¬ify_receive, ¬ify_connect,
421 ¬ify_disconnect, &start_cb,
424 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
425 ¬ify_receive, ¬ify_connect,
426 ¬ify_disconnect, &start_cb,
429 if ((p1 == NULL) || (p2 == NULL))
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
432 if (die_task != GNUNET_SCHEDULER_NO_TASK)
433 GNUNET_SCHEDULER_cancel (die_task);
434 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
443 static char *const argv[] = { "test-transport-api-manipulation",
445 "test_transport_api_data.conf",
448 static struct GNUNET_GETOPT_CommandLineOption options[] = {
449 GNUNET_GETOPT_OPTION_END
452 send_task = GNUNET_SCHEDULER_NO_TASK;
455 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
456 "nohelp", options, &run, &ok);
462 main (int argc, char *argv[])
466 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
467 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
468 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
471 GNUNET_log_setup (test_name,
474 tth = GNUNET_TRANSPORT_TESTING_init ();
476 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
477 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
481 GNUNET_free (cfg_file_p1);
482 GNUNET_free (cfg_file_p2);
484 GNUNET_free (test_source);
485 GNUNET_free (test_plugin);
486 GNUNET_free (test_name);
488 GNUNET_TRANSPORT_TESTING_done (tth);
493 /* end of test_transport_api_manipulation_recv_tcp.c */