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