aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_multiaddress.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-07-04 12:13:29 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-07-04 12:13:29 +0000
commit0a66575e5918346ab51804b0bd0059a54de18c5f (patch)
tree65ee7f7dab81a9617949d59e1e5304ad39709e6a /src/transport/test_transport_api_multiaddress.c
parenta7a0e7235a3e447197caa1aac23af2b097647c63 (diff)
downloadgnunet-0a66575e5918346ab51804b0bd0059a54de18c5f.tar.gz
gnunet-0a66575e5918346ab51804b0bd0059a54de18c5f.zip
Diffstat (limited to 'src/transport/test_transport_api_multiaddress.c')
-rw-r--r--src/transport/test_transport_api_multiaddress.c388
1 files changed, 388 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_multiaddress.c b/src/transport/test_transport_api_multiaddress.c
new file mode 100644
index 000000000..69f5ded4e
--- /dev/null
+++ b/src/transport/test_transport_api_multiaddress.c
@@ -0,0 +1,388 @@
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_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, 30)
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
58static struct PeerContext p1;
59
60static struct PeerContext p2;
61
62static int ok;
63
64static int is_tcp;
65
66static GNUNET_SCHEDULER_TaskIdentifier die_task;
67
68static GNUNET_SCHEDULER_TaskIdentifier tct;
69
70
71#if VERBOSE
72#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73#else
74#define OKPP do { ok++; } while (0)
75#endif
76
77
78
79static void
80stop_arm (struct PeerContext *p)
81{
82#if START_ARM
83 if (NULL != p->arm_proc)
84 {
85 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
86 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
87 GNUNET_OS_process_wait (p->arm_proc);
88 GNUNET_OS_process_close (p->arm_proc);
89 p->arm_proc = NULL;
90 }
91#endif
92 GNUNET_CONFIGURATION_destroy (p->cfg);
93}
94
95
96
97
98static void
99exchange_hello_last (void *cls,
100 const struct GNUNET_MessageHeader *message)
101{
102 struct PeerContext *me = cls;
103
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Exchanging HELLO of size %d with peer (%s)!\n",
106 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
107 GNUNET_i2s (&me->id));
108 GNUNET_assert (message != NULL);
109 GNUNET_assert (GNUNET_OK ==
110 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
111 message, &me->id));
112 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
113}
114
115
116static void
117exchange_hello (void *cls,
118 const struct GNUNET_MessageHeader *message)
119{
120 struct PeerContext *me = cls;
121
122 GNUNET_assert (message != NULL);
123 GNUNET_assert (GNUNET_OK ==
124 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
125 message, &me->id));
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Exchanging HELLO of size %d from peer %s!\n",
128 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
129 GNUNET_i2s (&me->id));
130 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
131}
132
133
134static void
135end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136{
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
138 GNUNET_break (0);
139 die_task = GNUNET_SCHEDULER_NO_TASK;
140 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
141 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
142 GNUNET_TRANSPORT_disconnect (p1.th);
143 GNUNET_TRANSPORT_disconnect (p2.th);
144 if (GNUNET_SCHEDULER_NO_TASK != tct)
145 {
146 GNUNET_SCHEDULER_cancel (tct);
147 tct = GNUNET_SCHEDULER_NO_TASK;
148 }
149 ok = 1;
150}
151
152static void
153end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154{
155 ok = 60;
156 die_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
158 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
159 GNUNET_TRANSPORT_disconnect (p1.th);
160 GNUNET_TRANSPORT_disconnect (p2.th);
161 if (GNUNET_SCHEDULER_NO_TASK != tct)
162 {
163 GNUNET_SCHEDULER_cancel (tct);
164 tct = GNUNET_SCHEDULER_NO_TASK;
165 }
166 ok = 0;
167}
168
169static void
170notify_connect (void *cls,
171 const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_TRANSPORT_ATS_Information *ats,
173 uint32_t ats_count)
174{
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Peer `%4s' connected to us (%p)!\n",
177 GNUNET_i2s (peer),
178 cls);
179 if (cls == &p1)
180 {
181 GNUNET_assert (ok >= 2);
182 OKPP;
183 OKPP;
184 if (GNUNET_SCHEDULER_NO_TASK != die_task)
185 GNUNET_SCHEDULER_cancel (die_task);
186 if (GNUNET_SCHEDULER_NO_TASK != tct)
187 GNUNET_SCHEDULER_cancel (tct);
188 tct = GNUNET_SCHEDULER_NO_TASK;
189 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
190 &end_normally, NULL);
191 }
192}
193
194
195static void
196notify_disconnect (void *cls,
197 const struct GNUNET_PeerIdentity *peer)
198{
199 if ( (ok != 60) &&
200 (GNUNET_SCHEDULER_NO_TASK != die_task) )
201 {
202 GNUNET_SCHEDULER_cancel (die_task);
203 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
204 }
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Peer `%4s' disconnected (%p)!\n",
207 GNUNET_i2s (peer), cls);
208}
209
210
211static void
212setup_peer (struct PeerContext *p,
213 const char *cfgname)
214{
215 p->cfg = GNUNET_CONFIGURATION_create ();
216
217 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
218 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
219 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
220 GNUNET_DISK_directory_remove (p->servicehome);
221
222#if START_ARM
223 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
224 "gnunet-service-arm",
225#if VERBOSE_ARM
226
227 if (is_https)
228 {
229 struct stat sbuf;
230 if (0 == stat (cert_file_p1, &sbuf ))
231 {
232 if (0 == remove(cert_file_p1))
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Successfully removed existing certificate file `%s'\n",cert_file_p1);
235 else
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Failed to remove certfile `%s'\n",cert_file_p1);
238 }
239
240 if (0 == stat (key_file_p1, &sbuf ))
241 {
242 if (0 == remove(key_file_p1))
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Successfully removed private key file `%s'\n",key_file_p1);
245 else
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Failed to private key file `%s'\n",key_file_p1);
248 }
249
250 if (0 == stat (cert_file_p2, &sbuf ))
251 {
252 if (0 == remove(cert_file_p2))
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Successfully removed existing certificate file `%s'\n",cert_file_p2);
255 else
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 "Failed to remove certfile `%s'\n",cert_file_p2);
258 }
259
260 if (0 == stat (key_file_p2, &sbuf ))
261 {
262 if (0 == remove(key_file_p2))
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Successfully removed private key file `%s'\n",key_file_p2);
265 else
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 "Failed to private key file `%s'\n",key_file_p2);
268 }
269 GNUNET_free(key_file_p1);
270 GNUNET_free(key_file_p2);
271 GNUNET_free(cert_file_p1);
272 GNUNET_free(cert_file_p2);
273 } "-L", "DEBUG",
274#endif
275 "-c", cfgname, NULL);
276#endif
277
278
279 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
280 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
281
282 p->th = GNUNET_TRANSPORT_connect (p->cfg,
283 NULL, p,
284 NULL,
285 &notify_connect, &notify_disconnect);
286 GNUNET_assert (p->th != NULL);
287}
288
289
290static void
291try_connect (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
293{
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Asking peers to connect...\n");
296 /* FIXME: 'pX.id' may still be all-zeros here... */
297 GNUNET_TRANSPORT_try_connect (p2.th,
298 &p1.id);
299 GNUNET_TRANSPORT_try_connect (p1.th,
300 &p2.id);
301 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
302 &try_connect,
303 NULL);
304}
305
306
307static void
308run (void *cls,
309 char *const *args,
310 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
311{
312 GNUNET_assert (ok == 1);
313 OKPP;
314 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
315 &end_badly, NULL);
316 if (is_tcp)
317 {
318 setup_peer (&p1, "test_transport_api_multiaddress_tcp_peer1.conf");
319 setup_peer (&p2, "test_transport_api_multiaddress_tcp_peer2.conf");
320 }
321 GNUNET_assert(p1.th != NULL);
322 GNUNET_assert(p2.th != NULL);
323
324 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
325 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
326 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
327}
328
329static int
330check ()
331{
332 static char *const argv[] = { "test-transport-api",
333 "-c",
334 "test_transport_api_data.conf",
335#if VERBOSE
336 "-L", "DEBUG",
337#endif
338 NULL
339 };
340 static struct GNUNET_GETOPT_CommandLineOption options[] = {
341 GNUNET_GETOPT_OPTION_END
342 };
343
344#if WRITECONFIG
345 setTransportOptions("test_transport_api_data.conf");
346#endif
347 ok = 1;
348 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
349 argv, "test-transport-api", "nohelp",
350 options, &run, &ok);
351 stop_arm (&p1);
352 stop_arm (&p2);
353
354 if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
355 {
356 GNUNET_DISK_directory_remove (p1.servicehome);
357 GNUNET_DISK_directory_remove (p2.servicehome);
358 GNUNET_free(p1.servicehome);
359 GNUNET_free(p2.servicehome);
360 }
361 return ok;
362}
363
364
365int
366main (int argc, char *argv[])
367{
368 int ret;
369
370 GNUNET_log_setup ("test-transport-api",
371#if VERBOSE
372 "DEBUG",
373#else
374 "WARNING",
375#endif
376 NULL);
377
378 if (strstr(argv[0], "tcp") != NULL)
379 {
380 is_tcp = GNUNET_YES;
381 }
382
383 ret = check ();
384
385 return ret;
386}
387
388/* end of test_transport_api.c */