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