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