blob: b8ff8eb9b2472e48dfb72f4f3f3e0059d7689aa1 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001// This file includes tests that attempt to do real lookups
2// of DNS names using the local machine's live infrastructure.
3// As a result, we don't check the results very closely, to allow
4// for varying local configurations.
5
6#include "ares-test.h"
7
8#ifdef HAVE_NETDB_H
9#include <netdb.h>
10#endif
11
12namespace ares {
13namespace test {
14
15// Use the address of Google's public DNS servers as example addresses that are
16// likely to be accessible everywhere/everywhen.
17unsigned char gdns_addr4[4] = {0x08, 0x08, 0x08, 0x08};
18unsigned char gdns_addr6[16] = {0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
19 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88};
20
21TEST_F(DefaultChannelTest, LiveGetHostByNameV4) {
22 HostResult result;
23 ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
24 Process();
25 EXPECT_TRUE(result.done_);
26 EXPECT_EQ(ARES_SUCCESS, result.status_);
27 EXPECT_LT(0, (int)result.host_.addrs_.size());
28 EXPECT_EQ(AF_INET, result.host_.addrtype_);
29}
30
31TEST_F(DefaultChannelTest, LiveGetHostByNameV6) {
32 HostResult result;
33 ares_gethostbyname(channel_, "www.google.com.", AF_INET6, HostCallback, &result);
34 Process();
35 EXPECT_TRUE(result.done_);
36 EXPECT_EQ(ARES_SUCCESS, result.status_);
37 EXPECT_LT(0, (int)result.host_.addrs_.size());
38 EXPECT_EQ(AF_INET6, result.host_.addrtype_);
39}
40
41TEST_F(DefaultChannelTest, LiveGetHostByAddrV4) {
42 HostResult result;
43 ares_gethostbyaddr(channel_, gdns_addr4, sizeof(gdns_addr4), AF_INET, HostCallback, &result);
44 Process();
45 EXPECT_TRUE(result.done_);
46 EXPECT_EQ(ARES_SUCCESS, result.status_);
47 EXPECT_LT(0, (int)result.host_.addrs_.size());
48 EXPECT_EQ(AF_INET, result.host_.addrtype_);
49}
50
51TEST_F(DefaultChannelTest, LiveGetHostByAddrV6) {
52 HostResult result;
53 ares_gethostbyaddr(channel_, gdns_addr6, sizeof(gdns_addr6), AF_INET6, HostCallback, &result);
54 Process();
55 EXPECT_TRUE(result.done_);
56 EXPECT_EQ(ARES_SUCCESS, result.status_);
57 EXPECT_LT(0, (int)result.host_.addrs_.size());
58 EXPECT_EQ(AF_INET6, result.host_.addrtype_);
59}
60
61TEST_F(DefaultChannelTest, LiveGetHostByNameFile) {
62 struct hostent *host = nullptr;
63
64 // Still need a channel even to query /etc/hosts.
65 EXPECT_EQ(ARES_ENOTFOUND,
66 ares_gethostbyname_file(nullptr, "localhost", AF_INET, &host));
67
68 int rc = ares_gethostbyname_file(channel_, "bogus.mcname", AF_INET, &host);
69 EXPECT_EQ(nullptr, host);
70 EXPECT_EQ(ARES_ENOTFOUND, rc);
71
72 rc = ares_gethostbyname_file(channel_, "localhost", AF_INET, &host);
73 if (rc == ARES_SUCCESS) {
74 EXPECT_NE(nullptr, host);
75 ares_free_hostent(host);
76 }
77}
78
79TEST_P(DefaultChannelModeTest, LiveGetLocalhostByNameV4) {
80 HostResult result;
81 ares_gethostbyname(channel_, "localhost", AF_INET, HostCallback, &result);
82 Process();
83 EXPECT_TRUE(result.done_);
84 if ((result.status_ != ARES_ENOTFOUND) && (result.status_ != ARES_ECONNREFUSED)) {
85 EXPECT_EQ(ARES_SUCCESS, result.status_);
86 EXPECT_EQ(1, (int)result.host_.addrs_.size());
87 EXPECT_EQ(AF_INET, result.host_.addrtype_);
88 EXPECT_NE(std::string::npos, result.host_.name_.find("localhost"));
89 }
90}
91
92TEST_P(DefaultChannelModeTest, LiveGetLocalhostByNameV6) {
93 HostResult result;
94 ares_gethostbyname(channel_, "localhost", AF_INET6, HostCallback, &result);
95 Process();
96 EXPECT_TRUE(result.done_);
97 if (result.status_ == ARES_SUCCESS) {
98 EXPECT_EQ(1, (int)result.host_.addrs_.size());
99 EXPECT_EQ(AF_INET6, result.host_.addrtype_);
100 std::stringstream ss;
101 ss << HostEnt(result.host_);
102 EXPECT_NE(std::string::npos, result.host_.name_.find("localhost"));
103 }
104}
105
106TEST_P(DefaultChannelModeTest, LiveGetLocalhostByNameIPV4) {
107 HostResult result;
108 ares_gethostbyname(channel_, "127.0.0.1", AF_INET, HostCallback, &result);
109 Process();
110 EXPECT_TRUE(result.done_);
111 EXPECT_EQ(ARES_SUCCESS, result.status_);
112 EXPECT_EQ(1, (int)result.host_.addrs_.size());
113 EXPECT_EQ(AF_INET, result.host_.addrtype_);
114 std::stringstream ss;
115 ss << HostEnt(result.host_);
116 EXPECT_EQ("{'127.0.0.1' aliases=[] addrs=[127.0.0.1]}", ss.str());
117}
118
119TEST_P(DefaultChannelModeTest, LiveGetLocalhostByNameIPV6) {
120 HostResult result;
121 ares_gethostbyname(channel_, "::1", AF_INET6, HostCallback, &result);
122 Process();
123 EXPECT_TRUE(result.done_);
124 if (result.status_ != ARES_ENOTFOUND) {
125 EXPECT_EQ(ARES_SUCCESS, result.status_);
126 EXPECT_EQ(1, (int)result.host_.addrs_.size());
127 EXPECT_EQ(AF_INET6, result.host_.addrtype_);
128 std::stringstream ss;
129 ss << HostEnt(result.host_);
130 EXPECT_EQ("{'::1' aliases=[] addrs=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
131 }
132}
133
134TEST_P(DefaultChannelModeTest, LiveGetLocalhostFailFamily) {
135 HostResult result;
136 ares_gethostbyname(channel_, "127.0.0.1", AF_INET+AF_INET6, HostCallback, &result);
137 Process();
138 EXPECT_TRUE(result.done_);
139 EXPECT_EQ(ARES_ENOTIMP, result.status_);
140}
141
142TEST_P(DefaultChannelModeTest, LiveGetLocalhostByAddrV4) {
143 HostResult result;
144 struct in_addr addr;
145 addr.s_addr = htonl(INADDR_LOOPBACK);
146 ares_gethostbyaddr(channel_, &addr, sizeof(addr), AF_INET, HostCallback, &result);
147 Process();
148 EXPECT_TRUE(result.done_);
149 if (result.status_ != ARES_ENOTFOUND) {
150 EXPECT_EQ(ARES_SUCCESS, result.status_);
151 EXPECT_LT(0, (int)result.host_.addrs_.size());
152 EXPECT_EQ(AF_INET, result.host_.addrtype_);
153 EXPECT_NE(std::string::npos,
154 result.host_.name_.find("localhost"));
155 }
156}
157
158TEST_P(DefaultChannelModeTest, LiveGetLocalhostByAddrV6) {
159 HostResult result;
160 struct in6_addr addr;
161 memset(&addr, 0, sizeof(addr));
162 addr.s6_addr[15] = 1; // in6addr_loopback
163 ares_gethostbyaddr(channel_, &addr, sizeof(addr), AF_INET6, HostCallback, &result);
164 Process();
165 EXPECT_TRUE(result.done_);
166 if (result.status_ != ARES_ENOTFOUND) {
167 EXPECT_EQ(ARES_SUCCESS, result.status_);
168 EXPECT_LT(0, (int)result.host_.addrs_.size());
169 EXPECT_EQ(AF_INET6, result.host_.addrtype_);
170 EXPECT_NE(std::string::npos,
171 result.host_.name_.find("localhost"));
172 }
173}
174
175TEST_P(DefaultChannelModeTest, LiveGetHostByAddrFailFamily) {
176 HostResult result;
177 unsigned char addr[4] = {8, 8, 8, 8};
178 ares_gethostbyaddr(channel_, addr, sizeof(addr), AF_INET6+AF_INET,
179 HostCallback, &result);
180 EXPECT_TRUE(result.done_);
181 EXPECT_EQ(ARES_ENOTIMP, result.status_);
182}
183
184TEST_P(DefaultChannelModeTest, LiveGetHostByAddrFailAddrSize) {
185 HostResult result;
186 unsigned char addr[4] = {8, 8, 8, 8};
187 ares_gethostbyaddr(channel_, addr, sizeof(addr) - 1, AF_INET,
188 HostCallback, &result);
189 EXPECT_TRUE(result.done_);
190 EXPECT_EQ(ARES_ENOTIMP, result.status_);
191}
192
193TEST_P(DefaultChannelModeTest, LiveGetHostByAddrFailAlloc) {
194 HostResult result;
195 unsigned char addr[4] = {8, 8, 8, 8};
196 SetAllocFail(1);
197 ares_gethostbyaddr(channel_, addr, sizeof(addr), AF_INET,
198 HostCallback, &result);
199 EXPECT_TRUE(result.done_);
200 EXPECT_EQ(ARES_ENOMEM, result.status_);
201}
202
203INSTANTIATE_TEST_CASE_P(Modes, DefaultChannelModeTest,
204 ::testing::Values("f", "b", "fb", "bf"));
205
206TEST_F(DefaultChannelTest, LiveSearchA) {
207 SearchResult result;
208 ares_search(channel_, "www.youtube.com.", ns_c_in, ns_t_a,
209 SearchCallback, &result);
210 Process();
211 EXPECT_TRUE(result.done_);
212 EXPECT_EQ(ARES_SUCCESS, result.status_);
213}
214
215TEST_F(DefaultChannelTest, LiveSearchEmptyA) {
216 SearchResult result;
217 ares_search(channel_, "", ns_c_in, ns_t_a,
218 SearchCallback, &result);
219 Process();
220 EXPECT_TRUE(result.done_);
221 EXPECT_NE(ARES_SUCCESS, result.status_);
222}
223
224TEST_F(DefaultChannelTest, LiveSearchNS) {
225 SearchResult result;
226 ares_search(channel_, "google.com.", ns_c_in, ns_t_ns,
227 SearchCallback, &result);
228 Process();
229 EXPECT_TRUE(result.done_);
230 EXPECT_EQ(ARES_SUCCESS, result.status_);
231}
232
233TEST_F(DefaultChannelTest, LiveSearchMX) {
234 SearchResult result;
235 ares_search(channel_, "google.com.", ns_c_in, ns_t_mx,
236 SearchCallback, &result);
237 Process();
238 EXPECT_TRUE(result.done_);
239 EXPECT_EQ(ARES_SUCCESS, result.status_);
240}
241
242TEST_F(DefaultChannelTest, LiveSearchTXT) {
243 SearchResult result;
244 ares_search(channel_, "google.com.", ns_c_in, ns_t_txt,
245 SearchCallback, &result);
246 Process();
247 EXPECT_TRUE(result.done_);
248 EXPECT_EQ(ARES_SUCCESS, result.status_);
249}
250
251TEST_F(DefaultChannelTest, LiveSearchSOA) {
252 SearchResult result;
253 ares_search(channel_, "google.com.", ns_c_in, ns_t_soa,
254 SearchCallback, &result);
255 Process();
256 EXPECT_TRUE(result.done_);
257 EXPECT_EQ(ARES_SUCCESS, result.status_);
258}
259
260TEST_F(DefaultChannelTest, LiveSearchSRV) {
261 SearchResult result;
262 ares_search(channel_, "_imap._tcp.gmail.com.", ns_c_in, ns_t_srv,
263 SearchCallback, &result);
264 Process();
265 EXPECT_TRUE(result.done_);
266 EXPECT_EQ(ARES_SUCCESS, result.status_);
267}
268
269TEST_F(DefaultChannelTest, LiveSearchANY) {
270 SearchResult result;
271 ares_search(channel_, "google.com.", ns_c_in, ns_t_any,
272 SearchCallback, &result);
273 Process();
274 EXPECT_TRUE(result.done_);
275 EXPECT_EQ(ARES_SUCCESS, result.status_);
276}
277
278TEST_F(DefaultChannelTest, LiveGetNameInfoV4) {
279 NameInfoResult result;
280 struct sockaddr_in sockaddr;
281 memset(&sockaddr, 0, sizeof(sockaddr));
282 sockaddr.sin_family = AF_INET;
283 sockaddr.sin_port = htons(53);
284 sockaddr.sin_addr.s_addr = htonl(0x08080808);
285 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
286 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
287 NameInfoCallback, &result);
288 Process();
289 EXPECT_TRUE(result.done_);
290 EXPECT_EQ(ARES_SUCCESS, result.status_);
291 if (verbose) std::cerr << "8.8.8.8:53 => " << result.node_ << "/" << result.service_ << std::endl;
292}
293
294TEST_F(DefaultChannelTest, LiveGetNameInfoV4NoPort) {
295 NameInfoResult result;
296 struct sockaddr_in sockaddr;
297 memset(&sockaddr, 0, sizeof(sockaddr));
298 sockaddr.sin_family = AF_INET;
299 sockaddr.sin_port = htons(0);
300 sockaddr.sin_addr.s_addr = htonl(0x08080808);
301 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
302 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
303 NameInfoCallback, &result);
304 Process();
305 EXPECT_TRUE(result.done_);
306 EXPECT_EQ(ARES_SUCCESS, result.status_);
307 if (verbose) std::cerr << "8.8.8.8:0 => " << result.node_ << "/" << result.service_ << std::endl;
308}
309
310TEST_F(DefaultChannelTest, LiveGetNameInfoV4UnassignedPort) {
311 NameInfoResult result;
312 struct sockaddr_in sockaddr;
313 memset(&sockaddr, 0, sizeof(sockaddr));
314 sockaddr.sin_family = AF_INET;
315 sockaddr.sin_port = htons(4); // Unassigned at IANA
316 sockaddr.sin_addr.s_addr = htonl(0x08080808);
317 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
318 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
319 NameInfoCallback, &result);
320 Process();
321 EXPECT_TRUE(result.done_);
322 EXPECT_EQ(ARES_SUCCESS, result.status_);
323 if (verbose) std::cerr << "8.8.8.8:4 => " << result.node_ << "/" << result.service_ << std::endl;
324}
325
326TEST_F(DefaultChannelTest, LiveGetNameInfoV6Both) {
327 NameInfoResult result;
328 struct sockaddr_in6 sockaddr;
329 memset(&sockaddr, 0, sizeof(sockaddr));
330 sockaddr.sin6_family = AF_INET6;
331 sockaddr.sin6_port = htons(53);
332 memcpy(sockaddr.sin6_addr.s6_addr, gdns_addr6, 16);
333 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
334 ARES_NI_TCP|ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_NOFQDN,
335 NameInfoCallback, &result);
336 Process();
337 EXPECT_TRUE(result.done_);
338 EXPECT_EQ(ARES_SUCCESS, result.status_);
339 if (verbose) std::cerr << "[2001:4860:4860::8888]:53 => " << result.node_ << "/" << result.service_ << std::endl;
340}
341
342TEST_F(DefaultChannelTest, LiveGetNameInfoV6Neither) {
343 NameInfoResult result;
344 struct sockaddr_in6 sockaddr;
345 memset(&sockaddr, 0, sizeof(sockaddr));
346 sockaddr.sin6_family = AF_INET6;
347 sockaddr.sin6_port = htons(53);
348 memcpy(sockaddr.sin6_addr.s6_addr, gdns_addr6, 16);
349 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
350 ARES_NI_TCP|ARES_NI_NOFQDN, // Neither specified => assume lookup host.
351 NameInfoCallback, &result);
352 Process();
353 EXPECT_TRUE(result.done_);
354 EXPECT_EQ(ARES_SUCCESS, result.status_);
355 if (verbose) std::cerr << "[2001:4860:4860::8888]:53 => " << result.node_ << "/" << result.service_ << std::endl;
356}
357
358TEST_F(DefaultChannelTest, LiveGetNameInfoV4Numeric) {
359 NameInfoResult result;
360 struct sockaddr_in sockaddr;
361 memset(&sockaddr, 0, sizeof(sockaddr));
362 sockaddr.sin_family = AF_INET;
363 sockaddr.sin_port = htons(53);
364 sockaddr.sin_addr.s_addr = htonl(0x08080808);
365 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
366 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_TCP|ARES_NI_NUMERICHOST,
367 NameInfoCallback, &result);
368 Process();
369 EXPECT_TRUE(result.done_);
370 EXPECT_EQ(ARES_SUCCESS, result.status_);
371 EXPECT_EQ("8.8.8.8", result.node_);
372 if (verbose) std::cerr << "8.8.8.8:53 => " << result.node_ << "/" << result.service_ << std::endl;
373}
374
375TEST_F(DefaultChannelTest, LiveGetNameInfoV6Numeric) {
376 NameInfoResult result;
377 struct sockaddr_in6 sockaddr;
378 memset(&sockaddr, 0, sizeof(sockaddr));
379 sockaddr.sin6_family = AF_INET6;
380 sockaddr.sin6_port = htons(53);
381 memcpy(sockaddr.sin6_addr.s6_addr, gdns_addr6, 16);
382 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
383 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_DCCP|ARES_NI_NUMERICHOST,
384 NameInfoCallback, &result);
385 Process();
386 EXPECT_TRUE(result.done_);
387 EXPECT_EQ(ARES_SUCCESS, result.status_);
388 EXPECT_EQ("2001:4860:4860::8888%0", result.node_);
389 if (verbose) std::cerr << "[2001:4860:4860::8888]:53 => " << result.node_ << "/" << result.service_ << std::endl;
390}
391
392TEST_F(DefaultChannelTest, LiveGetNameInfoV6LinkLocal) {
393 NameInfoResult result;
394 struct sockaddr_in6 sockaddr;
395 memset(&sockaddr, 0, sizeof(sockaddr));
396 sockaddr.sin6_family = AF_INET6;
397 sockaddr.sin6_port = htons(53);
398 unsigned char addr6[16] = {0xfe, 0x80, 0x01, 0x02, 0x01, 0x02, 0x00, 0x00,
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04};
400 memcpy(sockaddr.sin6_addr.s6_addr, addr6, 16);
401 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
402 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_DCCP|ARES_NI_NUMERICHOST,
403 NameInfoCallback, &result);
404 Process();
405 EXPECT_TRUE(result.done_);
406 EXPECT_EQ(ARES_SUCCESS, result.status_);
407 EXPECT_EQ("fe80:102:102::304%0", result.node_);
408 if (verbose) std::cerr << "[fe80:102:102::304]:53 => " << result.node_ << "/" << result.service_ << std::endl;
409}
410
411TEST_F(DefaultChannelTest, LiveGetNameInfoV4NotFound) {
412 NameInfoResult result;
413 struct sockaddr_in sockaddr;
414 memset(&sockaddr, 0, sizeof(sockaddr));
415 sockaddr.sin_family = AF_INET;
416 sockaddr.sin_port = htons(4); // Port 4 unassigned at IANA
417 // RFC5737 says 192.0.2.0 should not be used publically.
418 sockaddr.sin_addr.s_addr = htonl(0xC0000200);
419 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
420 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
421 NameInfoCallback, &result);
422 Process();
423 EXPECT_TRUE(result.done_);
424 EXPECT_EQ(ARES_SUCCESS, result.status_);
425 EXPECT_EQ("192.0.2.0", result.node_);
426 if (verbose) std::cerr << "192.0.2.0:53 => " << result.node_ << "/" << result.service_ << std::endl;
427}
428
429TEST_F(DefaultChannelTest, LiveGetNameInfoV4NotFoundFail) {
430 NameInfoResult result;
431 struct sockaddr_in sockaddr;
432 memset(&sockaddr, 0, sizeof(sockaddr));
433 sockaddr.sin_family = AF_INET;
434 sockaddr.sin_port = htons(53);
435 // RFC5737 says 192.0.2.0 should not be used publically.
436 sockaddr.sin_addr.s_addr = htonl(0xC0000200);
437 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
438 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP|ARES_NI_NAMEREQD,
439 NameInfoCallback, &result);
440 Process();
441 EXPECT_TRUE(result.done_);
442 EXPECT_EQ(ARES_ENOTFOUND, result.status_);
443}
444
445TEST_F(DefaultChannelTest, LiveGetNameInfoV6NotFound) {
446 NameInfoResult result;
447 struct sockaddr_in6 sockaddr;
448 memset(&sockaddr, 0, sizeof(sockaddr));
449 sockaddr.sin6_family = AF_INET6;
450 sockaddr.sin6_port = htons(53);
451 // 2001:db8::/32 is only supposed to be used in documentation.
452 unsigned char addr6[16] = {0x20, 0x01, 0x0d, 0xb8, 0x01, 0x02, 0x00, 0x00,
453 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04};
454 memcpy(sockaddr.sin6_addr.s6_addr, addr6, 16);
455 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
456 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
457 NameInfoCallback, &result);
458 Process();
459 EXPECT_TRUE(result.done_);
460 EXPECT_EQ(ARES_SUCCESS, result.status_);
461 EXPECT_EQ("2001:db8:102::304%0", result.node_);
462 if (verbose) std::cerr << "[2001:db8:102::304]:53 => " << result.node_ << "/" << result.service_ << std::endl;
463}
464
465TEST_F(DefaultChannelTest, LiveGetNameInvalidFamily) {
466 NameInfoResult result;
467 struct sockaddr_in6 sockaddr;
468 memset(&sockaddr, 0, sizeof(sockaddr));
469 sockaddr.sin6_family = AF_INET6 + AF_INET;
470 sockaddr.sin6_port = htons(53);
471 memcpy(sockaddr.sin6_addr.s6_addr, gdns_addr6, 16);
472 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
473 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
474 NameInfoCallback, &result);
475 Process();
476 EXPECT_TRUE(result.done_);
477 EXPECT_EQ(ARES_ENOTIMP, result.status_);
478}
479
480TEST_F(DefaultChannelTest, LiveGetNameInvalidFlags) {
481 NameInfoResult result;
482 struct sockaddr_in6 sockaddr;
483 memset(&sockaddr, 0, sizeof(sockaddr));
484 sockaddr.sin6_family = AF_INET6;
485 sockaddr.sin6_port = htons(53);
486 memcpy(sockaddr.sin6_addr.s6_addr, gdns_addr6, 16);
487 // Ask for both a name-required, and a numeric host.
488 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
489 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP|ARES_NI_NUMERICHOST|ARES_NI_NAMEREQD,
490 NameInfoCallback, &result);
491 Process();
492 EXPECT_TRUE(result.done_);
493 EXPECT_EQ(ARES_EBADFLAGS, result.status_);
494}
495
496TEST_F(DefaultChannelTest, LiveGetServiceInfo) {
497 NameInfoResult result;
498 struct sockaddr_in sockaddr;
499 memset(&sockaddr, 0, sizeof(sockaddr));
500 sockaddr.sin_family = AF_INET;
501 sockaddr.sin_port = htons(53);
502 sockaddr.sin_addr.s_addr = htonl(0x08080808);
503 // Just look up service info
504 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
505 ARES_NI_LOOKUPSERVICE|ARES_NI_SCTP,
506 NameInfoCallback, &result);
507 Process();
508 EXPECT_TRUE(result.done_);
509 EXPECT_EQ(ARES_SUCCESS, result.status_);
510 EXPECT_EQ("", result.node_);
511}
512
513TEST_F(DefaultChannelTest, LiveGetServiceInfoNumeric) {
514 NameInfoResult result;
515 struct sockaddr_in sockaddr;
516 memset(&sockaddr, 0, sizeof(sockaddr));
517 sockaddr.sin_family = AF_INET;
518 sockaddr.sin_port = htons(53);
519 sockaddr.sin_addr.s_addr = htonl(0x08080808);
520 // Just look up service info
521 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
522 ARES_NI_LOOKUPSERVICE|ARES_NI_SCTP|ARES_NI_NUMERICSERV,
523 NameInfoCallback, &result);
524 Process();
525 EXPECT_TRUE(result.done_);
526 EXPECT_EQ(ARES_SUCCESS, result.status_);
527 EXPECT_EQ("", result.node_);
528 EXPECT_EQ("53", result.service_);
529}
530
531TEST_F(DefaultChannelTest, LiveGetNameInfoAllocFail) {
532 NameInfoResult result;
533 struct sockaddr_in sockaddr;
534 memset(&sockaddr, 0, sizeof(sockaddr));
535 sockaddr.sin_family = AF_INET;
536 sockaddr.sin_port = htons(53);
537 sockaddr.sin_addr.s_addr = htonl(0x08080808);
538 SetAllocFail(1);
539 ares_getnameinfo(channel_, (const struct sockaddr*)&sockaddr, sizeof(sockaddr),
540 ARES_NI_LOOKUPHOST|ARES_NI_LOOKUPSERVICE|ARES_NI_UDP,
541 NameInfoCallback, &result);
542 Process();
543 EXPECT_TRUE(result.done_);
544 EXPECT_EQ(ARES_ENOMEM, result.status_);
545}
546
547TEST_F(DefaultChannelTest, GetSock) {
548 ares_socket_t socks[3] = {-1, -1, -1};
549 int bitmask = ares_getsock(channel_, socks, 3);
550 EXPECT_EQ(0, bitmask);
551 bitmask = ares_getsock(channel_, nullptr, 0);
552 EXPECT_EQ(0, bitmask);
553
554 // Ask again with a pending query.
555 HostResult result;
556 ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
557 bitmask = ares_getsock(channel_, socks, 3);
558 EXPECT_NE(0, bitmask);
559 bitmask = ares_getsock(channel_, nullptr, 0);
560 EXPECT_EQ(0, bitmask);
561
562 Process();
563}
564
565TEST_F(LibraryTest, GetTCPSock) {
566 ares_channel channel;
567 struct ares_options opts = {0};
568 opts.tcp_port = 53;
569 opts.flags = ARES_FLAG_USEVC;
570 int optmask = ARES_OPT_TCP_PORT | ARES_OPT_FLAGS;
571 EXPECT_EQ(ARES_SUCCESS, ares_init_options(&channel, &opts, optmask));
572 EXPECT_NE(nullptr, channel);
573
574 ares_socket_t socks[3] = {-1, -1, -1};
575 int bitmask = ares_getsock(channel, socks, 3);
576 EXPECT_EQ(0, bitmask);
577 bitmask = ares_getsock(channel, nullptr, 0);
578 EXPECT_EQ(0, bitmask);
579
580 // Ask again with a pending query.
581 HostResult result;
582 ares_gethostbyname(channel, "www.google.com.", AF_INET, HostCallback, &result);
583 bitmask = ares_getsock(channel, socks, 3);
584 EXPECT_NE(0, bitmask);
585 bitmask = ares_getsock(channel, nullptr, 0);
586 EXPECT_EQ(0, bitmask);
587
588 ProcessWork(channel, NoExtraFDs, nullptr);
589
590 ares_destroy(channel);
591}
592
593} // namespace test
594} // namespace ares