diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2013-02-05 15:53:24 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2013-02-05 15:53:24 +0000 |
commit | 5b8d8b20b1173decf43ab53bb0d431cc35553c3e (patch) | |
tree | 8917cc7a8ee52cfb6aadb40c52afed7c69c02194 /src/transport | |
parent | bd5e9a8250e9f1d30a8a8c772a84307ff0eb7fb2 (diff) | |
download | gnunet-5b8d8b20b1173decf43ab53bb0d431cc35553c3e.tar.gz gnunet-5b8d8b20b1173decf43ab53bb0d431cc35553c3e.zip |
new test
Diffstat (limited to 'src/transport')
-rw-r--r-- | src/transport/test_transport_api_manipulation_recv_tcp.c | 492 |
1 files changed, 492 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_manipulation_recv_tcp.c b/src/transport/test_transport_api_manipulation_recv_tcp.c new file mode 100644 index 000000000..ef734307e --- /dev/null +++ b/src/transport/test_transport_api_manipulation_recv_tcp.c | |||
@@ -0,0 +1,492 @@ | |||
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 | |||
47 | static char *test_source; | ||
48 | |||
49 | static char *test_plugin; | ||
50 | |||
51 | static char *test_name; | ||
52 | |||
53 | static int ok; | ||
54 | |||
55 | static int s_started; | ||
56 | |||
57 | static int s_connected; | ||
58 | |||
59 | static int s_sending; | ||
60 | |||
61 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
62 | |||
63 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
64 | |||
65 | static struct PeerContext *p1; | ||
66 | |||
67 | static struct PeerContext *p2; | ||
68 | |||
69 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
70 | |||
71 | static struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
72 | |||
73 | static struct GNUNET_TRANSPORT_TESTING_handle *tth; | ||
74 | |||
75 | static char *cfg_file_p1; | ||
76 | |||
77 | static char *cfg_file_p2; | ||
78 | |||
79 | static int messages_recv; | ||
80 | |||
81 | static struct GNUNET_TIME_Absolute start_normal; | ||
82 | static struct GNUNET_TIME_Relative dur_normal; | ||
83 | |||
84 | static struct GNUNET_TIME_Absolute start_delayed; | ||
85 | static struct GNUNET_TIME_Relative dur_delayed; | ||
86 | |||
87 | static void | ||
88 | end () | ||
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 | |||
106 | static void | ||
107 | end_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 | |||
159 | static void | ||
160 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
161 | |||
162 | static void | ||
163 | notify_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_DEBUG, | ||
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 (messages_recv <= 1) | ||
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 | |||
208 | struct GNUNET_ATS_Information ats[2]; | ||
209 | ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY); | ||
210 | ats[0].value = htonl (1000); | ||
211 | ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE); | ||
212 | ats[1].value = htonl (10); | ||
213 | |||
214 | GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_RECEIVE, ats, 2); | ||
215 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); | ||
216 | } | ||
217 | if (2 == messages_recv) | ||
218 | { | ||
219 | /* Received manipulated message */ | ||
220 | dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed); | ||
221 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
222 | "Received delayed message %u after %llu\n", | ||
223 | messages_recv, | ||
224 | (long long unsigned int) dur_delayed.rel_value); | ||
225 | if (dur_delayed.rel_value < 1000) | ||
226 | { | ||
227 | GNUNET_break (0); | ||
228 | ok += 1; | ||
229 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
230 | "Delayed message was not delayed correctly: took only %llu\n", | ||
231 | (long long unsigned int) dur_delayed.rel_value); | ||
232 | } | ||
233 | for (c = 0; c < ats_count; c++) | ||
234 | { | ||
235 | if (ntohl (ats[c].type) == GNUNET_ATS_QUALITY_NET_DISTANCE) | ||
236 | { | ||
237 | if (ntohl (ats[c].value) == 10) | ||
238 | ok += 0; | ||
239 | else | ||
240 | { | ||
241 | GNUNET_break (0); | ||
242 | ok += 1; | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | /* shutdown */ | ||
247 | end (); | ||
248 | } | ||
249 | |||
250 | messages_recv ++; | ||
251 | } | ||
252 | |||
253 | |||
254 | static size_t | ||
255 | notify_ready (void *cls, size_t size, void *buf) | ||
256 | { | ||
257 | struct PeerContext *p = cls; | ||
258 | struct GNUNET_MessageHeader *hdr; | ||
259 | |||
260 | th = NULL; | ||
261 | |||
262 | if (buf == NULL) | ||
263 | { | ||
264 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
265 | "Timeout occurred while waiting for transmit_ready\n"); | ||
266 | if (GNUNET_SCHEDULER_NO_TASK != die_task) | ||
267 | GNUNET_SCHEDULER_cancel (die_task); | ||
268 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
269 | ok = 42; | ||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | GNUNET_assert (size >= TEST_MESSAGE_SIZE); | ||
274 | if (buf != NULL) | ||
275 | { | ||
276 | memset (buf, '\0', TEST_MESSAGE_SIZE); | ||
277 | hdr = buf; | ||
278 | hdr->size = htons (TEST_MESSAGE_SIZE); | ||
279 | hdr->type = htons (TEST_MESSAGE_TYPE); | ||
280 | } | ||
281 | |||
282 | char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id)); | ||
283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n", | ||
285 | p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no, | ||
286 | GNUNET_i2s (&p->id)); | ||
287 | GNUNET_free (ps); | ||
288 | |||
289 | return TEST_MESSAGE_SIZE; | ||
290 | } | ||
291 | |||
292 | |||
293 | static void | ||
294 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
295 | { | ||
296 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
297 | |||
298 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
299 | return; | ||
300 | char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
301 | |||
302 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
303 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
304 | p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s); | ||
305 | GNUNET_free (receiver_s); | ||
306 | |||
307 | |||
308 | if (0 == messages_recv) | ||
309 | { | ||
310 | start_normal = GNUNET_TIME_absolute_get(); | ||
311 | } | ||
312 | if (1 == messages_recv) | ||
313 | { | ||
314 | start_delayed = GNUNET_TIME_absolute_get(); | ||
315 | } | ||
316 | |||
317 | s_sending = GNUNET_YES; | ||
318 | th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0, | ||
319 | TIMEOUT_TRANSMIT, ¬ify_ready, | ||
320 | p1); | ||
321 | } | ||
322 | |||
323 | |||
324 | static void | ||
325 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
326 | const struct GNUNET_ATS_Information *ats, uint32_t ats_count) | ||
327 | { | ||
328 | static int c; | ||
329 | |||
330 | c++; | ||
331 | struct PeerContext *p = cls; | ||
332 | struct PeerContext *t = NULL; | ||
333 | |||
334 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
335 | t = p1; | ||
336 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
337 | t = p2; | ||
338 | GNUNET_assert (t != NULL); | ||
339 | |||
340 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
341 | |||
342 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
343 | "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps, | ||
344 | t->no, GNUNET_i2s (peer)); | ||
345 | GNUNET_free (ps); | ||
346 | } | ||
347 | |||
348 | |||
349 | static void | ||
350 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
351 | { | ||
352 | struct PeerContext *p = cls; | ||
353 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
354 | |||
355 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
356 | "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps, | ||
357 | GNUNET_i2s (peer)); | ||
358 | |||
359 | GNUNET_free (ps); | ||
360 | |||
361 | if (th != NULL) | ||
362 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
363 | th = NULL; | ||
364 | } | ||
365 | |||
366 | |||
367 | static void | ||
368 | testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) | ||
369 | { | ||
370 | cc = NULL; | ||
371 | char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
372 | |||
373 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n", | ||
374 | p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id)); | ||
375 | GNUNET_free (p1_c); | ||
376 | |||
377 | s_connected = GNUNET_YES; | ||
378 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); | ||
379 | } | ||
380 | |||
381 | |||
382 | static void | ||
383 | start_cb (struct PeerContext *p, void *cls) | ||
384 | { | ||
385 | static int started; | ||
386 | started++; | ||
387 | |||
388 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no, | ||
389 | GNUNET_i2s (&p->id)); | ||
390 | |||
391 | if (started != 2) | ||
392 | return; | ||
393 | else | ||
394 | s_started = GNUNET_YES; | ||
395 | char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
396 | |||
397 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
398 | "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", | ||
399 | p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); | ||
400 | GNUNET_free (sender_c); | ||
401 | |||
402 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, | ||
403 | NULL); | ||
404 | |||
405 | } | ||
406 | |||
407 | |||
408 | static void | ||
409 | run (void *cls, char *const *args, const char *cfgfile, | ||
410 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
411 | { | ||
412 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
413 | |||
414 | s_started = GNUNET_NO; | ||
415 | s_connected = GNUNET_NO; | ||
416 | s_sending = GNUNET_NO; | ||
417 | |||
418 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1, | ||
419 | ¬ify_receive, ¬ify_connect, | ||
420 | ¬ify_disconnect, &start_cb, | ||
421 | NULL); | ||
422 | |||
423 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2, | ||
424 | ¬ify_receive, ¬ify_connect, | ||
425 | ¬ify_disconnect, &start_cb, | ||
426 | NULL); | ||
427 | |||
428 | if ((p1 == NULL) || (p2 == NULL)) | ||
429 | { | ||
430 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | ||
431 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | ||
432 | GNUNET_SCHEDULER_cancel (die_task); | ||
433 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
434 | return; | ||
435 | } | ||
436 | } | ||
437 | |||
438 | |||
439 | static int | ||
440 | check () | ||
441 | { | ||
442 | static char *const argv[] = { "test-transport-api-manipulation", | ||
443 | "-c", | ||
444 | "test_transport_api_data.conf", | ||
445 | NULL | ||
446 | }; | ||
447 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
448 | GNUNET_GETOPT_OPTION_END | ||
449 | }; | ||
450 | |||
451 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
452 | |||
453 | ok = 1; | ||
454 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name, | ||
455 | "nohelp", options, &run, &ok); | ||
456 | |||
457 | return ok; | ||
458 | } | ||
459 | |||
460 | int | ||
461 | main (int argc, char *argv[]) | ||
462 | { | ||
463 | int ret; | ||
464 | |||
465 | GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name); | ||
466 | GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source); | ||
467 | GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source, | ||
468 | &test_plugin); | ||
469 | |||
470 | GNUNET_log_setup (test_name, | ||
471 | "WARNING", | ||
472 | NULL); | ||
473 | tth = GNUNET_TRANSPORT_TESTING_init (); | ||
474 | |||
475 | GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1); | ||
476 | GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2); | ||
477 | |||
478 | ret = check (); | ||
479 | |||
480 | GNUNET_free (cfg_file_p1); | ||
481 | GNUNET_free (cfg_file_p2); | ||
482 | |||
483 | GNUNET_free (test_source); | ||
484 | GNUNET_free (test_plugin); | ||
485 | GNUNET_free (test_name); | ||
486 | |||
487 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
488 | |||
489 | return ret; | ||
490 | } | ||
491 | |||
492 | /* end of test_transport_api.c */ | ||