| 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 | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 24 | #include <gmock/gmock-matchers.h> |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 25 | #include <google/protobuf/text_format.h> |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 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 | |
| 41 | using android::base::StringPrintf; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 42 | using android::netdutils::ScopedAddrinfo; |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 43 | using std::chrono::milliseconds; |
| 44 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 45 | enum class DnsProtocol { CLEARTEXT, TLS }; |
| 46 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 47 | // The buffer size of resolv_gethostbyname(). |
| 48 | // TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h. |
| 49 | constexpr unsigned int MAXPACKET = 8 * 1024; |
| 50 | |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 51 | const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/"; |
| Hungming Chen | 2a56a62 | 2019-09-24 17:01:01 +0800 | [diff] [blame] | 52 | const 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 58 | const std::vector<std::string> kGoldFilesGetAddrInfoTls = {"getaddrinfo.tls.topsite.google.pbtxt"}; |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 59 | const std::vector<std::string> kGoldFilesGetHostByName = {"gethostbyname.topsite.youtube.pbtxt"}; |
| 60 | const std::vector<std::string> kGoldFilesGetHostByNameTls = { |
| 61 | "gethostbyname.tls.topsite.youtube.pbtxt"}; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 62 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 63 | // Fixture test class definition. |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 64 | class 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 Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 72 | // Clear TLS configuration for test |
| 73 | gPrivateDnsConfiguration.clear(TEST_NETID); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 74 | // Delete cache for test |
| 75 | resolv_delete_cache_for_net(TEST_NETID); |
| 76 | } |
| 77 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 78 | void SetResolverConfiguration(const std::vector<std::string>& servers, |
| 79 | const std::vector<std::string>& domains, |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 80 | 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 Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 100 | 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 Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 126 | GoldTest ToProto(const std::string& file) { |
| 127 | // Convert the testing configuration from .pbtxt file to proto. |
| 128 | std::string file_content; |
| 129 | EXPECT_TRUE(android::base::ReadFileToString(kTestDataPath + file, &file_content)) |
| 130 | << strerror(errno); |
| 131 | android::net::GoldTest goldtest; |
| 132 | EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(file_content, &goldtest)); |
| 133 | return goldtest; |
| 134 | } |
| 135 | |
| 136 | void SetupMappings(const android::net::GoldTest& goldtest, test::DNSResponder& dns) { |
| 137 | for (const auto& m : goldtest.packet_mapping()) { |
| 138 | // Convert string to bytes because .proto type "bytes" is "string" type in C++. |
| 139 | // See also the section "Scalar Value Types" in "Language Guide (proto3)". |
| 140 | // TODO: Use C++20 std::span in addMappingBinaryPacket. It helps to take both |
| 141 | // std::string and std::vector without conversions. |
| 142 | dns.addMappingBinaryPacket( |
| 143 | std::vector<uint8_t>(m.query().begin(), m.query().end()), |
| 144 | std::vector<uint8_t>(m.response().begin(), m.response().end())); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | android_net_context GetNetContext(const DnsProtocol protocol) { |
| 149 | return protocol == DnsProtocol::TLS ? kNetcontextTls : kNetcontext; |
| 150 | } |
| 151 | |
| 152 | template <class AddressType> |
| 153 | void VerifyAddress(const android::net::GoldTest& goldtest, const AddressType& result) { |
| 154 | if (goldtest.result().return_code() != GT_EAI_NO_ERROR) { |
| 155 | EXPECT_EQ(result, nullptr); |
| 156 | } else { |
| 157 | ASSERT_NE(result, nullptr); |
| 158 | const auto& addresses = goldtest.result().addresses(); |
| 159 | EXPECT_THAT(ToStrings(result), ::testing::UnorderedElementsAreArray(addresses)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void VerifyGetAddrInfo(const android::net::GoldTest& goldtest, const DnsProtocol protocol) { |
| 164 | ASSERT_TRUE(goldtest.config().has_addrinfo()); |
| 165 | const auto& args = goldtest.config().addrinfo(); |
| 166 | const addrinfo hints = { |
| 167 | // Clear the flag AI_ADDRCONFIG to avoid flaky test because AI_ADDRCONFIG looks at |
| 168 | // whether connectivity is available. It makes that the resolver may send only A |
| 169 | // or AAAA DNS query per connectivity even AF_UNSPEC has been assigned. See also |
| 170 | // have_ipv6() and have_ipv4() in packages/modules/DnsResolver/getaddrinfo.cpp. |
| 171 | // TODO: Consider keeping the configuration flag AI_ADDRCONFIG once the unit |
| 172 | // test can treat the IPv4 and IPv6 connectivity. |
| 173 | .ai_flags = args.ai_flags() & ~AI_ADDRCONFIG, |
| 174 | .ai_family = args.family(), |
| 175 | .ai_socktype = args.socktype(), |
| 176 | .ai_protocol = args.protocol(), |
| 177 | }; |
| 178 | addrinfo* res = nullptr; |
| 179 | const android_net_context netcontext = GetNetContext(protocol); |
| 180 | NetworkDnsEventReported event; |
| 181 | const int rv = |
| 182 | resolv_getaddrinfo(args.host().c_str(), nullptr, &hints, &netcontext, &res, &event); |
| 183 | ScopedAddrinfo result(res); |
| 184 | ASSERT_EQ(rv, goldtest.result().return_code()); |
| 185 | VerifyAddress(goldtest, result); |
| 186 | } |
| 187 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 188 | void VerifyGetHostByName(const android::net::GoldTest& goldtest, const DnsProtocol protocol) { |
| 189 | ASSERT_TRUE(goldtest.config().has_hostbyname()); |
| 190 | const auto& args = goldtest.config().hostbyname(); |
| 191 | hostent* hp = nullptr; |
| 192 | hostent hbuf; |
| 193 | char tmpbuf[MAXPACKET]; |
| 194 | const android_net_context netcontext = GetNetContext(protocol); |
| 195 | NetworkDnsEventReported event; |
| 196 | const int rv = resolv_gethostbyname(args.host().c_str(), args.family(), &hbuf, tmpbuf, |
| 197 | sizeof(tmpbuf), &netcontext, &hp, &event); |
| 198 | ASSERT_EQ(rv, goldtest.result().return_code()); |
| 199 | VerifyAddress(goldtest, hp); |
| 200 | } |
| 201 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 202 | void VerifyResolver(const android::net::GoldTest& goldtest, const test::DNSResponder& dns, |
| 203 | const test::DnsTlsFrontend& tls, const DnsProtocol protocol) { |
| 204 | size_t queries; |
| 205 | std::string name; |
| 206 | |
| 207 | // Verify DNS query calls and results by proto. Then, determine expected query times and |
| 208 | // queried name for checking server query status later. |
| 209 | switch (const auto calltype = goldtest.config().call()) { |
| 210 | case android::net::CallType::CALL_GETADDRINFO: |
| 211 | ASSERT_TRUE(goldtest.config().has_addrinfo()); |
| 212 | ASSERT_NO_FATAL_FAILURE(VerifyGetAddrInfo(goldtest, protocol)); |
| 213 | queries = goldtest.config().addrinfo().family() == AF_UNSPEC ? 2U : 1U; |
| 214 | name = goldtest.config().addrinfo().host(); |
| 215 | break; |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 216 | case android::net::CallType::CALL_GETHOSTBYNAME: |
| 217 | ASSERT_TRUE(goldtest.config().has_hostbyname()); |
| 218 | ASSERT_NO_FATAL_FAILURE(VerifyGetHostByName(goldtest, protocol)); |
| 219 | queries = 1U; |
| 220 | name = goldtest.config().hostbyname().host(); |
| 221 | break; |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 222 | default: |
| 223 | FAIL() << "Unsupported call type: " << calltype; |
| 224 | } |
| 225 | |
| 226 | // Verify DNS server query status. |
| 227 | EXPECT_EQ(GetNumQueries(dns, name.c_str()), queries); |
| 228 | if (protocol == DnsProtocol::TLS) EXPECT_EQ(tls.queries(), static_cast<int>(queries)); |
| 229 | } |
| 230 | |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 231 | static constexpr res_params kParams = { |
| 232 | .sample_validity = 300, |
| 233 | .success_threshold = 25, |
| 234 | .min_samples = 8, |
| 235 | .max_samples = 8, |
| 236 | .base_timeout_msec = 1000, |
| 237 | .retry_count = 2, |
| 238 | }; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 239 | static constexpr android_net_context kNetcontext = { |
| 240 | .app_netid = TEST_NETID, |
| 241 | .app_mark = MARK_UNSET, |
| 242 | .dns_netid = TEST_NETID, |
| 243 | .dns_mark = MARK_UNSET, |
| 244 | .uid = NET_CONTEXT_INVALID_UID, |
| 245 | }; |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 246 | static constexpr android_net_context kNetcontextTls = { |
| 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 | // Set TLS flags. See also maybeFixupNetContext() in |
| 253 | // packages/modules/DnsResolver/DnsProxyListener.cpp. |
| 254 | .flags = NET_CONTEXT_FLAG_USE_DNS_OVER_TLS | NET_CONTEXT_FLAG_USE_EDNS, |
| 255 | }; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 256 | }; |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 257 | class ResolvGetAddrInfo : public TestBase {}; |
| 258 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 259 | // Fixture tests. |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 260 | TEST_F(ResolvGetAddrInfo, RemovePacketMapping) { |
| 261 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 262 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 263 | ASSERT_NO_FATAL_FAILURE(SetResolvers()); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 264 | |
| 265 | dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4); |
| 266 | |
| 267 | addrinfo* res = nullptr; |
| 268 | const addrinfo hints = {.ai_family = AF_INET}; |
| 269 | NetworkDnsEventReported event; |
| 270 | int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event); |
| 271 | ScopedAddrinfo result(res); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 272 | ASSERT_NE(result, nullptr); |
| 273 | ASSERT_EQ(rv, 0); |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 274 | EXPECT_EQ(ToString(result), kHelloExampleComAddrV4); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 275 | |
| 276 | // Remove existing DNS record. |
| 277 | dns.removeMappingBinaryPacket(kHelloExampleComQueryV4); |
| 278 | |
| 279 | // Expect to have no answer in DNS query result. |
| 280 | rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event); |
| 281 | result.reset(res); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 282 | ASSERT_EQ(result, nullptr); |
| 283 | ASSERT_EQ(rv, EAI_NODATA); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | TEST_F(ResolvGetAddrInfo, ReplacePacketMapping) { |
| 287 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 288 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 289 | ASSERT_NO_FATAL_FAILURE(SetResolvers()); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 290 | |
| 291 | // Register the record which uses IPv4 address 1.2.3.4. |
| 292 | dns.addMappingBinaryPacket(kHelloExampleComQueryV4, kHelloExampleComResponseV4); |
| 293 | |
| 294 | // Expect that the DNS query returns IPv4 address 1.2.3.4. |
| 295 | addrinfo* res = nullptr; |
| 296 | const addrinfo hints = {.ai_family = AF_INET}; |
| 297 | NetworkDnsEventReported event; |
| 298 | int rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event); |
| 299 | ScopedAddrinfo result(res); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 300 | ASSERT_NE(result, nullptr); |
| 301 | ASSERT_EQ(rv, 0); |
| 302 | EXPECT_EQ(ToString(result), "1.2.3.4"); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 303 | |
| 304 | // Replace the registered record with a record which uses new IPv4 address 5.6.7.8. |
| 305 | std::vector<uint8_t> newHelloExampleComResponseV4 = { |
| 306 | /* Header */ |
| 307 | 0x00, 0x00, /* Transaction ID: 0x0000 */ |
| 308 | 0x81, 0x80, /* Flags: qr rd ra */ |
| 309 | 0x00, 0x01, /* Questions: 1 */ |
| 310 | 0x00, 0x01, /* Answer RRs: 1 */ |
| 311 | 0x00, 0x00, /* Authority RRs: 0 */ |
| 312 | 0x00, 0x00, /* Additional RRs: 0 */ |
| 313 | /* Queries */ |
| 314 | 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, |
| 315 | 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ |
| 316 | 0x00, 0x01, /* Type: A */ |
| 317 | 0x00, 0x01, /* Class: IN */ |
| 318 | /* Answers */ |
| 319 | 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, |
| 320 | 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */ |
| 321 | 0x00, 0x01, /* Type: A */ |
| 322 | 0x00, 0x01, /* Class: IN */ |
| 323 | 0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */ |
| 324 | 0x00, 0x04, /* Data length: 4 */ |
| 325 | 0x05, 0x06, 0x07, 0x08 /* Address: 5.6.7.8 */ |
| 326 | }; |
| 327 | dns.addMappingBinaryPacket(kHelloExampleComQueryV4, newHelloExampleComResponseV4); |
| 328 | |
| 329 | // Expect that DNS query returns new IPv4 address 5.6.7.8. |
| 330 | rv = resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontext, &res, &event); |
| 331 | result.reset(res); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 332 | ASSERT_NE(result, nullptr); |
| 333 | ASSERT_EQ(rv, 0); |
| 334 | EXPECT_EQ(ToString(result), "5.6.7.8"); |
| 335 | } |
| 336 | |
| 337 | TEST_F(ResolvGetAddrInfo, BasicTlsQuery) { |
| 338 | test::DNSResponder dns; |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 339 | dns.addMapping(kHelloExampleCom, ns_type::ns_t_a, kHelloExampleComAddrV4); |
| 340 | dns.addMapping(kHelloExampleCom, ns_type::ns_t_aaaa, kHelloExampleComAddrV6); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 341 | ASSERT_TRUE(dns.startServer()); |
| 342 | |
| 343 | test::DnsTlsFrontend tls; |
| 344 | ASSERT_TRUE(tls.startServer()); |
| 345 | ASSERT_NO_FATAL_FAILURE(SetResolversWithTls()); |
| 346 | EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address())); |
| 347 | |
| 348 | dns.clearQueries(); |
| 349 | addrinfo* res = nullptr; |
| 350 | // If the socket type is not specified, every address will appear twice, once for |
| 351 | // SOCK_STREAM and one for SOCK_DGRAM. Just pick one because the addresses for |
| 352 | // the second query of different socket type are responded by the cache. |
| 353 | const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM}; |
| 354 | NetworkDnsEventReported event; |
| 355 | const int rv = |
| 356 | resolv_getaddrinfo(kHelloExampleCom, nullptr, &hints, &kNetcontextTls, &res, &event); |
| 357 | ScopedAddrinfo result(res); |
| 358 | ASSERT_EQ(rv, 0); |
| 359 | EXPECT_EQ(GetNumQueries(dns, kHelloExampleCom), 2U); |
| 360 | const std::vector<std::string> result_strs = ToStrings(result); |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 361 | EXPECT_THAT(result_strs, testing::UnorderedElementsAreArray( |
| 362 | {kHelloExampleComAddrV4, kHelloExampleComAddrV6})); |
| Hungming Chen | c655662 | 2019-10-02 16:01:07 +0800 | [diff] [blame] | 363 | EXPECT_EQ(tls.queries(), 3); |
| Hungming Chen | f9cd4eb | 2019-08-06 20:55:28 +0900 | [diff] [blame] | 364 | } |
| 365 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 366 | // Parameterized test class definition. |
| 367 | using GoldTestParamType = std::tuple<DnsProtocol, std::string /* filename */>; |
| 368 | class ResolvGoldTest : public TestBase, public ::testing::WithParamInterface<GoldTestParamType> { |
| 369 | public: |
| 370 | // Generate readable string for test name from test parameters. |
| 371 | static std::string Name(::testing::TestParamInfo<GoldTestParamType> info) { |
| 372 | const auto& [protocol, file] = info.param; |
| 373 | std::string name = StringPrintf( |
| 374 | "%s_%s", protocol == DnsProtocol::CLEARTEXT ? "CLEARTEXT" : "TLS", file.c_str()); |
| 375 | std::replace_if( |
| 376 | std::begin(name), std::end(name), [](char ch) { return !std::isalnum(ch); }, '_'); |
| 377 | return name; |
| 378 | } |
| 379 | }; |
| 380 | |
| 381 | // GetAddrInfo tests. |
| 382 | INSTANTIATE_TEST_SUITE_P(GetAddrInfo, ResolvGoldTest, |
| 383 | ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT), |
| 384 | ::testing::ValuesIn(kGoldFilesGetAddrInfo)), |
| 385 | ResolvGoldTest::Name); |
| 386 | INSTANTIATE_TEST_SUITE_P(GetAddrInfoTls, ResolvGoldTest, |
| 387 | ::testing::Combine(::testing::Values(DnsProtocol::TLS), |
| 388 | ::testing::ValuesIn(kGoldFilesGetAddrInfoTls)), |
| 389 | ResolvGoldTest::Name); |
| 390 | |
| Hungming Chen | 9c748a2 | 2019-10-17 18:11:02 +0800 | [diff] [blame^] | 391 | // GetHostByName tests. |
| 392 | INSTANTIATE_TEST_SUITE_P(GetHostByName, ResolvGoldTest, |
| 393 | ::testing::Combine(::testing::Values(DnsProtocol::CLEARTEXT), |
| 394 | ::testing::ValuesIn(kGoldFilesGetHostByName)), |
| 395 | ResolvGoldTest::Name); |
| 396 | INSTANTIATE_TEST_SUITE_P(GetHostByNameTls, ResolvGoldTest, |
| 397 | ::testing::Combine(::testing::Values(DnsProtocol::TLS), |
| 398 | ::testing::ValuesIn(kGoldFilesGetHostByNameTls)), |
| 399 | ResolvGoldTest::Name); |
| 400 | |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 401 | TEST_P(ResolvGoldTest, GoldData) { |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 402 | const auto& [protocol, file] = GetParam(); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 403 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 404 | // Setup DNS server configuration. |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 405 | test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET); |
| 406 | ASSERT_TRUE(dns.startServer()); |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 407 | test::DnsTlsFrontend tls; |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 408 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 409 | if (protocol == DnsProtocol::CLEARTEXT) { |
| 410 | ASSERT_NO_FATAL_FAILURE(SetResolvers()); |
| 411 | } else if (protocol == DnsProtocol::TLS) { |
| 412 | ASSERT_TRUE(tls.startServer()); |
| 413 | ASSERT_NO_FATAL_FAILURE(SetResolversWithTls()); |
| 414 | EXPECT_TRUE(WaitForPrivateDnsValidation(tls.listen_address())); |
| 415 | tls.clearQueries(); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 416 | } |
| 417 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 418 | // Read test configuration from proto text file to proto. |
| 419 | const auto goldtest = ToProto(file); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 420 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 421 | // Register packet mappings (query, response) from proto. |
| 422 | SetupMappings(goldtest, dns); |
| 423 | |
| 424 | // Verify the resolver by proto. |
| 425 | VerifyResolver(goldtest, dns, tls, protocol); |
| Hungming Chen | 21c0f83 | 2019-09-20 18:38:47 +0800 | [diff] [blame] | 426 | } |
| 427 | |
| Hungming Chen | 7b6c23b | 2019-10-03 17:46:11 +0800 | [diff] [blame] | 428 | } // namespace android::net |