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