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