blob: 3d7235ff354419846265405bff4ff8be7ff005cf [file] [log] [blame]
Hungming Chenf9cd4eb2019-08-06 20:55:28 +09001/*
2 * Copyright (C) 2019 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
18#define LOG_TAG "resolv_gold_test"
19
Hungming Chenc6556622019-10-02 16:01:07 +080020#include <Fwmark.h>
21#include <android-base/chrono_utils.h>
Hungming Chen21c0f832019-09-20 18:38:47 +080022#include <android-base/file.h>
Hungming Chenc4b14162020-03-09 18:02:38 +080023#include <android-base/result.h>
Hungming Chen7b6c23b2019-10-03 17:46:11 +080024#include <android-base/stringprintf.h>
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090025#include <gmock/gmock-matchers.h>
Hungming Chen21c0f832019-09-20 18:38:47 +080026#include <google/protobuf/text_format.h>
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090027#include <gtest/gtest.h>
28
Hungming Chenc6556622019-10-02 16:01:07 +080029#include "PrivateDnsConfiguration.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090030#include "getaddrinfo.h"
Hungming Chen9c748a22019-10-17 18:11:02 +080031#include "gethnamaddr.h"
Hungming Chen21c0f832019-09-20 18:38:47 +080032#include "golddata.pb.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090033#include "resolv_cache.h"
34#include "resolv_test_utils.h"
Hungming Chen9c748a22019-10-17 18:11:02 +080035#include "tests/dns_responder/dns_responder.h"
36#include "tests/dns_responder/dns_responder_client_ndk.h"
Hungming Chenc6556622019-10-02 16:01:07 +080037#include "tests/dns_responder/dns_tls_certificate.h"
38#include "tests/dns_responder/dns_tls_frontend.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090039
Hungming Chen7b6c23b2019-10-03 17:46:11 +080040namespace android::net {
41
Hungming Chenc4b14162020-03-09 18:02:38 +080042using android::base::Result;
Hungming Chen7b6c23b2019-10-03 17:46:11 +080043using android::base::StringPrintf;
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090044using android::netdutils::ScopedAddrinfo;
Hungming Chenc6556622019-10-02 16:01:07 +080045using std::chrono::milliseconds;
46
Hungming Chen7b6c23b2019-10-03 17:46:11 +080047enum class DnsProtocol { CLEARTEXT, TLS };
48
Hungming Chen9c748a22019-10-17 18:11:02 +080049// The buffer size of resolv_gethostbyname().
50// TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h.
51constexpr unsigned int MAXPACKET = 8 * 1024;
52
Hungming Chenc6556622019-10-02 16:01:07 +080053const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/";
Hungming Chen2a56a622019-09-24 17:01:01 +080054const std::vector<std::string> kGoldFilesGetAddrInfo = {
55 "getaddrinfo.topsite.google.pbtxt", "getaddrinfo.topsite.youtube.pbtxt",
56 "getaddrinfo.topsite.amazon.pbtxt", "getaddrinfo.topsite.yahoo.pbtxt",
57 "getaddrinfo.topsite.facebook.pbtxt", "getaddrinfo.topsite.reddit.pbtxt",
58 "getaddrinfo.topsite.wikipedia.pbtxt", "getaddrinfo.topsite.ebay.pbtxt",
59 "getaddrinfo.topsite.netflix.pbtxt", "getaddrinfo.topsite.bing.pbtxt"};
Hungming Chen7b6c23b2019-10-03 17:46:11 +080060const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pbtxt"};
Hungming Chen9c748a22019-10-17 18:11:02 +080061const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pbtxt"};
62const std::vector<std::string> kGoldFilesGetHostByNameTls = {
63 "gethostbyname.tls.topsite.youtube.pbtxt"};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090064
Hungming Chen21c0f832019-09-20 18:38:47 +080065// Fixture test class definition.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090066class TestBase : public ::testing::Test {
67 protected:
68 void SetUp() override {
69 // Create cache for test
70 resolv_create_cache_for_net(TEST_NETID);
71 }
72
73 void TearDown() override {
Hungming Chenc6556622019-10-02 16:01:07 +080074 // Clear TLS configuration for test
75 gPrivateDnsConfiguration.clear(TEST_NETID);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090076 // Delete cache for test
77 resolv_delete_cache_for_net(TEST_NETID);
78 }
79
Hungming Chen9c748a22019-10-17 18:11:02 +080080 void SetResolverConfiguration(const std::vector<std::string>& servers,
81 const std::vector<std::string>& domains,
Hungming Chenc6556622019-10-02 16:01:07 +080082 const std::vector<std::string>& tlsServers = {},
83 const std::string& tlsHostname = "",
84 const std::string& caCert = "") {
85 // Determine the DNS configuration steps from setResolverConfiguration() in
86 // packages/modules/DnsResolver/ResolverController.cpp. The gold test just needs to setup
87 // simply DNS and DNS-over-TLS server configuration. Some implementation in
88 // setResolverConfiguration() are not required. For example, limiting TLS server amount is
89 // not necessary for gold test because gold test has only one TLS server for testing
90 // so far.
91 Fwmark fwmark;
92 fwmark.netId = TEST_NETID;
93 fwmark.explicitlySelected = true;
94 fwmark.protectedFromVpn = true;
95 fwmark.permission = PERMISSION_SYSTEM;
96 ASSERT_EQ(gPrivateDnsConfiguration.set(TEST_NETID, fwmark.intValue, tlsServers, tlsHostname,
97 caCert),
98 0);
99 ASSERT_EQ(resolv_set_nameservers(TEST_NETID, servers, domains, kParams), 0);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900100 }
101
Hungming Chenc6556622019-10-02 16:01:07 +0800102 void SetResolvers() { SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains); }
103
104 void SetResolversWithTls() {
105 // Pass servers as both network-assigned and TLS servers. Tests can
106 // determine on which server and by which protocol queries arrived.
107 // See also DnsClient::SetResolversWithTls() in
108 // packages/modules/DnsResolver/tests/dns_responder/dns_responder_client.h.
109 SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains, kDefaultServers,
110 kDefaultPrivateDnsHostName, kCaCert);
111 }
112
113 bool WaitForPrivateDnsValidation(const std::string& serverAddr) {
114 constexpr milliseconds retryIntervalMs{20};
115 constexpr milliseconds timeoutMs{3000};
116 android::base::Timer t;
117 while (t.duration() < timeoutMs) {
118 const auto& validatedServers =
119 gPrivateDnsConfiguration.getStatus(TEST_NETID).validatedServers();
120 for (const auto& server : validatedServers) {
121 if (serverAddr == ToString(&server.ss)) return true;
122 }
123 std::this_thread::sleep_for(retryIntervalMs);
124 }
125 return false;
126 }
127
Hungming Chenc4b14162020-03-09 18:02:38 +0800128 Result<GoldTest> ToProto(const std::string& filename) {
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800129 // Convert the testing configuration from .pbtxt file to proto.
Hungming Chenc4b14162020-03-09 18:02:38 +0800130 std::string content;
131 const std::string path = kTestDataPath + filename;
132
133 bool ret = android::base::ReadFileToString(path, &content);
134 if (!ret) return Errorf("Read {} failed: {}", path, strerror(errno));
135
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800136 android::net::GoldTest goldtest;
Hungming Chenc4b14162020-03-09 18:02:38 +0800137 ret = google::protobuf::TextFormat::ParseFromString(content, &goldtest);
138 if (!ret) return Errorf("Parse {} failed", path);
139
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800140 return goldtest;
141 }
142
143 void SetupMappings(const android::net::GoldTest& goldtest, test::DNSResponder& dns) {
144 for (const auto& m : goldtest.packet_mapping()) {
145 // Convert string to bytes because .proto type "bytes" is "string" type in C++.
146 // See also the section "Scalar Value Types" in "Language Guide (proto3)".
147 // TODO: Use C++20 std::span in addMappingBinaryPacket. It helps to take both
148 // std::string and std::vector without conversions.
149 dns.addMappingBinaryPacket(
150 std::vector<uint8_t>(m.query().begin(), m.query().end()),
151 std::vector<uint8_t>(m.response().begin(), m.response().end()));
152 }
153 }
154
155 android_net_context GetNetContext(const DnsProtocol protocol) {
156 return protocol == DnsProtocol::TLS ? kNetcontextTls : kNetcontext;
157 }
158
159 template <class AddressType>
160 void VerifyAddress(const android::net::GoldTest& goldtest, const AddressType& result) {
161 if (goldtest.result().return_code() != GT_EAI_NO_ERROR) {
162 EXPECT_EQ(result, nullptr);
163 } else {
164 ASSERT_NE(result, nullptr);
165 const auto& addresses = goldtest.result().addresses();
166 EXPECT_THAT(ToStrings(result), ::testing::UnorderedElementsAreArray(addresses));
167 }
168 }
169
170 void VerifyGetAddrInfo(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
171 ASSERT_TRUE(goldtest.config().has_addrinfo());
172 const auto& args = goldtest.config().addrinfo();
173 const addrinfo hints = {
174 // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at
175 // whether connectivity is available. It makes that the resolver may send only A
176 // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also
177 // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp.
178 // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit
179 // test can treat the IPv4 and IPv6 connectivity.
180 .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG,
181 .ai_family = args.family(),
182 .ai_socktype = args.socktype(),
183 .ai_protocol = args.protocol(),
184 };
185 addrinfo* res = nullptr;
186 const android_net_context netcontext = GetNetContext(protocol);
187 NetworkDnsEventReported event;
188 const int rv =
189 resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &netcontext, &res, &event);
190 ScopedAddrinfo result(res);
191 ASSERT_EQ(rv, goldtest.result().return_code());
192 VerifyAddress(goldtest, result);
193 }
194
Hungming Chen9c748a22019-10-17 18:11:02 +0800195 void VerifyGetHostByName(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
196 ASSERT_TRUE(goldtest.config().has_hostbyname());
197 const auto& args = goldtest.config().hostbyname();
198 hostent* hp = nullptr;
199 hostent hbuf;
200 char tmpbuf[MAXPACKET];
201 const android_net_context netcontext = GetNetContext(protocol);
202 NetworkDnsEventReported event;
203 const int rv = resolv_gethostbyname(args.host().c_str(), args.family(), &hbuf, tmpbuf,
204 sizeof(tmpbuf), &netcontext, &hp, &event);
205 ASSERT_EQ(rv, goldtest.result().return_code());
206 VerifyAddress(goldtest, hp);
207 }
208
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800209 void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns,
210 const test::DnsTlsFrontend& tls, const DnsProtocol protocol) {
211 size_t queries;
212 std::string name;
213
214 // Verify DNS query calls and results by proto. Then, determine expected query times and
215 // queried name for checking server query status later.
216 switch (const auto calltype = goldtest.config().call()) {
217 case android::net::CallType::CALL_GETADDRINFO:
218 ASSERT_TRUE(goldtest.config().has_addrinfo());
219 ASSERT_NO_FATAL_FAILURE(VerifyGetAddrInfo(goldtest, protocol));
220 queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U;
221 name = goldtest.config().addrinfo().host();
222 break;
Hungming Chen9c748a22019-10-17 18:11:02 +0800223 case android::net::CallType::CALL_GETHOSTBYNAME:
224 ASSERT_TRUE(goldtest.config().has_hostbyname());
225 ASSERT_NO_FATAL_FAILURE(VerifyGetHostByName(goldtest, protocol));
226 queries = 1U;
227 name = goldtest.config().hostbyname().host();
228 break;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800229 default:
230 FAIL() << "Unsupported call type: " << calltype;
231 }
232
233 // Verify DNS server query status.
234 EXPECT_EQ(GetNumQueries(dns, name.c_str()), queries);
235 if (protocol == DnsProtocol::TLS) EXPECT_EQ(tls.queries(), static_cast<int>(queries));
236 }
237
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900238 static constexpr res_params kParams = {
239 .sample_validity = 300,
240 .success_threshold = 25,
241 .min_samples = 8,
242 .max_samples = 8,
243 .base_timeout_msec = 1000,
244 .retry_count = 2,
245 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900246 static constexpr android_net_context kNetcontext = {
247 .app_netid = TEST_NETID,
248 .app_mark = MARK_UNSET,
249 .dns_netid = TEST_NETID,
250 .dns_mark = MARK_UNSET,
251 .uid = NET_CONTEXT_INVALID_UID,
252 };
Hungming Chenc6556622019-10-02 16:01:07 +0800253 static constexpr android_net_context kNetcontextTls = {
254 .app_netid = TEST_NETID,
255 .app_mark = MARK_UNSET,
256 .dns_netid = TEST_NETID,
257 .dns_mark = MARK_UNSET,
258 .uid = NET_CONTEXT_INVALID_UID,
259 // Set TLS flags. See also maybeFixupNetContext() in
260 // packages/modules/DnsResolver/DnsProxyListener.cpp.
261 .flags = NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS,
262 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900263};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900264class ResolvGetAddrInfo : public TestBase {};
265
Hungming Chen21c0f832019-09-20 18:38:47 +0800266// Fixture tests.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900267TEST_F(ResolvGetAddrInfo, RemovePacketMapping) {
268 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
269 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800270 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900271
272 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
273
274 addrinfo* res = nullptr;
275 const addrinfo hints = {.ai_family = AF_INET};
276 NetworkDnsEventReported event;
277 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
278 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800279 ASSERT_NE(result, nullptr);
280 ASSERT_EQ(rv, 0);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800281 EXPECT_EQ(ToString(result), kHelloExampleComAddrV4);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900282
283 // Remove existing DNS record.
284 dns.removeMappingBinaryPacket(kHelloExampleComQueryV4);
285
286 // Expect to have no answer in DNS query result.
287 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
288 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800289 ASSERT_EQ(result, nullptr);
290 ASSERT_EQ(rv, EAI_NODATA);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900291}
292
293TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) {
294 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
295 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800296 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900297
298 // Register the record which uses IPv4 address 1.2.3.4.
299 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
300
301 // Expect that the DNS query returns IPv4 address 1.2.3.4.
302 addrinfo* res = nullptr;
303 const addrinfo hints = {.ai_family = AF_INET};
304 NetworkDnsEventReported event;
305 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
306 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800307 ASSERT_NE(result, nullptr);
308 ASSERT_EQ(rv, 0);
309 EXPECT_EQ(ToString(result), "1.2.3.4");
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900310
311 // Replace the registered record with a record which uses new IPv4 address 5.6.7.8.
312 std::vector<uint8_t> newHelloExampleComResponseV4 = {
313 /* Header */
314 0x00, 0x00, /* Transaction ID: 0x0000 */
315 0x81, 0x80, /* Flags: qr rd ra */
316 0x00, 0x01, /* Questions: 1 */
317 0x00, 0x01, /* Answer RRs: 1 */
318 0x00, 0x00, /* Authority RRs: 0 */
319 0x00, 0x00, /* Additional RRs: 0 */
320 /* Queries */
321 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
322 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
323 0x00, 0x01, /* Type: A */
324 0x00, 0x01, /* Class: IN */
325 /* Answers */
326 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
327 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
328 0x00, 0x01, /* Type: A */
329 0x00, 0x01, /* Class: IN */
330 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
331 0x00, 0x04, /* Data length: 4 */
332 0x05, 0x06, 0x07, 0x08 /* Address: 5.6.7.8 */
333 };
334 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, newHelloExampleComResponseV4);
335
336 // Expect that DNS query returns new IPv4 address 5.6.7.8.
337 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
338 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800339 ASSERT_NE(result, nullptr);
340 ASSERT_EQ(rv, 0);
341 EXPECT_EQ(ToString(result), "5.6.7.8");
342}
343
344TEST_F(ResolvGetAddrInfo, BasicTlsQuery) {
345 test::DNSResponder dns;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800346 dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4);
347 dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6);
Hungming Chenc6556622019-10-02 16:01:07 +0800348 ASSERT_TRUE(dns.startServer());
349
350 test::DnsTlsFrontend tls;
351 ASSERT_TRUE(tls.startServer());
352 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
353 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
354
355 dns.clearQueries();
356 addrinfo* res = nullptr;
357 // If the socket type is not specified, every address will appear twice, once for
358 // SOCK_STREAM and one for SOCK_DGRAM. Just pick one because the addresses for
359 // the second query of different socket type are responded by the cache.
360 const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM};
361 NetworkDnsEventReported event;
362 const int rv =
363 resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontextTls, &res, &event);
364 ScopedAddrinfo result(res);
365 ASSERT_EQ(rv, 0);
366 EXPECT_EQ(GetNumQueries(dns, kHelloExampleCom), 2U);
367 const std::vector<std::string> result_strs = ToStrings(result);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800368 EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray(
369 {kHelloExampleComAddrV4, kHelloExampleComAddrV6}));
Hungming Chenc6556622019-10-02 16:01:07 +0800370 EXPECT_EQ(tls.queries(), 3);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900371}
372
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800373// Parameterized test class definition.
374using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>;
375class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<GoldTestParamType> {
376 public:
377 // Generate readable string for test name from test parameters.
378 static std::string Name(::testing::TestParamInfo<GoldTestParamType> info) {
379 const auto& [protocol, file] = info.param;
380 std::string name = StringPrintf(
381 "%s_%s", protocol == DnsProtocol::CLEARTEXT ? "CLEARTEXT" : "TLS", file.c_str());
382 std::replace_if(
383 std::begin(name), std::end(name), [](char ch) { return !std::isalnum(ch); }, '_');
384 return name;
385 }
386};
387
388// GetAddrInfo tests.
389INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest,
390 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
391 ::testing::ValuesIn(kGoldFilesGetAddrInfo)),
392 ResolvGoldTest::Name);
393INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest,
394 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
395 ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)),
396 ResolvGoldTest::Name);
397
Hungming Chen9c748a22019-10-17 18:11:02 +0800398// GetHostByName tests.
399INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest,
400 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
401 ::testing::ValuesIn(kGoldFilesGetHostByName)),
402 ResolvGoldTest::Name);
403INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest,
404 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
405 ::testing::ValuesIn(kGoldFilesGetHostByNameTls)),
406 ResolvGoldTest::Name);
407
Hungming Chen21c0f832019-09-20 18:38:47 +0800408TEST_P(ResolvGoldTest, GoldData) {
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800409 const auto& [protocol, file] = GetParam();
Hungming Chen21c0f832019-09-20 18:38:47 +0800410
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800411 // Setup DNS server configuration.
Hungming Chen21c0f832019-09-20 18:38:47 +0800412 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
413 ASSERT_TRUE(dns.startServer());
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800414 test::DnsTlsFrontend tls;
Hungming Chen21c0f832019-09-20 18:38:47 +0800415
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800416 if (protocol == DnsProtocol::CLEARTEXT) {
417 ASSERT_NO_FATAL_FAILURE(SetResolvers());
418 } else if (protocol == DnsProtocol::TLS) {
419 ASSERT_TRUE(tls.startServer());
420 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
421 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
422 tls.clearQueries();
Hungming Chen21c0f832019-09-20 18:38:47 +0800423 }
424
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800425 // Read test configuration from proto text file to proto.
Hungming Chenc4b14162020-03-09 18:02:38 +0800426 const Result<GoldTest> result = ToProto(file);
427 ASSERT_TRUE(result.ok()) << result.error().message();
428 const GoldTest& goldtest = result.value();
Hungming Chen21c0f832019-09-20 18:38:47 +0800429
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800430 // Register packet mappings (query, response) from proto.
431 SetupMappings(goldtest, dns);
432
433 // Verify the resolver by proto.
434 VerifyResolver(goldtest, dns, tls, protocol);
Hungming Chen21c0f832019-09-20 18:38:47 +0800435}
436
Bernie Innocenticebc76d2019-12-13 11:55:17 +0900437} // namespace android::net