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,
41 const struct sockaddr *sa,
51 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
52 "Got IP address `%s' for our host.\n",
53 GNUNET_a2s (sa, salen));
58 check_localhost_num (void *cls,
65 if (0 == strcmp (hostname, "127.0.0.1"))
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68 "Received correct hostname `%s'.\n",
74 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
75 "Received invalid hostname `%s'.\n",
83 check_localhost (void *cls,
90 if (0 == strcmp (hostname, "localhost"))
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Received correct hostname `%s'.\n",
99 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
100 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
107 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
110 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
114 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
115 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Received correct address.\n");
123 char buf[INET_ADDRSTRLEN];
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126 "Received incorrect address `%s'.\n",
137 check_local_fqdn (void *cls, const char *gnunet_fqdn)
141 struct hostent *host;
142 char hostname[GNUNET_OS_get_hostname_max_length () + 1];
144 if (0 != gethostname (hostname, sizeof (hostname) - 1))
146 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Resolving our FQDN `%s'\n",
153 host = gethostbyname (hostname);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 "Could not resolve our FQDN: %s %u\n",
163 GNUNET_assert (0 != host);
165 result = strcmp (host->h_name, gnunet_fqdn);
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 "Local resolved and resolver resolved fqdns are not equal\n");
171 GNUNET_assert (0 == result);
176 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
179 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
183 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
185 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "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 (NULL == hostname)
208 if (0 == strcmp (hostname, ROOTSERVER_NAME))
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Received correct rootserver hostname `%s'.\n",
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Received invalid rootserver hostname `%s', expected `%s'\n",
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);
236 const char *rootserver_name = ROOTSERVER_NAME;
237 struct hostent *rootserver;
238 struct in_addr rootserver_addr;
240 memset (&sa, 0, sizeof (sa));
241 sa.sin_family = AF_INET;
242 #if HAVE_SOCKADDR_IN_SIN_LEN
243 sa.sin_len = (u_char) sizeof (sa);
245 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
248 * Looking up our own fqdn
250 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
251 check_local_fqdn (NULL, own_fqdn);
252 GNUNET_free_non_null (own_fqdn);
255 * Testing non-local DNS resolution
256 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
259 rootserver = gethostbyname (rootserver_name);
260 if (NULL == rootserver)
262 /* Error: resolving ip addresses does not work */
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
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)
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "IP received range for root name server, but a root name server has only 1 IP\n");
283 /* Comparing to resolved address to the address the root name server should have */
285 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 "IP received and IP for root name server differ\n");
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293 "System's own forward name resolution is working\n");
294 /* Resolve the same using GNUNET */
295 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
296 &check_rootserver_ip, cls);
299 * Success: forward lookups work as expected
300 * Next step: reverse lookups
302 if (1 != inet_pton (AF_INET,
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Could not transform root name server IP address\n");
312 gethostbyaddr (&rootserver_addr,
313 sizeof (rootserver_addr),
315 if (NULL == rootserver)
317 /* Error: resolving IP addresses does not work */
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319 "gethostbyaddr() could not lookup hostname: %s\n",
320 hstrerror (h_errno));
325 if (0 != strcmp (rootserver->h_name, ROOTSERVER_NAME))
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Received hostname and hostname for root name server differ\n");
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
334 "System's own reverse name resolution is working\n");
335 /* Resolve the same using GNUNET */
336 memset (&sa, 0, sizeof (sa));
337 sa.sin_family = AF_INET;
338 #if HAVE_SOCKADDR_IN_SIN_LEN
339 sa.sin_len = (u_char) sizeof (sa);
342 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
344 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
346 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
347 sizeof (struct sockaddr), GNUNET_YES, timeout,
348 &check_rootserver_name, cls);
350 memset (&sa, 0, sizeof (sa));
351 sa.sin_family = AF_INET;
352 #if HAVE_SOCKADDR_IN_SIN_LEN
353 sa.sin_len = (u_char) sizeof (sa);
355 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
357 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
358 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
359 sizeof (struct sockaddr), GNUNET_YES, timeout,
360 &check_localhost, cls);
362 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
363 sizeof (struct sockaddr), GNUNET_NO, timeout,
364 &check_localhost_num, cls);
365 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
371 main (int argc, char *argv[])
373 int ok = 1 + 2 + 4 + 8;
375 struct GNUNET_OS_Process *proc;
376 char *const argvx[] = {
377 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
379 struct GNUNET_GETOPT_CommandLineOption options[] =
380 { GNUNET_GETOPT_OPTION_END };
382 GNUNET_log_setup ("test-resolver-api",
385 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
386 proc = GNUNET_OS_start_process (GNUNET_YES,
387 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
390 "gnunet-service-resolver",
391 "-c", "test_resolver_api_data.conf", NULL);
392 GNUNET_assert (NULL != proc);
394 GNUNET_assert (GNUNET_OK ==
395 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
396 argvx, "test-resolver-api", "nohelp",
397 options, &run, &ok));
398 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
400 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
403 GNUNET_OS_process_wait (proc);
404 GNUNET_OS_process_destroy (proc);
407 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
412 /* end of test_resolver_api.c */