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