diff options
Diffstat (limited to 'src/transport/test_transport_api_bidirectional_connect.c')
-rw-r--r-- | src/transport/test_transport_api_bidirectional_connect.c | 448 |
1 files changed, 0 insertions, 448 deletions
diff --git a/src/transport/test_transport_api_bidirectional_connect.c b/src/transport/test_transport_api_bidirectional_connect.c deleted file mode 100644 index 10b20e21e..000000000 --- a/src/transport/test_transport_api_bidirectional_connect.c +++ /dev/null | |||
@@ -1,448 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009, 2010 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file transport/test_transport_api_bidirectional_connect.c | ||
22 | * @brief base test case for transport implementations | ||
23 | * | ||
24 | * Perform a 3-way handshake connection set up in both directions at | ||
25 | * (almost) the same time | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_transport_service.h" | ||
29 | #include "transport-testing.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * How long until we give up on transmitting the message? | ||
34 | */ | ||
35 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
36 | |||
37 | /** | ||
38 | * How long until we give up on transmitting the message? | ||
39 | */ | ||
40 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | ||
41 | |||
42 | #define MTYPE 12345 | ||
43 | |||
44 | static char *test_source; | ||
45 | |||
46 | static char *test_plugin; | ||
47 | |||
48 | static char *test_name; | ||
49 | |||
50 | static int ok; | ||
51 | |||
52 | static struct GNUNET_SCHEDULER_Task * die_task; | ||
53 | |||
54 | static struct GNUNET_SCHEDULER_Task * send_task; | ||
55 | |||
56 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; | ||
57 | |||
58 | static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
59 | |||
60 | static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc1; | ||
61 | static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc2; | ||
62 | |||
63 | static struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
64 | |||
65 | static struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
66 | |||
67 | static char *cfg_file_p1; | ||
68 | |||
69 | static char *cfg_file_p2; | ||
70 | |||
71 | |||
72 | static void | ||
73 | end () | ||
74 | { | ||
75 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n"); | ||
76 | |||
77 | if (send_task != NULL) | ||
78 | { | ||
79 | GNUNET_SCHEDULER_cancel (send_task); | ||
80 | send_task = NULL; | ||
81 | } | ||
82 | |||
83 | if (die_task != NULL) | ||
84 | GNUNET_SCHEDULER_cancel (die_task); | ||
85 | |||
86 | if (NULL != th) | ||
87 | { | ||
88 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
89 | th = NULL; | ||
90 | } | ||
91 | |||
92 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
93 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
94 | } | ||
95 | |||
96 | |||
97 | static void | ||
98 | end_badly (void *cls) | ||
99 | { | ||
100 | die_task = NULL; | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n"); | ||
102 | if (send_task != NULL) | ||
103 | { | ||
104 | GNUNET_SCHEDULER_cancel (send_task); | ||
105 | send_task = NULL; | ||
106 | } | ||
107 | |||
108 | if (NULL != cc2) | ||
109 | { | ||
110 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n")); | ||
111 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc2); | ||
112 | cc2 = NULL; | ||
113 | } | ||
114 | if (NULL != cc1) | ||
115 | { | ||
116 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n")); | ||
117 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc1); | ||
118 | cc1 = NULL; | ||
119 | } | ||
120 | if (NULL != th) | ||
121 | { | ||
122 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
123 | th = NULL; | ||
124 | } | ||
125 | if (p1 != NULL) | ||
126 | GNUNET_TRANSPORT_TESTING_stop_peer (p1); | ||
127 | if (p2 != NULL) | ||
128 | GNUNET_TRANSPORT_TESTING_stop_peer (p2); | ||
129 | |||
130 | ok = GNUNET_SYSERR; | ||
131 | } | ||
132 | |||
133 | |||
134 | static void | ||
135 | notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
136 | const struct GNUNET_MessageHeader *message) | ||
137 | { | ||
138 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
139 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL; | ||
140 | |||
141 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
142 | t = p1; | ||
143 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
144 | t = p2; | ||
145 | GNUNET_assert (t != NULL); | ||
146 | |||
147 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
148 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
149 | "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n", | ||
150 | p->no, ps, ntohs (message->type), ntohs (message->size), t->no, | ||
151 | GNUNET_i2s (&t->id)); | ||
152 | GNUNET_free (ps); | ||
153 | if ((MTYPE == ntohs (message->type)) && | ||
154 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | ||
155 | { | ||
156 | ok = 0; | ||
157 | end (); | ||
158 | } | ||
159 | else | ||
160 | { | ||
161 | GNUNET_break (0); | ||
162 | ok = 1; | ||
163 | end (); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | |||
168 | static size_t | ||
169 | notify_ready (void *cls, size_t size, void *buf) | ||
170 | { | ||
171 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
172 | struct GNUNET_MessageHeader *hdr; | ||
173 | |||
174 | th = NULL; | ||
175 | |||
176 | if (buf == NULL) | ||
177 | { | ||
178 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
179 | "Timeout occurred while waiting for transmit_ready\n"); | ||
180 | if (NULL != die_task) | ||
181 | GNUNET_SCHEDULER_cancel (die_task); | ||
182 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
183 | ok = 42; | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | GNUNET_assert (size >= 256); | ||
188 | hdr = buf; | ||
189 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
190 | hdr->type = htons (MTYPE); | ||
191 | |||
192 | { | ||
193 | char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id)); | ||
194 | |||
195 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
196 | "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n", | ||
197 | p2->no, | ||
198 | ps, | ||
199 | ntohs (hdr->type), | ||
200 | ntohs (hdr->size), | ||
201 | p->no, | ||
202 | GNUNET_i2s (&p->id)); | ||
203 | GNUNET_free (ps); | ||
204 | } | ||
205 | |||
206 | return sizeof (struct GNUNET_MessageHeader); | ||
207 | } | ||
208 | |||
209 | |||
210 | static void | ||
211 | sendtask (void *cls) | ||
212 | { | ||
213 | char *receiver_s; | ||
214 | |||
215 | send_task = NULL; | ||
216 | receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
217 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
218 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
219 | p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s); | ||
220 | GNUNET_free (receiver_s); | ||
221 | |||
222 | th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, | ||
223 | TIMEOUT_TRANSMIT, ¬ify_ready, | ||
224 | p1); | ||
225 | } | ||
226 | |||
227 | |||
228 | static void | ||
229 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
230 | { | ||
231 | static int c; | ||
232 | |||
233 | c++; | ||
234 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
235 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL; | ||
236 | |||
237 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
238 | t = p1; | ||
239 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
240 | t = p2; | ||
241 | GNUNET_assert (t != NULL); | ||
242 | |||
243 | { | ||
244 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
245 | |||
246 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
247 | "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", | ||
248 | p->no, | ||
249 | ps, | ||
250 | t->no, | ||
251 | GNUNET_i2s (peer)); | ||
252 | GNUNET_free (ps); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | |||
257 | static void | ||
258 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
259 | { | ||
260 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
261 | |||
262 | { | ||
263 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
264 | |||
265 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
266 | "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", | ||
267 | p->no, | ||
268 | ps, | ||
269 | GNUNET_i2s (peer)); | ||
270 | GNUNET_free (ps); | ||
271 | } | ||
272 | if (NULL != send_task) | ||
273 | { | ||
274 | GNUNET_SCHEDULER_cancel(send_task); | ||
275 | GNUNET_break (0); | ||
276 | send_task = NULL; | ||
277 | } | ||
278 | if (NULL != th) | ||
279 | { | ||
280 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
281 | th = NULL; | ||
282 | } | ||
283 | } | ||
284 | |||
285 | |||
286 | static void | ||
287 | testing_connect_cb (void *cls) | ||
288 | { | ||
289 | static int connected = GNUNET_NO; | ||
290 | |||
291 | if ((cls == cc1) && (NULL != cc2)) | ||
292 | { | ||
293 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc2); | ||
294 | } | ||
295 | if ((cls == cc2) && (NULL != cc1)) | ||
296 | { | ||
297 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc1); | ||
298 | } | ||
299 | cc1 = NULL; | ||
300 | cc2 = NULL; | ||
301 | |||
302 | if (connected > 0) | ||
303 | return; | ||
304 | connected++; | ||
305 | |||
306 | { | ||
307 | char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
308 | |||
309 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
310 | "Peers connected: %u (%s) <-> %u (%s)\n", | ||
311 | p1->no, | ||
312 | p1_c, | ||
313 | p2->no, | ||
314 | GNUNET_i2s (&p2->id)); | ||
315 | GNUNET_free (p1_c); | ||
316 | } | ||
317 | send_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
318 | &sendtask, | ||
319 | NULL); | ||
320 | } | ||
321 | |||
322 | |||
323 | static void | ||
324 | start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls) | ||
325 | { | ||
326 | static int started; | ||
327 | |||
328 | started++; | ||
329 | |||
330 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
331 | "Peer %u (`%s') started\n", | ||
332 | p->no, | ||
333 | GNUNET_i2s (&p->id)); | ||
334 | |||
335 | if (started != 2) | ||
336 | return; | ||
337 | |||
338 | char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
339 | |||
340 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
341 | "Test tries to connect peer %u (`%s') <-> peer %u (`%s')\n", | ||
342 | p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); | ||
343 | GNUNET_free (sender_c); | ||
344 | cc1 = | ||
345 | GNUNET_TRANSPORT_TESTING_connect_peers (p2, | ||
346 | p1, | ||
347 | &testing_connect_cb, | ||
348 | cc1); | ||
349 | cc2 = | ||
350 | GNUNET_TRANSPORT_TESTING_connect_peers (p1, | ||
351 | p2, | ||
352 | &testing_connect_cb, | ||
353 | cc2); | ||
354 | } | ||
355 | |||
356 | |||
357 | static void | ||
358 | run (void *cls, char *const *args, const char *cfgfile, | ||
359 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
360 | { | ||
361 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
362 | |||
363 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, | ||
364 | cfg_file_p1, 1, | ||
365 | ¬ify_receive, | ||
366 | ¬ify_connect, | ||
367 | ¬ify_disconnect, | ||
368 | &start_cb, | ||
369 | NULL); | ||
370 | |||
371 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, | ||
372 | cfg_file_p2, | ||
373 | 2, | ||
374 | ¬ify_receive, | ||
375 | ¬ify_connect, | ||
376 | ¬ify_disconnect, | ||
377 | &start_cb, | ||
378 | NULL); | ||
379 | |||
380 | if ((p1 == NULL) || (p2 == NULL)) | ||
381 | { | ||
382 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n"); | ||
383 | if (die_task != NULL) | ||
384 | GNUNET_SCHEDULER_cancel (die_task); | ||
385 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
386 | return; | ||
387 | } | ||
388 | } | ||
389 | |||
390 | |||
391 | static int | ||
392 | check () | ||
393 | { | ||
394 | static char *const argv[] = { "test-transport-api", | ||
395 | "-c", | ||
396 | "test_transport_api_data.conf", | ||
397 | NULL | ||
398 | }; | ||
399 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
400 | GNUNET_GETOPT_OPTION_END | ||
401 | }; | ||
402 | |||
403 | #if WRITECONFIG | ||
404 | setTransportOptions ("test_transport_api_data.conf"); | ||
405 | #endif | ||
406 | send_task = NULL; | ||
407 | |||
408 | ok = 1; | ||
409 | GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name, | ||
410 | "nohelp", options, &run, &ok); | ||
411 | |||
412 | return ok; | ||
413 | } | ||
414 | |||
415 | int | ||
416 | main (int argc, char *argv[]) | ||
417 | { | ||
418 | int ret; | ||
419 | |||
420 | test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); | ||
421 | GNUNET_log_setup (test_name, | ||
422 | "WARNING", | ||
423 | NULL); | ||
424 | |||
425 | test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); | ||
426 | test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], | ||
427 | test_source); | ||
428 | |||
429 | tth = GNUNET_TRANSPORT_TESTING_init (); | ||
430 | |||
431 | cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1); | ||
432 | cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2); | ||
433 | |||
434 | ret = check (); | ||
435 | |||
436 | GNUNET_free (cfg_file_p1); | ||
437 | GNUNET_free (cfg_file_p2); | ||
438 | |||
439 | GNUNET_free (test_source); | ||
440 | GNUNET_free (test_plugin); | ||
441 | GNUNET_free (test_name); | ||
442 | |||
443 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
444 | |||
445 | return ret; | ||
446 | } | ||
447 | |||
448 | /* end of test_transport_api_bidirectional_connect.c */ | ||