-fix (C) notices
[oweals/gnunet.git] / src / util / test_resolver_api.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, 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_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen)
141 {
142   int *ok = cls;
143   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
144
145   if (NULL == sa)
146     return;
147   GNUNET_assert (sizeof (struct sockaddr_in) == salen);
148
149   if (0 == strcmp (inet_ntoa (sai->sin_addr), ROOTSERVER_IP))
150   {
151     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152                 "Received correct rootserver ip address.\n");
153     (*ok) &= ~1;
154   }
155   else
156   {
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158                 "Received incorrect rootserver ip address.\n");
159     GNUNET_break (0);
160   }
161 }
162
163
164 static void
165 check_rootserver_name (void *cls,
166                        const char *hostname)
167 {
168   int *ok = cls;
169
170   if (NULL == hostname)
171     return;
172
173   if (0 == strcmp (hostname, ROOTSERVER_NAME))
174   {
175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176                 "Received correct rootserver hostname `%s'.\n",
177                 hostname);
178     (*ok) &= ~2;
179   }
180   else
181   {
182     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
183                 "Received invalid rootserver hostname `%s', expected `%s'\n",
184                 hostname,
185                 ROOTSERVER_NAME);
186     GNUNET_break (disable_rootserver_check);
187   }
188 }
189
190
191 static void
192 run (void *cls, char *const *args, const char *cfgfile,
193      const struct GNUNET_CONFIGURATION_Handle *cfg)
194 {
195   int *ok = cls;
196   struct sockaddr_in sa;
197   struct GNUNET_TIME_Relative timeout =
198       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
199   int count_ips = 0;
200   char *own_fqdn;
201   const char *rootserver_name = ROOTSERVER_NAME;
202   struct hostent *rootserver;
203   struct in_addr rootserver_addr;
204
205   memset (&sa, 0, sizeof (sa));
206   sa.sin_family = AF_INET;
207 #if HAVE_SOCKADDR_IN_SIN_LEN
208   sa.sin_len = (u_char) sizeof (sa);
209 #endif
210   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
211
212   /*
213    * Looking up our own fqdn
214    */
215   own_fqdn = GNUNET_RESOLVER_local_fqdn_get ();
216   /* can't really check, only thing we can safely
217      compare against is our own identical logic... */
218   GNUNET_free_non_null (own_fqdn);
219
220   /*
221    * Testing non-local DNS resolution
222    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
223    */
224
225   rootserver = gethostbyname (rootserver_name);
226   if (NULL == rootserver)
227   {
228     /* Error: resolving ip addresses does not work */
229     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                 _("gethostbyname() could not lookup IP address: %s\n"),
231                 hstrerror (h_errno));
232     FPRINTF (stderr,
233              "%s",
234              "System seems to be off-line, will not run all DNS tests\n");
235     *ok = 0;                    /* mark test as passing anyway */
236     return;
237   }
238
239   /* Counting returned IP addresses */
240   while (NULL != rootserver->h_addr_list[count_ips])
241     count_ips++;
242   if (count_ips > 1)
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245                 "IP received range for root name server, but a root name server has only 1 IP\n");
246     GNUNET_break (0);
247   }
248
249   /* Comparing to resolved address to the address the root name server should have */
250   if (0 !=
251       strcmp (inet_ntoa (*(struct in_addr *) rootserver->h_addr_list[0]),
252               ROOTSERVER_IP))
253   {
254     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
255                 "IP received and IP for root name server differ\n");
256     GNUNET_break (0);
257   }
258   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259               "System's own forward name resolution is working\n");
260   /* Resolve the same using GNUNET */
261   GNUNET_RESOLVER_ip_get (ROOTSERVER_NAME, AF_INET, timeout,
262                           &check_rootserver_ip, cls);
263
264   /*
265    * Success: forward lookups work as expected
266    * Next step: reverse lookups
267    */
268   if (1 != inet_pton (AF_INET,
269                       ROOTSERVER_IP,
270                       &rootserver_addr))
271   {
272     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273                 "Could not transform root name server IP address\n");
274     GNUNET_break (0);
275   }
276
277   rootserver =
278       gethostbyaddr ((const void *) &rootserver_addr,
279                      sizeof (rootserver_addr),
280                      AF_INET);
281   if (NULL == rootserver)
282   {
283     /* Error: resolving IP addresses does not work */
284     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
285                 "gethostbyaddr() could not lookup hostname: %s\n",
286                 hstrerror (h_errno));
287     disable_rootserver_check = GNUNET_YES;
288   }
289   else
290   {
291     if (0 != strcmp (rootserver->h_name,
292                      ROOTSERVER_NAME))
293     {
294       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
295                   "Received hostname and hostname for root name server differ\n");
296       disable_rootserver_check = GNUNET_YES;
297     }
298   }
299
300   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301               "System's own reverse name resolution is working\n");
302   /* Resolve the same using GNUNET */
303   memset (&sa, 0, sizeof (sa));
304   sa.sin_family = AF_INET;
305 #if HAVE_SOCKADDR_IN_SIN_LEN
306   sa.sin_len = (u_char) sizeof (sa);
307 #endif
308 #ifndef MINGW
309   inet_aton (ROOTSERVER_IP, &sa.sin_addr);
310 #else
311   sa.sin_addr.S_un.S_addr = inet_addr (ROOTSERVER_IP);
312 #endif
313   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
314                                 sizeof (struct sockaddr), GNUNET_YES, timeout,
315                                 &check_rootserver_name, cls);
316
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   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
323
324   GNUNET_RESOLVER_ip_get ("localhost", AF_INET, timeout, &check_127, cls);
325   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
326                                 sizeof (struct sockaddr), GNUNET_YES, timeout,
327                                 &check_localhost, cls);
328
329   GNUNET_RESOLVER_hostname_get ((const struct sockaddr *) &sa,
330                                 sizeof (struct sockaddr), GNUNET_NO, timeout,
331                                 &check_localhost_num, cls);
332   GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, timeout, &check_hostname, cls);
333
334 }
335
336
337 int
338 main (int argc, char *argv[])
339 {
340   int ok = 1 + 2 + 4 + 8;
341   char *fn;
342   struct GNUNET_OS_Process *proc;
343   char *const argvx[] = {
344     "test-resolver-api", "-c", "test_resolver_api_data.conf", NULL
345   };
346   struct GNUNET_GETOPT_CommandLineOption options[] =
347       { GNUNET_GETOPT_OPTION_END };
348
349   GNUNET_log_setup ("test-resolver-api",
350                     "WARNING",
351                     NULL);
352   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
353   proc = GNUNET_OS_start_process (GNUNET_YES,
354                                   GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
355                                   NULL, NULL, NULL,
356                                   fn,
357                                   "gnunet-service-resolver",
358                                   "-c", "test_resolver_api_data.conf", NULL);
359   GNUNET_assert (NULL != proc);
360   GNUNET_free (fn);
361   GNUNET_assert (GNUNET_OK ==
362                  GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
363                                      argvx, "test-resolver-api", "nohelp",
364                                      options, &run, &ok));
365   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
366   {
367     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
368     ok = 1;
369   }
370   GNUNET_OS_process_wait (proc);
371   GNUNET_OS_process_destroy (proc);
372   proc = NULL;
373   if (0 != ok)
374     FPRINTF (stderr, "Missed some resolutions: %u\n", ok);
375   return ok;
376 }
377
378
379 /* end of test_resolver_api.c */