2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2016 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
49 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
51 static struct GNUNET_TRANSPORT_TransmitHandle *th;
53 static struct GNUNET_SCHEDULER_Task *send_task;
55 static int messages_recv;
57 static struct GNUNET_TIME_Absolute start_normal;
59 static struct GNUNET_TIME_Relative dur_normal;
61 static struct GNUNET_TIME_Absolute start_delayed;
63 static struct GNUNET_TIME_Relative dur_delayed;
67 custom_shutdown (void *cls)
71 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
74 if (NULL != send_task)
76 GNUNET_SCHEDULER_cancel (send_task);
87 notify_receive (void *cls,
88 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
89 const struct GNUNET_PeerIdentity *sender,
90 const struct GNUNET_MessageHeader *message)
93 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
99 ntohs (message->type),
100 ntohs (message->size),
101 GNUNET_i2s (sender));
104 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
105 (TEST_MESSAGE_SIZE == ntohs (message->size)))
107 if (GNUNET_NO == ccc->global_ret)
108 ccc->global_ret = GNUNET_OK;
113 ccc->global_ret = GNUNET_SYSERR;
114 GNUNET_SCHEDULER_shutdown ();
118 if (messages_recv <= 1)
120 /* Received non-delayed message */
121 dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "Received non-delayed message %u after %s\n",
125 GNUNET_STRINGS_relative_time_to_string (dur_normal,
128 struct GNUNET_ATS_Properties prop;
129 struct GNUNET_TIME_Relative delay;
131 delay.rel_value_us = 1000 * 1000LL;
132 memset (&prop, 0, sizeof (prop));
134 GNUNET_TRANSPORT_set_traffic_metric (ccc->p[0]->th,
138 GNUNET_TIME_UNIT_ZERO);
139 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
141 if (2 == messages_recv)
143 /* Received manipulated message */
144 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Received delayed message %u after %s\n",
148 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
150 if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
153 ccc->global_ret = GNUNET_SYSERR;
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "Delayed message was not delayed correctly: took only %s\n",
156 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
160 GNUNET_SCHEDULER_shutdown ();
167 notify_ready (void *cls, size_t size, void *buf)
169 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
170 struct GNUNET_MessageHeader *hdr;
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Timeout occurred while waiting for transmit_ready\n");
177 GNUNET_SCHEDULER_shutdown ();
178 ccc->global_ret = 42;
182 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
183 memset (buf, '\0', TEST_MESSAGE_SIZE);
185 hdr->size = htons (TEST_MESSAGE_SIZE);
186 hdr->type = htons (TEST_MESSAGE_TYPE);
189 char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Peer %u (`%s') sending message with type %u and size %u bytes to peer %u (`%s')\n",
198 GNUNET_i2s (&p->id));
202 return TEST_MESSAGE_SIZE;
209 if (NULL == send_task)
210 ccc->global_ret = GNUNET_NO; /* first call! */
213 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
217 ccc->p[1]->no, GNUNET_i2s (&ccc->p[1]->id), ccc->p[0]->no, receiver_s);
218 GNUNET_free (receiver_s);
220 if (0 == messages_recv)
222 start_normal = GNUNET_TIME_absolute_get();
224 if (1 == messages_recv)
226 start_delayed = GNUNET_TIME_absolute_get();
229 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
239 notify_disconnect (void *cls,
240 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
241 const struct GNUNET_PeerIdentity *other)
243 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
248 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
255 main (int argc, char *argv[])
257 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
258 .connect_continuation = &sendtask,
259 .config_file = "test_transport_api_data.conf",
260 .rec = ¬ify_receive,
261 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
262 .nd = ¬ify_disconnect,
263 .shutdown_task = &custom_shutdown,
269 GNUNET_TRANSPORT_TESTING_main (2,
270 &GNUNET_TRANSPORT_TESTING_connect_check,
276 /* end of test_transport_api_manipulation_recv_tcp.c */