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_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 ms
29 * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY ms
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
51 #define TEST_DELAY 100
53 static char *test_source;
55 static char *test_plugin;
57 static char *test_name;
63 static int s_connected;
67 static GNUNET_SCHEDULER_TaskIdentifier die_task;
69 static GNUNET_SCHEDULER_TaskIdentifier send_task;
71 static struct PeerContext *p1;
73 static struct PeerContext *p2;
75 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
77 static struct GNUNET_TRANSPORT_TransmitHandle *th;
79 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
81 static char *cfg_file_p1;
83 static char *cfg_file_p2;
85 static struct GNUNET_TIME_Absolute start_request;
86 static struct GNUNET_TIME_Absolute start_response;
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 notify_request_ready (void *cls, size_t size, void *buf)
163 struct PeerContext *p = cls;
164 struct GNUNET_MessageHeader *hdr;
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 "Timeout occurred while waiting for transmit_ready\n");
172 if (GNUNET_SCHEDULER_NO_TASK != die_task)
173 GNUNET_SCHEDULER_cancel (die_task);
174 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
179 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
182 memset (buf, '\0', TEST_MESSAGE_SIZE);
184 hdr->size = htons (TEST_MESSAGE_SIZE);
185 hdr->type = htons (TEST_REQUEST_MESSAGE_TYPE);
188 char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Sending request message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
191 p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
192 GNUNET_i2s (&p->id));
194 return TEST_MESSAGE_SIZE;
199 sendtask_request_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
201 send_task = GNUNET_SCHEDULER_NO_TASK;
203 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
205 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
209 p1->no, GNUNET_i2s (&p1->id), p2->no, receiver_s);
210 GNUNET_free (receiver_s);
212 s_sending = GNUNET_YES;
213 start_request = GNUNET_TIME_absolute_get();
214 th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, TEST_MESSAGE_SIZE, 0,
215 TIMEOUT_TRANSMIT, ¬ify_request_ready,
221 notify_response_ready (void *cls, size_t size, void *buf)
223 struct PeerContext *p = cls;
224 struct GNUNET_MessageHeader *hdr;
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Timeout occurred while waiting for transmit_ready\n");
232 if (GNUNET_SCHEDULER_NO_TASK != die_task)
233 GNUNET_SCHEDULER_cancel (die_task);
234 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
242 memset (buf, '\0', TEST_MESSAGE_SIZE);
244 hdr->size = htons (TEST_MESSAGE_SIZE);
245 hdr->type = htons (TEST_RESPONSE_MESSAGE_TYPE);
248 char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Sending response message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
251 p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
252 GNUNET_i2s (&p->id));
255 return TEST_MESSAGE_SIZE;
259 sendtask_response_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 send_task = GNUNET_SCHEDULER_NO_TASK;
263 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
265 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
269 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
270 GNUNET_free (receiver_s);
272 s_sending = GNUNET_YES;
273 start_response = GNUNET_TIME_absolute_get();
274 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
275 TIMEOUT_TRANSMIT, ¬ify_response_ready,
282 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
283 const struct GNUNET_MessageHeader *message)
285 struct PeerContext *p = cls;
286 struct PeerContext *t = NULL;
287 struct GNUNET_TIME_Relative duration;
289 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
291 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
293 GNUNET_assert (t != NULL);
295 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
299 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
300 GNUNET_i2s (&t->id));
303 switch (ntohs (message->type)) {
304 case TEST_REQUEST_MESSAGE_TYPE:
305 duration = GNUNET_TIME_absolute_get_difference(start_request,
306 GNUNET_TIME_absolute_get());
307 if (duration.rel_value >= TEST_DELAY)
308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
309 "Request message was delayed for %llu ms\n",
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 "Request message was delayed for %llu ms, should be %u ms\n",
315 duration.rel_value, TEST_DELAY);
320 send_task = GNUNET_SCHEDULER_add_now (&sendtask_response_task, NULL);
323 case TEST_RESPONSE_MESSAGE_TYPE:
324 duration = GNUNET_TIME_absolute_get_difference(start_response,
325 GNUNET_TIME_absolute_get());
326 if (duration.rel_value >= TEST_DELAY)
327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328 "Response message was delayed for %llu ms\n", duration);
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 "Response message was delayed for %llu ms, should be %u ms\n",
333 duration.rel_value, TEST_DELAY);
348 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
353 struct PeerContext *p = cls;
354 struct PeerContext *t = NULL;
356 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
358 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
360 GNUNET_assert (t != NULL);
362 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
366 t->no, GNUNET_i2s (peer));
372 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
374 struct PeerContext *p = cls;
375 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
384 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
390 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
393 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
396 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
399 s_connected = GNUNET_YES;
400 send_task = GNUNET_SCHEDULER_add_now (&sendtask_request_task, NULL);
405 start_cb (struct PeerContext *p, void *cls)
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
411 GNUNET_i2s (&p->id));
416 s_started = GNUNET_YES;
417 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
421 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
422 GNUNET_free (sender_c);
424 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
431 run (void *cls, char *const *args, const char *cfgfile,
432 const struct GNUNET_CONFIGURATION_Handle *cfg)
434 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
436 s_started = GNUNET_NO;
437 s_connected = GNUNET_NO;
438 s_sending = GNUNET_NO;
440 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
441 ¬ify_receive, ¬ify_connect,
442 ¬ify_disconnect, &start_cb,
445 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
446 ¬ify_receive, ¬ify_connect,
447 ¬ify_disconnect, &start_cb,
450 if ((p1 == NULL) || (p2 == NULL))
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
453 if (die_task != GNUNET_SCHEDULER_NO_TASK)
454 GNUNET_SCHEDULER_cancel (die_task);
455 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
464 static char *const argv[] = { "test-transport-api-manipulation",
466 "test_transport_api_data.conf",
469 static struct GNUNET_GETOPT_CommandLineOption options[] = {
470 GNUNET_GETOPT_OPTION_END
473 send_task = GNUNET_SCHEDULER_NO_TASK;
476 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
477 "nohelp", options, &run, &ok);
483 main (int argc, char *argv[])
487 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
488 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
489 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
492 GNUNET_log_setup (test_name,
495 tth = GNUNET_TRANSPORT_TESTING_init ();
497 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
498 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
502 GNUNET_free (cfg_file_p1);
503 GNUNET_free (cfg_file_p2);
505 GNUNET_free (test_source);
506 GNUNET_free (test_plugin);
507 GNUNET_free (test_name);
509 GNUNET_TRANSPORT_TESTING_done (tth);
514 /* end of test_transport_api.c */