aboutsummaryrefslogtreecommitdiff
path: root/src/service/util/test_resolver_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/util/test_resolver_api.c')
-rw-r--r--src/service/util/test_resolver_api.c378
1 files changed, 378 insertions, 0 deletions
diff --git a/src/service/util/test_resolver_api.c b/src/service/util/test_resolver_api.c
new file mode 100644
index 000000000..f8a2164a2
--- /dev/null
+++ b/src/service/util/test_resolver_api.c
@@ -0,0 +1,378 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file resolver/test_resolver_api.c
22 * @brief testcase for resolver_api.c
23 */
24
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_resolver_service.h"
28#include "../lib/util/resolver.h"
29
30
31static int disable_rootserver_check;
32
33
34/**
35 * Using DNS root servers to check gnunet's resolver service
36 * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
37 * For more information have a look at IANA's website http://www.root-servers.org/
38 */
39#define ROOTSERVER_NAME "a.root-servers.net"
40#define ROOTSERVER_IP "198.41.0.4"
41
42
43static void
44check_hostname (void *cls,
45 const struct sockaddr *sa,
46 socklen_t salen)
47{
48 int *ok = cls;
49
50 if (0 == salen)
51 {
52 (*ok) &= ~8;
53 return;
54 }
55 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
56 "Got IP address `%s' for our host.\n",
57 GNUNET_a2s (sa, salen));
58}
59
60
61static void
62check_localhost_num (void *cls,
63 const char *hostname)
64{
65 int *ok = cls;
66
67 if (hostname == NULL)
68 return;
69 if (0 == strcmp (hostname, "127.0.0.1"))
70 {
71 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72 "Received correct hostname `%s'.\n",
73 hostname);
74 (*ok) &= ~4;
75 }
76 else
77 {
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79 "Received invalid hostname `%s'.\n",
80 hostname);
81 GNUNET_break (0);
82 }
83}
84
85
86static void
87check_localhost (void *cls,
88 const char *hostname)
89{
90 int *ok = cls;
91
92 if (NULL == hostname)
93 return;
94 if (0 == strcmp (hostname, "localhost"))
95 {
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Received correct hostname `%s'.\n",
98 hostname);
99 (*ok) &= ~2;
100 }
101 else
102 {
103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
104 "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n",
105 hostname);
106 }
107}
108
109
110static void
111check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
112{
113 int *ok = cls;
114 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
115
116 if (NULL == sa)
117 return;
118 GNUNET_assert (sizeof(struct sockaddr_in) == salen);
119 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
120 {
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122 "Received correct address.\n");
123 (*ok) &= ~1;
124 }
125 else
126 {
127 char buf[INET_ADDRSTRLEN];
128
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 "Received incorrect address `%s'.\n",
131 inet_ntop (AF_INET,
132 &sai->sin_addr,
133 buf,
134 sizeof(buf)));
135 GNUNET_break (0);
136 }
137}
138
139
140static void
141check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
142{
143 int *ok = cls;
144 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
145
146 if (NULL == sa)
147 return;
148 GNUNET_assert (sizeof(struct sockaddr_in) == salen);
149
150 if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
151 {
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Received correct rootserver ip address.\n");
154 (*ok) &= ~1;
155 }
156 else
157 {
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Received incorrect rootserver ip address.\n");
160 GNUNET_break (0);
161 }
162}
163
164
165static void
166check_rootserver_name (void *cls,
167 const char *hostname)
168{
169 int *ok = cls;
170
171 if (NULL == hostname)
172 return;
173
174 if (0 == strcmp (hostname, ROOTSERVER_NAME))
175 {
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Received correct rootserver hostname `%s'.\n",
178 hostname);
179 (*ok) &= ~2;
180 }
181 else
182 {
183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
184 "Received invalid rootserver hostname `%s', expected `%s'\n",
185 hostname,
186 ROOTSERVER_NAME);
187 GNUNET_break (disable_rootserver_check);
188 }
189}
190
191
192static void
193run (void *cls, char *const *args, const char *cfgfile,
194 const struct GNUNET_CONFIGURATION_Handle *cfg)
195{
196 int *ok = cls;
197 struct sockaddr_in sa;
198 struct GNUNET_TIME_Relative timeout =
199 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
200 int count_ips = 0;
201 char *own_fqdn;
202 const char *rootserver_name = ROOTSERVER_NAME;
203 struct hostent *rootserver;
204 struct in_addr rootserver_addr;
205
206 memset (&sa, 0, sizeof(sa));
207 sa.sin_family = AF_INET;
208#if HAVE_SOCKADDR_IN_SIN_LEN
209 sa.sin_len = (u_char) sizeof(sa);
210#endif
211 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
212
213 /*
214 * Looking up our own fqdn
215 */
216 own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
217 /* can't really check, only thing we can safely
218 compare against is our own identical logic... */
219 GNUNET_free (own_fqdn);
220
221 /*
222 * Testing non-local DNS resolution
223 * DNS rootserver to test: a.root-servers.net - 198.41.0.4
224 */
225
226 rootserver = gethostbyname (rootserver_name);
227 if (NULL == rootserver)
228 {
229 /* Error: resolving ip addresses does not work */
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _ ("gethostbyname() could not lookup IP address: %s\n"),
232 hstrerror (h_errno));
233 fprintf (stderr,
234 "%s",
235 "System seems to be off-line, will not run all DNS tests\n");
236 *ok = 0; /* mark test as passing anyway */
237 return;
238 }
239
240 /* Counting returned IP addresses */
241 while (NULL != rootserver->h_addr_list[count_ips])
242 count_ips++;
243 if (count_ips > 1)
244 {
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "IP received range for root name server, but a root name server has only 1 IP\n");
247 GNUNET_break (0);
248 }
249
250 /* Comparing to resolved address to the address the root name server should have */
251 if (0 !=
252 strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
253 ROOTSERVER_IP))
254 {
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "IP received and IP for root name server differ\n");
257 GNUNET_break (0);
258 }
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 "System's own forward name resolution is working\n");
261 /* Resolve the same using GNUNET */
262 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
263 &check_rootserver_ip, cls);
264 GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
265 &check_rootserver_ip, cls);
266
267 /*
268 * Success: forward lookups work as expected
269 * Next step: reverse lookups
270 */
271 if (1 != inet_pton (AF_INET,
272 ROOTSERVER_IP,
273 &rootserver_addr))
274 {
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Could not transform root name server IP address\n");
277 GNUNET_break (0);
278 }
279
280 rootserver =
281 gethostbyaddr ((const void *) &rootserver_addr,
282 sizeof(rootserver_addr),
283 AF_INET);
284 if (NULL == rootserver)
285 {
286 /* Error: resolving IP addresses does not work */
287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
288 "gethostbyaddr() could not lookup hostname: %s\n",
289 hstrerror (h_errno));
290 disable_rootserver_check = GNUNET_YES;
291 }
292 else
293 {
294 if (0 != strcmp (rootserver->h_name,
295 ROOTSERVER_NAME))
296 {
297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
298 "Received hostname and hostname for root name server differ\n");
299 disable_rootserver_check = GNUNET_YES;
300 }
301 }
302
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "System's own reverse name resolution is working\n");
305 /* Resolve the same using GNUNET */
306 memset (&sa, 0, sizeof(sa));
307 sa.sin_family = AF_INET;
308#if HAVE_SOCKADDR_IN_SIN_LEN
309 sa.sin_len = (u_char) sizeof(sa);
310#endif
311 inet_aton (ROOTSERVER_IP, &sa.sin_addr);
312
313 GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
314 sizeof(struct sockaddr), GNUNET_YES, timeout,
315 &check_rootserver_name, cls);
316
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);
321#endif
322 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
323
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);
328
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);
333}
334
335
336int
337main (int argc, char *argv[])
338{
339 int ok = 1 + 2 + 4 + 8;
340 char *fn;
341 struct GNUNET_OS_Process *proc;
342 char *const argvx[] = {
343 "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
344 };
345 struct GNUNET_GETOPT_CommandLineOption options[] =
346 { GNUNET_GETOPT_OPTION_END };
347
348 GNUNET_log_setup ("test-resolver-api",
349 "WARNING",
350 NULL);
351 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
352 proc = GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
353 | GNUNET_OS_USE_PIPE_CONTROL,
354 NULL, NULL, NULL,
355 fn,
356 "gnunet-service-resolver",
357 "-c", "test_resolver_api_data.conf", NULL);
358 GNUNET_assert (NULL != proc);
359 GNUNET_free (fn);
360 GNUNET_assert (GNUNET_OK ==
361 GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
362 argvx, "test-resolver-api", "nohelp",
363 options, &run, &ok));
364 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
365 {
366 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
367 ok = 1;
368 }
369 GNUNET_OS_process_wait (proc);
370 GNUNET_OS_process_destroy (proc);
371 proc = NULL;
372 if (0 != ok)
373 fprintf (stderr, "Missed some resolutions: %u\n", ok);
374 return ok;
375}
376
377
378/* end of test_resolver_api.c */