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