2 This file is part of GNUnet.
3 Copyright (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"
30 static int disable_rootserver_check;
34 * Using DNS root servers to check gnunet's resolver service
35 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
36 * For more information have a look at IANA's website http://www.root-servers.org/
38 #define ROOTSERVER_NAME "a.root-servers.net"
39 #define ROOTSERVER_IP "198.41.0.4"
43 check_hostname (void *cls,
44 const struct sockaddr *sa,
54 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
55 "Got IP address `%s' for our host.\n",
56 GNUNET_a2s (sa, salen));
61 check_localhost_num (void *cls,
68 if (0 == strcmp (hostname, "127.0.0.1"))
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71 "Received correct hostname `%s'.\n",
77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78 "Received invalid hostname `%s'.\n",
86 check_localhost (void *cls,
93 if (0 == strcmp (hostname, "localhost"))
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Received correct hostname `%s'.\n",
102 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
103 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
110 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
113 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
117 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
118 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Received correct address.\n");
126 char buf[INET_ADDRSTRLEN];
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 "Received incorrect address `%s'.\n",
140 check_local_fqdn (void *cls, const char *gnunet_fqdn)
144 struct hostent *host;
145 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
147 if (0 != gethostname (hostname, sizeof (hostname) - 1))
149 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Resolving our FQDN `%s'\n",
156 host = gethostbyname (hostname);
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Could not resolve our FQDN: %s %u\n",
166 GNUNET_assert (0 != host);
168 result = strcmp (host->h_name, gnunet_fqdn);
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 "Local resolved and resolver resolved fqdns are not equal\n");
174 GNUNET_assert (0 == result);
179 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
182 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
186 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
188 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Received correct rootserver ip address.\n");
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Received incorrect rootserver ip address.\n");
204 check_rootserver_name (void *cls,
205 const char *hostname)
209 if (NULL == hostname)
212 if (0 == strcmp (hostname, ROOTSERVER_NAME))
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Received correct rootserver hostname `%s'.\n",
221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
222 "Received invalid rootserver hostname `%s', expected `%s'\n",
225 GNUNET_break (disable_rootserver_check);
231 run (void *cls, char *const *args, const char *cfgfile,
232 const struct GNUNET_CONFIGURATION_Handle *cfg)
235 struct sockaddr_in sa;
236 struct GNUNET_TIME_Relative timeout =
237 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
240 const char *rootserver_name = ROOTSERVER_NAME;
241 struct hostent *rootserver;
242 struct in_addr rootserver_addr;
244 memset (&sa, 0, sizeof (sa));
245 sa.sin_family = AF_INET;
246 #if HAVE_SOCKADDR_IN_SIN_LEN
247 sa.sin_len = (u_char) sizeof (sa);
249 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
252 * Looking up our own fqdn
254 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
255 check_local_fqdn (NULL, own_fqdn);
256 GNUNET_free_non_null (own_fqdn);
259 * Testing non-local DNS resolution
260 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
263 rootserver = gethostbyname (rootserver_name);
264 if (NULL == rootserver)
266 /* Error: resolving ip addresses does not work */
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 _("gethostbyname() could not lookup IP address: %s\n"),
269 hstrerror (h_errno));
272 "System seems to be off-line, will not run all DNS tests\n");
273 *ok = 0; /* mark test as passing anyway */
277 /* Counting returned IP addresses */
278 while (NULL != rootserver->h_addr_list[count_ips])
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "IP received range for root name server, but a root name server has only 1 IP\n");
287 /* Comparing to resolved address to the address the root name server should have */
289 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 "IP received and IP for root name server differ\n");
296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
297 "System's own forward name resolution is working\n");
298 /* Resolve the same using GNUNET */
299 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
300 &check_rootserver_ip, cls);
303 * Success: forward lookups work as expected
304 * Next step: reverse lookups
306 if (1 != inet_pton (AF_INET,
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Could not transform root name server IP address\n");
316 gethostbyaddr (&rootserver_addr,
317 sizeof (rootserver_addr),
319 if (NULL == rootserver)
321 /* 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));
325 disable_rootserver_check = GNUNET_YES;
329 if (0 != strcmp (rootserver->h_name,
332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
333 "Received hostname and hostname for root name server differ\n");
334 disable_rootserver_check = GNUNET_YES;
338 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
339 "System's own reverse name resolution is working\n");
340 /* Resolve the same using GNUNET */
341 memset (&sa, 0, sizeof (sa));
342 sa.sin_family = AF_INET;
343 #if HAVE_SOCKADDR_IN_SIN_LEN
344 sa.sin_len = (u_char) sizeof (sa);
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 ((const struct sockaddr *) &sa,
352 sizeof (struct sockaddr), GNUNET_YES, timeout,
353 &check_rootserver_name, cls);
355 memset (&sa, 0, sizeof (sa));
356 sa.sin_family = AF_INET;
357 #if HAVE_SOCKADDR_IN_SIN_LEN
358 sa.sin_len = (u_char) sizeof (sa);
360 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
362 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
363 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
364 sizeof (struct sockaddr), GNUNET_YES, timeout,
365 &check_localhost, cls);
367 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
368 sizeof (struct sockaddr), GNUNET_NO, timeout,
369 &check_localhost_num, cls);
370 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
376 main (int argc, char *argv[])
378 int ok = 1 + 2 + 4 + 8;
380 struct GNUNET_OS_Process *proc;
381 char *const argvx[] = {
382 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
384 struct GNUNET_GETOPT_CommandLineOption options[] =
385 { GNUNET_GETOPT_OPTION_END };
387 GNUNET_log_setup ("test-resolver-api",
390 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
391 proc = GNUNET_OS_start_process (GNUNET_YES,
392 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
395 "gnunet-service-resolver",
396 "-c", "test_resolver_api_data.conf", NULL);
397 GNUNET_assert (NULL != proc);
399 GNUNET_assert (GNUNET_OK ==
400 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
401 argvx, "test-resolver-api", "nohelp",
402 options, &run, &ok));
403 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
405 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
408 GNUNET_OS_process_wait (proc);
409 GNUNET_OS_process_destroy (proc);
412 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
417 /* end of test_resolver_api.c */