cd3e42d23033ded0f8f554f91d25a08fdd6b6ff4
[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 3, 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       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
98                   "Received unexpected hostname `%s', expected `localhost' (this could be OK).\n", 
99                   hostname);
100     }
101 }
102
103 static void
104 check_127(void *cls, const struct sockaddr *sa, socklen_t salen)
105 {
106   int *ok = cls;
107   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
108
109   if (sa == NULL)
110     return;
111   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
112   if (sai->sin_addr.s_addr == htonl(INADDR_LOOPBACK))
113     {
114 #if DEBUG_RESOLVER
115       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
116 #endif
117       (*ok) &= ~1;
118     }
119   else
120     {
121 #if DEBUG_RESOLVER
122       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
123 #endif
124       GNUNET_break(0);
125     }
126 }
127
128 static void
129 check_local_fqdn(void *cls, const char *gnunet_fqdn)
130 {
131   int result = 0;
132
133   struct hostent *host;
134   char hostname[GNUNET_OS_get_hostname_max_length() + 1];
135
136   if (0 != gethostname (hostname, sizeof (hostname) - 1))
137     {
138       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
139                            GNUNET_ERROR_TYPE_BULK, "gethostname");
140       return;
141     }
142 #if DEBUG_RESOLVER
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144               _("Resolving our FQDN `%s'\n"), hostname);
145 #endif
146   host = gethostbyname ( hostname );
147   if ( NULL == host)
148   {
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150                 _("Could not resolve our FQDN : %s %u\n"),
151                 hstrerror (h_errno), h_errno);
152     return;
153   }
154
155   GNUNET_assert( 0 != host);
156
157   result = strcmp(host->h_name, gnunet_fqdn);
158   if ( 0 != result )
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161         "Local resolved and resolver resolved fqdns are not equal\n");
162   }
163   GNUNET_assert( 0 == result);
164 }
165
166
167
168 static void
169 check_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
170 {
171   int *ok = cls;
172   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
173
174   if (sa == NULL)
175     return;
176   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
177
178   if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
179     {
180 #if DEBUG_RESOLVER
181       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
182 #endif
183       (*ok) &= ~1;
184     }
185   else
186     {
187 #if DEBUG_RESOLVER
188       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
189 #endif
190       GNUNET_break(0);
191     }
192 }
193
194 static void
195 check_rootserver_name(void *cls, const char *hostname)
196 {
197   int *ok = cls;
198   if (hostname == NULL)
199     return;
200
201   if (0 == strcmp(hostname, ROOTSERVER_NAME))
202     {
203 #if DEBUG_RESOLVER
204       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205           "Received correct rootserver hostname `%s'.\n", hostname);
206 #endif
207       (*ok) &= ~2;
208     }
209   else
210     {
211 #if DEBUG_RESOLVER
212       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213           "Received invalid rootserver hostname `%s'.\n", hostname);
214 #endif
215       GNUNET_break(0);
216     }
217 }
218
219 static void
220 run(void *cls, char * const *args,
221     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
222 {
223   int *ok = cls;
224   struct sockaddr_in sa;
225   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
226       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
227   int count_ips = 0;
228   char * own_fqdn;
229
230   memset(&sa, 0, sizeof(sa));
231   sa.sin_family = AF_INET;
232 #if HAVE_SOCKADDR_IN_SIN_LEN
233   sa.sin_len = (u_char) sizeof (sa);
234 #endif
235   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
236   GNUNET_RESOLVER_ip_get(cfg, "localhost", AF_INET, timeout, &check_127,
237       cls);
238   GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
239       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
240   GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
241       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
242   GNUNET_RESOLVER_hostname_resolve(cfg, AF_UNSPEC, timeout,
243       &check_hostname, cls);
244
245
246   /*
247    * Looking up our own fqdn
248    */
249   own_fqdn = GNUNET_RESOLVER_local_fqdn_get();
250   check_local_fqdn( NULL, own_fqdn);
251   GNUNET_free_non_null (own_fqdn);
252
253   /*
254    * Testing non-local DNS resolution
255    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
256    */
257
258   const char * rootserver_name = ROOTSERVER_NAME;
259   struct hostent *rootserver;
260
261   rootserver = gethostbyname(rootserver_name);
262   if (rootserver == NULL)
263     {
264       /* Error: resolving ip addresses does not work */
265 #if DEBUG_RESOLVER
266       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267           _("gethostbyname() could not lookup IP address: %s\n"),
268           hstrerror (h_errno));
269 #endif
270       fprintf (stderr,
271                "System seems to be off-line, will not run all DNS tests\n");
272       *ok = 0; /* mark test as passing anyway */
273       return;
274     }
275
276   /* Counting returned IP addresses */
277   while (rootserver->h_addr_list[count_ips] != NULL)
278     count_ips++;
279   if (count_ips > 1)
280     {
281 #if DEBUG_RESOLVER
282       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
283 #endif
284       GNUNET_break(0);
285     }
286
287   /* Comparing to resolved address to the address the root name server should have */
288   if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
289       ROOTSERVER_IP) != 0)
290     {
291 #if DEBUG_RESOLVER
292       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
293 #endif
294       GNUNET_break(0);
295     }
296 #if DEBUG_RESOLVER
297   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
298 #endif
299
300   /* Resolve the same using GNUNET */
301   GNUNET_RESOLVER_ip_get(cfg, ROOTSERVER_NAME, AF_INET, timeout,
302       &check_rootserver_ip, cls);
303
304   /*
305    * Success: forward lookups work as expected
306    * Next step: reverse lookups
307    */
308
309   struct in_addr rootserver_addr;
310   rootserver->h_name = "";
311   if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
312     {
313 #if DEBUG_RESOLVER
314       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
315 #endif
316       GNUNET_break(0);
317     }
318
319   rootserver
320       = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
321   if (rootserver == NULL)
322     {
323       /* Error: resolving IP addresses does not work */
324 #if DEBUG_RESOLVER
325       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
326           _("gethostbyaddr() could not lookup hostname: %s\n"),
327           hstrerror (h_errno));
328 #endif
329       GNUNET_break(0);
330     }
331   else
332   {
333     if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
334     {
335 #if DEBUG_RESOLVER
336       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
337 #endif
338       GNUNET_break(0);
339     }
340   }
341
342 #if DEBUG_RESOLVER
343   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
344 #endif
345   /* Resolve the same using GNUNET */
346
347   memset(&sa, 0, sizeof(sa));
348   sa.sin_family = AF_INET;
349 #if HAVE_SOCKADDR_IN_SIN_LEN
350   sa.sin_len = (u_char) sizeof (sa);
351 #endif
352 #ifndef MINGW
353   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
354 #else
355   sa.sin_addr.S_un.S_addr = inet_addr(ROOTSERVER_IP);
356 #endif
357   GNUNET_RESOLVER_hostname_get(cfg, (const struct sockaddr *) &sa,
358       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
359 }
360
361 static int
362 check()
363 {
364   int ok = 1 + 2 + 4 + 8;
365   char *fn;
366   char *pfx;
367   struct GNUNET_OS_Process *proc;
368   char * const argv[] =
369     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
370 #if VERBOSE
371         "-L", "DEBUG",
372 #endif
373         NULL };
374   struct GNUNET_GETOPT_CommandLineOption options[] =
375     { GNUNET_GETOPT_OPTION_END };
376   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
377   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
378   GNUNET_free(pfx);
379   proc = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
380 #if VERBOSE
381       "-L", "DEBUG",
382 #endif
383       "-c", "test_resolver_api_data.conf", NULL);
384   GNUNET_assert (NULL != proc);
385   GNUNET_free(fn);
386   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
387       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
388   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
389     {
390       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
391       ok = 1;
392     }
393   GNUNET_OS_process_wait (proc);
394   GNUNET_OS_process_close (proc);
395   proc = NULL;
396   if (ok != 0)
397     fprintf(stderr, "Missed some resolutions: %u\n", ok);
398   return ok;
399 }
400
401 int
402 main(int argc, char *argv[])
403 {
404   int ret;
405
406   GNUNET_log_setup("test-resolver-api",
407 #if VERBOSE
408       "DEBUG",
409 #else
410       "WARNING",
411 #endif
412       NULL);
413   ret = check();
414
415   return ret;
416 }
417
418 /* end of test_resolver_api.c */