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_NO
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));
58 check_localhost_num(void *cls, const char *hostname)
63 if (0 == strcmp(hostname, "127.0.0.1"))
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "Received correct hostname `%s'.\n", hostname);
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "Received invalid hostname `%s'.\n", hostname);
82 check_localhost(void *cls, const char *hostname)
87 if (0 == strcmp(hostname, "localhost"))
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Received correct hostname `%s'.\n", hostname);
97 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
98 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
104 check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
107 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
111 GNUNET_assert(sizeof(struct sockaddr_in) == salen);
112 if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
129 check_local_fqdn(void *cls, const char *gnunet_fqdn)
133 struct hostent *host;
134 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
136 if (0 != gethostname (hostname, sizeof (hostname) - 1))
138 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
139 GNUNET_ERROR_TYPE_BULK, "gethostname");
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 _("Resolving our FQDN `%s'\n"), hostname);
146 host = gethostbyname ( hostname );
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 _("Could not resolve our FQDN : %s %u\n"),
151 hstrerror (h_errno), h_errno);
155 GNUNET_assert( 0 != host);
157 result = strcmp(host->h_name, gnunet_fqdn);
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 "Local resolved and resolver resolved fqdns are not equal\n");
163 GNUNET_assert( 0 == result);
169 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
172 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
176 GNUNET_assert(sizeof(struct sockaddr_in) == salen);
178 if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
195 check_rootserver_name(void *cls, const char *hostname)
198 if (hostname == NULL)
201 if (0 == strcmp(hostname, ROOTSERVER_NAME))
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Received correct rootserver hostname `%s'.\n", hostname);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Received invalid rootserver hostname `%s'.\n", hostname);
220 run(void *cls, char * const *args,
221 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
224 struct sockaddr_in sa;
225 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
226 GNUNET_TIME_UNIT_MILLISECONDS, 2500);
230 memset(&sa, 0, sizeof(sa));
231 sa.sin_family = AF_INET;
232 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
233 GNUNET_RESOLVER_ip_get(cfg, "localhost", AF_INET, timeout, &check_127,
235 GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
236 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
237 GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
238 sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
239 GNUNET_RESOLVER_hostname_resolve(cfg, AF_UNSPEC, timeout,
240 &check_hostname, cls);
244 * Looking up our own fqdn
246 own_fqdn = GNUNET_RESOLVER_local_fqdn_get();
247 check_local_fqdn( NULL, own_fqdn);
248 GNUNET_free_non_null (own_fqdn);
251 * Testing non-local DNS resolution
252 * 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 "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, "IP received range for root name server, but a root name server has only 1 IP\n");
284 /* Comparing to resolved address to the address the root name server should have */
285 if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
294 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
297 /* Resolve the same using GNUNET */
298 GNUNET_RESOLVER_ip_get(cfg, ROOTSERVER_NAME, AF_INET, timeout,
299 &check_rootserver_ip, cls);
302 * Success: forward lookups work as expected
303 * Next step: reverse lookups
306 struct in_addr rootserver_addr;
307 rootserver->h_name = "";
308 if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
317 = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
318 if (rootserver == NULL)
320 /* Error: resolving IP addresses does not work */
322 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
323 _("gethostbyaddr() could not lookup hostname: %s\n"),
324 hstrerror (h_errno));
330 if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
342 /* Resolve the same using GNUNET */
344 memset(&sa, 0, sizeof(sa));
345 sa.sin_family = AF_INET;
347 inet_aton(ROOTSERVER_IP, &sa.sin_addr);
349 sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
351 GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
352 sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
358 int ok = 1 + 2 + 4 + 8;
361 struct GNUNET_OS_Process *proc;
362 char * const argv[] =
363 { "test-resolver-api", "-c", "test_resolver_api_data.conf",
368 struct GNUNET_GETOPT_CommandLineOption options[] =
369 { GNUNET_GETOPT_OPTION_END };
370 pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
371 GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
373 proc = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
377 "-c", "test_resolver_api_data.conf", NULL);
379 GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
380 - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
381 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
383 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
386 GNUNET_OS_process_wait (proc);
387 GNUNET_OS_process_close (proc);
390 fprintf(stderr, "Missed some resolutions: %u\n", ok);
395 main(int argc, char *argv[])
399 GNUNET_log_setup("test-resolver-api",
411 /* end of test_resolver_api.c */