diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-08-06 20:43:50 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-08-06 20:43:50 +0000 |
commit | 6ad7a1bffd1688f8ee1ecb37eacb3a55f671748c (patch) | |
tree | 0035690827086b06505e297aa4795fa5f0429608 /src/transport/test_transport_api_disconnect.c | |
parent | 372124795880b0d925ee04d11b38f64a49c6ed94 (diff) | |
download | gnunet-6ad7a1bffd1688f8ee1ecb37eacb3a55f671748c.tar.gz gnunet-6ad7a1bffd1688f8ee1ecb37eacb3a55f671748c.zip |
plane hacking
Diffstat (limited to 'src/transport/test_transport_api_disconnect.c')
-rw-r--r-- | src/transport/test_transport_api_disconnect.c | 452 |
1 files changed, 452 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_disconnect.c b/src/transport/test_transport_api_disconnect.c new file mode 100644 index 000000000..35cda3d04 --- /dev/null +++ b/src/transport/test_transport_api_disconnect.c | |||
@@ -0,0 +1,452 @@ | |||
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_disconnect.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_common.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_getopt_lib.h" | ||
33 | #include "gnunet_os_lib.h" | ||
34 | #include "gnunet_program_lib.h" | ||
35 | #include "gnunet_scheduler_lib.h" | ||
36 | #include "gnunet_transport_service.h" | ||
37 | #include "transport.h" | ||
38 | #include "transport-testing.h" | ||
39 | |||
40 | #define VERBOSE GNUNET_NO | ||
41 | |||
42 | #define VERBOSE_ARM GNUNET_NO | ||
43 | |||
44 | #define START_ARM GNUNET_YES | ||
45 | |||
46 | /** | ||
47 | * How long until we give up on transmitting the message? | ||
48 | */ | ||
49 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
50 | |||
51 | /** | ||
52 | * How long until we give up on transmitting the message? | ||
53 | */ | ||
54 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
55 | |||
56 | #define MTYPE 12345 | ||
57 | |||
58 | #define ITERATIONS 50 | ||
59 | |||
60 | static struct PeerContext p1; | ||
61 | |||
62 | static struct PeerContext p2; | ||
63 | |||
64 | static int ok; | ||
65 | |||
66 | static int peers_connected = 0; | ||
67 | static int counter; | ||
68 | static int msgs_recv; | ||
69 | |||
70 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | ||
71 | |||
72 | static GNUNET_SCHEDULER_TaskIdentifier tct; | ||
73 | |||
74 | struct GNUNET_TRANSPORT_TransmitHandle * th; | ||
75 | |||
76 | #if VERBOSE | ||
77 | #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | ||
78 | #else | ||
79 | #define OKPP do { ok++; } while (0) | ||
80 | #endif | ||
81 | |||
82 | |||
83 | static void | ||
84 | peers_connect (); | ||
85 | |||
86 | static void | ||
87 | end () | ||
88 | { | ||
89 | if (GNUNET_SCHEDULER_NO_TASK != die_task) | ||
90 | GNUNET_SCHEDULER_cancel (die_task); | ||
91 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
92 | "Disconnecting from transports!\n"); | ||
93 | if (th != NULL) | ||
94 | GNUNET_TRANSPORT_notify_transmit_ready_cancel(th); | ||
95 | th = NULL; | ||
96 | |||
97 | if (p1.th != NULL) | ||
98 | GNUNET_TRANSPORT_disconnect (p1.th); | ||
99 | |||
100 | if (p2.th != NULL) | ||
101 | GNUNET_TRANSPORT_disconnect (p2.th); | ||
102 | die_task = GNUNET_SCHEDULER_NO_TASK; | ||
103 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
104 | "Transports disconnected, returning success!\n"); | ||
105 | ok = 0; | ||
106 | } | ||
107 | |||
108 | static void | ||
109 | stop_arm (struct PeerContext *p) | ||
110 | { | ||
111 | #if START_ARM | ||
112 | if (NULL != p->arm_proc) | ||
113 | { | ||
114 | if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM)) | ||
115 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
116 | GNUNET_OS_process_wait (p->arm_proc); | ||
117 | GNUNET_OS_process_close (p->arm_proc); | ||
118 | p->arm_proc = NULL; | ||
119 | } | ||
120 | #endif | ||
121 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
122 | } | ||
123 | |||
124 | |||
125 | |||
126 | |||
127 | static void | ||
128 | exchange_hello_last (void *cls, | ||
129 | const struct GNUNET_MessageHeader *message) | ||
130 | { | ||
131 | struct PeerContext *me = cls; | ||
132 | |||
133 | GNUNET_assert (message != NULL); | ||
134 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
135 | "Exchanging HELLO of size %d with peer (%s)!\n", | ||
136 | (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message), | ||
137 | GNUNET_i2s (&me->id)); | ||
138 | GNUNET_assert (GNUNET_OK == | ||
139 | GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) | ||
140 | message, &me->id)); | ||
141 | GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL); | ||
142 | } | ||
143 | |||
144 | |||
145 | static void | ||
146 | exchange_hello (void *cls, | ||
147 | const struct GNUNET_MessageHeader *message) | ||
148 | { | ||
149 | struct PeerContext *me = cls; | ||
150 | |||
151 | GNUNET_assert (message != NULL); | ||
152 | GNUNET_assert (GNUNET_OK == | ||
153 | GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) | ||
154 | message, &me->id)); | ||
155 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
156 | "Exchanging HELLO of size %d from peer %s!\n", | ||
157 | (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message), | ||
158 | GNUNET_i2s (&me->id)); | ||
159 | GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL); | ||
160 | } | ||
161 | |||
162 | |||
163 | static void | ||
164 | end_badly () | ||
165 | { | ||
166 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n"); | ||
167 | GNUNET_break (0); | ||
168 | |||
169 | if (th != NULL) | ||
170 | { | ||
171 | GNUNET_TRANSPORT_notify_transmit_ready_cancel(th); | ||
172 | th = NULL; | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2); | ||
177 | GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1); | ||
178 | } | ||
179 | |||
180 | GNUNET_TRANSPORT_disconnect (p1.th); | ||
181 | GNUNET_TRANSPORT_disconnect (p2.th); | ||
182 | if (GNUNET_SCHEDULER_NO_TASK != tct) | ||
183 | { | ||
184 | GNUNET_SCHEDULER_cancel (tct); | ||
185 | tct = GNUNET_SCHEDULER_NO_TASK; | ||
186 | } | ||
187 | ok = 1; | ||
188 | } | ||
189 | |||
190 | |||
191 | static void | ||
192 | notify_receive (void *cls, | ||
193 | const struct GNUNET_PeerIdentity *peer, | ||
194 | const struct GNUNET_MessageHeader *message, | ||
195 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
196 | uint32_t ats_count) | ||
197 | { | ||
198 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
199 | "Received message of type %d from peer %s!\n", | ||
200 | ntohs(message->type), | ||
201 | GNUNET_i2s (peer)); | ||
202 | OKPP; | ||
203 | GNUNET_assert (MTYPE == ntohs (message->type)); | ||
204 | GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == | ||
205 | ntohs (message->size)); | ||
206 | |||
207 | msgs_recv++; | ||
208 | } | ||
209 | |||
210 | static void | ||
211 | peers_disconnect (void *cls, | ||
212 | const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
213 | |||
214 | static size_t | ||
215 | notify_ready (void *cls, size_t size, void *buf) | ||
216 | { | ||
217 | struct PeerContext *p = cls; | ||
218 | struct GNUNET_MessageHeader *hdr; | ||
219 | |||
220 | th = NULL; | ||
221 | |||
222 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
223 | "Transmitting message with %u bytes to peer %s\n", | ||
224 | sizeof (struct GNUNET_MessageHeader), | ||
225 | GNUNET_i2s (&p->id)); | ||
226 | GNUNET_assert (size >= 256); | ||
227 | OKPP; | ||
228 | if (buf != NULL) | ||
229 | { | ||
230 | hdr = buf; | ||
231 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
232 | hdr->type = htons (MTYPE); | ||
233 | } | ||
234 | |||
235 | GNUNET_SCHEDULER_add_now (&peers_disconnect, NULL); | ||
236 | |||
237 | return sizeof (struct GNUNET_MessageHeader); | ||
238 | } | ||
239 | |||
240 | |||
241 | static void | ||
242 | notify_connect (void *cls, | ||
243 | const struct GNUNET_PeerIdentity *peer, | ||
244 | const struct GNUNET_TRANSPORT_ATS_Information *ats, | ||
245 | uint32_t ats_count) | ||
246 | { | ||
247 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
248 | "Peer `%4s' connected to us (%p)!\n", | ||
249 | GNUNET_i2s (peer), | ||
250 | cls); | ||
251 | peers_connected++; | ||
252 | if (cls == &p1) | ||
253 | { | ||
254 | GNUNET_assert (ok >= 2); | ||
255 | OKPP; | ||
256 | OKPP; | ||
257 | if (GNUNET_SCHEDULER_NO_TASK != die_task) | ||
258 | GNUNET_SCHEDULER_cancel (die_task); | ||
259 | if (GNUNET_SCHEDULER_NO_TASK != tct) | ||
260 | GNUNET_SCHEDULER_cancel (tct); | ||
261 | tct = GNUNET_SCHEDULER_NO_TASK; | ||
262 | |||
263 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT, | ||
264 | &end_badly, NULL); | ||
265 | th = GNUNET_TRANSPORT_notify_transmit_ready (p1.th, | ||
266 | &p2.id, | ||
267 | 256, 0, TIMEOUT, ¬ify_ready, | ||
268 | &p1); | ||
269 | } | ||
270 | } | ||
271 | |||
272 | |||
273 | static void | ||
274 | notify_disconnect (void *cls, | ||
275 | const struct GNUNET_PeerIdentity *peer) | ||
276 | { | ||
277 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
278 | "Peer `%4s' disconnected (%p)!\n", | ||
279 | GNUNET_i2s (peer), cls); | ||
280 | peers_connected--; | ||
281 | } | ||
282 | |||
283 | |||
284 | static void | ||
285 | setup_peer (struct PeerContext *p, | ||
286 | const char *cfgname) | ||
287 | { | ||
288 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
289 | |||
290 | GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
291 | if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME")) | ||
292 | GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome); | ||
293 | if (NULL != p->servicehome) | ||
294 | GNUNET_DISK_directory_remove (p->servicehome); | ||
295 | |||
296 | #if START_ARM | ||
297 | p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", | ||
298 | "gnunet-service-arm", | ||
299 | #if VERBOSE_ARM | ||
300 | "-L", "DEBUG", | ||
301 | #endif | ||
302 | "-c", cfgname, NULL); | ||
303 | #endif | ||
304 | |||
305 | } | ||
306 | |||
307 | |||
308 | static void | ||
309 | try_connect (void *cls, | ||
310 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
311 | { | ||
312 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
313 | "Asking peers to connect...\n"); | ||
314 | /* FIXME: 'pX.id' may still be all-zeros here... */ | ||
315 | GNUNET_TRANSPORT_try_connect (p2.th, | ||
316 | &p1.id); | ||
317 | GNUNET_TRANSPORT_try_connect (p1.th, | ||
318 | &p2.id); | ||
319 | tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
320 | &try_connect, | ||
321 | NULL); | ||
322 | } | ||
323 | |||
324 | |||
325 | static void | ||
326 | peers_disconnect (void *cls, | ||
327 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
328 | { | ||
329 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
330 | return; | ||
331 | |||
332 | //while (peers_connected > 0); | ||
333 | |||
334 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
335 | "Disconnecting from Transport \n"); | ||
336 | |||
337 | GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2); | ||
338 | GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1); | ||
339 | |||
340 | GNUNET_TRANSPORT_disconnect (p1.th); | ||
341 | p1.th = NULL; | ||
342 | |||
343 | GNUNET_TRANSPORT_disconnect (p2.th); | ||
344 | p2.th = NULL; | ||
345 | |||
346 | while (peers_connected > 0); | ||
347 | |||
348 | if (counter < ITERATIONS) | ||
349 | peers_connect(); | ||
350 | else | ||
351 | end (); | ||
352 | } | ||
353 | |||
354 | static void | ||
355 | peers_connect () | ||
356 | { | ||
357 | counter ++; | ||
358 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iteration %i of %i\n", counter, ITERATIONS); | ||
359 | |||
360 | GNUNET_assert (p1.th == NULL); | ||
361 | p1.th = GNUNET_TRANSPORT_connect (p1.cfg, | ||
362 | NULL, &p1, | ||
363 | ¬ify_receive, | ||
364 | ¬ify_connect, ¬ify_disconnect); | ||
365 | |||
366 | GNUNET_assert (p2.th == NULL); | ||
367 | p2.th = GNUNET_TRANSPORT_connect (p2.cfg, | ||
368 | NULL, &p2, | ||
369 | ¬ify_receive, | ||
370 | ¬ify_connect, ¬ify_disconnect); | ||
371 | |||
372 | GNUNET_assert(p1.th != NULL); | ||
373 | GNUNET_assert(p2.th != NULL); | ||
374 | |||
375 | GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1); | ||
376 | GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2); | ||
377 | tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL); | ||
378 | } | ||
379 | |||
380 | static void | ||
381 | run (void *cls, | ||
382 | char *const *args, | ||
383 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
384 | { | ||
385 | GNUNET_assert (ok == 1); | ||
386 | OKPP; | ||
387 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
388 | &end_badly, NULL); | ||
389 | |||
390 | setup_peer (&p1, "test_transport_api_tcp_peer1.conf"); | ||
391 | setup_peer (&p2, "test_transport_api_tcp_peer2.conf"); | ||
392 | |||
393 | peers_connect (); | ||
394 | } | ||
395 | |||
396 | static int | ||
397 | check () | ||
398 | { | ||
399 | static char *const argv[] = { "test-transport-api", | ||
400 | "-c", | ||
401 | "test_transport_api_data.conf", | ||
402 | #if VERBOSE | ||
403 | "-L", "DEBUG", | ||
404 | #endif | ||
405 | NULL | ||
406 | }; | ||
407 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
408 | GNUNET_GETOPT_OPTION_END | ||
409 | }; | ||
410 | |||
411 | #if WRITECONFIG | ||
412 | setTransportOptions("test_transport_api_data.conf"); | ||
413 | #endif | ||
414 | ok = 1; | ||
415 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, | ||
416 | argv, "test-transport-api", "nohelp", | ||
417 | options, &run, &ok); | ||
418 | stop_arm (&p1); | ||
419 | stop_arm (&p2); | ||
420 | |||
421 | if (p1.servicehome != NULL) | ||
422 | { | ||
423 | GNUNET_DISK_directory_remove (p1.servicehome); | ||
424 | GNUNET_free(p1.servicehome); | ||
425 | } | ||
426 | if (p2.servicehome != NULL) | ||
427 | { | ||
428 | GNUNET_DISK_directory_remove (p2.servicehome); | ||
429 | GNUNET_free(p2.servicehome); | ||
430 | } | ||
431 | return ok; | ||
432 | } | ||
433 | |||
434 | int | ||
435 | main (int argc, char *argv[]) | ||
436 | { | ||
437 | int ret; | ||
438 | |||
439 | GNUNET_log_setup ("test_transport_api_disconnect", | ||
440 | #if VERBOSE | ||
441 | "DEBUG", | ||
442 | #else | ||
443 | "WARNING", | ||
444 | #endif | ||
445 | NULL); | ||
446 | |||
447 | ret = check (); | ||
448 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Messages received: %i\n", msgs_recv); | ||
449 | return ret; | ||
450 | } | ||
451 | |||
452 | /* end of test_transport_api_disconnect.c */ | ||