blob: 99bcb924d394e3bada8b7590034745b7e82fba21 [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>
26#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
Hungming Chenc4b14162020-03-09 18:02:38 +080041using android::base::Result;
Hungming Chen7b6c23b2019-10-03 17:46:11 +080042using android::base::StringPrintf;
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090043using android::netdutils::ScopedAddrinfo;
Hungming Chenc6556622019-10-02 16:01:07 +080044using std::chrono::milliseconds;
45
Hungming Chen7b6c23b2019-10-03 17:46:11 +080046enum class DnsProtocol { CLEARTEXT, TLS };
47
Hungming Chen9c748a22019-10-17 18:11:02 +080048// The buffer size of resolv_gethostbyname().
49// TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h.
50constexpr unsigned int MAXPACKET = 8 * 1024;
51
Hungming Chen6cb2be02020-03-06 15:25:25 +080052// The testdata/pb/*.pb are generated from testdata/*.pbtext.
53// TODO: Generate .pb files via precompiler.
54const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/pb/";
Hungming Chen2a56a622019-09-24 17:01:01 +080055const std::vector<std::string> kGoldFilesGetAddrInfo = {
Hungming Chen6cb2be02020-03-06 15:25:25 +080056 "getaddrinfo.topsite.google.pb", "getaddrinfo.topsite.youtube.pb",
57 "getaddrinfo.topsite.amazon.pb", "getaddrinfo.topsite.yahoo.pb",
58 "getaddrinfo.topsite.facebook.pb", "getaddrinfo.topsite.reddit.pb",
59 "getaddrinfo.topsite.wikipedia.pb", "getaddrinfo.topsite.ebay.pb",
60 "getaddrinfo.topsite.netflix.pb", "getaddrinfo.topsite.bing.pb"};
61const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pb"};
62const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pb"};
Hungming Chen9c748a22019-10-17 18:11:02 +080063const std::vector<std::string> kGoldFilesGetHostByNameTls = {
Hungming Chen6cb2be02020-03-06 15:25:25 +080064 "gethostbyname.tls.topsite.youtube.pb"};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090065
Hungming Chen21c0f832019-09-20 18:38:47 +080066// Fixture test class definition.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090067class TestBase : public ::testing::Test {
68 protected:
69 void SetUp() override {
70 // Create cache for test
71 resolv_create_cache_for_net(TEST_NETID);
72 }
73
74 void TearDown() override {
Hungming Chenc6556622019-10-02 16:01:07 +080075 // Clear TLS configuration for test
76 gPrivateDnsConfiguration.clear(TEST_NETID);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +090077 // Delete cache for test
78 resolv_delete_cache_for_net(TEST_NETID);
79 }
80
Hungming Chen9c748a22019-10-17 18:11:02 +080081 void SetResolverConfiguration(const std::vector<std::string>& servers,
82 const std::vector<std::string>& domains,
Hungming Chenc6556622019-10-02 16:01:07 +080083 const std::vector<std::string>& tlsServers = {},
84 const std::string& tlsHostname = "",
85 const std::string& caCert = "") {
86 // Determine the DNS configuration steps from setResolverConfiguration() in
87 // packages/modules/DnsResolver/ResolverController.cpp. The gold test just needs to setup
88 // simply DNS and DNS-over-TLS server configuration. Some implementation in
89 // setResolverConfiguration() are not required. For example, limiting TLS server amount is
90 // not necessary for gold test because gold test has only one TLS server for testing
91 // so far.
92 Fwmark fwmark;
93 fwmark.netId = TEST_NETID;
94 fwmark.explicitlySelected = true;
95 fwmark.protectedFromVpn = true;
96 fwmark.permission = PERMISSION_SYSTEM;
97 ASSERT_EQ(gPrivateDnsConfiguration.set(TEST_NETID, fwmark.intValue, tlsServers, tlsHostname,
98 caCert),
99 0);
100 ASSERT_EQ(resolv_set_nameservers(TEST_NETID, servers, domains, kParams), 0);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900101 }
102
Hungming Chenc6556622019-10-02 16:01:07 +0800103 void SetResolvers() { SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains); }
104
105 void SetResolversWithTls() {
106 // Pass servers as both network-assigned and TLS servers. Tests can
107 // determine on which server and by which protocol queries arrived.
108 // See also DnsClient::SetResolversWithTls() in
109 // packages/modules/DnsResolver/tests/dns_responder/dns_responder_client.h.
110 SetResolverConfiguration(kDefaultServers, kDefaultSearchDomains, kDefaultServers,
111 kDefaultPrivateDnsHostName, kCaCert);
112 }
113
114 bool WaitForPrivateDnsValidation(const std::string& serverAddr) {
115 constexpr milliseconds retryIntervalMs{20};
116 constexpr milliseconds timeoutMs{3000};
117 android::base::Timer t;
118 while (t.duration() < timeoutMs) {
119 const auto& validatedServers =
120 gPrivateDnsConfiguration.getStatus(TEST_NETID).validatedServers();
121 for (const auto& server : validatedServers) {
122 if (serverAddr == ToString(&server.ss)) return true;
123 }
124 std::this_thread::sleep_for(retryIntervalMs);
125 }
126 return false;
127 }
128
Hungming Chenc4b14162020-03-09 18:02:38 +0800129 Result<GoldTest> ToProto(const std::string& filename) {
Hungming Chen6cb2be02020-03-06 15:25:25 +0800130 // Convert the testing configuration from binary .pb file to proto.
Hungming Chenc4b14162020-03-09 18:02:38 +0800131 std::string content;
132 const std::string path = kTestDataPath + filename;
133
134 bool ret = android::base::ReadFileToString(path, &content);
135 if (!ret) return Errorf("Read {} failed: {}", path, strerror(errno));
136
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800137 android::net::GoldTest goldtest;
Hungming Chen6cb2be02020-03-06 15:25:25 +0800138 ret = goldtest.ParseFromString(content);
Hungming Chenc4b14162020-03-09 18:02:38 +0800139 if (!ret) return Errorf("Parse {} failed", path);
140
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800141 return goldtest;
142 }
143
144 void SetupMappings(const android::net::GoldTest& goldtest, test::DNSResponder& dns) {
145 for (const auto& m : goldtest.packet_mapping()) {
146 // Convert string to bytes because .proto type "bytes" is "string" type in C++.
147 // See also the section "Scalar Value Types" in "Language Guide (proto3)".
148 // TODO: Use C++20 std::span in addMappingBinaryPacket. It helps to take both
149 // std::string and std::vector without conversions.
150 dns.addMappingBinaryPacket(
151 std::vector<uint8_t>(m.query().begin(), m.query().end()),
152 std::vector<uint8_t>(m.response().begin(), m.response().end()));
153 }
154 }
155
156 android_net_context GetNetContext(const DnsProtocol protocol) {
157 return protocol == DnsProtocol::TLS ? kNetcontextTls : kNetcontext;
158 }
159
160 template <class AddressType>
161 void VerifyAddress(const android::net::GoldTest& goldtest, const AddressType& result) {
162 if (goldtest.result().return_code() != GT_EAI_NO_ERROR) {
163 EXPECT_EQ(result, nullptr);
164 } else {
165 ASSERT_NE(result, nullptr);
166 const auto& addresses = goldtest.result().addresses();
167 EXPECT_THAT(ToStrings(result), ::testing::UnorderedElementsAreArray(addresses));
168 }
169 }
170
171 void VerifyGetAddrInfo(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
172 ASSERT_TRUE(goldtest.config().has_addrinfo());
173 const auto& args = goldtest.config().addrinfo();
174 const addrinfo hints = {
175 // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at
176 // whether connectivity is available. It makes that the resolver may send only A
177 // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also
178 // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp.
179 // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit
180 // test can treat the IPv4 and IPv6 connectivity.
181 .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG,
182 .ai_family = args.family(),
183 .ai_socktype = args.socktype(),
184 .ai_protocol = args.protocol(),
185 };
186 addrinfo* res = nullptr;
187 const android_net_context netcontext = GetNetContext(protocol);
188 NetworkDnsEventReported event;
189 const int rv =
190 resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &netcontext, &res, &event);
191 ScopedAddrinfo result(res);
192 ASSERT_EQ(rv, goldtest.result().return_code());
193 VerifyAddress(goldtest, result);
194 }
195
Hungming Chen9c748a22019-10-17 18:11:02 +0800196 void VerifyGetHostByName(const android::net::GoldTest& goldtest, const DnsProtocol protocol) {
197 ASSERT_TRUE(goldtest.config().has_hostbyname());
198 const auto& args = goldtest.config().hostbyname();
199 hostent* hp = nullptr;
200 hostent hbuf;
201 char tmpbuf[MAXPACKET];
202 const android_net_context netcontext = GetNetContext(protocol);
203 NetworkDnsEventReported event;
204 const int rv = resolv_gethostbyname(args.host().c_str(), args.family(), &hbuf, tmpbuf,
205 sizeof(tmpbuf), &netcontext, &hp, &event);
206 ASSERT_EQ(rv, goldtest.result().return_code());
207 VerifyAddress(goldtest, hp);
208 }
209
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800210 void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns,
211 const test::DnsTlsFrontend& tls, const DnsProtocol protocol) {
212 size_t queries;
213 std::string name;
214
215 // Verify DNS query calls and results by proto. Then, determine expected query times and
216 // queried name for checking server query status later.
217 switch (const auto calltype = goldtest.config().call()) {
218 case android::net::CallType::CALL_GETADDRINFO:
219 ASSERT_TRUE(goldtest.config().has_addrinfo());
220 ASSERT_NO_FATAL_FAILURE(VerifyGetAddrInfo(goldtest, protocol));
221 queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U;
222 name = goldtest.config().addrinfo().host();
223 break;
Hungming Chen9c748a22019-10-17 18:11:02 +0800224 case android::net::CallType::CALL_GETHOSTBYNAME:
225 ASSERT_TRUE(goldtest.config().has_hostbyname());
226 ASSERT_NO_FATAL_FAILURE(VerifyGetHostByName(goldtest, protocol));
227 queries = 1U;
228 name = goldtest.config().hostbyname().host();
229 break;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800230 default:
231 FAIL() << "Unsupported call type: " << calltype;
232 }
233
234 // Verify DNS server query status.
235 EXPECT_EQ(GetNumQueries(dns, name.c_str()), queries);
236 if (protocol == DnsProtocol::TLS) EXPECT_EQ(tls.queries(), static_cast<int>(queries));
237 }
238
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900239 static constexpr res_params kParams = {
240 .sample_validity = 300,
241 .success_threshold = 25,
242 .min_samples = 8,
243 .max_samples = 8,
244 .base_timeout_msec = 1000,
245 .retry_count = 2,
246 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900247 static constexpr android_net_context kNetcontext = {
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 };
Hungming Chenc6556622019-10-02 16:01:07 +0800254 static constexpr android_net_context kNetcontextTls = {
255 .app_netid = TEST_NETID,
256 .app_mark = MARK_UNSET,
257 .dns_netid = TEST_NETID,
258 .dns_mark = MARK_UNSET,
259 .uid = NET_CONTEXT_INVALID_UID,
260 // Set TLS flags. See also maybeFixupNetContext() in
261 // packages/modules/DnsResolver/DnsProxyListener.cpp.
262 .flags = NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS,
263 };
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900264};
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900265class ResolvGetAddrInfo : public TestBase {};
266
Hungming Chen21c0f832019-09-20 18:38:47 +0800267// Fixture tests.
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900268TEST_F(ResolvGetAddrInfo, RemovePacketMapping) {
269 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
270 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800271 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900272
273 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
274
275 addrinfo* res = nullptr;
276 const addrinfo hints = {.ai_family = AF_INET};
277 NetworkDnsEventReported event;
278 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
279 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800280 ASSERT_NE(result, nullptr);
281 ASSERT_EQ(rv, 0);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800282 EXPECT_EQ(ToString(result), kHelloExampleComAddrV4);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900283
284 // Remove existing DNS record.
285 dns.removeMappingBinaryPacket(kHelloExampleComQueryV4);
286
287 // Expect to have no answer in DNS query result.
288 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
289 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800290 ASSERT_EQ(result, nullptr);
291 ASSERT_EQ(rv, EAI_NODATA);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900292}
293
294TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) {
295 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
296 ASSERT_TRUE(dns.startServer());
Hungming Chenc6556622019-10-02 16:01:07 +0800297 ASSERT_NO_FATAL_FAILURE(SetResolvers());
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900298
299 // Register the record which uses IPv4 address 1.2.3.4.
300 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4);
301
302 // Expect that the DNS query returns IPv4 address 1.2.3.4.
303 addrinfo* res = nullptr;
304 const addrinfo hints = {.ai_family = AF_INET};
305 NetworkDnsEventReported event;
306 int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
307 ScopedAddrinfo result(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800308 ASSERT_NE(result, nullptr);
309 ASSERT_EQ(rv, 0);
310 EXPECT_EQ(ToString(result), "1.2.3.4");
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900311
312 // Replace the registered record with a record which uses new IPv4 address 5.6.7.8.
313 std::vector<uint8_t> newHelloExampleComResponseV4 = {
314 /* Header */
315 0x00, 0x00, /* Transaction ID: 0x0000 */
316 0x81, 0x80, /* Flags: qr rd ra */
317 0x00, 0x01, /* Questions: 1 */
318 0x00, 0x01, /* Answer RRs: 1 */
319 0x00, 0x00, /* Authority RRs: 0 */
320 0x00, 0x00, /* Additional RRs: 0 */
321 /* Queries */
322 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
323 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
324 0x00, 0x01, /* Type: A */
325 0x00, 0x01, /* Class: IN */
326 /* Answers */
327 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
328 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
329 0x00, 0x01, /* Type: A */
330 0x00, 0x01, /* Class: IN */
331 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
332 0x00, 0x04, /* Data length: 4 */
333 0x05, 0x06, 0x07, 0x08 /* Address: 5.6.7.8 */
334 };
335 dns.addMappingBinaryPacket(kHelloExampleComQueryV4, newHelloExampleComResponseV4);
336
337 // Expect that DNS query returns new IPv4 address 5.6.7.8.
338 rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event);
339 result.reset(res);
Hungming Chenc6556622019-10-02 16:01:07 +0800340 ASSERT_NE(result, nullptr);
341 ASSERT_EQ(rv, 0);
342 EXPECT_EQ(ToString(result), "5.6.7.8");
343}
344
345TEST_F(ResolvGetAddrInfo, BasicTlsQuery) {
346 test::DNSResponder dns;
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800347 dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4);
348 dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6);
Hungming Chenc6556622019-10-02 16:01:07 +0800349 ASSERT_TRUE(dns.startServer());
350
351 test::DnsTlsFrontend tls;
352 ASSERT_TRUE(tls.startServer());
353 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
354 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
355
356 dns.clearQueries();
357 addrinfo* res = nullptr;
358 // If the socket type is not specified, every address will appear twice, once for
359 // SOCK_STREAM and one for SOCK_DGRAM. Just pick one because the addresses for
360 // the second query of different socket type are responded by the cache.
361 const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM};
362 NetworkDnsEventReported event;
363 const int rv =
364 resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontextTls, &res, &event);
365 ScopedAddrinfo result(res);
366 ASSERT_EQ(rv, 0);
367 EXPECT_EQ(GetNumQueries(dns, kHelloExampleCom), 2U);
368 const std::vector<std::string> result_strs = ToStrings(result);
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800369 EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray(
370 {kHelloExampleComAddrV4, kHelloExampleComAddrV6}));
Hungming Chenc6556622019-10-02 16:01:07 +0800371 EXPECT_EQ(tls.queries(), 3);
Hungming Chenf9cd4eb2019-08-06 20:55:28 +0900372}
373
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800374// Parameterized test class definition.
375using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>;
376class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<GoldTestParamType> {
377 public:
378 // Generate readable string for test name from test parameters.
379 static std::string Name(::testing::TestParamInfo<GoldTestParamType> info) {
380 const auto& [protocol, file] = info.param;
381 std::string name = StringPrintf(
382 "%s_%s", protocol == DnsProtocol::CLEARTEXT ? "CLEARTEXT" : "TLS", file.c_str());
383 std::replace_if(
384 std::begin(name), std::end(name), [](char ch) { return !std::isalnum(ch); }, '_');
385 return name;
386 }
387};
388
389// GetAddrInfo tests.
390INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest,
391 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
392 ::testing::ValuesIn(kGoldFilesGetAddrInfo)),
393 ResolvGoldTest::Name);
394INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest,
395 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
396 ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)),
397 ResolvGoldTest::Name);
398
Hungming Chen9c748a22019-10-17 18:11:02 +0800399// GetHostByName tests.
400INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest,
401 ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT),
402 ::testing::ValuesIn(kGoldFilesGetHostByName)),
403 ResolvGoldTest::Name);
404INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest,
405 ::testing::Combine(::testing::Values(DnsProtocol::TLS),
406 ::testing::ValuesIn(kGoldFilesGetHostByNameTls)),
407 ResolvGoldTest::Name);
408
Hungming Chen21c0f832019-09-20 18:38:47 +0800409TEST_P(ResolvGoldTest, GoldData) {
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800410 const auto& [protocol, file] = GetParam();
Hungming Chen21c0f832019-09-20 18:38:47 +0800411
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800412 // Setup DNS server configuration.
Hungming Chen21c0f832019-09-20 18:38:47 +0800413 test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
414 ASSERT_TRUE(dns.startServer());
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800415 test::DnsTlsFrontend tls;
Hungming Chen21c0f832019-09-20 18:38:47 +0800416
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800417 if (protocol == DnsProtocol::CLEARTEXT) {
418 ASSERT_NO_FATAL_FAILURE(SetResolvers());
419 } else if (protocol == DnsProtocol::TLS) {
420 ASSERT_TRUE(tls.startServer());
421 ASSERT_NO_FATAL_FAILURE(SetResolversWithTls());
422 EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address()));
423 tls.clearQueries();
Hungming Chen21c0f832019-09-20 18:38:47 +0800424 }
425
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800426 // Read test configuration from proto text file to proto.
Hungming Chenc4b14162020-03-09 18:02:38 +0800427 const Result<GoldTest> result = ToProto(file);
428 ASSERT_TRUE(result.ok()) << result.error().message();
429 const GoldTest& goldtest = result.value();
Hungming Chen21c0f832019-09-20 18:38:47 +0800430
Hungming Chen7b6c23b2019-10-03 17:46:11 +0800431 // Register packet mappings (query, response) from proto.
432 SetupMappings(goldtest, dns);
433
434 // Verify the resolver by proto.
435 VerifyResolver(goldtest, dns, tls, protocol);
Hungming Chen21c0f832019-09-20 18:38:47 +0800436}
437
Bernie Innocenticebc76d2019-12-13 11:55:17 +0900438} // namespace android::net