| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 1 | /* |
| 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 20 | #include <Fwmark.h> |
| 21 | #include <android-base/chrono_utils.h> |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 22 | #include <android-base/file.h> |
| Hungming Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 23 | #include <android-base/result.h> |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 25 | #include <gmock/gmock-matchers.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 28 | #include "PrivateDnsConfiguration.h" |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 29 | #include "getaddrinfo.h" |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 30 | #include "gethnamaddr.h" |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 31 | #include "golddata.pb.h" |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 32 | #include "resolv_cache.h" |
| 33 | #include "resolv_test_utils.h" |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 34 | #include "tests/dns_responder/dns_responder.h" |
| 35 | #include "tests/dns_responder/dns_responder_client_ndk.h" |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 36 | #include "tests/dns_responder/dns_tls_certificate.h" |
| 37 | #include "tests/dns_responder/dns_tls_frontend.h" |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 38 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 39 | namespace android::net { |
| 40 | |
| Hungming Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 41 | using android::base::Result; |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 42 | using android::base::StringPrintf; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 43 | using android::netdutils::ScopedAddrinfo; |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 44 | using std::chrono::milliseconds; |
| 45 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 46 | enum class DnsProtocol { CLEARTEXT, TLS }; |
| 47 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 48 | // The buffer size of resolv_gethostbyname(). |
| 49 | // TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h. |
| 50 | constexpr unsigned int MAXPACKET = 8 * 1024; |
| 51 | |
| Hungming Chen | 6cb2be0 | 2020-03-06 15:25:25 +0800 | [diff] [blame^] | 52 | // The testdata/pb/*.pb are generated from testdata/*.pbtext. |
| 53 | // TODO: Generate .pb files via precompiler. |
| 54 | const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/pb/"; |
| Hungming Chen | 2a56a62 | 2019-09-24 17:01:01 +0800 | [diff] [blame] | 55 | const std::vector<std::string> kGoldFilesGetAddrInfo = { |
| Hungming Chen | 6cb2be0 | 2020-03-06 15:25:25 +0800 | [diff] [blame^] | 56 | "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"}; |
| 61 | const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pb"}; |
| 62 | const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pb"}; |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 63 | const std::vector<std::string> kGoldFilesGetHostByNameTls = { |
| Hungming Chen | 6cb2be0 | 2020-03-06 15:25:25 +0800 | [diff] [blame^] | 64 | "gethostbyname.tls.topsite.youtube.pb"}; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 65 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 66 | // Fixture test class definition. |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 67 | class 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 75 | // Clear TLS configuration for test |
| 76 | gPrivateDnsConfiguration.clear(TEST_NETID); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 77 | // Delete cache for test |
| 78 | resolv_delete_cache_for_net(TEST_NETID); |
| 79 | } |
| 80 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 81 | void SetResolverConfiguration(const std::vector<std::string>& servers, |
| 82 | const std::vector<std::string>& domains, |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 83 | 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 Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 103 | 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 Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 129 | Result<GoldTest> ToProto(const std::string& filename) { |
| Hungming Chen | 6cb2be0 | 2020-03-06 15:25:25 +0800 | [diff] [blame^] | 130 | // Convert the testing configuration from binary .pb file to proto. |
| Hungming Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 131 | 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 137 | android::net::GoldTest goldtest; |
| Hungming Chen | 6cb2be0 | 2020-03-06 15:25:25 +0800 | [diff] [blame^] | 138 | ret = goldtest.ParseFromString(content); |
| Hungming Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 139 | if (!ret) return Errorf("Parse {} failed", path); |
| 140 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 141 | 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 Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 196 | 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 210 | 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 Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 224 | 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 230 | 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 Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 239 | 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 Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 247 | 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 254 | 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 Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 264 | }; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 265 | class ResolvGetAddrInfo : public TestBase {}; |
| 266 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 267 | // Fixture tests. |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 268 | TEST_F(ResolvGetAddrInfo, RemovePacketMapping) { |
| 269 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 270 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 271 | ASSERT_NO_FATAL_FAILURE(SetResolvers()); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 272 | |
| 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 280 | ASSERT_NE(result, nullptr); |
| 281 | ASSERT_EQ(rv, 0); |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 282 | EXPECT_EQ(ToString(result), kHelloExampleComAddrV4); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 283 | |
| 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 290 | ASSERT_EQ(result, nullptr); |
| 291 | ASSERT_EQ(rv, EAI_NODATA); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) { |
| 295 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 296 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 297 | ASSERT_NO_FATAL_FAILURE(SetResolvers()); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 298 | |
| 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 308 | ASSERT_NE(result, nullptr); |
| 309 | ASSERT_EQ(rv, 0); |
| 310 | EXPECT_EQ(ToString(result), "1.2.3.4"); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 311 | |
| 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 340 | ASSERT_NE(result, nullptr); |
| 341 | ASSERT_EQ(rv, 0); |
| 342 | EXPECT_EQ(ToString(result), "5.6.7.8"); |
| 343 | } |
| 344 | |
| 345 | TEST_F(ResolvGetAddrInfo, BasicTlsQuery) { |
| 346 | test::DNSResponder dns; |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 347 | dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4); |
| 348 | dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 349 | 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 369 | EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray( |
| 370 | {kHelloExampleComAddrV4, kHelloExampleComAddrV6})); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 371 | EXPECT_EQ(tls.queries(), 3); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 372 | } |
| 373 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 374 | // Parameterized test class definition. |
| 375 | using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>; |
| 376 | class 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. |
| 390 | INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest, |
| 391 | ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT), |
| 392 | ::testing::ValuesIn(kGoldFilesGetAddrInfo)), |
| 393 | ResolvGoldTest::Name); |
| 394 | INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest, |
| 395 | ::testing::Combine(::testing::Values(DnsProtocol::TLS), |
| 396 | ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)), |
| 397 | ResolvGoldTest::Name); |
| 398 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame] | 399 | // GetHostByName tests. |
| 400 | INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest, |
| 401 | ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT), |
| 402 | ::testing::ValuesIn(kGoldFilesGetHostByName)), |
| 403 | ResolvGoldTest::Name); |
| 404 | INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest, |
| 405 | ::testing::Combine(::testing::Values(DnsProtocol::TLS), |
| 406 | ::testing::ValuesIn(kGoldFilesGetHostByNameTls)), |
| 407 | ResolvGoldTest::Name); |
| 408 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 409 | TEST_P(ResolvGoldTest, GoldData) { |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 410 | const auto& [protocol, file] = GetParam(); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 411 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 412 | // Setup DNS server configuration. |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 413 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 414 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 415 | test::DnsTlsFrontend tls; |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 416 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 417 | 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 Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 424 | } |
| 425 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 426 | // Read test configuration from proto text file to proto. |
| Hungming Chen | c4b1416 | 2020-03-09 18:02:38 +0800 | [diff] [blame] | 427 | const Result<GoldTest> result = ToProto(file); |
| 428 | ASSERT_TRUE(result.ok()) << result.error().message(); |
| 429 | const GoldTest& goldtest = result.value(); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 430 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 431 | // 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 Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 436 | } |
| 437 | |
| Bernie Innocenti | cebc76d | 2019-12-13 11:55:17 +0900 | [diff] [blame] | 438 | } // namespace android::net |