aboutsummaryrefslogtreecommitdiff
path: root/src/resolver/test_resolver_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/resolver/test_resolver_api.c')
-rw-r--r--src/resolver/test_resolver_api.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/resolver/test_resolver_api.c b/src/resolver/test_resolver_api.c
new file mode 100644
index 000000000..240879d73
--- /dev/null
+++ b/src/resolver/test_resolver_api.c
@@ -0,0 +1,229 @@
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
36static void
37check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen)
38{
39 char buf[INET6_ADDRSTRLEN];
40 int *ok = cls;
41
42 if (salen == 0)
43 {
44 (*ok) &= ~8;
45 return;
46 }
47 if (salen == sizeof (struct sockaddr_in))
48 {
49 struct sockaddr_in *in = (struct sockaddr_in *) sa;
50 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
51 _("Got IP address `%s' for our host.\n"),
52 inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)));
53 }
54 else if (salen == sizeof (struct sockaddr_in6))
55 {
56 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) sa;
57 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
58 _("Got IP address `%s' for our host.\n"),
59 inet_ntop (AF_INET6, &in6->sin6_addr, buf, sizeof (buf)));
60 }
61 else
62 {
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64 _("Got address of bogus length %u\n"), salen);
65 GNUNET_assert (0);
66 }
67}
68
69
70static void
71check_localhost_num (void *cls, const char *hostname)
72{
73 int *ok = cls;
74 if (hostname == NULL)
75 return;
76 if (0 == strcmp (hostname, "127.0.0.1"))
77 {
78#if DEBUG_RESOLVER
79 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80 "Received correct hostname `%s'.\n", hostname);
81#endif
82 (*ok) &= ~4;
83 }
84 else
85 {
86#if DEBUG_RESOLVER
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
88 "Received invalid hostname `%s'.\n", hostname);
89#endif
90 GNUNET_break (0);
91 }
92}
93
94static void
95check_localhost (void *cls, const char *hostname)
96{
97 int *ok = cls;
98 if (hostname == NULL)
99 return;
100 if (0 == strcmp (hostname, "localhost"))
101 {
102#if DEBUG_RESOLVER
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104 "Received correct hostname `%s'.\n", hostname);
105#endif
106 (*ok) &= ~2;
107 }
108 else
109 {
110#if DEBUG_RESOLVER
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "Received invalid hostname `%s'.\n", hostname);
113#endif
114 GNUNET_break (0);
115 }
116}
117
118static void
119check_127 (void *cls, const struct sockaddr *sa, socklen_t salen)
120{
121 int *ok = cls;
122 const struct sockaddr_in *sai = (const struct sockaddr_in *) sa;
123
124 if (sa == NULL)
125 return;
126 GNUNET_assert (sizeof (struct sockaddr_in) == salen);
127 if (sai->sin_addr.s_addr == htonl (INADDR_LOOPBACK))
128 {
129#if DEBUG_RESOLVER
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct address.\n");
131#endif
132 (*ok) &= ~1;
133 }
134 else
135 {
136#if DEBUG_RESOLVER
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect address.\n");
138#endif
139 GNUNET_break (0);
140 }
141}
142
143static void
144run (void *cls,
145 struct GNUNET_SCHEDULER_Handle *sched,
146 char *const *args,
147 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
148{
149 struct sockaddr_in sa;
150 struct GNUNET_TIME_Relative timeout =
151 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
152 2500);
153 memset (&sa, 0, sizeof (sa));
154 sa.sin_family = AF_INET;
155 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
156 GNUNET_RESOLVER_ip_get (sched,
157 cfg,
158 "localhost", AF_INET, timeout, &check_127, cls);
159 GNUNET_RESOLVER_hostname_get (sched,
160 cfg,
161 (const struct sockaddr *) &sa,
162 sizeof (struct sockaddr),
163 GNUNET_YES, timeout, &check_localhost, cls);
164 GNUNET_RESOLVER_hostname_get (sched,
165 cfg,
166 (const struct sockaddr *) &sa,
167 sizeof (struct sockaddr),
168 GNUNET_NO,
169 timeout, &check_localhost_num, cls);
170 GNUNET_RESOLVER_hostname_resolve (sched,
171 cfg,
172 AF_UNSPEC, timeout, &check_hostname, cls);
173}
174
175static int
176check ()
177{
178 int ok = 1 + 2 + 4 + 8;
179 pid_t pid;
180 char *const argv[] = { "test-resolver-api",
181 "-c",
182 "test_resolver_api_data.conf",
183#if VERBOSE
184 "-L", "DEBUG",
185#endif
186 NULL
187 };
188 struct GNUNET_GETOPT_CommandLineOption options[] = {
189 GNUNET_GETOPT_OPTION_END
190 };
191 pid = GNUNET_OS_start_process ("gnunet-service-resolver",
192 "gnunet-service-resolver",
193#if VERBOSE
194 "-L", "DEBUG",
195#endif
196 "-c", "test_resolver_api_data.conf", NULL);
197 sleep (1);
198 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
199 argv, "test-resolver-api", "nohelp",
200 options, &run, &ok);
201 if (0 != PLIBC_KILL (pid, SIGTERM))
202 {
203 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
204 ok = 1;
205 }
206 waitpid (pid, NULL, 0);
207 if (ok != 0)
208 fprintf (stderr, "Missed some resolutions: %u\n", ok);
209 return ok;
210}
211
212int
213main (int argc, char *argv[])
214{
215 int ret;
216
217 GNUNET_log_setup ("test-resolver-api",
218#if VERBOSE
219 "DEBUG",
220#else
221 "WARNING",
222#endif
223 NULL);
224 ret = check ();
225
226 return ret;
227}
228
229/* end of test_resolver_api.c */