2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file resolver/test_resolver_api.c
20 * @brief testcase for resolver_api.c
23 #include "gnunet_util_lib.h"
24 #include "gnunet_resolver_service.h"
28 static int disable_rootserver_check;
32 * Using DNS root servers to check gnunet's resolver service
33 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
34 * For more information have a look at IANA's website http://www.root-servers.org/
36 #define ROOTSERVER_NAME "a.root-servers.net"
37 #define ROOTSERVER_IP "198.41.0.4"
41 check_hostname (void *cls,
42 const struct sockaddr *sa,
52 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
53 "Got IP address `%s' for our host.\n",
54 GNUNET_a2s (sa, salen));
59 check_localhost_num (void *cls,
66 if (0 == strcmp (hostname, "127.0.0.1"))
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 "Received correct hostname `%s'.\n",
75 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
76 "Received invalid hostname `%s'.\n",
84 check_localhost (void *cls,
91 if (0 == strcmp (hostname, "localhost"))
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94 "Received correct hostname `%s'.\n",
100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
101 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
108 check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
111 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
115 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
116 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Received correct address.\n");
124 char buf[INET_ADDRSTRLEN];
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 "Received incorrect address `%s'.\n",
138 check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
141 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
145 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
147 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Received correct rootserver ip address.\n");
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Received incorrect rootserver ip address.\n");
163 check_rootserver_name (void *cls,
164 const char *hostname)
168 if (NULL == hostname)
171 if (0 == strcmp (hostname, ROOTSERVER_NAME))
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Received correct rootserver hostname `%s'.\n",
180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
181 "Received invalid rootserver hostname `%s', expected `%s'\n",
184 GNUNET_break (disable_rootserver_check);
190 run (void *cls, char *const *args, const char *cfgfile,
191 const struct GNUNET_CONFIGURATION_Handle *cfg)
194 struct sockaddr_in sa;
195 struct GNUNET_TIME_Relative timeout =
196 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
199 const char *rootserver_name = ROOTSERVER_NAME;
200 struct hostent *rootserver;
201 struct in_addr rootserver_addr;
203 memset (&sa, 0, sizeof (sa));
204 sa.sin_family = AF_INET;
205 #if HAVE_SOCKADDR_IN_SIN_LEN
206 sa.sin_len = (u_char) sizeof (sa);
208 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
211 * Looking up our own fqdn
213 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
214 /* can't really check, only thing we can safely
215 compare against is our own identical logic... */
216 GNUNET_free_non_null (own_fqdn);
219 * Testing non-local DNS resolution
220 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
223 rootserver = gethostbyname (rootserver_name);
224 if (NULL == rootserver)
226 /* Error: resolving ip addresses does not work */
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 _("gethostbyname() could not lookup IP address: %s\n"),
229 hstrerror (h_errno));
232 "System seems to be off-line, will not run all DNS tests\n");
233 *ok = 0; /* mark test as passing anyway */
237 /* Counting returned IP addresses */
238 while (NULL != rootserver->h_addr_list[count_ips])
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "IP received range for root name server, but a root name server has only 1 IP\n");
247 /* Comparing to resolved address to the address the root name server should have */
249 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "IP received and IP for root name server differ\n");
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257 "System's own forward name resolution is working\n");
258 /* Resolve the same using GNUNET */
259 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
260 &check_rootserver_ip, cls);
261 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
262 &check_rootserver_ip, cls);
265 * Success: forward lookups work as expected
266 * Next step: reverse lookups
268 if (1 != inet_pton (AF_INET,
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Could not transform root name server IP address\n");
278 gethostbyaddr ((const void *) &rootserver_addr,
279 sizeof (rootserver_addr),
281 if (NULL == rootserver)
283 /* Error: resolving IP addresses does not work */
284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
285 "gethostbyaddr() could not lookup hostname: %s\n",
286 hstrerror (h_errno));
287 disable_rootserver_check = GNUNET_YES;
291 if (0 != strcmp (rootserver->h_name,
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295 "Received hostname and hostname for root name server differ\n");
296 disable_rootserver_check = GNUNET_YES;
300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301 "System's own reverse name resolution is working\n");
302 /* Resolve the same using GNUNET */
303 memset (&sa, 0, sizeof (sa));
304 sa.sin_family = AF_INET;
305 #if HAVE_SOCKADDR_IN_SIN_LEN
306 sa.sin_len = (u_char) sizeof (sa);
309 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
311 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
313 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
314 sizeof (struct sockaddr), GNUNET_YES, timeout,
315 &check_rootserver_name, cls);
317 memset (&sa, 0, sizeof (sa));
318 sa.sin_family = AF_INET;
319 #if HAVE_SOCKADDR_IN_SIN_LEN
320 sa.sin_len = (u_char) sizeof (sa);
322 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
324 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
325 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
326 sizeof (struct sockaddr), GNUNET_YES, timeout,
327 &check_localhost, cls);
329 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
330 sizeof (struct sockaddr), GNUNET_NO, timeout,
331 &check_localhost_num, cls);
332 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
338 main (int argc, char *argv[])
340 int ok = 1 + 2 + 4 + 8;
342 struct GNUNET_OS_Process *proc;
343 char *const argvx[] = {
344 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
346 struct GNUNET_GETOPT_CommandLineOption options[] =
347 { GNUNET_GETOPT_OPTION_END };
349 GNUNET_log_setup ("test-resolver-api",
352 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
353 proc = GNUNET_OS_start_process (GNUNET_YES,
354 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
357 "gnunet-service-resolver",
358 "-c", "test_resolver_api_data.conf", NULL);
359 GNUNET_assert (NULL != proc);
361 GNUNET_assert (GNUNET_OK ==
362 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
363 argvx, "test-resolver-api", "nohelp",
364 options, &run, &ok));
365 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
367 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
370 GNUNET_OS_process_wait (proc);
371 GNUNET_OS_process_destroy (proc);
374 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
379 /* end of test_resolver_api.c */