blob: 5216268695fdb4b5b7b53b3a958f8233ddd70c71 [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 Chen7b6c23b2019-10-03 17:46:11 +080023#include <android-base/stringprintf.h>
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090024#include <gmock/gmock-matchers.h>
Hungming Chen21c0f832019-09-20 18:38:47 +080025#include <google/protobuf/text_format.h>
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090026#include <gtest/gtest.h>
27
Hungming Chenc6556622019-10-02 16:01:07 +080028#include "PrivateDnsConfiguration.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090029#include "getaddrinfo.h"
Hungming Chen9c748a22019-10-17 18:11:02 +080030#include "gethnamaddr.h"
Hungming Chen21c0f832019-09-20 18:38:47 +080031#include "golddata.pb.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090032#include "resolv_cache.h"
33#include "resolv_test_utils.h"
Hungming Chen9c748a22019-10-17 18:11:02 +080034#include "tests/dns_responder/dns_responder.h"
35#include "tests/dns_responder/dns_responder_client_ndk.h"
Hungming Chenc6556622019-10-02 16:01:07 +080036#include "tests/dns_responder/dns_tls_certificate.h"
37#include "tests/dns_responder/dns_tls_frontend.h"
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090038
Hungming Chen7b6c23b2019-10-03 17:46:11 +080039namespace android::net {
40
41using android::base::StringPrintf;
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090042using android::netdutils::ScopedAddrinfo;
Hungming Chenc6556622019-10-02 16:01:07 +080043using std::chrono::milliseconds;
44
Hungming Chen7b6c23b2019-10-03 17:46:11 +080045enum class DnsProtocol { CLEARTEXT, TLS };
46
Hungming Chen9c748a22019-10-17 18:11:02 +080047// The buffer size of resolv_gethostbyname().
48// TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h.
49constexpr unsigned int MAXPACKET = 8 * 1024;
50
Hungming Chenc6556622019-10-02 16:01:07 +080051const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/";
Hungming Chen2a56a622019-09-24 17:01:01 +080052const std::vector<std::string> kGoldFilesGetAddrInfo = {
53 "getaddrinfo.topsite.google.pbtxt", "getaddrinfo.topsite.youtube.pbtxt",
54 "getaddrinfo.topsite.amazon.pbtxt", "getaddrinfo.topsite.yahoo.pbtxt",
55 "getaddrinfo.topsite.facebook.pbtxt", "getaddrinfo.topsite.reddit.pbtxt",
56 "getaddrinfo.topsite.wikipedia.pbtxt", "getaddrinfo.topsite.ebay.pbtxt",
57 "getaddrinfo.topsite.netflix.pbtxt", "getaddrinfo.topsite.bing.pbtxt"};
Hungming Chen7b6c23b2019-10-03 17:46:11 +080058const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pbtxt"};
Hungming Chen9c748a22019-10-17 18:11:02 +080059const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pbtxt"};
60const std::vector<std::string> kGoldFilesGetHostByNameTls = {
61 "gethostbyname.tls.topsite.youtube.pbtxt"};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090062
Hungming Chen21c0f832019-09-20 18:38:47 +080063// Fixture test class definition.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090064class TestBase : public ::testing::Test {
65 protected:
66 void SetUp() override {
67 // Create cache for test
68 resolv_create_cache_for_net(TEST_NETID);
69 }
70
71 void TearDown() override {
Hungming Chenc6556622019-10-02 16:01:07 +080072 // Clear TLS configuration for test
73 gPrivateDnsConfiguration.clear(TEST_NETID);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090074 // Delete cache for test
75 resolv_delete_cache_for_net(TEST_NETID);
76 }
77
Hungming Chen9c748a22019-10-17 18:11:02 +080078 void SetResolverConfiguration(const std::vector<std::string>& servers,
79 const std::vector<std::string>& domains,
Hungming Chenc6556622019-10-02 16:01:07 +080080 const std::vector<std::string>& tlsServers = {},
81 const std::string& tlsHostname = "",
82 const std::string& caCert = "") {
83 // Determine the DNS configuration steps from setResolverConfiguration() in
84 // packages/modules/DnsResolver/ResolverController.cpp. The gold test just needs to setup
85 // simply DNS and DNS-over-TLS server configuration. Some implementation in
86 // setResolverConfiguration() are not required. For example, limiting TLS server amount is
87 // not necessary for gold test because gold test has only one TLS server for testing
88 // so far.
89 Fwmark fwmark;
90 fwmark.netId = TEST_NETID;
91 fwmark.explicitlySelected = true;
92 fwmark.protectedFromVpn = true;
93 fwmark.permission = PERMISSION_SYSTEM;
94 ASSERT_EQ(gPrivateDnsConfiguration.set(TEST_NETID, fwmark.intValue, tlsServers, tlsHostname,
95 caCert),
96 0);
97 ASSERT_EQ(resolv_set_nameservers(TEST_NETID, servers, domains, kParams), 0);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090098 }
99
Hungming Chenc6556622019-10-02 16:01:07 +0800100 void SetResolvers() { SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains); }
101
102 void SetResolversWithTls() {
103 // Pass servers as both network-assigned and TLS servers. Tests can
104 // determine on which server and by which protocol queries arrived.
105 // See also DnsClient::SetResolversWithTls() in
106 // packages/modules/DnsResolver/tests/dns_responder/dns_responder_client.h.
107 SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains, kDefaultServers,
108 kDefaultPrivateDnsHostName, kCaCert);
109 }
110
111 bool WaitForPrivateDnsValidation(const std::string& serverAddr) {
112 constexpr milliseconds retryIntervalMs{20};
113 constexpr milliseconds timeoutMs{3000};
114 android::base::Timer t;
115 while (t.duration() < timeoutMs) {
116 const auto& validatedServers =
117 gPrivateDnsConfiguration.getStatus(TEST_NETID).validatedServers();
118 for (const auto& server : validatedServers) {
119 if (serverAddr == ToString(&server.ss)) return true;
120 }
121 std::this_thread::sleep_for(retryIntervalMs);
122 }
123 return false;
124 }
125
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800126 GoldTest ToProto(const std::string& file) {
127 // Convert the testing configuration from .pbtxt file to proto.
128 std::string file_content;
Bernie Innocenticebc76d2019-12-13 11:55:17 +0900129 const std::string file_name = kTestDataPath + file;
130 EXPECT_TRUE(android::base::ReadFileToString(file_name, &file_content))
131 << "Failed to read " << file_name << ": " << strerror(errno);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800132 android::net::GoldTest goldtest;
133 EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(file_content, &goldtest));
134 return goldtest;
135 }
136
137 void SetupMappings(const android::net::GoldTest& goldtest, test::DNSResponder& dns) {
138 for (const auto& m : goldtest.packet_mapping()) {
139 // Convert string to bytes because .proto type "bytes" is "string" type in C++.
140 // See also the section "Scalar Value Types" in "Language Guide (proto3)".
141 // TODO: Use C++20 std::span in addMappingBinaryPacket. It helps to take both
142 // std::string and std::vector without conversions.
143 dns.addMappingBinaryPacket(
144 std::vector<uint8_t>(m.query().begin(), m.query().end()),
145 std::vector<uint8_t>(m.response().begin(), m.response().end()));
146 }
147 }
148
149 android_net_context GetNetContext(const DnsProtocol protocol) {
150 return protocol == DnsProtocol::TLS ? kNetcontextTls : kNetcontext;
151 }
152
153 template <class AddressType>
154 void VerifyAddress(const android::net::GoldTest& goldtest, const AddressType& result) {
155 if (goldtest.result().return_code() != GT_EAI_NO_ERROR) {
156 EXPECT_EQ(result, nullptr);
157 } else {
158 ASSERT_NE(result, nullptr);
159 const auto& addresses = goldtest.result().addresses();
160 EXPECT_THAT(ToStrings(result), ::testing::UnorderedElementsAreArray(addresses));
161 }
162 }
163
164 void VerifyGetAddrInfo(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
165 ASSERT_TRUE(goldtest.config().has_addrinfo());
166 const auto& args = goldtest.config().addrinfo();
167 const addrinfo hints = {
168 // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at
169 // whether connectivity is available. It makes that the resolver may send only A
170 // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also
171 // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp.
172 // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit
173 // test can treat the IPv4 and IPv6 connectivity.
174 .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG,
175 .ai_family = args.family(),
176 .ai_socktype = args.socktype(),
177 .ai_protocol = args.protocol(),
178 };
179 addrinfo* res = nullptr;
180 const android_net_context netcontext = GetNetContext(protocol);
181 NetworkDnsEventReported event;
182 const int rv =
183 resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &netcontext, &res, &event);
184 ScopedAddrinfo result(res);
185 ASSERT_EQ(rv, goldtest.result().return_code());
186 VerifyAddress(goldtest, result);
187 }
188
Hungming Chen9c748a22019-10-17 18:11:02 +0800189 void VerifyGetHostByName(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
190 ASSERT_TRUE(goldtest.config().has_hostbyname());
191 const auto& args = goldtest.config().hostbyname();
192 hostent* hp = nullptr;
193 hostent hbuf;
194 char tmpbuf[MAXPACKET];
195 const android_net_context netcontext = GetNetContext(protocol);
196 NetworkDnsEventReported event;
197 const int rv = resolv_gethostbyname(args.host().c_str(), args.family(), &hbuf, tmpbuf,
198 sizeof(tmpbuf), &netcontext, &hp, &event);
199 ASSERT_EQ(rv, goldtest.result().return_code());
200 VerifyAddress(goldtest, hp);
201 }
202
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800203 void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns,
204 const test::DnsTlsFrontend& tls, const DnsProtocol protocol) {
205 size_t queries;
206 std::string name;
207
208 // Verify DNS query calls and results by proto. Then, determine expected query times and
209 // queried name for checking server query status later.
210 switch (const auto calltype = goldtest.config().call()) {
211 case android::net::CallType::CALL_GETADDRINFO:
212 ASSERT_TRUE(goldtest.config().has_addrinfo());
213 ASSERT_NO_FATAL_FAILURE(VerifyGetAddrInfo(goldtest, protocol));
214 queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U;
215 name = goldtest.config().addrinfo().host();
216 break;
Hungming Chen9c748a22019-10-17 18:11:02 +0800217 case android::net::CallType::CALL_GETHOSTBYNAME:
218 ASSERT_TRUE(goldtest.config().has_hostbyname());
219 ASSERT_NO_FATAL_FAILURE(VerifyGetHostByName(goldtest, protocol));
220 queries = 1U;
221 name = goldtest.config().hostbyname().host();
222 break;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800223 default:
224 FAIL() << "Unsupported call type: " << calltype;
225 }
226
227 // Verify DNS server query status.
228 EXPECT_EQ(GetNumQueries(dns, name.c_str()), queries);
229 if (protocol == DnsProtocol::TLS) EXPECT_EQ(tls.queries(), static_cast<int>(queries));
230 }
231
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900232 static constexpr res_params kParams = {
233 .sample_validity = 300,
234 .success_threshold = 25,
235 .min_samples = 8,
236 .max_samples = 8,
237 .base_timeout_msec = 1000,
238 .retry_count = 2,
239 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900240 static constexpr android_net_context kNetcontext = {
241 .app_netid = TEST_NETID,
242 .app_mark = MARK_UNSET,
243 .dns_netid = TEST_NETID,
244 .dns_mark = MARK_UNSET,
245 .uid = NET_CONTEXT_INVALID_UID,
246 };
Hungming Chenc6556622019-10-02 16:01:07 +0800247 static constexpr android_net_context kNetcontextTls = {
248 .app_netid = TEST_NETID,
249 .app_mark = MARK_UNSET,
250 .dns_netid = TEST_NETID,
251 .dns_mark = MARK_UNSET,
252 .uid = NET_CONTEXT_INVALID_UID,
253 // Set TLS flags. See also maybeFixupNetContext() in
254 // packages/modules/DnsResolver/DnsProxyListener.cpp.
255 .flags = NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS,
256 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900257};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900258class ResolvGetAddrInfo : public TestBase {};
259
Hungming Chen21c0f832019-09-20 18:38:47 +0800260// Fixture tests.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900261TEST_F(ResolvGetAddrInfo, RemovePacketMapping) {
262 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
263 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800264 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900265
266 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
267
268 addrinfo* res = nullptr;
269 const addrinfo hints = {.ai_family = AF_INET};
270 NetworkDnsEventReported event;
271 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
272 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800273 ASSERT_NE(result, nullptr);
274 ASSERT_EQ(rv, 0);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800275 EXPECT_EQ(ToString(result), kHelloExampleComAddrV4);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900276
277 // Remove existing DNS record.
278 dns.removeMappingBinaryPacket(kHelloExampleComQueryV4);
279
280 // Expect to have no answer in DNS query result.
281 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
282 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800283 ASSERT_EQ(result, nullptr);
284 ASSERT_EQ(rv, EAI_NODATA);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900285}
286
287TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) {
288 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
289 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800290 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900291
292 // Register the record which uses IPv4 address 1.2.3.4.
293 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
294
295 // Expect that the DNS query returns IPv4 address 1.2.3.4.
296 addrinfo* res = nullptr;
297 const addrinfo hints = {.ai_family = AF_INET};
298 NetworkDnsEventReported event;
299 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
300 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800301 ASSERT_NE(result, nullptr);
302 ASSERT_EQ(rv, 0);
303 EXPECT_EQ(ToString(result), "1.2.3.4");
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900304
305 // Replace the registered record with a record which uses new IPv4 address 5.6.7.8.
306 std::vector<uint8_t> newHelloExampleComResponseV4 = {
307 /* Header */
308 0x00, 0x00, /* Transaction ID: 0x0000 */
309 0x81, 0x80, /* Flags: qr rd ra */
310 0x00, 0x01, /* Questions: 1 */
311 0x00, 0x01, /* Answer RRs: 1 */
312 0x00, 0x00, /* Authority RRs: 0 */
313 0x00, 0x00, /* Additional RRs: 0 */
314 /* Queries */
315 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
316 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
317 0x00, 0x01, /* Type: A */
318 0x00, 0x01, /* Class: IN */
319 /* Answers */
320 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
321 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
322 0x00, 0x01, /* Type: A */
323 0x00, 0x01, /* Class: IN */
324 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
325 0x00, 0x04, /* Data length: 4 */
326 0x05, 0x06, 0x07, 0x08 /* Address: 5.6.7.8 */
327 };
328 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, newHelloExampleComResponseV4);
329
330 // Expect that DNS query returns new IPv4 address 5.6.7.8.
331 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
332 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800333 ASSERT_NE(result, nullptr);
334 ASSERT_EQ(rv, 0);
335 EXPECT_EQ(ToString(result), "5.6.7.8");
336}
337
338TEST_F(ResolvGetAddrInfo, BasicTlsQuery) {
339 test::DNSResponder dns;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800340 dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4);
341 dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6);
Hungming Chenc6556622019-10-02 16:01:07 +0800342 ASSERT_TRUE(dns.startServer());
343
344 test::DnsTlsFrontend tls;
345 ASSERT_TRUE(tls.startServer());
346 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
347 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
348
349 dns.clearQueries();
350 addrinfo* res = nullptr;
351 // If the socket type is not specified, every address will appear twice, once for
352 // SOCK_STREAM and one for SOCK_DGRAM. Just pick one because the addresses for
353 // the second query of different socket type are responded by the cache.
354 const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM};
355 NetworkDnsEventReported event;
356 const int rv =
357 resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontextTls, &res, &event);
358 ScopedAddrinfo result(res);
359 ASSERT_EQ(rv, 0);
360 EXPECT_EQ(GetNumQueries(dns, kHelloExampleCom), 2U);
361 const std::vector<std::string> result_strs = ToStrings(result);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800362 EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray(
363 {kHelloExampleComAddrV4, kHelloExampleComAddrV6}));
Hungming Chenc6556622019-10-02 16:01:07 +0800364 EXPECT_EQ(tls.queries(), 3);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900365}
366
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800367// Parameterized test class definition.
368using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>;
369class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<GoldTestParamType> {
370 public:
371 // Generate readable string for test name from test parameters.
372 static std::string Name(::testing::TestParamInfo<GoldTestParamType> info) {
373 const auto& [protocol, file] = info.param;
374 std::string name = StringPrintf(
375 "%s_%s", protocol == DnsProtocol::CLEARTEXT ? "CLEARTEXT" : "TLS", file.c_str());
376 std::replace_if(
377 std::begin(name), std::end(name), [](char ch) { return !std::isalnum(ch); }, '_');
378 return name;
379 }
380};
381
382// GetAddrInfo tests.
383INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest,
384 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
385 ::testing::ValuesIn(kGoldFilesGetAddrInfo)),
386 ResolvGoldTest::Name);
387INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest,
388 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
389 ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)),
390 ResolvGoldTest::Name);
391
Hungming Chen9c748a22019-10-17 18:11:02 +0800392// GetHostByName tests.
393INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest,
394 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
395 ::testing::ValuesIn(kGoldFilesGetHostByName)),
396 ResolvGoldTest::Name);
397INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest,
398 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
399 ::testing::ValuesIn(kGoldFilesGetHostByNameTls)),
400 ResolvGoldTest::Name);
401
Hungming Chen21c0f832019-09-20 18:38:47 +0800402TEST_P(ResolvGoldTest, GoldData) {
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800403 const auto& [protocol, file] = GetParam();
Hungming Chen21c0f832019-09-20 18:38:47 +0800404
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800405 // Setup DNS server configuration.
Hungming Chen21c0f832019-09-20 18:38:47 +0800406 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
407 ASSERT_TRUE(dns.startServer());
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800408 test::DnsTlsFrontend tls;
Hungming Chen21c0f832019-09-20 18:38:47 +0800409
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800410 if (protocol == DnsProtocol::CLEARTEXT) {
411 ASSERT_NO_FATAL_FAILURE(SetResolvers());
412 } else if (protocol == DnsProtocol::TLS) {
413 ASSERT_TRUE(tls.startServer());
414 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
415 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
416 tls.clearQueries();
Hungming Chen21c0f832019-09-20 18:38:47 +0800417 }
418
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800419 // Read test configuration from proto text file to proto.
420 const auto goldtest = ToProto(file);
Hungming Chen21c0f832019-09-20 18:38:47 +0800421
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800422 // Register packet mappings (query, response) from proto.
423 SetupMappings(goldtest, dns);
424
425 // Verify the resolver by proto.
426 VerifyResolver(goldtest, dns, tls, protocol);
Hungming Chen21c0f832019-09-20 18:38:47 +0800427}
428
Bernie Innocenticebc76d2019-12-13 11:55:17 +0900429} // namespace android::net