aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_manipulation_send_tcp.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-02-05 15:35:32 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-02-05 15:35:32 +0000
commit9ea6140052f8340045bf0b363030a1e6ab79a435 (patch)
treec18a5b21e01f3026fef9df36c2e109fc841904dd /src/transport/test_transport_api_manipulation_send_tcp.c
parent1c388b347390fbc2e784891dad42a2287e90a584 (diff)
downloadgnunet-9ea6140052f8340045bf0b363030a1e6ab79a435.tar.gz
gnunet-9ea6140052f8340045bf0b363030a1e6ab79a435.zip
traffic manipulation: receive delay
Diffstat (limited to 'src/transport/test_transport_api_manipulation_send_tcp.c')
-rw-r--r--src/transport/test_transport_api_manipulation_send_tcp.c499
1 files changed, 499 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_manipulation_send_tcp.c b/src/transport/test_transport_api_manipulation_send_tcp.c
new file mode 100644
index 000000000..cdef9921b
--- /dev/null
+++ b/src/transport/test_transport_api_manipulation_send_tcp.c
@@ -0,0 +1,499 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20/**
21 * @file transport/test_transport_api.c
22 * @brief base test case for transport implementations
23 *
24 * This test case serves as a base for tcp, udp, and udp-nat
25 * transport test cases. Based on the executable being run
26 * the correct test case will be performed. Conservation of
27 * C code apparently.
28 */
29#include "platform.h"
30#include "gnunet_transport_service.h"
31#include "transport-testing.h"
32
33/**
34 * How long until we give up on transmitting the message?
35 */
36#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
37
38/**
39 * How long until we give up on transmitting the message?
40 */
41#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42
43#define TEST_MESSAGE_SIZE 2600
44
45#define TEST_MESSAGE_TYPE 12345
46
47static char *test_source;
48
49static char *test_plugin;
50
51static char *test_name;
52
53static int ok;
54
55static int s_started;
56
57static int s_connected;
58
59static int s_sending;
60
61static GNUNET_SCHEDULER_TaskIdentifier die_task;
62
63static GNUNET_SCHEDULER_TaskIdentifier send_task;
64
65static struct PeerContext *p1;
66
67static struct PeerContext *p2;
68
69static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
70
71static struct GNUNET_TRANSPORT_TransmitHandle *th;
72
73static struct GNUNET_TRANSPORT_TESTING_handle *tth;
74
75static char *cfg_file_p1;
76
77static char *cfg_file_p2;
78
79static int messages_recv;
80
81static struct GNUNET_TIME_Absolute start_normal;
82static struct GNUNET_TIME_Relative dur_normal;
83
84static struct GNUNET_TIME_Absolute start_delayed;
85static struct GNUNET_TIME_Relative dur_delayed;
86
87static void
88end ()
89{
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
91
92 if (send_task != GNUNET_SCHEDULER_NO_TASK)
93 GNUNET_SCHEDULER_cancel (send_task);
94
95 if (die_task != GNUNET_SCHEDULER_NO_TASK)
96 GNUNET_SCHEDULER_cancel (die_task);
97
98 if (th != NULL)
99 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
100 th = NULL;
101
102 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
103 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
104}
105
106static void
107end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108{
109 die_task = GNUNET_SCHEDULER_NO_TASK;
110
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
112
113
114 if (send_task != GNUNET_SCHEDULER_NO_TASK)
115 GNUNET_SCHEDULER_cancel (send_task);
116
117 if (cc != NULL)
118 {
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
120 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
121 cc = NULL;
122 }
123
124 if (th != NULL)
125 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
126 else
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
128
129 if (s_started == GNUNET_NO)
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
131 else
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
133
134 if (s_connected == GNUNET_NO)
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
136 else
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
138
139 if (s_sending == GNUNET_NO)
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
141 else
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
143
144 th = NULL;
145
146 if (p1 != NULL)
147 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
148 else
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
150 if (p2 != NULL)
151 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
152 else
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
154
155 ok = GNUNET_SYSERR;
156}
157
158
159static void
160sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
161
162static void
163notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
164 const struct GNUNET_MessageHeader *message,
165 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
166{
167 struct PeerContext *p = cls;
168 struct PeerContext *t = NULL;
169 int c;
170
171 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
172 t = p1;
173 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
174 t = p2;
175 GNUNET_assert (t != NULL);
176
177 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
178
179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
181 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
182 GNUNET_i2s (&t->id));
183 GNUNET_free (ps);
184
185 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
186 (TEST_MESSAGE_SIZE == ntohs (message->size)))
187 {
188 ok = 0;
189
190 }
191 else
192 {
193 GNUNET_break (0);
194 ok = 1;
195 end ();
196 return;
197 }
198
199 if (0 == messages_recv)
200 {
201 /* Received non-delayed message */
202 dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Received non-delayed message %u after %llu\n",
205 messages_recv,
206 (long long unsigned int) dur_normal.rel_value);
207 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
208 }
209 if (1 == messages_recv)
210 {
211 /* Received manipulated message */
212 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 "Received delayed message %u after %llu\n",
215 messages_recv,
216 (long long unsigned int) dur_delayed.rel_value);
217 if (dur_delayed.rel_value < 1000)
218 {
219 GNUNET_break (0);
220 ok += 1;
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 "Delayed message was not delayed correctly: took only %llu\n",
223 (long long unsigned int) dur_delayed.rel_value);
224 }
225 for (c = 0; c < ats_count; c++)
226 {
227 if (ntohl (ats[c].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
228 {
229 if (ntohl (ats[c].value) == 10)
230 ok += 0;
231 else
232 {
233 GNUNET_break (0);
234 ok += 1;
235 }
236 }
237 }
238 /* shutdown */
239 end ();
240 }
241 messages_recv ++;
242}
243
244
245static size_t
246notify_ready (void *cls, size_t size, void *buf)
247{
248 struct PeerContext *p = cls;
249 struct GNUNET_MessageHeader *hdr;
250
251 th = NULL;
252
253 if (buf == NULL)
254 {
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "Timeout occurred while waiting for transmit_ready\n");
257 if (GNUNET_SCHEDULER_NO_TASK != die_task)
258 GNUNET_SCHEDULER_cancel (die_task);
259 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
260 ok = 42;
261 return 0;
262 }
263
264 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
265 if (buf != NULL)
266 {
267 memset (buf, '\0', TEST_MESSAGE_SIZE);
268 hdr = buf;
269 hdr->size = htons (TEST_MESSAGE_SIZE);
270 hdr->type = htons (TEST_MESSAGE_TYPE);
271 }
272
273 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
276 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
277 GNUNET_i2s (&p->id));
278 GNUNET_free (ps);
279
280 return TEST_MESSAGE_SIZE;
281}
282
283
284static void
285sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
286{
287 struct GNUNET_ATS_Information ats[2];
288 send_task = GNUNET_SCHEDULER_NO_TASK;
289
290 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
291 return;
292 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
293
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
296 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
297 GNUNET_free (receiver_s);
298
299
300 if (0 == messages_recv)
301 {
302 start_normal = GNUNET_TIME_absolute_get();
303 }
304 if (1 == messages_recv)
305 {
306 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
307 ats[0].value = htonl (1000);
308 ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
309 ats[1].value = htonl (10);
310
311 GNUNET_TRANSPORT_set_traffic_metric (p2->th, &p1->id, TM_SEND, ats, 2);
312
313 start_delayed = GNUNET_TIME_absolute_get();
314 }
315
316 s_sending = GNUNET_YES;
317 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
318 TIMEOUT_TRANSMIT, &notify_ready,
319 p1);
320}
321
322
323static void
324notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
325 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
326{
327 static int c;
328
329 c++;
330 struct PeerContext *p = cls;
331 struct PeerContext *t = NULL;
332
333 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
334 t = p1;
335 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
336 t = p2;
337 GNUNET_assert (t != NULL);
338
339 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
340
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
343 t->no, GNUNET_i2s (peer));
344 GNUNET_free (ps);
345}
346
347
348static void
349notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
350{
351 struct PeerContext *p = cls;
352 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
353
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
356 GNUNET_i2s (peer));
357
358 GNUNET_free (ps);
359
360 if (th != NULL)
361 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
362 th = NULL;
363}
364
365
366static void
367testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
368{
369 cc = NULL;
370 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
371
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
373 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
374 GNUNET_free (p1_c);
375
376 s_connected = GNUNET_YES;
377 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
378}
379
380
381static void
382start_cb (struct PeerContext *p, void *cls)
383{
384 static int started;
385 started++;
386
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
388 GNUNET_i2s (&p->id));
389
390 if (started != 2)
391 return;
392 else
393 s_started = GNUNET_YES;
394 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
395
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
398 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
399 GNUNET_free (sender_c);
400 /*
401 struct GNUNET_ATS_Information ats[2];
402 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
403 ats[0].value = htonl (1000);
404 ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
405 ats[1].value = htonl (10);
406
407 GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_RECEIVE, ats, 2);
408*/
409 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
410 NULL);
411
412}
413
414
415static void
416run (void *cls, char *const *args, const char *cfgfile,
417 const struct GNUNET_CONFIGURATION_Handle *cfg)
418{
419 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
420
421 s_started = GNUNET_NO;
422 s_connected = GNUNET_NO;
423 s_sending = GNUNET_NO;
424
425 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
426 &notify_receive, &notify_connect,
427 &notify_disconnect, &start_cb,
428 NULL);
429
430 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
431 &notify_receive, &notify_connect,
432 &notify_disconnect, &start_cb,
433 NULL);
434
435 if ((p1 == NULL) || (p2 == NULL))
436 {
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
438 if (die_task != GNUNET_SCHEDULER_NO_TASK)
439 GNUNET_SCHEDULER_cancel (die_task);
440 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
441 return;
442 }
443}
444
445
446static int
447check ()
448{
449 static char *const argv[] = { "test-transport-api-manipulation",
450 "-c",
451 "test_transport_api_data.conf",
452 NULL
453 };
454 static struct GNUNET_GETOPT_CommandLineOption options[] = {
455 GNUNET_GETOPT_OPTION_END
456 };
457
458 send_task = GNUNET_SCHEDULER_NO_TASK;
459
460 ok = 1;
461 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
462 "nohelp", options, &run, &ok);
463
464 return ok;
465}
466
467int
468main (int argc, char *argv[])
469{
470 int ret;
471
472 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
473 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
474 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
475 &test_plugin);
476
477 GNUNET_log_setup (test_name,
478 "WARNING",
479 NULL);
480 tth = GNUNET_TRANSPORT_TESTING_init ();
481
482 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
483 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
484
485 ret = check ();
486
487 GNUNET_free (cfg_file_p1);
488 GNUNET_free (cfg_file_p2);
489
490 GNUNET_free (test_source);
491 GNUNET_free (test_plugin);
492 GNUNET_free (test_name);
493
494 GNUNET_TRANSPORT_TESTING_done (tth);
495
496 return ret;
497}
498
499/* end of test_transport_api.c */