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