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
25 * Peer 1 has inbound and outbound delay of 100ms
26 * Peer 2 has no inbound and outbound delay
28 * We send a request from P1 to P2 and expect delay of >= TEST_DELAY us
29 * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY us
32 #include "gnunet_transport_service.h"
33 #include "transport-testing.h"
36 * How long until we give up on transmitting the message?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41 * How long until we give up on transmitting the message?
43 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45 #define TEST_MESSAGE_SIZE 2600
47 #define TEST_REQUEST_MESSAGE_TYPE 12345
49 #define TEST_RESPONSE_MESSAGE_TYPE 12346
52 * Test delay, in microseconds.
54 #define TEST_DELAY 100 * 1000LL
56 static char *test_source;
58 static char *test_plugin;
60 static char *test_name;
66 static int s_connected;
70 static struct GNUNET_SCHEDULER_Task * die_task;
72 static struct GNUNET_SCHEDULER_Task * send_task;
74 static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
76 static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
78 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
80 static struct GNUNET_TRANSPORT_TransmitHandle *th;
82 static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
84 static char *cfg_file_p1;
86 static char *cfg_file_p2;
88 static struct GNUNET_TIME_Absolute start_request;
89 static struct GNUNET_TIME_Absolute start_response;
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
96 if (send_task != NULL)
97 GNUNET_SCHEDULER_cancel (send_task);
100 GNUNET_SCHEDULER_cancel (die_task);
103 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
106 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
107 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
112 end_badly (void *cls)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
119 if (send_task != NULL)
120 GNUNET_SCHEDULER_cancel (send_task);
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
125 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
130 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
134 if (s_started == GNUNET_NO)
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
139 if (s_connected == GNUNET_NO)
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
144 if (s_sending == GNUNET_NO)
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
152 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
156 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
165 notify_request_ready (void *cls, size_t size, void *buf)
167 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
168 struct GNUNET_MessageHeader *hdr;
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Timeout occurred while waiting for transmit_ready\n");
176 if (NULL != die_task)
177 GNUNET_SCHEDULER_cancel (die_task);
178 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
183 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
184 memset (buf, '\0', TEST_MESSAGE_SIZE);
186 hdr->size = htons (TEST_MESSAGE_SIZE);
187 hdr->type = htons (TEST_REQUEST_MESSAGE_TYPE);
190 char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Sending request message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
198 GNUNET_i2s (&p->id));
202 return TEST_MESSAGE_SIZE;
207 sendtask_request_task (void *cls)
210 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
214 p1->no, GNUNET_i2s (&p1->id), p2->no, receiver_s);
215 GNUNET_free (receiver_s);
217 s_sending = GNUNET_YES;
218 start_request = GNUNET_TIME_absolute_get();
219 th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, TEST_MESSAGE_SIZE,
220 TIMEOUT_TRANSMIT, ¬ify_request_ready,
226 notify_response_ready (void *cls, size_t size, void *buf)
228 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
229 struct GNUNET_MessageHeader *hdr;
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Timeout occurred while waiting for transmit_ready\n");
237 if (NULL != die_task)
238 GNUNET_SCHEDULER_cancel (die_task);
239 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
244 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
245 memset (buf, '\0', TEST_MESSAGE_SIZE);
247 hdr->size = htons (TEST_MESSAGE_SIZE);
248 hdr->type = htons (TEST_RESPONSE_MESSAGE_TYPE);
251 char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Sending response message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
259 GNUNET_i2s (&p->id));
263 return TEST_MESSAGE_SIZE;
268 sendtask_response_task (void *cls)
272 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
277 GNUNET_i2s (&p2->id),
280 GNUNET_free (receiver_s);
283 s_sending = GNUNET_YES;
284 start_response = GNUNET_TIME_absolute_get();
285 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
289 ¬ify_response_ready,
296 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
297 const struct GNUNET_MessageHeader *message)
299 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
300 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL;
301 struct GNUNET_TIME_Relative duration;
303 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
305 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
307 GNUNET_assert (t != NULL);
309 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
313 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
314 GNUNET_i2s (&t->id));
317 switch (ntohs (message->type)) {
318 case TEST_REQUEST_MESSAGE_TYPE:
319 duration = GNUNET_TIME_absolute_get_difference(start_request,
320 GNUNET_TIME_absolute_get());
321 if (duration.rel_value_us >= TEST_DELAY)
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323 "Request message was delayed for %s\n",
324 GNUNET_STRINGS_relative_time_to_string (duration,
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "Request message was delayed for unexpected duration %s\n",
330 GNUNET_STRINGS_relative_time_to_string (duration,
336 send_task = GNUNET_SCHEDULER_add_now (&sendtask_response_task, NULL);
339 case TEST_RESPONSE_MESSAGE_TYPE:
340 duration = GNUNET_TIME_absolute_get_difference(start_response,
341 GNUNET_TIME_absolute_get());
342 if (duration.rel_value_us >= TEST_DELAY)
343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
344 "Response message was delayed for %s\n",
345 GNUNET_STRINGS_relative_time_to_string (duration,
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 "Response message was delayed for unexpected duration %s\n",
351 GNUNET_STRINGS_relative_time_to_string (duration,
367 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
372 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
373 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL;
375 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
377 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
379 GNUNET_assert (t != NULL);
381 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
385 t->no, GNUNET_i2s (peer));
391 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
393 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
394 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
403 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
409 testing_connect_cb (void *cls)
412 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
415 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
418 s_connected = GNUNET_YES;
419 send_task = GNUNET_SCHEDULER_add_now (&sendtask_request_task, NULL);
424 start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
430 GNUNET_i2s (&p->id));
435 s_started = GNUNET_YES;
436 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
440 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
441 GNUNET_free (sender_c);
443 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb,
450 run (void *cls, char *const *args, const char *cfgfile,
451 const struct GNUNET_CONFIGURATION_Handle *cfg)
453 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
455 s_started = GNUNET_NO;
456 s_connected = GNUNET_NO;
457 s_sending = GNUNET_NO;
459 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
460 ¬ify_receive, ¬ify_connect,
461 ¬ify_disconnect, &start_cb,
464 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
465 ¬ify_receive, ¬ify_connect,
466 ¬ify_disconnect, &start_cb,
469 if ((p1 == NULL) || (p2 == NULL))
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
472 if (die_task != NULL)
473 GNUNET_SCHEDULER_cancel (die_task);
474 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
483 static char *const argv[] = { "test-transport-api-manipulation",
485 "test_transport_api_data.conf",
488 static struct GNUNET_GETOPT_CommandLineOption options[] = {
489 GNUNET_GETOPT_OPTION_END
495 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
496 "nohelp", options, &run, &ok);
502 main (int argc, char *argv[])
506 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]);
507 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__);
508 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
511 GNUNET_log_setup (test_name,
514 tth = GNUNET_TRANSPORT_TESTING_init ();
516 cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1);
517 cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2);
521 GNUNET_free (cfg_file_p1);
522 GNUNET_free (cfg_file_p2);
524 GNUNET_free (test_source);
525 GNUNET_free (test_plugin);
526 GNUNET_free (test_name);
528 GNUNET_TRANSPORT_TESTING_done (tth);
533 /* end of test_transport_api.c */