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_util_lib.h"
26 #include "gnunet_resolver_service.h"
31 * Using DNS root servers to check gnunet's resolver service
32 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
33 * For more information have a look at IANA's website http://www.root-servers.org/
35 #define ROOTSERVER_NAME "a.root-servers.net"
36 #define ROOTSERVER_IP "198.41.0.4"
40 check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
49 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
50 GNUNET_a2s (sa, salen));
55 check_localhost_num (void *cls, const char *hostname)
61 if (0 == strcmp (hostname, "127.0.0.1"))
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid hostname `%s'.\n",
77 check_localhost (void *cls, const char *hostname)
83 if (0 == strcmp (hostname, "localhost"))
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct hostname `%s'.\n",
91 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
92 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
99 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
102 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
106 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
107 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
114 char buf[INET_ADDRSTRLEN];
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received incorrect address`%s'.\n",
117 inet_ntop (AF_INET, &sai->sin_addr, buf, sizeof (buf)));
124 check_local_fqdn (void *cls, const char *gnunet_fqdn)
128 struct hostent *host;
129 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
131 if (0 != gethostname (hostname, sizeof (hostname) - 1))
133 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolving our FQDN `%s'\n"),
139 host = gethostbyname (hostname);
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 _("Could not resolve our FQDN: %s %u\n"), hstrerror (h_errno),
148 GNUNET_assert (0 != host);
150 result = strcmp (host->h_name, gnunet_fqdn);
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 "Local resolved and resolver resolved fqdns are not equal\n");
156 GNUNET_assert (0 == result);
161 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
164 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
168 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
170 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Received correct rootserver ip address.\n");
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Received incorrect rootserver ip address.\n");
186 check_rootserver_name (void *cls, const char *hostname)
190 if (NULL == hostname)
193 if (0 == strcmp (hostname, ROOTSERVER_NAME))
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Received correct rootserver hostname `%s'.\n", hostname);
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Received invalid rootserver hostname `%s'.\n", hostname);
209 run (void *cls, char *const *args, const char *cfgfile,
210 const struct GNUNET_CONFIGURATION_Handle *cfg)
213 struct sockaddr_in sa;
214 struct GNUNET_TIME_Relative timeout =
215 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
219 memset (&sa, 0, sizeof (sa));
220 sa.sin_family = AF_INET;
221 #if HAVE_SOCKADDR_IN_SIN_LEN
222 sa.sin_len = (u_char) sizeof (sa);
224 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
227 * Looking up our own fqdn
229 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
230 check_local_fqdn (NULL, own_fqdn);
231 GNUNET_free_non_null (own_fqdn);
234 * Testing non-local DNS resolution
235 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
237 const char *rootserver_name = ROOTSERVER_NAME;
238 struct hostent *rootserver;
240 rootserver = gethostbyname (rootserver_name);
241 if (rootserver == NULL)
243 /* Error: resolving ip addresses does not work */
244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245 _("gethostbyname() could not lookup IP address: %s\n"),
246 hstrerror (h_errno));
248 "%s", "System seems to be off-line, will not run all DNS tests\n");
249 *ok = 0; /* mark test as passing anyway */
253 /* Counting returned IP addresses */
254 while (rootserver->h_addr_list[count_ips] != NULL)
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "IP received range for root name server, but a root name server has only 1 IP\n");
263 /* Comparing to resolved address to the address the root name server should have */
265 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "IP received and IP for root name server differ\n");
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273 "System's own forward name resolution is working\n");
274 /* Resolve the same using GNUNET */
275 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
276 &check_rootserver_ip, cls);
279 * Success: forward lookups work as expected
280 * Next step: reverse lookups
283 struct in_addr rootserver_addr;
285 rootserver->h_name = "";
286 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr))
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Could not transform root name server IP address\n");
294 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET);
295 if (rootserver == NULL)
297 /* Error: resolving IP addresses does not work */
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 _("gethostbyaddr() could not lookup hostname: %s\n"),
300 hstrerror (h_errno));
305 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Received hostname and hostname for root name server differ\n");
313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
314 "System's own reverse name resolution is working\n");
315 /* Resolve the same using GNUNET */
316 memset (&sa, 0, sizeof (sa));
317 sa.sin_family = AF_INET;
318 #if HAVE_SOCKADDR_IN_SIN_LEN
319 sa.sin_len = (u_char) sizeof (sa);
322 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
324 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
326 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
327 sizeof (struct sockaddr), GNUNET_YES, timeout,
328 &check_rootserver_name, cls);
330 memset (&sa, 0, sizeof (sa));
331 sa.sin_family = AF_INET;
332 #if HAVE_SOCKADDR_IN_SIN_LEN
333 sa.sin_len = (u_char) sizeof (sa);
335 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
337 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
338 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
339 sizeof (struct sockaddr), GNUNET_YES, timeout,
340 &check_localhost, cls);
342 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
343 sizeof (struct sockaddr), GNUNET_NO, timeout,
344 &check_localhost_num, cls);
345 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
351 main (int argc, char *argv[])
353 int ok = 1 + 2 + 4 + 8;
355 struct GNUNET_OS_Process *proc;
356 char *const argvx[] = {
357 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
359 struct GNUNET_GETOPT_CommandLineOption options[] =
360 { GNUNET_GETOPT_OPTION_END };
362 GNUNET_log_setup ("test-resolver-api",
365 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
366 proc = GNUNET_OS_start_process (GNUNET_YES,
367 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
369 "gnunet-service-resolver",
370 "-c", "test_resolver_api_data.conf", NULL);
371 GNUNET_assert (NULL != proc);
373 GNUNET_assert (GNUNET_OK ==
374 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
375 argvx, "test-resolver-api", "nohelp",
376 options, &run, &ok));
377 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
379 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
382 GNUNET_OS_process_wait (proc);
383 GNUNET_OS_process_destroy (proc);
386 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
391 /* end of test_resolver_api.c */