blob: 2bd27fd7123c75800c4a05d2665cd73e77a59aa8 [file] [log] [blame]
nuccachen5c2d32f2018-11-28 18:19:39 +08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "libnetd_resolv_test"
18
19#include <gtest/gtest.h>
20
21#include <android-base/stringprintf.h>
Hungming Chen7f0d3292018-12-27 18:33:19 +080022#include <arpa/inet.h>
nuccachen5c2d32f2018-11-28 18:19:39 +080023#include <netdb.h>
24
25#include "dns_responder.h"
26#include "netd_resolv/resolv.h"
27
28// TODO: make this dynamic and stop depending on implementation details.
29constexpr unsigned int TEST_NETID = 30;
30
31// Specifying 0 in ai_socktype or ai_protocol of struct addrinfo indicates that any type or
32// protocol can be returned by getaddrinfo().
33constexpr unsigned int ANY = 0;
34
35using android::base::StringPrintf;
36
37namespace android {
38namespace net {
39
40// Minimize class ResolverTest to be class TestBase because class TestBase doesn't need all member
41// functions of class ResolverTest and class DnsResponderClient.
42class TestBase : public ::testing::Test {
43 protected:
44 void TearDown() {
45 resolv_delete_cache_for_net(TEST_NETID);
46 resolv_set_nameservers_for_net(TEST_NETID, nullptr, 0, "", nullptr);
47 }
48
Hungming Chen7f0d3292018-12-27 18:33:19 +080049 static std::string ToString(const hostent* he) {
50 if (he == nullptr) return "<null>";
51 char buffer[INET6_ADDRSTRLEN];
52 if (!inet_ntop(he->h_addrtype, he->h_addr_list[0], buffer, sizeof(buffer))) {
53 return "<invalid>";
54 }
55 return buffer;
56 }
57
nuccachen5c2d32f2018-11-28 18:19:39 +080058 static std::string ToString(const addrinfo* ai) {
59 if (!ai) return "<null>";
60 for (const auto* aip = ai; aip != nullptr; aip = aip->ai_next) {
61 char host[NI_MAXHOST];
62 int rv = getnameinfo(aip->ai_addr, aip->ai_addrlen, host, sizeof(host), nullptr, 0,
63 NI_NUMERICHOST);
64 if (rv != 0) return gai_strerror(rv);
65 return host;
66 }
67 return "<invalid>";
68 }
69
70 size_t GetNumQueries(const test::DNSResponder& dns, const char* name) const {
71 auto queries = dns.queries();
72 size_t found = 0;
73 for (const auto& p : queries) {
74 if (p.first == name) {
75 ++found;
76 }
77 }
78 return found;
79 }
80
81 const char* mDefaultSearchDomains = "example.com";
82 const __res_params mDefaultParams_Binder = {
83 .sample_validity = 300,
84 .success_threshold = 25,
85 .min_samples = 8,
86 .max_samples = 8,
87 .base_timeout_msec = 100,
88 };
Hungming Chen7f0d3292018-12-27 18:33:19 +080089 const android_net_context mNetcontext = {
90 .app_netid = TEST_NETID,
nuccachen5c2d32f2018-11-28 18:19:39 +080091 .app_mark = MARK_UNSET,
Hungming Chen7f0d3292018-12-27 18:33:19 +080092 .dns_netid = TEST_NETID,
nuccachen5c2d32f2018-11-28 18:19:39 +080093 .dns_mark = MARK_UNSET,
94 .uid = NET_CONTEXT_INVALID_UID,
95 };
Hungming Chen7f0d3292018-12-27 18:33:19 +080096};
nuccachen5c2d32f2018-11-28 18:19:39 +080097
Hungming Chen7f0d3292018-12-27 18:33:19 +080098class GetAddrInfoForNetContextTest : public TestBase {};
99class GetHostByNameForNetContextTest : public TestBase {};
100
101TEST_F(GetAddrInfoForNetContextTest, InvalidParameters) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800102 // Both null "netcontext" and null "res" of android_getaddrinfofornetcontext() are not tested
103 // here because they are checked by assert() without returning any error number.
104
105 // Invalid hostname and servname.
106 // Both hostname and servname are null pointers. Expect error number EAI_NONAME.
Hungming Chen7f0d3292018-12-27 18:33:19 +0800107 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800108 int rv = android_getaddrinfofornetcontext(nullptr /*hostname*/, nullptr /*servname*/,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800109 nullptr /*hints*/, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800110 EXPECT_EQ(EAI_NONAME, rv);
111 if (result) {
112 freeaddrinfo(result);
113 result = nullptr;
114 }
115
116 // Invalid hints.
117 // These place holders are used to test function call with unrequired parameters.
118 // The content is not important because function call returns error directly if
119 // there have any unrequired parameter.
120 char placeholder_cname[] = "invalid_cname";
121 sockaddr placeholder_addr = {};
122 addrinfo placeholder_next = {};
123 static const struct TestConfig {
124 int ai_flags;
125 socklen_t ai_addrlen;
126 char* ai_canonname;
127 struct sockaddr* ai_addr;
128 struct addrinfo* ai_next;
129 int expected_errorno; // expected result
130
131 std::string asParameters() const {
132 return StringPrintf("0x%x/%u/%s/%p/%p", ai_flags, ai_addrlen,
133 ai_canonname ? ai_canonname : "(null)", (void*) ai_addr,
134 (void*) ai_next);
135 }
136 } testConfigs[]{
137 {0, sizeof(struct in_addr) /*bad*/, nullptr, nullptr, nullptr, EAI_BADHINTS},
138 {0, 0, placeholder_cname /*bad*/, nullptr, nullptr, EAI_BADHINTS},
139 {0, 0, nullptr, &placeholder_addr /*bad*/, nullptr, EAI_BADHINTS},
140 {0, 0, nullptr, nullptr, &placeholder_next /*bad*/, EAI_BADHINTS},
141 {AI_ALL /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
142 {AI_V4MAPPED_CFG /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
143 {AI_V4MAPPED /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
144 {AI_DEFAULT /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
145 };
146
147 for (const auto& config : testConfigs) {
148 SCOPED_TRACE(config.asParameters());
149
150 // In current test configuration set, ai_family, ai_protocol and ai_socktype are not
151 // checked because other fields cause hints error check failed first.
152 const struct addrinfo hints = {
153 .ai_flags = config.ai_flags,
154 .ai_family = AF_UNSPEC,
155 .ai_socktype = ANY,
156 .ai_protocol = ANY,
157 .ai_addrlen = config.ai_addrlen,
158 .ai_canonname = config.ai_canonname,
159 .ai_addr = config.ai_addr,
160 .ai_next = config.ai_next,
161 };
162
163 rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800164 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800165 EXPECT_EQ(config.expected_errorno, rv);
166
167 if (result) {
168 freeaddrinfo(result);
169 result = nullptr;
170 }
171 }
172}
173
174TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_Family) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800175 for (int family = 0; family < AF_MAX; ++family) {
176 if (family == AF_UNSPEC || family == AF_INET || family == AF_INET6) {
177 continue; // skip supported family
178 }
179 SCOPED_TRACE(StringPrintf("family: %d", family));
180
181 struct addrinfo* result = nullptr;
182 const struct addrinfo hints = {
183 .ai_family = family, // unsupported family
184 };
185
186 int rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800187 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800188 EXPECT_EQ(EAI_FAMILY, rv);
189
190 if (result) freeaddrinfo(result);
191 }
192}
193
194TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_MeaningfulSocktypeAndProtocolCombination) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800195 static const int families[] = {PF_INET, PF_INET6, PF_UNSPEC};
196 // Skip to test socket type SOCK_RAW in meaningful combination (explore_options[]) of
197 // system\netd\resolv\getaddrinfo.cpp. In explore_options[], the socket type SOCK_RAW always
198 // comes with protocol ANY which causes skipping meaningful socktype/protocol combination
199 // check. So it nerver returns error number EAI_BADHINTS which we want to test in this test
200 // case.
201 static const int socktypes[] = {SOCK_STREAM, SOCK_DGRAM};
202
203 // If both socktype/protocol are specified, check non-meaningful combination returns
204 // expected error number EAI_BADHINTS. See meaningful combination in explore_options[] of
205 // system\netd\resolv\getaddrinfo.cpp.
206 for (const auto& family : families) {
207 for (const auto& socktype : socktypes) {
208 for (int protocol = 0; protocol < IPPROTO_MAX; ++protocol) {
209 SCOPED_TRACE(StringPrintf("family: %d, socktype: %d, protocol: %d", family,
210 socktype, protocol));
211
212 // Both socktype/protocol need to be specified.
213 if (!socktype || !protocol) continue;
214
215 // Skip meaningful combination in explore_options[] of
216 // system\netd\resolv\getaddrinfo.cpp.
217 if ((family == AF_INET6 && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
218 (family == AF_INET6 && socktype == SOCK_STREAM && protocol == IPPROTO_TCP) ||
219 (family == AF_INET && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
220 (family == AF_INET && socktype == SOCK_STREAM && protocol == IPPROTO_TCP) ||
221 (family == AF_UNSPEC && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
222 (family == AF_UNSPEC && socktype == SOCK_STREAM && protocol == IPPROTO_TCP)) {
223 continue;
224 }
225
226 struct addrinfo* result = nullptr;
227 const struct addrinfo hints = {
228 .ai_family = family,
229 .ai_protocol = protocol,
230 .ai_socktype = socktype,
231 };
232
233 int rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800234 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800235 EXPECT_EQ(EAI_BADHINTS, rv);
236
237 if (result) freeaddrinfo(result);
238 }
239 }
240 }
241}
242
243TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_PortNameAndNumber) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800244 constexpr char http_portno[] = "80";
245 constexpr char invalid_portno[] = "65536"; // out of valid port range from 0 to 65535
246 constexpr char http_portname[] = "http";
247 constexpr char invalid_portname[] = "invalid_portname";
248
249 static const struct TestConfig {
250 int ai_flags;
251 int ai_family;
252 int ai_socktype;
253 const char* servname;
254 int expected_errorno; // expected result
255
256 std::string asParameters() const {
257 return StringPrintf("0x%x/%d/%d/%s", ai_flags, ai_family, ai_socktype,
258 servname ? servname : "(null)");
259 }
260 } testConfigs[]{
261 {0, AF_INET, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
262 {0, AF_INET6, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
263 {0, AF_UNSPEC, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
264 {0, AF_INET, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
265 {0, AF_INET6, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
266 {0, AF_UNSPEC, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
267 {0, AF_INET, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
268 {0, AF_INET6, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
269 {0, AF_UNSPEC, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
270 {0, AF_INET, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
271 {0, AF_INET6, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
272 {0, AF_UNSPEC, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
273 {0, AF_INET, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
274 {0, AF_INET6, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
275 {0, AF_UNSPEC, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
276 {0, AF_INET, ANY, invalid_portno /*bad*/, EAI_SERVICE},
277 {0, AF_INET, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
278 {0, AF_INET, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
279 {0, AF_INET6, ANY, invalid_portno /*bad*/, EAI_SERVICE},
280 {0, AF_INET6, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
281 {0, AF_INET6, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
282 {0, AF_UNSPEC, ANY, invalid_portno /*bad*/, EAI_SERVICE},
283 {0, AF_UNSPEC, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
284 {0, AF_UNSPEC, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
285 {AI_NUMERICSERV, AF_INET, ANY, http_portname /*bad*/, EAI_NONAME},
286 {AI_NUMERICSERV, AF_INET, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
287 {AI_NUMERICSERV, AF_INET, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
288 {AI_NUMERICSERV, AF_INET6, ANY, http_portname /*bad*/, EAI_NONAME},
289 {AI_NUMERICSERV, AF_INET6, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
290 {AI_NUMERICSERV, AF_INET6, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
291 {AI_NUMERICSERV, AF_UNSPEC, ANY, http_portname /*bad*/, EAI_NONAME},
292 {AI_NUMERICSERV, AF_UNSPEC, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
293 {AI_NUMERICSERV, AF_UNSPEC, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
294 {0, AF_INET, ANY, invalid_portname /*bad*/, EAI_SERVICE},
295 {0, AF_INET, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
296 {0, AF_INET, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
297 {0, AF_INET6, ANY, invalid_portname /*bad*/, EAI_SERVICE},
298 {0, AF_INET6, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
299 {0, AF_INET6, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
300 {0, AF_UNSPEC, ANY, invalid_portname /*bad*/, EAI_SERVICE},
301 {0, AF_UNSPEC, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
302 {0, AF_UNSPEC, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
303 };
304
305 for (const auto& config : testConfigs) {
306 const std::string testParameters = config.asParameters();
307 SCOPED_TRACE(testParameters);
308
309 const struct addrinfo hints = {
310 .ai_flags = config.ai_flags,
311 .ai_family = config.ai_family,
312 .ai_socktype = config.ai_socktype,
313 };
314
315 struct addrinfo* result = nullptr;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800316 int rv = android_getaddrinfofornetcontext("localhost", config.servname, &hints,
317 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800318 EXPECT_EQ(config.expected_errorno, rv);
319
320 if (result) freeaddrinfo(result);
321 }
322}
323
nuccachen5c2d32f2018-11-28 18:19:39 +0800324TEST_F(GetAddrInfoForNetContextTest, AlphabeticalHostname_NoData) {
325 constexpr char listen_addr[] = "127.0.0.3";
326 constexpr char listen_srv[] = "53";
327 constexpr char v4_host_name[] = "v4only.example.com.";
nuccachen5c2d32f2018-11-28 18:19:39 +0800328 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
329 dns.addMapping(v4_host_name, ns_type::ns_t_a, "1.2.3.3");
330 ASSERT_TRUE(dns.startServer());
331 const char* servers[] = {listen_addr};
332 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
333 sizeof(servers) / sizeof(servers[0]),
334 mDefaultSearchDomains, &mDefaultParams_Binder));
335 dns.clearQueries();
336
nuccachen5c2d32f2018-11-28 18:19:39 +0800337 // Want AAAA answer but DNS server has A answer only.
Hungming Chen7f0d3292018-12-27 18:33:19 +0800338 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800339 const addrinfo hints = {.ai_family = AF_INET6};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800340 int rv = android_getaddrinfofornetcontext("v4only", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800341 EXPECT_LE(1U, GetNumQueries(dns, v4_host_name));
342 EXPECT_TRUE(result == nullptr);
343 EXPECT_EQ(EAI_NODATA, rv);
344
345 if (result) freeaddrinfo(result);
346}
nuccachen5c2d32f2018-11-28 18:19:39 +0800347
348TEST_F(GetAddrInfoForNetContextTest, AlphabeticalHostname) {
349 constexpr char listen_addr[] = "127.0.0.3";
350 constexpr char listen_srv[] = "53";
351 constexpr char host_name[] = "sawadee.example.com.";
352 constexpr char v4addr[] = "1.2.3.4";
353 constexpr char v6addr[] = "::1.2.3.4";
354
355 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
356 dns.addMapping(host_name, ns_type::ns_t_a, v4addr);
357 dns.addMapping(host_name, ns_type::ns_t_aaaa, v6addr);
358 ASSERT_TRUE(dns.startServer());
359 const char* servers[] = {listen_addr};
360 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
361 sizeof(servers) / sizeof(servers[0]),
362 mDefaultSearchDomains, &mDefaultParams_Binder));
363
nuccachen5c2d32f2018-11-28 18:19:39 +0800364 static const struct TestConfig {
365 int ai_family;
366 const std::string expected_addr;
367 } testConfigs[]{
368 {AF_INET, v4addr},
369 {AF_INET6, v6addr},
370 };
371
372 for (const auto& config : testConfigs) {
373 SCOPED_TRACE(StringPrintf("family: %d", config.ai_family));
374 dns.clearQueries();
375
376 struct addrinfo* result = nullptr;
377 const struct addrinfo hints = {.ai_family = config.ai_family};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800378 int rv =
379 android_getaddrinfofornetcontext("sawadee", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800380 EXPECT_EQ(0, rv);
381 EXPECT_TRUE(result != nullptr);
382 EXPECT_EQ(1U, GetNumQueries(dns, host_name));
383 EXPECT_EQ(config.expected_addr, ToString(result));
384
385 if (result) freeaddrinfo(result);
386 }
387}
388
389TEST_F(GetAddrInfoForNetContextTest, ServerResponseError) {
390 constexpr char listen_addr[] = "127.0.0.3";
391 constexpr char listen_srv[] = "53";
392 constexpr char host_name[] = "hello.example.com.";
393
Hungming Chen7f0d3292018-12-27 18:33:19 +0800394 static const struct TestConfig {
395 ns_rcode rcode;
396 int expected_errorno; // expected result
397
398 // Only test failure RCODE [1..5] in RFC 1035 section 4.1.1 and skip successful RCODE 0
399 // which means no error.
400 } testConfigs[]{
401 // clang-format off
402 {ns_rcode::ns_r_formerr, EAI_FAIL},
403 {ns_rcode::ns_r_servfail, EAI_AGAIN},
404 {ns_rcode::ns_r_nxdomain, EAI_NODATA},
405 {ns_rcode::ns_r_notimpl, EAI_FAIL},
406 {ns_rcode::ns_r_refused, EAI_FAIL},
407 // clang-format on
nuccachen5c2d32f2018-11-28 18:19:39 +0800408 };
409
Hungming Chen7f0d3292018-12-27 18:33:19 +0800410 for (const auto& config : testConfigs) {
411 SCOPED_TRACE(StringPrintf("rcode: %d", config.rcode));
nuccachen5c2d32f2018-11-28 18:19:39 +0800412
Hungming Chen7f0d3292018-12-27 18:33:19 +0800413 test::DNSResponder dns(listen_addr, listen_srv, 250,
414 config.rcode /*response specific rcode*/);
nuccachen5c2d32f2018-11-28 18:19:39 +0800415 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
416 dns.setResponseProbability(0.0); // always ignore requests and response preset rcode
417 ASSERT_TRUE(dns.startServer());
418 const char* servers[] = {listen_addr};
419 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
420 sizeof(servers) / sizeof(servers[0]),
421 mDefaultSearchDomains, &mDefaultParams_Binder));
422
423 struct addrinfo* result = nullptr;
424 const struct addrinfo hints = {.ai_family = AF_UNSPEC};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800425 int rv =
426 android_getaddrinfofornetcontext(host_name, nullptr, &hints, &mNetcontext, &result);
427 EXPECT_EQ(config.expected_errorno, rv);
nuccachen5c2d32f2018-11-28 18:19:39 +0800428
429 if (result) freeaddrinfo(result);
430 }
431}
432
433// TODO: Add private DNS server timeout test.
434TEST_F(GetAddrInfoForNetContextTest, ServerTimeout) {
435 constexpr char listen_addr[] = "127.0.0.3";
436 constexpr char listen_srv[] = "53";
437 constexpr char host_name[] = "hello.example.com.";
438 test::DNSResponder dns(listen_addr, listen_srv, 250, static_cast<ns_rcode>(-1) /*no response*/);
439 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
440 dns.setResponseProbability(0.0); // always ignore requests and don't response
441 ASSERT_TRUE(dns.startServer());
442 const char* servers[] = {listen_addr};
443 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
444 sizeof(servers) / sizeof(servers[0]),
445 mDefaultSearchDomains, &mDefaultParams_Binder));
446
447 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800448 const struct addrinfo hints = {.ai_family = AF_UNSPEC};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800449 int rv = android_getaddrinfofornetcontext("hello", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800450 EXPECT_EQ(NETD_RESOLV_TIMEOUT, rv);
451
452 if (result) freeaddrinfo(result);
453}
454
Hungming Chen7f0d3292018-12-27 18:33:19 +0800455TEST_F(GetHostByNameForNetContextTest, AlphabeticalHostname) {
456 constexpr char listen_addr[] = "127.0.0.3";
457 constexpr char listen_srv[] = "53";
458 constexpr char host_name[] = "jiababuei.example.com.";
459 constexpr char v4addr[] = "1.2.3.4";
460 constexpr char v6addr[] = "::1.2.3.4";
nuccachen5c2d32f2018-11-28 18:19:39 +0800461
Hungming Chen7f0d3292018-12-27 18:33:19 +0800462 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
463 dns.addMapping(host_name, ns_type::ns_t_a, v4addr);
464 dns.addMapping(host_name, ns_type::ns_t_aaaa, v6addr);
465 ASSERT_TRUE(dns.startServer());
466 const char* servers[] = {listen_addr};
467 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
468 sizeof(servers) / sizeof(servers[0]),
469 mDefaultSearchDomains, &mDefaultParams_Binder));
470
471 static const struct TestConfig {
472 int ai_family;
473 const std::string expected_addr;
474 } testConfigs[]{
475 {AF_INET, v4addr},
476 {AF_INET6, v6addr},
477 };
478
479 for (const auto& config : testConfigs) {
480 SCOPED_TRACE(StringPrintf("family: %d", config.ai_family));
481 dns.clearQueries();
482
483 struct hostent* hp = nullptr;
484 int rv = android_gethostbynamefornetcontext("jiababuei", config.ai_family, &mNetcontext,
485 &hp);
486 EXPECT_EQ(0, rv);
487 EXPECT_TRUE(hp != nullptr);
488 EXPECT_EQ(1U, GetNumQueries(dns, host_name));
489 EXPECT_EQ(config.expected_addr, ToString(hp));
490 }
491}
492
493TEST_F(GetHostByNameForNetContextTest, NoData) {
494 constexpr char listen_addr[] = "127.0.0.3";
495 constexpr char listen_srv[] = "53";
496 constexpr char v4_host_name[] = "v4only.example.com.";
497 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
498 dns.addMapping(v4_host_name, ns_type::ns_t_a, "1.2.3.3");
499 ASSERT_TRUE(dns.startServer());
500 const char* servers[] = {listen_addr};
501 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
502 sizeof(servers) / sizeof(servers[0]),
503 mDefaultSearchDomains, &mDefaultParams_Binder));
504 dns.clearQueries();
505
506 // Want AAAA answer but DNS server has A answer only.
507 struct hostent* hp = nullptr;
508 int rv = android_gethostbynamefornetcontext("v4only", AF_INET6, &mNetcontext, &hp);
509 EXPECT_LE(1U, GetNumQueries(dns, v4_host_name));
510 EXPECT_TRUE(hp == nullptr);
511 EXPECT_EQ(EAI_NODATA, rv);
512}
513
514TEST_F(GetHostByNameForNetContextTest, ServerResponseError) {
515 constexpr char listen_addr[] = "127.0.0.3";
516 constexpr char listen_srv[] = "53";
517 constexpr char host_name[] = "hello.example.com.";
518
519 static const struct TestConfig {
520 ns_rcode rcode;
521 int expected_errorno; // expected result
522
523 // Only test failure RCODE [1..5] in RFC 1035 section 4.1.1 and skip successful RCODE 0
524 // which means no error. Note that the return error codes aren't mapped by rcode in the
525 // test case SERVFAIL, NOTIMP and REFUSED. See the comment of res_nsend()
526 // in system\netd\resolv\res_query.cpp for more detail.
527 } testConfigs[]{
528 // clang-format off
529 {ns_rcode::ns_r_formerr, EAI_FAIL},
530 {ns_rcode::ns_r_servfail, EAI_AGAIN}, // Not mapped by rcode.
531 {ns_rcode::ns_r_nxdomain, EAI_NODATA},
532 {ns_rcode::ns_r_notimpl, EAI_AGAIN}, // Not mapped by rcode.
533 {ns_rcode::ns_r_refused, EAI_AGAIN}, // Not mapped by rcode.
534 // clang-format on
535 };
536
537 for (const auto& config : testConfigs) {
538 SCOPED_TRACE(StringPrintf("rcode: %d", config.rcode));
539
540 test::DNSResponder dns(listen_addr, listen_srv, 250,
541 config.rcode /*response specific rcode*/);
542 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
543 dns.setResponseProbability(0.0); // always ignore requests and response preset rcode
544 ASSERT_TRUE(dns.startServer());
545 const char* servers[] = {listen_addr};
546 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
547 sizeof(servers) / sizeof(servers[0]),
548 mDefaultSearchDomains, &mDefaultParams_Binder));
549
550 struct hostent* hp = nullptr;
551 int rv = android_gethostbynamefornetcontext(host_name, AF_INET, &mNetcontext, &hp);
552 EXPECT_TRUE(hp == nullptr);
553 EXPECT_EQ(config.expected_errorno, rv);
554 }
555}
556
557// TODO: Add private DNS server timeout test.
558TEST_F(GetHostByNameForNetContextTest, ServerTimeout) {
559 constexpr char listen_addr[] = "127.0.0.3";
560 constexpr char listen_srv[] = "53";
561 constexpr char host_name[] = "hello.example.com.";
562 test::DNSResponder dns(listen_addr, listen_srv, 250, static_cast<ns_rcode>(-1) /*no response*/);
563 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
564 dns.setResponseProbability(0.0); // always ignore requests and don't response
565 ASSERT_TRUE(dns.startServer());
566 const char* servers[] = {listen_addr};
567 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
568 sizeof(servers) / sizeof(servers[0]),
569 mDefaultSearchDomains, &mDefaultParams_Binder));
570
571 struct hostent* hp = nullptr;
572 int rv = android_gethostbynamefornetcontext(host_name, AF_INET, &mNetcontext, &hp);
573 EXPECT_EQ(NETD_RESOLV_TIMEOUT, rv);
574}
575
576// Note that local host file function, files_getaddrinfo(), of android_getaddrinfofornetcontext()
577// is not tested because it only returns a boolean (success or failure) without any error number.
578
579// TODO: Add test NULL hostname, or numeric hostname for android_getaddrinfofornetcontext.
580// TODO: Add test invalid parameters for android_gethostbynamefornetcontext.
581// TODO: Add test for android_gethostbyaddrfornetcontext.
nuccachen5c2d32f2018-11-28 18:19:39 +0800582
583} // end of namespace net
584} // end of namespace android