c24646d07d8f1c2b9883f2138fc1c9614ca5602b
[oweals/gnunet.git] / src / util / test_resolver_api.c
1 /*
2  This file is part of GNUnet.
3  (C) 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8  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  General Public License for more details.
14
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.
19  */
20 /**
21  * @file resolver/test_resolver_api.c
22  * @brief testcase for resolver_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_resolver_service.h"
31 #include "resolver.h"
32
33 #define VERBOSE GNUNET_NO
34
35 /** 
36  * Using DNS root servers to check gnunet's resolver service
37  * a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years
38  * For more information have a look at IANA's website http://www.root-servers.org/
39  */
40 #define ROOTSERVER_NAME "a.root-servers.net"
41 #define ROOTSERVER_IP   "198.41.0.4"
42
43 static void
44 check_hostname(void *cls, const struct sockaddr *sa, socklen_t salen)
45 {
46   int *ok = cls;
47
48   if (salen == 0)
49     {
50       (*ok) &= ~8;
51       return;
52     }
53   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Got IP address `%s' for our host.\n"),
54       GNUNET_a2s(sa, salen));
55 }
56
57 static void
58 check_localhost_num(void *cls, const char *hostname)
59 {
60   int *ok = cls;
61   if (hostname == NULL)
62     return;
63   if (0 == strcmp(hostname, "127.0.0.1"))
64     {
65 #if DEBUG_RESOLVER
66       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67           "Received correct hostname `%s'.\n", hostname);
68 #endif
69       (*ok) &= ~4;
70     }
71   else
72     {
73 #if DEBUG_RESOLVER
74       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75           "Received invalid hostname `%s'.\n", hostname);
76 #endif
77       GNUNET_break(0);
78     }
79 }
80
81 static void
82 check_localhost(void *cls, const char *hostname)
83 {
84   int *ok = cls;
85   if (hostname == NULL)
86     return;
87   if (0 == strcmp(hostname, "localhost"))
88     {
89 #if DEBUG_RESOLVER
90       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91           "Received correct hostname `%s'.\n", hostname);
92 #endif
93       (*ok) &= ~2;
94     }
95   else
96     {
97 #if DEBUG_RESOLVER
98       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99           "Received invalid hostname `%s'.\n", hostname);
100 #endif
101       GNUNET_break(0);
102     }
103 }
104
105 static void
106 check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
107 {
108   int *ok = cls;
109   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
110
111   if (sa == NULL)
112     return;
113   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
114   if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
115     {
116 #if DEBUG_RESOLVER
117       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
118 #endif
119       (*ok) &= ~1;
120     }
121   else
122     {
123 #if DEBUG_RESOLVER
124       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
125 #endif
126       GNUNET_break(0);
127     }
128 }
129
130 static void
131 check_local_fqdn(void *cls, const char *gnunet_fqdn)
132 {
133   int result = 0;
134
135   struct hostent *host;
136   char hostname[GNUNET_OS_get_hostname_max_length() + 1];
137
138   if (0 != gethostname (hostname, sizeof (hostname) - 1))
139     {
140       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
141                            GNUNET_ERROR_TYPE_BULK, "gethostname");
142       return;
143     }
144 #if DEBUG_RESOLVER
145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146               _("Resolving our FQDN `%s'\n"), hostname);
147 #endif
148   host = gethostbyname ( hostname );
149   if ( NULL == host)
150   {
151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152                 _("Could not resolve our FQDN : %s %u\n"),
153                 hstrerror (h_errno), h_errno);
154     return;
155   }
156
157   GNUNET_assert( 0 != host);
158
159   result = strcmp(host->h_name, gnunet_fqdn);
160   if ( 0 != result )
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163         "Local resolved and resolver resolved fqdns are not equal\n");
164   }
165   GNUNET_assert( 0 == result);
166 }
167
168
169
170 static void
171 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
172 {
173   int *ok = cls;
174   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
175
176   if (sa == NULL)
177     return;
178   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
179
180   if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
181     {
182 #if DEBUG_RESOLVER
183       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
184 #endif
185       (*ok) &= ~1;
186     }
187   else
188     {
189 #if DEBUG_RESOLVER
190       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
191 #endif
192       GNUNET_break(0);
193     }
194 }
195
196 static void
197 check_rootserver_name(void *cls, const char *hostname)
198 {
199   int *ok = cls;
200   if (hostname == NULL)
201     return;
202
203   if (0 == strcmp(hostname, ROOTSERVER_NAME))
204     {
205 #if DEBUG_RESOLVER
206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207           "Received correct rootserver hostname `%s'.\n", hostname);
208 #endif
209       (*ok) &= ~2;
210     }
211   else
212     {
213 #if DEBUG_RESOLVER
214       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215           "Received invalid rootserver hostname `%s'.\n", hostname);
216 #endif
217       GNUNET_break(0);
218     }
219 }
220
221 static void
222 run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
223     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   int *ok = cls;
226   struct sockaddr_in sa;
227   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
228       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
229   int count_ips = 0;
230   char * own_fqdn;
231
232   memset(&sa, 0, sizeof(sa));
233   sa.sin_family = AF_INET;
234   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
235   GNUNET_RESOLVER_ip_get(sched, cfg, "localhost", AF_INET, timeout, &check_127,
236       cls);
237   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
238       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
239   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
240       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
241   GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
242       &check_hostname, cls);
243
244
245   /*
246    * Looking up our own fqdn
247    */
248   own_fqdn = GNUNET_RESOLVER_local_fqdn_get();
249   check_local_fqdn( NULL, own_fqdn);
250   GNUNET_free_non_null (own_fqdn);
251
252   /*
253    * Testing non-local DNS resolution
254    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
255    */
256
257   char const * rootserver_name = ROOTSERVER_NAME;
258   struct hostent *rootserver;
259
260   rootserver = gethostbyname(rootserver_name);
261   if (rootserver == NULL)
262     {
263       /* Error: resolving ip addresses does not work */
264 #if DEBUG_RESOLVER
265       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266           _("gethostbyname() could not lookup IP address: %s\n"),
267           hstrerror (h_errno));
268 #endif
269       fprintf (stderr,
270                "System seems to be off-line, will not run all DNS tests\n");
271       *ok = 0; /* mark test as passing anyway */
272       return;
273     }
274
275   /* Counting returned IP addresses */
276   while (rootserver->h_addr_list[count_ips] != NULL)
277     count_ips++;
278   if (count_ips > 1)
279     {
280 #if DEBUG_RESOLVER
281       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
282 #endif
283       GNUNET_break(0);
284     }
285
286   /* Comparing to resolved address to the address the root name server should have */
287   if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
288       ROOTSERVER_IP) != 0)
289     {
290 #if DEBUG_RESOLVER
291       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
292 #endif
293       GNUNET_break(0);
294     }
295 #if DEBUG_RESOLVER
296   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
297 #endif
298
299   /* Resolve the same using GNUNET */
300   GNUNET_RESOLVER_ip_get(sched, cfg, ROOTSERVER_NAME, AF_INET, timeout,
301       &check_rootserver_ip, cls);
302
303   /*
304    * Success: forward lookups work as expected
305    * Next step: reverse lookups
306    */
307
308   struct in_addr rootserver_addr;
309   rootserver->h_name = "";
310   if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
311     {
312 #if DEBUG_RESOLVER
313       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
314 #endif
315       GNUNET_break(0);
316     }
317
318   rootserver
319       = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
320   if (rootserver == NULL)
321     {
322       /* Error: resolving IP addresses does not work */
323 #if DEBUG_RESOLVER
324       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
325           _("gethostbyaddr() could not lookup hostname: %s\n"),
326           hstrerror (h_errno));
327 #endif
328       GNUNET_break(0);
329     }
330   else
331   {
332     if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
333     {
334 #if DEBUG_RESOLVER
335       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
336 #endif
337       GNUNET_break(0);
338     }
339   }
340
341 #if DEBUG_RESOLVER
342   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
343 #endif
344   /* Resolve the same using GNUNET */
345
346   memset(&sa, 0, sizeof(sa));
347   sa.sin_family = AF_INET;
348 #ifndef MINGW
349   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
350 #else
351   sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
352 #endif
353   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
354       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
355 }
356
357 static int
358 check()
359 {
360   int ok = 1 + 2 + 4 + 8;
361   char *fn;
362   char *pfx;
363   pid_t pid;
364   char * const argv[] =
365     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
366 #if VERBOSE
367         "-L", "DEBUG",
368 #endif
369         NULL };
370   struct GNUNET_GETOPT_CommandLineOption options[] =
371     { GNUNET_GETOPT_OPTION_END };
372   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
373   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
374   GNUNET_free(pfx);
375   pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
376 #if VERBOSE
377       "-L", "DEBUG",
378 #endif
379       "-c", "test_resolver_api_data.conf", NULL);
380   GNUNET_free(fn);
381   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
382       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
383   if (0 != PLIBC_KILL(pid, SIGTERM))
384     {
385       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
386       ok = 1;
387     }
388   GNUNET_OS_process_wait(pid);
389   if (ok != 0)
390     fprintf(stderr, "Missed some resolutions: %u\n", ok);
391   return ok;
392 }
393
394 int
395 main(int argc, char *argv[])
396 {
397   int ret;
398
399   GNUNET_log_setup("test-resolver-api",
400 #if VERBOSE
401       "DEBUG",
402 #else
403       "WARNING",
404 #endif
405       NULL);
406   ret = check();
407
408   return ret;
409 }
410
411 /* end of test_resolver_api.c */