(no commit message)
[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_rootserver_ip(void *cls, const struct sockaddr *sa, socklen_t salen)
132 {
133   int *ok = cls;
134   const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
135
136   if (sa == NULL)
137     return;
138   GNUNET_assert(sizeof(struct sockaddr_in) == salen);
139
140   if (0 == strcmp(inet_ntoa(sai->sin_addr), ROOTSERVER_IP))
141     {
142 #if DEBUG_RESOLVER
143       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n");
144 #endif
145       (*ok) &= ~1;
146     }
147   else
148     {
149 #if DEBUG_RESOLVER
150       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n");
151 #endif
152       GNUNET_break(0);
153     }
154 }
155
156 static void
157 check_rootserver_name(void *cls, const char *hostname)
158 {
159   int *ok = cls;
160   if (hostname == NULL)
161     return;
162
163   if (0 == strcmp(hostname, ROOTSERVER_NAME))
164     {
165 #if DEBUG_RESOLVER
166       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167           "Received correct rootserver hostname `%s'.\n", hostname);
168 #endif
169       (*ok) &= ~2;
170     }
171   else
172     {
173 #if DEBUG_RESOLVER
174       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175           "Received invalid rootserver hostname `%s'.\n", hostname);
176 #endif
177       GNUNET_break(0);
178     }
179 }
180
181 static void
182 run(void *cls, struct GNUNET_SCHEDULER_Handle *sched, char * const *args,
183     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
184 {
185   struct sockaddr_in sa;
186   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply(
187       GNUNET_TIME_UNIT_MILLISECONDS, 2500);
188   int count_ips = 0;
189
190   memset(&sa, 0, sizeof(sa));
191   sa.sin_family = AF_INET;
192   sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
193   GNUNET_RESOLVER_ip_get(sched, cfg, "localhost", AF_INET, timeout, &check_127,
194       cls);
195   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
196       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_localhost, cls);
197   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
198       sizeof(struct sockaddr), GNUNET_NO, timeout, &check_localhost_num, cls);
199   GNUNET_RESOLVER_hostname_resolve(sched, cfg, AF_UNSPEC, timeout,
200       &check_hostname, cls);
201   /*
202    * Testing non-local DNS resolution
203    * DNS rootserver to test: a.root-servers.net - 198.41.0.4
204    */
205
206   char const * rootserver_name = ROOTSERVER_NAME;
207   struct hostent *rootserver;
208
209   rootserver = gethostbyname(rootserver_name);
210   if (rootserver == NULL)
211     {
212       /* Error: resolving ip addresses does not work */
213 #if DEBUG_RESOLVER
214       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
215           _("gethostbyname() could not lookup IP address: %s\n"),
216           hstrerror (h_errno));
217 #endif
218       GNUNET_break(0);
219       return;
220     }
221
222   /* Counting returned IP addresses */
223   while (rootserver->h_addr_list[count_ips] != NULL)
224     count_ips++;
225   if (count_ips > 1)
226     {
227 #if DEBUG_RESOLVER
228       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received range for root name server, but a root name server has only 1 IP\n");
229 #endif
230       GNUNET_break(0);
231     }
232
233   /* Comparing to resolved address to the address the root name server should have */
234   if (strcmp(inet_ntoa(*(struct in_addr *) rootserver->h_addr_list[0]),
235       ROOTSERVER_IP) != 0)
236     {
237 #if DEBUG_RESOLVER
238       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "IP received and IP for root name server differ\n");
239 #endif
240       GNUNET_break(0);
241     }
242 #if DEBUG_RESOLVER
243   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n");
244 #endif
245
246   /* Resolve the same using GNUNET */
247   GNUNET_RESOLVER_ip_get(sched, cfg, ROOTSERVER_NAME, AF_INET, timeout,
248       &check_rootserver_ip, cls);
249
250   /*
251    * Success: forward lookups work as expected
252    * Next step: reverse lookups
253    */
254
255   struct in_addr rootserver_addr;
256   rootserver->h_name = "";
257   if (1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr))
258     {
259 #if DEBUG_RESOLVER
260       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root name server IP address\n");
261 #endif
262       GNUNET_break(0);
263     }
264
265   rootserver
266       = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET);
267   if (rootserver == NULL)
268     {
269       /* Error: resolving IP addresses does not work */
270 #if DEBUG_RESOLVER
271       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
272           _("gethostbyaddr() could not lookup hostname: %s\n"),
273           hstrerror (h_errno));
274 #endif
275       GNUNET_break(0);
276     }
277   else
278   {
279     if (0 != strcmp(rootserver->h_name, ROOTSERVER_NAME))
280     {
281 #if DEBUG_RESOLVER
282       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n");
283 #endif
284       GNUNET_break(0);
285     }
286   }
287
288 #if DEBUG_RESOLVER
289   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n");
290 #endif
291   /* Resolve the same using GNUNET */
292
293   memset(&sa, 0, sizeof(sa));
294   sa.sin_family = AF_INET;
295   inet_aton(ROOTSERVER_IP, &sa.sin_addr);
296
297   GNUNET_RESOLVER_hostname_get(sched, cfg, (const struct sockaddr *) &sa,
298       sizeof(struct sockaddr), GNUNET_YES, timeout, &check_rootserver_name, cls);
299 }
300
301 static int
302 check()
303 {
304   int ok = 1 + 2 + 4 + 8;
305   char *fn;
306   char *pfx;
307   pid_t pid;
308   char * const argv[] =
309     { "test-resolver-api", "-c", "test_resolver_api_data.conf",
310 #if VERBOSE
311         "-L", "DEBUG",
312 #endif
313         NULL };
314   struct GNUNET_GETOPT_CommandLineOption options[] =
315     { GNUNET_GETOPT_OPTION_END };
316   pfx = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_BINDIR);
317   GNUNET_asprintf(&fn, "%s%cgnunet-service-resolver", pfx, DIR_SEPARATOR);
318   GNUNET_free(pfx);
319   pid = GNUNET_OS_start_process(NULL, NULL, fn, "gnunet-service-resolver",
320 #if VERBOSE
321       "-L", "DEBUG",
322 #endif
323       "-c", "test_resolver_api_data.conf", NULL);
324   GNUNET_free(fn);
325   GNUNET_assert(GNUNET_OK == GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *))
326       - 1, argv, "test-resolver-api", "nohelp", options, &run, &ok));
327   if (0 != PLIBC_KILL(pid, SIGTERM))
328     {
329       GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
330       ok = 1;
331     }
332   GNUNET_OS_process_wait(pid);
333   if (ok != 0)
334     fprintf(stderr, "Missed some resolutions: %u\n", ok);
335   return ok;
336 }
337
338 int
339 main(int argc, char *argv[])
340 {
341   int ret;
342
343   GNUNET_log_setup("test-resolver-api",
344 #if VERBOSE
345       "DEBUG",
346 #else
347       "WARNING",
348 #endif
349       NULL);
350   ret = check();
351
352   return ret;
353 }
354
355 /* end of test_resolver_api.c */