blob: d5b1b73fd43487b0a1cb944ed20161633867368f [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";
Bernie Innocenti34de3ba2019-02-19 18:08:36 +090082 const res_params mDefaultParams_Binder = {
nuccachen5c2d32f2018-11-28 18:19:39 +080083 .sample_validity = 300,
84 .success_threshold = 25,
85 .min_samples = 8,
86 .max_samples = 8,
waynema2371eab2019-01-18 14:02:31 +080087 .base_timeout_msec = 1000,
88 .retry_count = 2,
nuccachen5c2d32f2018-11-28 18:19:39 +080089 };
Hungming Chen7f0d3292018-12-27 18:33:19 +080090 const android_net_context mNetcontext = {
91 .app_netid = TEST_NETID,
nuccachen5c2d32f2018-11-28 18:19:39 +080092 .app_mark = MARK_UNSET,
Hungming Chen7f0d3292018-12-27 18:33:19 +080093 .dns_netid = TEST_NETID,
nuccachen5c2d32f2018-11-28 18:19:39 +080094 .dns_mark = MARK_UNSET,
95 .uid = NET_CONTEXT_INVALID_UID,
96 };
Hungming Chen7f0d3292018-12-27 18:33:19 +080097};
nuccachen5c2d32f2018-11-28 18:19:39 +080098
Hungming Chen7f0d3292018-12-27 18:33:19 +080099class GetAddrInfoForNetContextTest : public TestBase {};
100class GetHostByNameForNetContextTest : public TestBase {};
101
102TEST_F(GetAddrInfoForNetContextTest, InvalidParameters) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800103 // Both null "netcontext" and null "res" of android_getaddrinfofornetcontext() are not tested
104 // here because they are checked by assert() without returning any error number.
105
106 // Invalid hostname and servname.
107 // Both hostname and servname are null pointers. Expect error number EAI_NONAME.
Hungming Chen7f0d3292018-12-27 18:33:19 +0800108 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800109 int rv = android_getaddrinfofornetcontext(nullptr /*hostname*/, nullptr /*servname*/,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800110 nullptr /*hints*/, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800111 EXPECT_EQ(EAI_NONAME, rv);
112 if (result) {
113 freeaddrinfo(result);
114 result = nullptr;
115 }
116
117 // Invalid hints.
118 // These place holders are used to test function call with unrequired parameters.
119 // The content is not important because function call returns error directly if
120 // there have any unrequired parameter.
121 char placeholder_cname[] = "invalid_cname";
122 sockaddr placeholder_addr = {};
123 addrinfo placeholder_next = {};
124 static const struct TestConfig {
125 int ai_flags;
126 socklen_t ai_addrlen;
127 char* ai_canonname;
128 struct sockaddr* ai_addr;
129 struct addrinfo* ai_next;
130 int expected_errorno; // expected result
131
132 std::string asParameters() const {
133 return StringPrintf("0x%x/%u/%s/%p/%p", ai_flags, ai_addrlen,
134 ai_canonname ? ai_canonname : "(null)", (void*) ai_addr,
135 (void*) ai_next);
136 }
137 } testConfigs[]{
138 {0, sizeof(struct in_addr) /*bad*/, nullptr, nullptr, nullptr, EAI_BADHINTS},
139 {0, 0, placeholder_cname /*bad*/, nullptr, nullptr, EAI_BADHINTS},
140 {0, 0, nullptr, &placeholder_addr /*bad*/, nullptr, EAI_BADHINTS},
141 {0, 0, nullptr, nullptr, &placeholder_next /*bad*/, EAI_BADHINTS},
142 {AI_ALL /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
143 {AI_V4MAPPED_CFG /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
144 {AI_V4MAPPED /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
145 {AI_DEFAULT /*bad*/, 0, nullptr, nullptr, nullptr, EAI_BADFLAGS},
146 };
147
148 for (const auto& config : testConfigs) {
149 SCOPED_TRACE(config.asParameters());
150
151 // In current test configuration set, ai_family, ai_protocol and ai_socktype are not
152 // checked because other fields cause hints error check failed first.
153 const struct addrinfo hints = {
154 .ai_flags = config.ai_flags,
155 .ai_family = AF_UNSPEC,
156 .ai_socktype = ANY,
157 .ai_protocol = ANY,
158 .ai_addrlen = config.ai_addrlen,
159 .ai_canonname = config.ai_canonname,
160 .ai_addr = config.ai_addr,
161 .ai_next = config.ai_next,
162 };
163
164 rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800165 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800166 EXPECT_EQ(config.expected_errorno, rv);
167
168 if (result) {
169 freeaddrinfo(result);
170 result = nullptr;
171 }
172 }
173}
174
175TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_Family) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800176 for (int family = 0; family < AF_MAX; ++family) {
177 if (family == AF_UNSPEC || family == AF_INET || family == AF_INET6) {
178 continue; // skip supported family
179 }
180 SCOPED_TRACE(StringPrintf("family: %d", family));
181
182 struct addrinfo* result = nullptr;
183 const struct addrinfo hints = {
184 .ai_family = family, // unsupported family
185 };
186
187 int rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800188 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800189 EXPECT_EQ(EAI_FAMILY, rv);
190
191 if (result) freeaddrinfo(result);
192 }
193}
194
195TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_MeaningfulSocktypeAndProtocolCombination) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800196 static const int families[] = {PF_INET, PF_INET6, PF_UNSPEC};
197 // Skip to test socket type SOCK_RAW in meaningful combination (explore_options[]) of
198 // system\netd\resolv\getaddrinfo.cpp. In explore_options[], the socket type SOCK_RAW always
199 // comes with protocol ANY which causes skipping meaningful socktype/protocol combination
200 // check. So it nerver returns error number EAI_BADHINTS which we want to test in this test
201 // case.
202 static const int socktypes[] = {SOCK_STREAM, SOCK_DGRAM};
203
204 // If both socktype/protocol are specified, check non-meaningful combination returns
205 // expected error number EAI_BADHINTS. See meaningful combination in explore_options[] of
206 // system\netd\resolv\getaddrinfo.cpp.
207 for (const auto& family : families) {
208 for (const auto& socktype : socktypes) {
209 for (int protocol = 0; protocol < IPPROTO_MAX; ++protocol) {
210 SCOPED_TRACE(StringPrintf("family: %d, socktype: %d, protocol: %d", family,
211 socktype, protocol));
212
213 // Both socktype/protocol need to be specified.
214 if (!socktype || !protocol) continue;
215
216 // Skip meaningful combination in explore_options[] of
217 // system\netd\resolv\getaddrinfo.cpp.
218 if ((family == AF_INET6 && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
219 (family == AF_INET6 && socktype == SOCK_STREAM && protocol == IPPROTO_TCP) ||
220 (family == AF_INET && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
221 (family == AF_INET && socktype == SOCK_STREAM && protocol == IPPROTO_TCP) ||
222 (family == AF_UNSPEC && socktype == SOCK_DGRAM && protocol == IPPROTO_UDP) ||
223 (family == AF_UNSPEC && socktype == SOCK_STREAM && protocol == IPPROTO_TCP)) {
224 continue;
225 }
226
227 struct addrinfo* result = nullptr;
228 const struct addrinfo hints = {
229 .ai_family = family,
230 .ai_protocol = protocol,
231 .ai_socktype = socktype,
232 };
233
234 int rv = android_getaddrinfofornetcontext("localhost", nullptr /*servname*/, &hints,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800235 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800236 EXPECT_EQ(EAI_BADHINTS, rv);
237
238 if (result) freeaddrinfo(result);
239 }
240 }
241 }
242}
243
244TEST_F(GetAddrInfoForNetContextTest, InvalidParameters_PortNameAndNumber) {
nuccachen5c2d32f2018-11-28 18:19:39 +0800245 constexpr char http_portno[] = "80";
246 constexpr char invalid_portno[] = "65536"; // out of valid port range from 0 to 65535
247 constexpr char http_portname[] = "http";
248 constexpr char invalid_portname[] = "invalid_portname";
249
250 static const struct TestConfig {
251 int ai_flags;
252 int ai_family;
253 int ai_socktype;
254 const char* servname;
255 int expected_errorno; // expected result
256
257 std::string asParameters() const {
258 return StringPrintf("0x%x/%d/%d/%s", ai_flags, ai_family, ai_socktype,
259 servname ? servname : "(null)");
260 }
261 } testConfigs[]{
262 {0, AF_INET, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
263 {0, AF_INET6, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
264 {0, AF_UNSPEC, SOCK_RAW /*bad*/, http_portno, EAI_SERVICE},
265 {0, AF_INET, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
266 {0, AF_INET6, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
267 {0, AF_UNSPEC, SOCK_RDM /*bad*/, http_portno, EAI_SOCKTYPE},
268 {0, AF_INET, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
269 {0, AF_INET6, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
270 {0, AF_UNSPEC, SOCK_SEQPACKET /*bad*/, http_portno, EAI_SOCKTYPE},
271 {0, AF_INET, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
272 {0, AF_INET6, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
273 {0, AF_UNSPEC, SOCK_DCCP /*bad*/, http_portno, EAI_SOCKTYPE},
274 {0, AF_INET, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
275 {0, AF_INET6, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
276 {0, AF_UNSPEC, SOCK_PACKET /*bad*/, http_portno, EAI_SOCKTYPE},
277 {0, AF_INET, ANY, invalid_portno /*bad*/, EAI_SERVICE},
278 {0, AF_INET, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
279 {0, AF_INET, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
280 {0, AF_INET6, ANY, invalid_portno /*bad*/, EAI_SERVICE},
281 {0, AF_INET6, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
282 {0, AF_INET6, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
283 {0, AF_UNSPEC, ANY, invalid_portno /*bad*/, EAI_SERVICE},
284 {0, AF_UNSPEC, SOCK_STREAM, invalid_portno /*bad*/, EAI_SERVICE},
285 {0, AF_UNSPEC, SOCK_DGRAM, invalid_portno /*bad*/, EAI_SERVICE},
286 {AI_NUMERICSERV, AF_INET, ANY, http_portname /*bad*/, EAI_NONAME},
287 {AI_NUMERICSERV, AF_INET, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
288 {AI_NUMERICSERV, AF_INET, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
289 {AI_NUMERICSERV, AF_INET6, ANY, http_portname /*bad*/, EAI_NONAME},
290 {AI_NUMERICSERV, AF_INET6, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
291 {AI_NUMERICSERV, AF_INET6, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
292 {AI_NUMERICSERV, AF_UNSPEC, ANY, http_portname /*bad*/, EAI_NONAME},
293 {AI_NUMERICSERV, AF_UNSPEC, SOCK_STREAM, http_portname /*bad*/, EAI_NONAME},
294 {AI_NUMERICSERV, AF_UNSPEC, SOCK_DGRAM, http_portname /*bad*/, EAI_NONAME},
295 {0, AF_INET, ANY, invalid_portname /*bad*/, EAI_SERVICE},
296 {0, AF_INET, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
297 {0, AF_INET, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
298 {0, AF_INET6, ANY, invalid_portname /*bad*/, EAI_SERVICE},
299 {0, AF_INET6, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
300 {0, AF_INET6, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
301 {0, AF_UNSPEC, ANY, invalid_portname /*bad*/, EAI_SERVICE},
302 {0, AF_UNSPEC, SOCK_STREAM, invalid_portname /*bad*/, EAI_SERVICE},
303 {0, AF_UNSPEC, SOCK_DGRAM, invalid_portname /*bad*/, EAI_SERVICE},
304 };
305
306 for (const auto& config : testConfigs) {
307 const std::string testParameters = config.asParameters();
308 SCOPED_TRACE(testParameters);
309
310 const struct addrinfo hints = {
311 .ai_flags = config.ai_flags,
312 .ai_family = config.ai_family,
313 .ai_socktype = config.ai_socktype,
314 };
315
316 struct addrinfo* result = nullptr;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800317 int rv = android_getaddrinfofornetcontext("localhost", config.servname, &hints,
318 &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800319 EXPECT_EQ(config.expected_errorno, rv);
320
321 if (result) freeaddrinfo(result);
322 }
323}
324
nuccachen5c2d32f2018-11-28 18:19:39 +0800325TEST_F(GetAddrInfoForNetContextTest, AlphabeticalHostname_NoData) {
326 constexpr char listen_addr[] = "127.0.0.3";
327 constexpr char listen_srv[] = "53";
328 constexpr char v4_host_name[] = "v4only.example.com.";
nuccachen5c2d32f2018-11-28 18:19:39 +0800329 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
330 dns.addMapping(v4_host_name, ns_type::ns_t_a, "1.2.3.3");
331 ASSERT_TRUE(dns.startServer());
332 const char* servers[] = {listen_addr};
333 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
334 sizeof(servers) / sizeof(servers[0]),
335 mDefaultSearchDomains, &mDefaultParams_Binder));
336 dns.clearQueries();
337
nuccachen5c2d32f2018-11-28 18:19:39 +0800338 // Want AAAA answer but DNS server has A answer only.
Hungming Chen7f0d3292018-12-27 18:33:19 +0800339 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800340 const addrinfo hints = {.ai_family = AF_INET6};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800341 int rv = android_getaddrinfofornetcontext("v4only", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800342 EXPECT_LE(1U, GetNumQueries(dns, v4_host_name));
343 EXPECT_TRUE(result == nullptr);
344 EXPECT_EQ(EAI_NODATA, rv);
345
346 if (result) freeaddrinfo(result);
347}
nuccachen5c2d32f2018-11-28 18:19:39 +0800348
349TEST_F(GetAddrInfoForNetContextTest, AlphabeticalHostname) {
350 constexpr char listen_addr[] = "127.0.0.3";
351 constexpr char listen_srv[] = "53";
352 constexpr char host_name[] = "sawadee.example.com.";
353 constexpr char v4addr[] = "1.2.3.4";
354 constexpr char v6addr[] = "::1.2.3.4";
355
356 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
357 dns.addMapping(host_name, ns_type::ns_t_a, v4addr);
358 dns.addMapping(host_name, ns_type::ns_t_aaaa, v6addr);
359 ASSERT_TRUE(dns.startServer());
360 const char* servers[] = {listen_addr};
361 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
362 sizeof(servers) / sizeof(servers[0]),
363 mDefaultSearchDomains, &mDefaultParams_Binder));
364
nuccachen5c2d32f2018-11-28 18:19:39 +0800365 static const struct TestConfig {
366 int ai_family;
367 const std::string expected_addr;
368 } testConfigs[]{
369 {AF_INET, v4addr},
370 {AF_INET6, v6addr},
371 };
372
373 for (const auto& config : testConfigs) {
374 SCOPED_TRACE(StringPrintf("family: %d", config.ai_family));
375 dns.clearQueries();
376
377 struct addrinfo* result = nullptr;
378 const struct addrinfo hints = {.ai_family = config.ai_family};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800379 int rv =
380 android_getaddrinfofornetcontext("sawadee", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800381 EXPECT_EQ(0, rv);
382 EXPECT_TRUE(result != nullptr);
383 EXPECT_EQ(1U, GetNumQueries(dns, host_name));
384 EXPECT_EQ(config.expected_addr, ToString(result));
385
386 if (result) freeaddrinfo(result);
387 }
388}
389
390TEST_F(GetAddrInfoForNetContextTest, ServerResponseError) {
391 constexpr char listen_addr[] = "127.0.0.3";
392 constexpr char listen_srv[] = "53";
393 constexpr char host_name[] = "hello.example.com.";
394
Hungming Chen7f0d3292018-12-27 18:33:19 +0800395 static const struct TestConfig {
396 ns_rcode rcode;
397 int expected_errorno; // expected result
398
399 // Only test failure RCODE [1..5] in RFC 1035 section 4.1.1 and skip successful RCODE 0
400 // which means no error.
401 } testConfigs[]{
402 // clang-format off
403 {ns_rcode::ns_r_formerr, EAI_FAIL},
404 {ns_rcode::ns_r_servfail, EAI_AGAIN},
405 {ns_rcode::ns_r_nxdomain, EAI_NODATA},
406 {ns_rcode::ns_r_notimpl, EAI_FAIL},
407 {ns_rcode::ns_r_refused, EAI_FAIL},
408 // clang-format on
nuccachen5c2d32f2018-11-28 18:19:39 +0800409 };
410
Hungming Chen7f0d3292018-12-27 18:33:19 +0800411 for (const auto& config : testConfigs) {
412 SCOPED_TRACE(StringPrintf("rcode: %d", config.rcode));
nuccachen5c2d32f2018-11-28 18:19:39 +0800413
Hungming Chen7f0d3292018-12-27 18:33:19 +0800414 test::DNSResponder dns(listen_addr, listen_srv, 250,
415 config.rcode /*response specific rcode*/);
nuccachen5c2d32f2018-11-28 18:19:39 +0800416 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
417 dns.setResponseProbability(0.0); // always ignore requests and response preset rcode
418 ASSERT_TRUE(dns.startServer());
419 const char* servers[] = {listen_addr};
420 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
421 sizeof(servers) / sizeof(servers[0]),
422 mDefaultSearchDomains, &mDefaultParams_Binder));
423
424 struct addrinfo* result = nullptr;
425 const struct addrinfo hints = {.ai_family = AF_UNSPEC};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800426 int rv =
427 android_getaddrinfofornetcontext(host_name, nullptr, &hints, &mNetcontext, &result);
428 EXPECT_EQ(config.expected_errorno, rv);
nuccachen5c2d32f2018-11-28 18:19:39 +0800429
430 if (result) freeaddrinfo(result);
431 }
432}
433
434// TODO: Add private DNS server timeout test.
435TEST_F(GetAddrInfoForNetContextTest, ServerTimeout) {
436 constexpr char listen_addr[] = "127.0.0.3";
437 constexpr char listen_srv[] = "53";
438 constexpr char host_name[] = "hello.example.com.";
439 test::DNSResponder dns(listen_addr, listen_srv, 250, static_cast<ns_rcode>(-1) /*no response*/);
440 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
441 dns.setResponseProbability(0.0); // always ignore requests and don't response
442 ASSERT_TRUE(dns.startServer());
443 const char* servers[] = {listen_addr};
444 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
445 sizeof(servers) / sizeof(servers[0]),
446 mDefaultSearchDomains, &mDefaultParams_Binder));
447
448 struct addrinfo* result = nullptr;
nuccachen5c2d32f2018-11-28 18:19:39 +0800449 const struct addrinfo hints = {.ai_family = AF_UNSPEC};
Hungming Chen7f0d3292018-12-27 18:33:19 +0800450 int rv = android_getaddrinfofornetcontext("hello", nullptr, &hints, &mNetcontext, &result);
nuccachen5c2d32f2018-11-28 18:19:39 +0800451 EXPECT_EQ(NETD_RESOLV_TIMEOUT, rv);
452
453 if (result) freeaddrinfo(result);
454}
455
Hungming Chen7f0d3292018-12-27 18:33:19 +0800456TEST_F(GetHostByNameForNetContextTest, AlphabeticalHostname) {
457 constexpr char listen_addr[] = "127.0.0.3";
458 constexpr char listen_srv[] = "53";
459 constexpr char host_name[] = "jiababuei.example.com.";
460 constexpr char v4addr[] = "1.2.3.4";
461 constexpr char v6addr[] = "::1.2.3.4";
nuccachen5c2d32f2018-11-28 18:19:39 +0800462
Hungming Chen7f0d3292018-12-27 18:33:19 +0800463 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
464 dns.addMapping(host_name, ns_type::ns_t_a, v4addr);
465 dns.addMapping(host_name, ns_type::ns_t_aaaa, v6addr);
466 ASSERT_TRUE(dns.startServer());
467 const char* servers[] = {listen_addr};
468 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
469 sizeof(servers) / sizeof(servers[0]),
470 mDefaultSearchDomains, &mDefaultParams_Binder));
471
472 static const struct TestConfig {
473 int ai_family;
474 const std::string expected_addr;
475 } testConfigs[]{
476 {AF_INET, v4addr},
477 {AF_INET6, v6addr},
478 };
479
480 for (const auto& config : testConfigs) {
481 SCOPED_TRACE(StringPrintf("family: %d", config.ai_family));
482 dns.clearQueries();
483
484 struct hostent* hp = nullptr;
485 int rv = android_gethostbynamefornetcontext("jiababuei", config.ai_family, &mNetcontext,
486 &hp);
487 EXPECT_EQ(0, rv);
488 EXPECT_TRUE(hp != nullptr);
489 EXPECT_EQ(1U, GetNumQueries(dns, host_name));
490 EXPECT_EQ(config.expected_addr, ToString(hp));
491 }
492}
493
494TEST_F(GetHostByNameForNetContextTest, NoData) {
495 constexpr char listen_addr[] = "127.0.0.3";
496 constexpr char listen_srv[] = "53";
497 constexpr char v4_host_name[] = "v4only.example.com.";
498 test::DNSResponder dns(listen_addr, listen_srv, 250, ns_rcode::ns_r_servfail);
499 dns.addMapping(v4_host_name, ns_type::ns_t_a, "1.2.3.3");
500 ASSERT_TRUE(dns.startServer());
501 const char* servers[] = {listen_addr};
502 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
503 sizeof(servers) / sizeof(servers[0]),
504 mDefaultSearchDomains, &mDefaultParams_Binder));
505 dns.clearQueries();
506
507 // Want AAAA answer but DNS server has A answer only.
508 struct hostent* hp = nullptr;
509 int rv = android_gethostbynamefornetcontext("v4only", AF_INET6, &mNetcontext, &hp);
510 EXPECT_LE(1U, GetNumQueries(dns, v4_host_name));
511 EXPECT_TRUE(hp == nullptr);
512 EXPECT_EQ(EAI_NODATA, rv);
513}
514
515TEST_F(GetHostByNameForNetContextTest, ServerResponseError) {
516 constexpr char listen_addr[] = "127.0.0.3";
517 constexpr char listen_srv[] = "53";
518 constexpr char host_name[] = "hello.example.com.";
519
520 static const struct TestConfig {
521 ns_rcode rcode;
522 int expected_errorno; // expected result
523
524 // Only test failure RCODE [1..5] in RFC 1035 section 4.1.1 and skip successful RCODE 0
525 // which means no error. Note that the return error codes aren't mapped by rcode in the
526 // test case SERVFAIL, NOTIMP and REFUSED. See the comment of res_nsend()
527 // in system\netd\resolv\res_query.cpp for more detail.
528 } testConfigs[]{
529 // clang-format off
530 {ns_rcode::ns_r_formerr, EAI_FAIL},
531 {ns_rcode::ns_r_servfail, EAI_AGAIN}, // Not mapped by rcode.
532 {ns_rcode::ns_r_nxdomain, EAI_NODATA},
533 {ns_rcode::ns_r_notimpl, EAI_AGAIN}, // Not mapped by rcode.
534 {ns_rcode::ns_r_refused, EAI_AGAIN}, // Not mapped by rcode.
535 // clang-format on
536 };
537
538 for (const auto& config : testConfigs) {
539 SCOPED_TRACE(StringPrintf("rcode: %d", config.rcode));
540
541 test::DNSResponder dns(listen_addr, listen_srv, 250,
542 config.rcode /*response specific rcode*/);
543 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
544 dns.setResponseProbability(0.0); // always ignore requests and response preset rcode
545 ASSERT_TRUE(dns.startServer());
546 const char* servers[] = {listen_addr};
547 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
548 sizeof(servers) / sizeof(servers[0]),
549 mDefaultSearchDomains, &mDefaultParams_Binder));
550
551 struct hostent* hp = nullptr;
552 int rv = android_gethostbynamefornetcontext(host_name, AF_INET, &mNetcontext, &hp);
553 EXPECT_TRUE(hp == nullptr);
554 EXPECT_EQ(config.expected_errorno, rv);
555 }
556}
557
558// TODO: Add private DNS server timeout test.
559TEST_F(GetHostByNameForNetContextTest, ServerTimeout) {
560 constexpr char listen_addr[] = "127.0.0.3";
561 constexpr char listen_srv[] = "53";
562 constexpr char host_name[] = "hello.example.com.";
563 test::DNSResponder dns(listen_addr, listen_srv, 250, static_cast<ns_rcode>(-1) /*no response*/);
564 dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.4");
565 dns.setResponseProbability(0.0); // always ignore requests and don't response
566 ASSERT_TRUE(dns.startServer());
567 const char* servers[] = {listen_addr};
568 ASSERT_EQ(0, resolv_set_nameservers_for_net(TEST_NETID, servers,
569 sizeof(servers) / sizeof(servers[0]),
570 mDefaultSearchDomains, &mDefaultParams_Binder));
571
572 struct hostent* hp = nullptr;
573 int rv = android_gethostbynamefornetcontext(host_name, AF_INET, &mNetcontext, &hp);
574 EXPECT_EQ(NETD_RESOLV_TIMEOUT, rv);
575}
576
577// Note that local host file function, files_getaddrinfo(), of android_getaddrinfofornetcontext()
578// is not tested because it only returns a boolean (success or failure) without any error number.
579
580// TODO: Add test NULL hostname, or numeric hostname for android_getaddrinfofornetcontext.
581// TODO: Add test invalid parameters for android_gethostbynamefornetcontext.
582// TODO: Add test for android_gethostbyaddrfornetcontext.
nuccachen5c2d32f2018-11-28 18:19:39 +0800583
584} // end of namespace net
585} // end of namespace android