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