1 | /* |
---|
2 | * getaddrinfo test suite. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * Copyright (c) 2004, 2005, 2006, 2007 |
---|
7 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
8 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
9 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
10 | * |
---|
11 | * See LICENSE for licensing terms. |
---|
12 | */ |
---|
13 | |
---|
14 | #include <config.h> |
---|
15 | #include <portable/system.h> |
---|
16 | #include <portable/socket.h> |
---|
17 | |
---|
18 | #include <tests/tap/basic.h> |
---|
19 | #include <util/util.h> |
---|
20 | |
---|
21 | /* |
---|
22 | * If the native platform doesn't support AI_NUMERICSERV or AI_NUMERICHOST, |
---|
23 | * pick some other values for them that match the values set in our |
---|
24 | * implementation. |
---|
25 | */ |
---|
26 | #if AI_NUMERICSERV == 0 |
---|
27 | # undef AI_NUMERICSERV |
---|
28 | # define AI_NUMERICSERV 0x0080 |
---|
29 | #endif |
---|
30 | #if AI_NUMERICHOST == 0 |
---|
31 | # undef AI_NUMERICHOST |
---|
32 | # define AI_NUMERICHOST 0x0100 |
---|
33 | #endif |
---|
34 | |
---|
35 | const char *test_gai_strerror(int); |
---|
36 | void test_freeaddrinfo(struct addrinfo *); |
---|
37 | int test_getaddrinfo(const char *, const char *, const struct addrinfo *, |
---|
38 | struct addrinfo **); |
---|
39 | |
---|
40 | int |
---|
41 | main(void) |
---|
42 | { |
---|
43 | struct addrinfo *ai, *first; |
---|
44 | struct addrinfo hints; |
---|
45 | struct sockaddr_in *saddr; |
---|
46 | struct hostent *host; |
---|
47 | struct in_addr addr; |
---|
48 | struct servent *service; |
---|
49 | int i; |
---|
50 | int found; |
---|
51 | |
---|
52 | plan(75); |
---|
53 | |
---|
54 | is_string("Host name lookup failure", test_gai_strerror(1), |
---|
55 | "gai_strerror(1)"); |
---|
56 | is_string("System error", test_gai_strerror(9), "gai_strerror(9)"); |
---|
57 | is_string("Unknown error", test_gai_strerror(40), "gai_strerror(40)"); |
---|
58 | is_string("Unknown error", test_gai_strerror(-37), "gai_strerror(-37)"); |
---|
59 | |
---|
60 | ok(test_getaddrinfo(NULL, "25", NULL, &ai) == 0, "service of 25"); |
---|
61 | is_int(AF_INET, ai->ai_family, "...right family"); |
---|
62 | is_int(0, ai->ai_socktype, "...right socktype"); |
---|
63 | is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol"); |
---|
64 | is_string(NULL, ai->ai_canonname, "...no canonname"); |
---|
65 | is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen"); |
---|
66 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
67 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
68 | ok(saddr->sin_addr.s_addr == INADDR_LOOPBACK, "...right address"); |
---|
69 | test_freeaddrinfo(ai); |
---|
70 | |
---|
71 | memset(&hints, 0, sizeof(hints)); |
---|
72 | hints.ai_flags = AI_PASSIVE; |
---|
73 | hints.ai_socktype = SOCK_STREAM; |
---|
74 | ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0, "passive lookup"); |
---|
75 | is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype"); |
---|
76 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
77 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
78 | ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address"); |
---|
79 | test_freeaddrinfo(ai); |
---|
80 | |
---|
81 | service = getservbyname("smtp", "tcp"); |
---|
82 | if (service == NULL) |
---|
83 | skip_block(4, "smtp service not found"); |
---|
84 | else { |
---|
85 | hints.ai_socktype = 0; |
---|
86 | ok(test_getaddrinfo(NULL, "smtp", &hints, &ai) == 0, |
---|
87 | "service of smtp"); |
---|
88 | is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype"); |
---|
89 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
90 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
91 | ok(saddr->sin_addr.s_addr == INADDR_ANY, "...right address"); |
---|
92 | test_freeaddrinfo(ai); |
---|
93 | } |
---|
94 | |
---|
95 | hints.ai_flags = AI_NUMERICSERV; |
---|
96 | ok(test_getaddrinfo(NULL, "smtp", &hints, &ai) == EAI_NONAME, |
---|
97 | "AI_NUMERICSERV with smtp"); |
---|
98 | ok(test_getaddrinfo(NULL, "25 smtp", &hints, &ai) == EAI_NONAME, |
---|
99 | "AI_NUMERICSERV with 25 smtp"); |
---|
100 | ok(test_getaddrinfo(NULL, "25 ", &hints, &ai) == EAI_NONAME, |
---|
101 | "AI_NUMERICSERV with 25 space"); |
---|
102 | ok(test_getaddrinfo(NULL, "25", &hints, &ai) == 0, |
---|
103 | "valid AI_NUMERICSERV"); |
---|
104 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
105 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
106 | ok(saddr->sin_addr.s_addr == INADDR_LOOPBACK, "...right address"); |
---|
107 | test_freeaddrinfo(ai); |
---|
108 | |
---|
109 | ok(test_getaddrinfo(NULL, NULL, NULL, &ai) == EAI_NONAME, "EAI_NONAME"); |
---|
110 | hints.ai_flags = 2000; |
---|
111 | ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_BADFLAGS, |
---|
112 | "EAI_BADFLAGS"); |
---|
113 | hints.ai_flags = 0; |
---|
114 | hints.ai_socktype = SOCK_RAW; |
---|
115 | ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_SOCKTYPE, |
---|
116 | "EAI_SOCKTYPE"); |
---|
117 | hints.ai_socktype = 0; |
---|
118 | hints.ai_family = AF_UNIX; |
---|
119 | ok(test_getaddrinfo(NULL, "25", &hints, &ai) == EAI_FAMILY, "EAI_FAMILY"); |
---|
120 | hints.ai_family = AF_UNSPEC; |
---|
121 | |
---|
122 | inet_aton("10.20.30.40", &addr); |
---|
123 | ok(test_getaddrinfo("10.20.30.40", NULL, NULL, &ai) == 0, |
---|
124 | "IP address lookup"); |
---|
125 | is_int(AF_INET, ai->ai_family, "...right family"); |
---|
126 | is_int(0, ai->ai_socktype, "...right socktype"); |
---|
127 | is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol"); |
---|
128 | is_string(NULL, ai->ai_canonname, "...no canonname"); |
---|
129 | is_int(sizeof(struct sockaddr_in), ai->ai_addrlen, "...right addrlen"); |
---|
130 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
131 | is_int(0, saddr->sin_port, "...right port"); |
---|
132 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address"); |
---|
133 | test_freeaddrinfo(ai); |
---|
134 | |
---|
135 | if (service == NULL) |
---|
136 | skip_block(7, "smtp service not found"); |
---|
137 | else { |
---|
138 | ok(test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0, |
---|
139 | "IP address lookup with smtp service"); |
---|
140 | is_int(AF_INET, ai->ai_family, "...right family"); |
---|
141 | is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype"); |
---|
142 | is_int(IPPROTO_TCP, ai->ai_protocol, "...right protocol"); |
---|
143 | is_string(NULL, ai->ai_canonname, "...no canonname"); |
---|
144 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
145 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
146 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address"); |
---|
147 | test_freeaddrinfo(ai); |
---|
148 | } |
---|
149 | |
---|
150 | hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; |
---|
151 | ok(test_getaddrinfo("10.2.3.4", "smtp", &hints, &ai) == EAI_NONAME, |
---|
152 | "AI_NUMERICHOST and AI_NUMERICSERV with symbolic service"); |
---|
153 | ok(test_getaddrinfo("example.com", "25", &hints, &ai) == EAI_NONAME, |
---|
154 | "AI_NUMERICHOST and AI_NUMERICSERV with symbolic name"); |
---|
155 | ok(test_getaddrinfo("10.20.30.40", "25", &hints, &ai) == 0, |
---|
156 | "valid AI_NUMERICHOST and AI_NUMERICSERV"); |
---|
157 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
158 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
159 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address"); |
---|
160 | test_freeaddrinfo(ai); |
---|
161 | |
---|
162 | if (service == NULL) |
---|
163 | skip_block(4, "smtp service not found"); |
---|
164 | else { |
---|
165 | hints.ai_flags = AI_NUMERICHOST | AI_CANONNAME; |
---|
166 | ok(test_getaddrinfo("10.20.30.40", "smtp", &hints, &ai) == 0, |
---|
167 | "AI_NUMERICHOST and AI_CANONNAME"); |
---|
168 | is_string("10.20.30.40", ai->ai_canonname, "...right canonname"); |
---|
169 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
170 | is_int(htons(25), saddr->sin_port, "...right port"); |
---|
171 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address"); |
---|
172 | test_freeaddrinfo(ai); |
---|
173 | } |
---|
174 | |
---|
175 | service = getservbyname("domain", "udp"); |
---|
176 | if (service == NULL) |
---|
177 | skip_block(5, "domain service not found"); |
---|
178 | else { |
---|
179 | hints.ai_flags = 0; |
---|
180 | hints.ai_socktype = SOCK_DGRAM; |
---|
181 | ok(test_getaddrinfo("10.20.30.40", "domain", &hints, &ai) == 0, |
---|
182 | "domain service with UDP hint"); |
---|
183 | is_int(SOCK_DGRAM, ai->ai_socktype, "...right socktype"); |
---|
184 | is_string(NULL, ai->ai_canonname, "...no canonname"); |
---|
185 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
186 | is_int(htons(53), saddr->sin_port, "...right port"); |
---|
187 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...right address"); |
---|
188 | test_freeaddrinfo(ai); |
---|
189 | } |
---|
190 | |
---|
191 | /* Hopefully this will always resolve. */ |
---|
192 | host = gethostbyname("www.isc.org"); |
---|
193 | if (host == NULL) |
---|
194 | skip_block(9, "cannot look up www.isc.org"); |
---|
195 | else { |
---|
196 | hints.ai_flags = 0; |
---|
197 | hints.ai_socktype = SOCK_STREAM; |
---|
198 | ok(test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0, |
---|
199 | "lookup of www.isc.org"); |
---|
200 | is_int(SOCK_STREAM, ai->ai_socktype, "...right socktype"); |
---|
201 | is_string(NULL, ai->ai_canonname, "...no canonname"); |
---|
202 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
203 | is_int(htons(80), saddr->sin_port, "...right port"); |
---|
204 | ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something"); |
---|
205 | addr = saddr->sin_addr; |
---|
206 | test_freeaddrinfo(ai); |
---|
207 | |
---|
208 | hints.ai_flags = AI_CANONNAME; |
---|
209 | ok(test_getaddrinfo("www.isc.org", "80", &hints, &ai) == 0, |
---|
210 | "lookup of www.isc.org with A_CANONNAME"); |
---|
211 | ok(ai->ai_canonname != NULL, "...canonname isn't null"); |
---|
212 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
213 | is_int(htons(80), saddr->sin_port, "...right port"); |
---|
214 | ok(saddr->sin_addr.s_addr == addr.s_addr, "...and same address"); |
---|
215 | test_freeaddrinfo(ai); |
---|
216 | } |
---|
217 | |
---|
218 | /* Included because it had multiple A records. */ |
---|
219 | host = gethostbyname("cnn.com"); |
---|
220 | if (host == NULL) |
---|
221 | skip_block(3, "cannot look up cnn.com"); |
---|
222 | else { |
---|
223 | ok(test_getaddrinfo("cnn.com", "80", NULL, &ai) == 0, |
---|
224 | "lookup of cnn.com with multiple A records"); |
---|
225 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
226 | is_int(htons(80), saddr->sin_port, "...right port"); |
---|
227 | ok(saddr->sin_addr.s_addr != INADDR_ANY, "...address is something"); |
---|
228 | test_freeaddrinfo(ai); |
---|
229 | } |
---|
230 | |
---|
231 | hints.ai_socktype = SOCK_STREAM; |
---|
232 | hints.ai_flags = AI_CANONNAME; |
---|
233 | ok(test_getaddrinfo("foo.invalid", NULL, NULL, &ai) == EAI_NONAME, |
---|
234 | "lookup of invalid address"); |
---|
235 | |
---|
236 | host = gethostbyname("cnn.com"); |
---|
237 | if (host == NULL) { |
---|
238 | skip_block(3, "cannot look up cnn.com"); |
---|
239 | exit(0); |
---|
240 | } |
---|
241 | ok(test_getaddrinfo("cnn.com", NULL, &hints, &ai) == 0, |
---|
242 | "lookup of cnn.com"); |
---|
243 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
244 | is_int(0, saddr->sin_port, "...port is 0"); |
---|
245 | first = ai; |
---|
246 | for (found = 0; ai != NULL; ai = ai->ai_next) { |
---|
247 | if (!strcmp(ai->ai_canonname, first->ai_canonname) == 0) { |
---|
248 | ok(0, "...canonname matches"); |
---|
249 | break; |
---|
250 | } |
---|
251 | if (ai != first && ai->ai_canonname == first->ai_canonname) { |
---|
252 | ok(0, "...each canonname is a separate pointer"); |
---|
253 | break; |
---|
254 | } |
---|
255 | found = 0; |
---|
256 | saddr = (struct sockaddr_in *) ai->ai_addr; |
---|
257 | addr = saddr->sin_addr; |
---|
258 | for (i = 0; host->h_addr_list[i] != NULL; i++) |
---|
259 | if (memcmp(&addr, host->h_addr_list[i], host->h_length) == 0) |
---|
260 | found = 1; |
---|
261 | if (!found) { |
---|
262 | ok(0, "...result found in gethostbyname address list"); |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | if (found) |
---|
267 | ok(1, "...result found in gethostbyname address list"); |
---|
268 | test_freeaddrinfo(ai); |
---|
269 | |
---|
270 | return 0; |
---|
271 | } |
---|