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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
143 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
147 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
149 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Received correct rootserver ip address.\n");
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Received incorrect rootserver ip address.\n");
165 check_rootserver_name (void *cls,
166 const char *hostname)
170 if (NULL == hostname)
173 if (0 == strcmp (hostname, ROOTSERVER_NAME))
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Received correct rootserver hostname `%s'.\n",
182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
183 "Received invalid rootserver hostname `%s', expected `%s'\n",
186 GNUNET_break (disable_rootserver_check);
192 run (void *cls, char *const *args, const char *cfgfile,
193 const struct GNUNET_CONFIGURATION_Handle *cfg)
196 struct sockaddr_in sa;
197 struct GNUNET_TIME_Relative timeout =
198 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
201 const char *rootserver_name = ROOTSERVER_NAME;
202 struct hostent *rootserver;
203 struct in_addr rootserver_addr;
205 memset (&sa, 0, sizeof (sa));
206 sa.sin_family = AF_INET;
207 #if HAVE_SOCKADDR_IN_SIN_LEN
208 sa.sin_len = (u_char) sizeof (sa);
210 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
213 * Looking up our own fqdn
215 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
216 /* can't really check, only thing we can safely
217 compare against is our own identical logic... */
218 GNUNET_free_non_null (own_fqdn);
221 * Testing non-local DNS resolution
222 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
225 rootserver = gethostbyname (rootserver_name);
226 if (NULL == rootserver)
228 /* Error: resolving ip addresses does not work */
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 _("gethostbyname() could not lookup IP address: %s\n"),
231 hstrerror (h_errno));
234 "System seems to be off-line, will not run all DNS tests\n");
235 *ok = 0; /* mark test as passing anyway */
239 /* Counting returned IP addresses */
240 while (NULL != rootserver->h_addr_list[count_ips])
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "IP received range for root name server, but a root name server has only 1 IP\n");
249 /* Comparing to resolved address to the address the root name server should have */
251 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255 "IP received and IP for root name server differ\n");
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259 "System's own forward name resolution is working\n");
260 /* Resolve the same using GNUNET */
261 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
262 &check_rootserver_ip, cls);
263 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
264 &check_rootserver_ip, cls);
267 * Success: forward lookups work as expected
268 * Next step: reverse lookups
270 if (1 != inet_pton (AF_INET,
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Could not transform root name server IP address\n");
280 gethostbyaddr ((const void *) &rootserver_addr,
281 sizeof (rootserver_addr),
283 if (NULL == rootserver)
285 /* Error: resolving IP addresses does not work */
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 "gethostbyaddr() could not lookup hostname: %s\n",
288 hstrerror (h_errno));
289 disable_rootserver_check = GNUNET_YES;
293 if (0 != strcmp (rootserver->h_name,
296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
297 "Received hostname and hostname for root name server differ\n");
298 disable_rootserver_check = GNUNET_YES;
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "System's own reverse name resolution is working\n");
304 /* Resolve the same using GNUNET */
305 memset (&sa, 0, sizeof (sa));
306 sa.sin_family = AF_INET;
307 #if HAVE_SOCKADDR_IN_SIN_LEN
308 sa.sin_len = (u_char) sizeof (sa);
311 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
313 sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
315 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
316 sizeof (struct sockaddr), GNUNET_YES, timeout,
317 &check_rootserver_name, cls);
319 memset (&sa, 0, sizeof (sa));
320 sa.sin_family = AF_INET;
321 #if HAVE_SOCKADDR_IN_SIN_LEN
322 sa.sin_len = (u_char) sizeof (sa);
324 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
326 GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
327 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
328 sizeof (struct sockaddr), GNUNET_YES, timeout,
329 &check_localhost, cls);
331 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
332 sizeof (struct sockaddr), GNUNET_NO, timeout,
333 &check_localhost_num, cls);
334 GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
340 main (int argc, char *argv[])
342 int ok = 1 + 2 + 4 + 8;
344 struct GNUNET_OS_Process *proc;
345 char *const argvx[] = {
346 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
348 struct GNUNET_GETOPT_CommandLineOption options[] =
349 { GNUNET_GETOPT_OPTION_END };
351 GNUNET_log_setup ("test-resolver-api",
354 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
355 proc = GNUNET_OS_start_process (GNUNET_YES,
356 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
359 "gnunet-service-resolver",
360 "-c", "test_resolver_api_data.conf", NULL);
361 GNUNET_assert (NULL != proc);
363 GNUNET_assert (GNUNET_OK ==
364 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
365 argvx, "test-resolver-api", "nohelp",
366 options, &run, &ok));
367 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
372 GNUNET_OS_process_wait (proc);
373 GNUNET_OS_process_destroy (proc);
376 FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
381 /* end of test_resolver_api.c */