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