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', expected `%s'\n",
211 run (void *cls, char *const *args, const char *cfgfile,
212 const struct GNUNET_CONFIGURATION_Handle *cfg)
215 struct sockaddr_in sa;
216 struct GNUNET_TIME_Relative timeout =
217 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
221 memset (&sa, 0, sizeof (sa));
222 sa.sin_family = AF_INET;
223 #if HAVE_SOCKADDR_IN_SIN_LEN
224 sa.sin_len = (u_char) sizeof (sa);
226 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
229 * Looking up our own fqdn
231 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
232 check_local_fqdn (NULL, own_fqdn);
233 GNUNET_free_non_null (own_fqdn);
236 * Testing non-local DNS resolution
237 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
239 const char *rootserver_name = ROOTSERVER_NAME;
240 struct hostent *rootserver;
242 rootserver = gethostbyname (rootserver_name);
243 if (rootserver == NULL)
245 /* Error: resolving ip addresses does not work */
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 _("gethostbyname() could not lookup IP address: %s\n"),
248 hstrerror (h_errno));
250 "%s", "System seems to be off-line, will not run all DNS tests\n");
251 *ok = 0; /* mark test as passing anyway */
255 /* Counting returned IP addresses */
256 while (rootserver->h_addr_list[count_ips] != NULL)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "IP received range for root name server, but a root name server has only 1 IP\n");
265 /* Comparing to resolved address to the address the root name server should have */
267 (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "IP received and IP for root name server differ\n");
274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
275 "System's own forward name resolution is working\n");
276 /* Resolve the same using GNUNET */
277 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
278 &check_rootserver_ip, cls);
281 * Success: forward lookups work as expected
282 * Next step: reverse lookups
285 struct in_addr rootserver_addr;
287 rootserver->h_name = "";
288 if (1 != inet_pton (AF_INET, ROOTSERVER_IP, &rootserver_addr))
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Could not transform root name server IP address\n");
296 gethostbyaddr (&rootserver_addr, sizeof (rootserver_addr), AF_INET);
297 if (NULL == rootserver)
299 /* Error: resolving IP addresses does not work */
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301 "gethostbyaddr() could not lookup hostname: %s\n",
302 hstrerror (h_errno));
307 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Received hostname and hostname for root name server differ\n");
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 "System's own reverse name resolution is working\n");
317 /* Resolve the same using GNUNET */
318 memset (&sa, 0, sizeof (sa));
319 sa.sin_family = AF_INET;
320 #if HAVE_SOCKADDR_IN_SIN_LEN
321 sa.sin_len = (u_char) sizeof (sa);
324 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
326 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
328 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
329 sizeof (struct sockaddr), GNUNET_YES, timeout,
330 &check_rootserver_name, cls);
332 memset (&sa, 0, sizeof (sa));
333 sa.sin_family = AF_INET;
334 #if HAVE_SOCKADDR_IN_SIN_LEN
335 sa.sin_len = (u_char) sizeof (sa);
337 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
339 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
340 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
341 sizeof (struct sockaddr), GNUNET_YES, timeout,
342 &check_localhost, cls);
344 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
345 sizeof (struct sockaddr), GNUNET_NO, timeout,
346 &check_localhost_num, cls);
347 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
353 main (int argc, char *argv[])
355 int ok = 1 + 2 + 4 + 8;
357 struct GNUNET_OS_Process *proc;
358 char *const argvx[] = {
359 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
361 struct GNUNET_GETOPT_CommandLineOption options[] =
362 { GNUNET_GETOPT_OPTION_END };
364 GNUNET_log_setup ("test-resolver-api",
367 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
368 proc = GNUNET_OS_start_process (GNUNET_YES,
369 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
372 "gnunet-service-resolver",
373 "-c", "test_resolver_api_data.conf", NULL);
374 GNUNET_assert (NULL != proc);
376 GNUNET_assert (GNUNET_OK ==
377 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
378 argvx, "test-resolver-api", "nohelp",
379 options, &run, &ok));
380 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
382 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
385 GNUNET_OS_process_wait (proc);
386 GNUNET_OS_process_destroy (proc);
389 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
394 /* end of test_resolver_api.c */