aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_ats.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-03-17 16:53:06 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-03-17 16:53:06 +0000
commit4cbd3d6f60160226c1bc30da655838c2e2fad4e3 (patch)
tree6405340f76dfa4a385f5135e9f15416a0b88ae2a /src/transport/test_transport_ats.c
parent4759a55fd2bba8b4aabef3ef361c8c6183fbe3e5 (diff)
downloadgnunet-4cbd3d6f60160226c1bc30da655838c2e2fad4e3.tar.gz
gnunet-4cbd3d6f60160226c1bc30da655838c2e2fad4e3.zip
ats testcase
Diffstat (limited to 'src/transport/test_transport_ats.c')
-rw-r--r--src/transport/test_transport_ats.c641
1 files changed, 641 insertions, 0 deletions
diff --git a/src/transport/test_transport_ats.c b/src/transport/test_transport_ats.c
new file mode 100644
index 000000000..4c636cab6
--- /dev/null
+++ b/src/transport/test_transport_ats.c
@@ -0,0 +1,641 @@
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_ats.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
39#define VERBOSE GNUNET_NO
40
41#define VERBOSE_ARM GNUNET_NO
42
43#define START_ARM GNUNET_YES
44
45/**
46 * How long until we give up on transmitting the message?
47 */
48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49
50/**
51 * How long until we give up on transmitting the message?
52 */
53#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54
55#define MTYPE 12345
56
57struct PeerContext
58{
59 struct GNUNET_CONFIGURATION_Handle *cfg;
60 struct GNUNET_TRANSPORT_Handle *th;
61 struct GNUNET_PeerIdentity id;
62#if START_ARM
63 struct GNUNET_OS_Process *arm_proc;
64#endif
65};
66
67static struct PeerContext p1;
68
69static struct PeerContext p2;
70
71static int ok;
72
73static int is_tcp;
74
75static int is_tcp_nat;
76
77static int is_udp;
78
79static int is_unix;
80
81static int is_udp_nat;
82
83static int is_http;
84
85static int is_https;
86
87static int is_multi_protocol;
88
89static int is_wlan;
90
91static GNUNET_SCHEDULER_TaskIdentifier die_task;
92
93static char * key_file_p1;
94static char * cert_file_p1;
95
96static char * key_file_p2;
97static char * cert_file_p2;
98
99#if VERBOSE
100#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
101#else
102#define OKPP do { ok++; } while (0)
103#endif
104
105
106static void
107end ()
108{
109 /* do work here */
110 GNUNET_assert (ok == 6);
111 GNUNET_SCHEDULER_cancel (die_task);
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
113 GNUNET_TRANSPORT_disconnect (p1.th);
114 GNUNET_TRANSPORT_disconnect (p2.th);
115
116 die_task = GNUNET_SCHEDULER_NO_TASK;
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
118 ok = 0;
119}
120
121static void
122stop_arm (struct PeerContext *p)
123{
124#if START_ARM
125 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
127 GNUNET_OS_process_wait (p->arm_proc);
128 GNUNET_OS_process_close (p->arm_proc);
129 p->arm_proc = NULL;
130#endif
131 GNUNET_CONFIGURATION_destroy (p->cfg);
132}
133
134
135static void
136end_badly ()
137{
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
139 GNUNET_break (0);
140 GNUNET_TRANSPORT_disconnect (p1.th);
141 GNUNET_TRANSPORT_disconnect (p2.th);
142 ok = 1;
143}
144
145static void
146notify_receive (void *cls,
147 const struct GNUNET_PeerIdentity *peer,
148 const struct GNUNET_MessageHeader *message,
149 const struct GNUNET_TRANSPORT_ATS_Information *ats,
150 uint32_t ats_count)
151{
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
153 ok);
154
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
156 ntohs(message->type), cls);
157
158 GNUNET_assert (ok == 5);
159 OKPP;
160
161 GNUNET_assert (MTYPE == ntohs (message->type));
162 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
163 ntohs (message->size));
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
165 cls);
166 end ();
167}
168
169
170static size_t
171notify_ready (void *cls, size_t size, void *buf)
172{
173 struct GNUNET_MessageHeader *hdr;
174
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
177 GNUNET_assert (size >= 256);
178 GNUNET_assert (ok == 4);
179 OKPP;
180
181 if (buf != NULL)
182 {
183 hdr = buf;
184 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
185 hdr->type = htons (MTYPE);
186 }
187
188 return sizeof (struct GNUNET_MessageHeader);
189}
190
191static size_t
192notify_ready_connect (void *cls, size_t size, void *buf)
193{
194 return 0;
195}
196
197
198static void
199notify_connect (void *cls,
200 const struct GNUNET_PeerIdentity *peer,
201 const struct GNUNET_TRANSPORT_ATS_Information *ats,
202 uint32_t ats_count)
203{
204 if (cls == &p1)
205 {
206 GNUNET_SCHEDULER_cancel (die_task);
207 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
208 &end_badly, NULL);
209
210 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
211 &p2.id,
212 256, 0, TIMEOUT, &notify_ready,
213 &p1);
214 }
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
217}
218
219
220static void
221notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
222{
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Peer `%4s' disconnected (%p)!\n",
225 GNUNET_i2s (peer), cls);
226}
227
228
229static void
230setup_peer (struct PeerContext *p, const char *cfgname)
231{
232 p->cfg = GNUNET_CONFIGURATION_create ();
233#if START_ARM
234 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
235 "gnunet-service-arm",
236#if VERBOSE_ARM
237 "-L", "DEBUG",
238#endif
239 "-c", cfgname, NULL);
240#endif
241 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
242
243 if (is_https)
244 {
245 struct stat sbuf;
246 if (p==&p1)
247 {
248 if (GNUNET_CONFIGURATION_have_value (p->cfg,
249 "transport-https", "KEY_FILE"))
250 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
251 if (key_file_p1==NULL)
252 GNUNET_asprintf(&key_file_p1,"https.key");
253 if (0 == stat (key_file_p1, &sbuf ))
254 {
255 if (0 == remove(key_file_p1))
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
257 else
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
259 }
260 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
261 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
262 if (cert_file_p1==NULL)
263 GNUNET_asprintf(&cert_file_p1,"https.cert");
264 if (0 == stat (cert_file_p1, &sbuf ))
265 {
266 if (0 == remove(cert_file_p1))
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
268 else
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
270 }
271 }
272 else if (p==&p2)
273 {
274 if (GNUNET_CONFIGURATION_have_value (p->cfg,
275 "transport-https", "KEY_FILE"))
276 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
277 if (key_file_p2==NULL)
278 GNUNET_asprintf(&key_file_p2,"https.key");
279 if (0 == stat (key_file_p2, &sbuf ))
280 {
281 if (0 == remove(key_file_p2))
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
283 else
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
285 }
286 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
287 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
288 if (cert_file_p2==NULL)
289 GNUNET_asprintf(&cert_file_p2,"https.cert");
290 if (0 == stat (cert_file_p2, &sbuf ))
291 {
292 if (0 == remove(cert_file_p2))
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
294 else
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
296 }
297 }
298 }
299
300 p->th = GNUNET_TRANSPORT_connect (p->cfg,
301 NULL, p,
302 &notify_receive,
303 &notify_connect, &notify_disconnect);
304 GNUNET_assert (p->th != NULL);
305}
306
307
308static void
309exchange_hello_last (void *cls,
310 const struct GNUNET_MessageHeader *message)
311{
312 struct PeerContext *me = cls;
313
314 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Exchanging HELLO with peer (%p)!\n", cls);
317 GNUNET_assert (ok >= 3);
318 OKPP;
319 GNUNET_assert (message != NULL);
320 GNUNET_assert (GNUNET_OK ==
321 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
322 message, &me->id));
323
324 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
325 &p1.id,
326 sizeof (struct GNUNET_MessageHeader), 0,
327 TIMEOUT,
328 &notify_ready_connect,
329 NULL));
330
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Finished exchanging HELLOs, now waiting for transmission!\n");
333}
334
335static void
336exchange_hello (void *cls,
337 const struct GNUNET_MessageHeader *message)
338{
339 return;
340 struct PeerContext *me = cls;
341
342 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Exchanging HELLO with peer (%p)!\n", cls);
345 GNUNET_assert (ok >= 2);
346 OKPP;
347 GNUNET_assert (message != NULL);
348 GNUNET_assert (GNUNET_OK ==
349 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
350 message, &me->id));
351
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
354
355 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
356 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
357}
358
359
360static void
361run (void *cls,
362 char *const *args,
363 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
364{
365 GNUNET_assert (ok == 1);
366 OKPP;
367 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
368 &end_badly, NULL);
369
370 setup_peer (&p1, "test_transport_ats_peer1.conf");
371 setup_peer (&p2, "test_transport_ats_peer2.conf");
372
373 GNUNET_assert(p1.th != NULL);
374 GNUNET_assert(p2.th != NULL);
375
376 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
377}
378
379static int
380check ()
381{
382 static char *const argv[] = { "test-transport-api",
383 "-c",
384 "test_transport_api_data.conf",
385#if VERBOSE
386 "-L", "DEBUG",
387#endif
388 NULL
389 };
390 static struct GNUNET_GETOPT_CommandLineOption options[] = {
391 GNUNET_GETOPT_OPTION_END
392 };
393
394#if WRITECONFIG
395 setTransportOptions("test_transport_api_data.conf");
396#endif
397 ok = 1;
398 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
399 argv, "test-transport-api", "nohelp",
400 options, &run, &ok);
401 stop_arm (&p1);
402 stop_arm (&p2);
403
404 if (is_https)
405 {
406 struct stat sbuf;
407 if (0 == stat (cert_file_p1, &sbuf ))
408 {
409 if (0 == remove(cert_file_p1))
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
411 else
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
413 }
414
415 if (0 == stat (key_file_p1, &sbuf ))
416 {
417 if (0 == remove(key_file_p1))
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
419 else
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
421 }
422
423 if (0 == stat (cert_file_p2, &sbuf ))
424 {
425 if (0 == remove(cert_file_p2))
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
427 else
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
429 }
430
431 if (0 == stat (key_file_p2, &sbuf ))
432 {
433 if (0 == remove(key_file_p2))
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
435 else
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
437 }
438 GNUNET_free(key_file_p1);
439 GNUNET_free(key_file_p2);
440 GNUNET_free(cert_file_p1);
441 GNUNET_free(cert_file_p2);
442 }
443 return ok;
444}
445
446/**
447 * Return the actual path to a file found in the current
448 * PATH environment variable.
449 *
450 * @param binary the name of the file to find
451 */
452static char *
453get_path_from_PATH (char *binary)
454{
455 char *path;
456 char *pos;
457 char *end;
458 char *buf;
459 const char *p;
460
461 p = getenv ("PATH");
462 if (p == NULL)
463 {
464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
465 _("PATH environment variable is unset.\n"));
466 return NULL;
467 }
468 path = GNUNET_strdup (p); /* because we write on it */
469 buf = GNUNET_malloc (strlen (path) + 20);
470 pos = path;
471
472 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
473 {
474 *end = '\0';
475 sprintf (buf, "%s/%s", pos, binary);
476 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
477 {
478 GNUNET_free (path);
479 return buf;
480 }
481 pos = end + 1;
482 }
483 sprintf (buf, "%s/%s", pos, binary);
484 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
485 {
486 GNUNET_free (path);
487 return buf;
488 }
489 GNUNET_free (buf);
490 GNUNET_free (path);
491 return NULL;
492}
493
494/**
495 * Check whether the suid bit is set on a file.
496 * Attempts to find the file using the current
497 * PATH environment variable as a search path.
498 *
499 * @param binary the name of the file to check
500 *
501 * @return GNUNET_YES if the binary is found and
502 * can be run properly, GNUNET_NO otherwise
503 */
504static int
505check_gnunet_nat_binary(char *binary)
506{
507 struct stat statbuf;
508 char *p;
509#ifdef MINGW
510 SOCKET rawsock;
511#endif
512
513#ifdef MINGW
514 char *binaryexe;
515 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
516 p = get_path_from_PATH (binaryexe);
517 free (binaryexe);
518#else
519 p = get_path_from_PATH (binary);
520#endif
521 if (p == NULL)
522 {
523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
524 _("Could not find binary `%s' in PATH!\n"),
525 binary);
526 return GNUNET_NO;
527 }
528 if (0 != STAT (p, &statbuf))
529 {
530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
531 _("stat (%s) failed: %s\n"),
532 p,
533 STRERROR (errno));
534 GNUNET_free (p);
535 return GNUNET_SYSERR;
536 }
537 GNUNET_free (p);
538#ifndef MINGW
539 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
540 (statbuf.st_uid == 0) )
541 return GNUNET_YES;
542 return GNUNET_NO;
543#else
544 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
545 if (INVALID_SOCKET == rawsock)
546 {
547 DWORD err = GetLastError ();
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
550 return GNUNET_NO; /* not running as administrator */
551 }
552 closesocket (rawsock);
553 return GNUNET_YES;
554#endif
555}
556
557int
558main (int argc, char *argv[])
559{
560 int ret;
561#ifdef MINGW
562 return GNUNET_SYSERR;
563#endif
564
565 GNUNET_log_setup ("test-transport-ats",
566#if VERBOSE
567 "DEBUG",
568#else
569 "WARNING",
570#endif
571 NULL);
572
573 if (strstr(argv[0], "tcp_nat") != NULL)
574 {
575 is_tcp_nat = GNUNET_YES;
576 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
577 {
578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
579 "`%s' not properly installed, cannot run NAT test!\n",
580 "gnunet-nat-server");
581 return 0;
582 }
583 }
584 else if (strstr(argv[0], "tcp") != NULL)
585 {
586 is_tcp = GNUNET_YES;
587 }
588 else if (strstr(argv[0], "udp_nat") != NULL)
589 {
590 is_udp_nat = GNUNET_YES;
591 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
592 {
593 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
594 "`%s' not properly installed, cannot run NAT test!\n",
595 "gnunet-nat-server");
596 return 0;
597 }
598 }
599 else if (strstr(argv[0], "udp") != NULL)
600 {
601 is_udp = GNUNET_YES;
602 }
603 else if (strstr(argv[0], "unix") != NULL)
604 {
605 is_unix = GNUNET_YES;
606 }
607 else if (strstr(argv[0], "https") != NULL)
608 {
609 is_https = GNUNET_YES;
610 }
611 else if (strstr(argv[0], "http") != NULL)
612 {
613 is_http = GNUNET_YES;
614 }
615 else if (strstr(argv[0], "wlan") != NULL)
616 {
617 is_wlan = GNUNET_YES;
618 }
619 else if (strstr(argv[0], "multi") != NULL)
620 {
621 is_multi_protocol = GNUNET_YES;
622 }
623
624 ret = check ();
625 if (is_multi_protocol)
626 {
627 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
628 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
629 }
630 else
631 {
632 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
633 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
634 }
635
636 //return ret;
637 /* GGG */
638 return 0;
639}
640
641/* end of test_transport_ats.c */