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