2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file resolver/test_resolver_api.c
22 * @brief testcase for resolver_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_resolver_service.h"
33 #define VERBOSE GNUNET_EXTRA_LOGGING
36 * Using DNS root servers to check gnunet's resolver service
37 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
38 * For more information have a look at IANA's website http://www.root-servers.org/
40 #define ROOTSERVER_NAME "a.root-servers.net"
41 #define ROOTSERVER_IP "198.41.0.4"
44 check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
53 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
54 GNUNET_a2s (sa, salen));
59 check_localhost_num (void *cls, const char *hostname)
65 if (0 == strcmp (hostname, "127.0.0.1"))
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
76 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received invalid hostname `%s'.\n",
85 check_localhost (void *cls, const char *hostname)
91 if (0 == strcmp (hostname, "localhost"))
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
108 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
111 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
115 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
116 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
133 check_local_fqdn (void *cls, const char *gnunet_fqdn)
137 struct hostent *host;
138 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
140 if (0 != gethostname (hostname, sizeof (hostname) - 1))
142 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"),
150 host = gethostbyname (hostname);
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 _("Could not resolve our FQDN : %s %u\n"), hstrerror (h_errno),
159 GNUNET_assert (0 != host);
161 result = strcmp (host->h_name, gnunet_fqdn);
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "Local resolved and resolver resolved fqdns are not equal\n");
167 GNUNET_assert (0 == result);
173 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
176 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
180 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
182 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Received correct rootserver ip address.\n");
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Received incorrect rootserver ip address.\n");
201 check_rootserver_name (void *cls, const char *hostname)
205 if (hostname == NULL)
208 if (0 == strcmp (hostname, ROOTSERVER_NAME))
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Received correct rootserver hostname `%s'.\n", hostname);
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Received invalid rootserver hostname `%s'.\n", hostname);
227 run (void *cls, char *const *args, const char *cfgfile,
228 const struct GNUNET_CONFIGURATION_Handle *cfg)
231 struct sockaddr_in sa;
232 struct GNUNET_TIME_Relative timeout =
233 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
237 memset (&sa, 0, sizeof (sa));
238 sa.sin_family = AF_INET;
239 #if HAVE_SOCKADDR_IN_SIN_LEN
240 sa.sin_len = (u_char) sizeof (sa);
242 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
245 * Looking up our own fqdn
247 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
248 check_local_fqdn (NULL, own_fqdn);
249 GNUNET_free_non_null (own_fqdn);
252 * Testing non-local DNS resolution
253 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
255 const char *rootserver_name = ROOTSERVER_NAME;
256 struct hostent *rootserver;
258 rootserver = gethostbyname (rootserver_name);
259 if (rootserver == NULL)
261 /* Error: resolving ip addresses does not work */
263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
264 _("gethostbyname() could not lookup IP address: %s\n"),
265 hstrerror (h_errno));
268 "%s", "System seems to be off-line, will not run all DNS tests\n");
269 *ok = 0; /* mark test as passing anyway */
273 /* Counting returned IP addresses */
274 while (rootserver->h_addr_list[count_ips] != NULL)
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "IP received range for root name server, but a root name server has only 1 IP\n");
285 /* Comparing to resolved address to the address the root name server should have */
287 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "IP received and IP for root name server differ\n");
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "System's own forward name resolution is working\n");
301 /* Resolve the same using GNUNET */
302 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
303 &check_rootserver_ip, cls);
306 * Success: forward lookups work as expected
307 * Next step: reverse lookups
310 struct in_addr rootserver_addr;
312 rootserver->h_name = "";
313 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr))
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Could not transform root name server IP address\n");
323 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET);
324 if (rootserver == NULL)
326 /* Error: resolving IP addresses does not work */
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
329 _("gethostbyaddr() could not lookup hostname: %s\n"),
330 hstrerror (h_errno));
336 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Received hostname and hostname for root name server differ\n");
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 "System's own reverse name resolution is working\n");
351 /* Resolve the same using GNUNET */
352 memset (&sa, 0, sizeof (sa));
353 sa.sin_family = AF_INET;
354 #if HAVE_SOCKADDR_IN_SIN_LEN
355 sa.sin_len = (u_char) sizeof (sa);
358 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
360 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
362 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
363 sizeof (struct sockaddr), GNUNET_YES, timeout,
364 &check_rootserver_name, cls);
366 memset (&sa, 0, sizeof (sa));
367 sa.sin_family = AF_INET;
368 #if HAVE_SOCKADDR_IN_SIN_LEN
369 sa.sin_len = (u_char) sizeof (sa);
371 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
373 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
374 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
375 sizeof (struct sockaddr), GNUNET_YES, timeout,
376 &check_localhost, cls);
378 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
379 sizeof (struct sockaddr), GNUNET_NO, timeout,
380 &check_localhost_num, cls);
381 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
388 int ok = 1 + 2 + 4 + 8;
391 struct GNUNET_OS_Process *proc;
394 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
400 struct GNUNET_GETOPT_CommandLineOption options[] =
401 { GNUNET_GETOPT_OPTION_END };
402 pfx = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
403 GNUNET_asprintf (&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
405 proc = GNUNET_OS_start_process (NULL, NULL, fn, "gnunet-service-resolver",
409 "-c", "test_resolver_api_data.conf", NULL);
410 GNUNET_assert (NULL != proc);
412 GNUNET_assert (GNUNET_OK ==
413 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
414 argv, "test-resolver-api", "nohelp",
415 options, &run, &ok));
416 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
418 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
421 GNUNET_OS_process_wait (proc);
422 GNUNET_OS_process_close (proc);
425 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
430 main (int argc, char *argv[])
434 GNUNET_log_setup ("test-resolver-api",
446 /* end of test_resolver_api.c */