Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | * |
Lorenzo Colitti | 0a8b4aa | 2019-06-26 22:31:03 +0900 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 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 | |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 18 | #define LOG_TAG "resolv_integration_test" |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 19 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 21 | #include <android-base/parseint.h> |
| 22 | #include <android-base/stringprintf.h> |
| 23 | #include <android-base/unique_fd.h> |
| 24 | #include <android/multinetwork.h> // ResNsendFlags |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 25 | #include <arpa/inet.h> |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 26 | #include <arpa/nameser.h> |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 27 | #include <binder/ProcessState.h> |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 28 | #include <bpf/BpfUtils.h> |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 29 | #include <cutils/sockets.h> |
| 30 | #include <gmock/gmock-matchers.h> |
| 31 | #include <gtest/gtest.h> |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 32 | #include <netdb.h> |
Luke Huang | 9d2d25b | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 33 | #include <netdutils/InternetAddresses.h> |
Luke Huang | 579ddc7 | 2019-06-14 00:59:39 +0800 | [diff] [blame] | 34 | #include <netdutils/NetworkConstants.h> // SHA256_SIZE |
Luke Huang | 9d2d25b | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 35 | #include <netdutils/ResponseCode.h> |
| 36 | #include <netdutils/SocketOption.h> |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 37 | #include <netinet/in.h> |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 38 | #include <openssl/base64.h> |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 39 | #include <poll.h> /* poll */ |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 40 | #include <private/android_filesystem_config.h> |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 41 | #include <resolv.h> |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 42 | #include <stdarg.h> |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 43 | #include <stdlib.h> |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 44 | #include <sys/socket.h> |
| 45 | #include <sys/un.h> |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 46 | #include <unistd.h> |
| 47 | |
| 48 | #include <algorithm> |
| 49 | #include <chrono> |
| 50 | #include <iterator> |
| 51 | #include <numeric> |
| 52 | #include <thread> |
| 53 | |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 54 | #include "NetdClient.h" |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 55 | #include "ResolverStats.h" |
| 56 | #include "android/net/IDnsResolver.h" |
| 57 | #include "binder/IServiceManager.h" |
Bernie Innocenti | 41b82c4 | 2019-06-05 22:38:25 +0900 | [diff] [blame] | 58 | #include "netd_resolv/params.h" // MAXNS |
chenbruce | b43ec75 | 2019-07-24 20:19:41 +0800 | [diff] [blame] | 59 | #include "netid_client.h" // NETID_UNSET |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 60 | #include "test_utils.h" |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 61 | #include "tests/dns_metrics_listener/dns_metrics_listener.h" |
chenbruce | b43ec75 | 2019-07-24 20:19:41 +0800 | [diff] [blame] | 62 | #include "tests/dns_responder/dns_responder.h" |
| 63 | #include "tests/dns_responder/dns_responder_client.h" |
| 64 | #include "tests/dns_responder/dns_tls_frontend.h" |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 65 | #include "tests/resolv_test_utils.h" |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 66 | |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 67 | // Valid VPN netId range is 100 ~ 65535 |
| 68 | constexpr int TEST_VPN_NETID = 65502; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 69 | constexpr int MAXPACKET = (8 * 1024); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 70 | |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 71 | // Use maximum reserved appId for applications to avoid conflict with existing uids. |
| 72 | static const int TEST_UID = 99999; |
| 73 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 74 | // Semi-public Bionic hook used by the NDK (frameworks/base/native/android/net.c) |
| 75 | // Tested here for convenience. |
| 76 | extern "C" int android_getaddrinfofornet(const char* hostname, const char* servname, |
| 77 | const addrinfo* hints, unsigned netid, unsigned mark, |
| 78 | struct addrinfo** result); |
| 79 | |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 80 | using android::base::ParseInt; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 81 | using android::base::StringPrintf; |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 82 | using android::base::unique_fd; |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 83 | using android::net::INetd; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 84 | using android::net::ResolverStats; |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 85 | using android::net::metrics::DnsMetricsListener; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 86 | using android::netdutils::enableSockopt; |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 87 | using android::netdutils::ResponseCode; |
Luke Huang | 9d2d25b | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 88 | using android::netdutils::ScopedAddrinfo; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 89 | |
| 90 | // TODO: move into libnetdutils? |
| 91 | namespace { |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 92 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 93 | ScopedAddrinfo safe_getaddrinfo(const char* node, const char* service, |
| 94 | const struct addrinfo* hints) { |
| 95 | addrinfo* result = nullptr; |
| 96 | if (getaddrinfo(node, service, hints, &result) != 0) { |
| 97 | result = nullptr; // Should already be the case, but... |
| 98 | } |
| 99 | return ScopedAddrinfo(result); |
| 100 | } |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 101 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 102 | } // namespace |
| 103 | |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 104 | class ResolverTest : public ::testing::Test { |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 105 | public: |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 106 | static void SetUpTestCase() { |
| 107 | // Get binder service. |
| 108 | // Note that |mDnsClient| is not used for getting binder service in this static function. |
| 109 | // The reason is that wants to keep |mDnsClient| as a non-static data member. |mDnsClient| |
| 110 | // which sets up device network configuration could be independent from every test. |
Hungming Chen | 9e6185a | 2019-06-04 16:09:19 +0800 | [diff] [blame] | 111 | // TODO: Perhaps add a static function in resolv_test_utils.{cpp,h} to get binder service. |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 112 | auto resolvBinder = |
| 113 | android::defaultServiceManager()->getService(android::String16("dnsresolver")); |
| 114 | auto resolvService = android::interface_cast<android::net::IDnsResolver>(resolvBinder); |
| 115 | ASSERT_NE(nullptr, resolvService.get()); |
| 116 | |
| 117 | // Subscribe the death recipient to the service IDnsResolver for detecting Netd death. |
| 118 | sResolvDeathRecipient = new ResolvDeathRecipient(); |
| 119 | ASSERT_EQ(android::NO_ERROR, resolvBinder->linkToDeath(sResolvDeathRecipient)); |
| 120 | |
| 121 | // Subscribe the DNS listener for verifying DNS metrics event contents. |
| 122 | sDnsMetricsListener = new DnsMetricsListener(TEST_NETID /*monitor specific network*/); |
| 123 | ASSERT_TRUE(resolvService->registerEventListener(sDnsMetricsListener).isOk()); |
| 124 | |
| 125 | // Start the binder thread pool for listening DNS metrics events and receiving death |
| 126 | // recipient. |
| 127 | android::ProcessState::self()->startThreadPool(); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 130 | protected: |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 131 | struct DnsRecord { |
| 132 | std::string host_name; // host name |
| 133 | ns_type type; // record type |
| 134 | std::string addr; // ipv4/v6 address |
| 135 | }; |
| 136 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 137 | class ResolvDeathRecipient : public android::IBinder::DeathRecipient { |
| 138 | public: |
| 139 | ~ResolvDeathRecipient() override = default; |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 140 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 141 | // GTEST assertion macros are not invoked for generating a test failure in the death |
| 142 | // recipient because the macros can't indicate failed test if Netd died between tests. |
| 143 | // Moreover, continuing testing may have no meaningful after Netd death. Therefore, the |
| 144 | // death recipient aborts process by GTEST_LOG_(FATAL) once Netd died. |
| 145 | void binderDied(const android::wp<android::IBinder>& /*who*/) override { |
Hungming Chen | 9e6185a | 2019-06-04 16:09:19 +0800 | [diff] [blame] | 146 | constexpr char errorMessage[] = "Netd died"; |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 147 | LOG(ERROR) << errorMessage; |
| 148 | GTEST_LOG_(FATAL) << errorMessage; |
| 149 | } |
| 150 | }; |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 151 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 152 | void SetUp() { mDnsClient.SetUp(); } |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 153 | void TearDown() { mDnsClient.TearDown(); } |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 154 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 155 | void StartDns(test::DNSResponder& dns, const std::vector<DnsRecord>& records) { |
| 156 | for (const auto& r : records) { |
| 157 | dns.addMapping(r.host_name, r.type, r.addr); |
| 158 | } |
| 159 | |
| 160 | ASSERT_TRUE(dns.startServer()); |
| 161 | dns.clearQueries(); |
| 162 | } |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 163 | |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 164 | bool WaitForNat64Prefix(ExpectNat64PrefixStatus status, |
| 165 | std::chrono::milliseconds timeout = std::chrono::milliseconds(1000)) { |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 166 | return sDnsMetricsListener->waitForNat64Prefix(status, timeout); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 169 | DnsResponderClient mDnsClient; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 170 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 171 | // Use a shared static DNS listener for all tests to avoid registering lots of listeners |
| 172 | // which may be released late until process terminated. Currently, registered DNS listener |
| 173 | // is removed by binder death notification which is fired when the process hosting an |
| 174 | // IBinder has gone away. If every test in ResolverTest registers its DNS listener, Netd |
| 175 | // may temporarily hold lots of dead listeners until the unit test process terminates. |
| 176 | // TODO: Perhaps add an unregistering listener binder call or fork a listener process which |
| 177 | // could be terminated earlier. |
| 178 | static android::sp<DnsMetricsListener> sDnsMetricsListener; // Initialized in SetUpTestCase. |
| 179 | |
| 180 | // Use a shared static death recipient to monitor the service death. The static death |
| 181 | // recipient could monitor the death not only during the test but also between tests. |
| 182 | static android::sp<ResolvDeathRecipient> |
| 183 | sResolvDeathRecipient; // Initialized in SetUpTestCase. |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 184 | }; |
| 185 | |
Hungming Chen | 5bf0977 | 2019-04-25 11:16:13 +0800 | [diff] [blame] | 186 | // Initialize static member of class. |
| 187 | android::sp<DnsMetricsListener> ResolverTest::sDnsMetricsListener; |
| 188 | android::sp<ResolverTest::ResolvDeathRecipient> ResolverTest::sResolvDeathRecipient; |
| 189 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 190 | TEST_F(ResolverTest, GetHostByName) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 191 | constexpr char nonexistent_host_name[] = "nonexistent.example.com."; |
| 192 | |
| 193 | test::DNSResponder dns; |
| 194 | StartDns(dns, {{kHelloExampleCom, ns_type::ns_t_a, "1.2.3.3"}}); |
| 195 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 196 | |
| 197 | const hostent* result; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 198 | result = gethostbyname("nonexistent"); |
| 199 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, nonexistent_host_name)); |
| 200 | ASSERT_TRUE(result == nullptr); |
| 201 | ASSERT_EQ(HOST_NOT_FOUND, h_errno); |
| 202 | |
| 203 | dns.clearQueries(); |
| 204 | result = gethostbyname("hello"); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 205 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, kHelloExampleCom)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 206 | ASSERT_FALSE(result == nullptr); |
| 207 | ASSERT_EQ(4, result->h_length); |
| 208 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 209 | EXPECT_EQ("1.2.3.3", ToString(result)); |
| 210 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 211 | } |
| 212 | |
lifr | 4e4a2e0 | 2019-01-29 16:53:51 +0800 | [diff] [blame] | 213 | TEST_F(ResolverTest, GetHostByName_cnames) { |
| 214 | constexpr char host_name[] = "host.example.com."; |
| 215 | size_t cnamecount = 0; |
| 216 | test::DNSResponder dns; |
| 217 | |
| 218 | const std::vector<DnsRecord> records = { |
| 219 | {kHelloExampleCom, ns_type::ns_t_cname, "a.example.com."}, |
| 220 | {"a.example.com.", ns_type::ns_t_cname, "b.example.com."}, |
| 221 | {"b.example.com.", ns_type::ns_t_cname, "c.example.com."}, |
| 222 | {"c.example.com.", ns_type::ns_t_cname, "d.example.com."}, |
| 223 | {"d.example.com.", ns_type::ns_t_cname, "e.example.com."}, |
| 224 | {"e.example.com.", ns_type::ns_t_cname, host_name}, |
| 225 | {host_name, ns_type::ns_t_a, "1.2.3.3"}, |
| 226 | {host_name, ns_type::ns_t_aaaa, "2001:db8::42"}, |
| 227 | }; |
| 228 | StartDns(dns, records); |
| 229 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 230 | |
| 231 | // using gethostbyname2() to resolve ipv4 hello.example.com. to 1.2.3.3 |
| 232 | // Ensure the v4 address and cnames are correct |
| 233 | const hostent* result; |
| 234 | result = gethostbyname2("hello", AF_INET); |
| 235 | ASSERT_FALSE(result == nullptr); |
| 236 | |
| 237 | for (int i = 0; result != nullptr && result->h_aliases[i] != nullptr; i++) { |
| 238 | std::string domain_name = records[i].host_name.substr(0, records[i].host_name.size() - 1); |
| 239 | EXPECT_EQ(result->h_aliases[i], domain_name); |
| 240 | cnamecount++; |
| 241 | } |
| 242 | // The size of "Non-cname type" record in DNS records is 2 |
| 243 | ASSERT_EQ(cnamecount, records.size() - 2); |
| 244 | ASSERT_EQ(4, result->h_length); |
| 245 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 246 | EXPECT_EQ("1.2.3.3", ToString(result)); |
| 247 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 248 | EXPECT_EQ(1U, dns.queries().size()) << dns.dumpQueries(); |
| 249 | |
| 250 | // using gethostbyname2() to resolve ipv6 hello.example.com. to 2001:db8::42 |
| 251 | // Ensure the v6 address and cnames are correct |
| 252 | cnamecount = 0; |
| 253 | dns.clearQueries(); |
| 254 | result = gethostbyname2("hello", AF_INET6); |
| 255 | for (unsigned i = 0; result != nullptr && result->h_aliases[i] != nullptr; i++) { |
| 256 | std::string domain_name = records[i].host_name.substr(0, records[i].host_name.size() - 1); |
| 257 | EXPECT_EQ(result->h_aliases[i], domain_name); |
| 258 | cnamecount++; |
| 259 | } |
| 260 | // The size of "Non-cname type" DNS record in records is 2 |
| 261 | ASSERT_EQ(cnamecount, records.size() - 2); |
| 262 | ASSERT_FALSE(result == nullptr); |
| 263 | ASSERT_EQ(16, result->h_length); |
| 264 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 265 | EXPECT_EQ("2001:db8::42", ToString(result)); |
| 266 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 267 | } |
| 268 | |
| 269 | TEST_F(ResolverTest, GetHostByName_cnamesInfiniteLoop) { |
| 270 | test::DNSResponder dns; |
| 271 | const std::vector<DnsRecord> records = { |
| 272 | {kHelloExampleCom, ns_type::ns_t_cname, "a.example.com."}, |
| 273 | {"a.example.com.", ns_type::ns_t_cname, kHelloExampleCom}, |
| 274 | }; |
| 275 | StartDns(dns, records); |
| 276 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 277 | |
| 278 | const hostent* result; |
| 279 | result = gethostbyname2("hello", AF_INET); |
| 280 | ASSERT_TRUE(result == nullptr); |
| 281 | |
| 282 | dns.clearQueries(); |
| 283 | result = gethostbyname2("hello", AF_INET6); |
| 284 | ASSERT_TRUE(result == nullptr); |
| 285 | } |
| 286 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 287 | TEST_F(ResolverTest, GetHostByName_localhost) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 288 | constexpr char name_camelcase[] = "LocalHost"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 289 | constexpr char name_ip6_dot[] = "ip6-localhost."; |
| 290 | constexpr char name_ip6_fqdn[] = "ip6-localhost.example.com."; |
| 291 | |
| 292 | // Add a dummy nameserver which shouldn't receive any queries |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 293 | test::DNSResponder dns; |
| 294 | StartDns(dns, {}); |
| 295 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 296 | |
| 297 | // Expect no DNS queries; localhost is resolved via /etc/hosts |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 298 | const hostent* result = gethostbyname(kLocalHost); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 299 | EXPECT_TRUE(dns.queries().empty()) << dns.dumpQueries(); |
| 300 | ASSERT_FALSE(result == nullptr); |
| 301 | ASSERT_EQ(4, result->h_length); |
| 302 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 303 | EXPECT_EQ(kLocalHostAddr, ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 304 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 305 | |
| 306 | // Ensure the hosts file resolver ignores case of hostnames |
| 307 | result = gethostbyname(name_camelcase); |
| 308 | EXPECT_TRUE(dns.queries().empty()) << dns.dumpQueries(); |
| 309 | ASSERT_FALSE(result == nullptr); |
| 310 | ASSERT_EQ(4, result->h_length); |
| 311 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 312 | EXPECT_EQ(kLocalHostAddr, ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 313 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 314 | |
| 315 | // The hosts file also contains ip6-localhost, but gethostbyname() won't |
chenbruce | 018fdb2 | 2019-06-12 18:08:04 +0800 | [diff] [blame] | 316 | // return it. This would be easy to |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 317 | // change, but there's no point in changing the legacy behavior; new code |
| 318 | // should be calling getaddrinfo() anyway. |
| 319 | // So we check the legacy behavior, which results in amusing A-record |
| 320 | // lookups for ip6-localhost, with and without search domains appended. |
| 321 | dns.clearQueries(); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 322 | result = gethostbyname(kIp6LocalHost); |
Sehee Park | bbca020 | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 323 | EXPECT_EQ(2U, dns.queries().size()) << dns.dumpQueries(); |
| 324 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, name_ip6_dot)) << dns.dumpQueries(); |
| 325 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, name_ip6_fqdn)) << dns.dumpQueries(); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 326 | ASSERT_TRUE(result == nullptr); |
| 327 | |
| 328 | // Finally, use gethostbyname2() to resolve ip6-localhost to ::1 from |
| 329 | // the hosts file. |
| 330 | dns.clearQueries(); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 331 | result = gethostbyname2(kIp6LocalHost, AF_INET6); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 332 | EXPECT_TRUE(dns.queries().empty()) << dns.dumpQueries(); |
| 333 | ASSERT_FALSE(result == nullptr); |
| 334 | ASSERT_EQ(16, result->h_length); |
| 335 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 336 | EXPECT_EQ(kIp6LocalHostAddr, ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 337 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST_F(ResolverTest, GetHostByName_numeric) { |
| 341 | // Add a dummy nameserver which shouldn't receive any queries |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 342 | test::DNSResponder dns; |
| 343 | StartDns(dns, {}); |
| 344 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 345 | |
| 346 | // Numeric v4 address: expect no DNS queries |
| 347 | constexpr char numeric_v4[] = "192.168.0.1"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 348 | const hostent* result = gethostbyname(numeric_v4); |
Sehee Park | bbca020 | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 349 | EXPECT_EQ(0U, dns.queries().size()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 350 | ASSERT_FALSE(result == nullptr); |
| 351 | ASSERT_EQ(4, result->h_length); // v4 |
| 352 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 353 | EXPECT_EQ(numeric_v4, ToString(result)); |
| 354 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 355 | |
| 356 | // gethostbyname() recognizes a v6 address, and fails with no DNS queries |
| 357 | constexpr char numeric_v6[] = "2001:db8::42"; |
| 358 | dns.clearQueries(); |
| 359 | result = gethostbyname(numeric_v6); |
Sehee Park | bbca020 | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 360 | EXPECT_EQ(0U, dns.queries().size()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 361 | EXPECT_TRUE(result == nullptr); |
| 362 | |
| 363 | // Numeric v6 address with gethostbyname2(): succeeds with no DNS queries |
| 364 | dns.clearQueries(); |
| 365 | result = gethostbyname2(numeric_v6, AF_INET6); |
Sehee Park | bbca020 | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 366 | EXPECT_EQ(0U, dns.queries().size()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 367 | ASSERT_FALSE(result == nullptr); |
| 368 | ASSERT_EQ(16, result->h_length); // v6 |
| 369 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 370 | EXPECT_EQ(numeric_v6, ToString(result)); |
| 371 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 372 | |
| 373 | // Numeric v6 address with scope work with getaddrinfo(), |
| 374 | // but gethostbyname2() does not understand them; it issues two dns |
| 375 | // queries, then fails. This hardly ever happens, there's no point |
| 376 | // in fixing this. This test simply verifies the current (bogus) |
| 377 | // behavior to avoid further regressions (like crashes, or leaks). |
| 378 | constexpr char numeric_v6_scope[] = "fe80::1%lo"; |
| 379 | dns.clearQueries(); |
| 380 | result = gethostbyname2(numeric_v6_scope, AF_INET6); |
Sehee Park | bbca020 | 2018-11-16 10:53:16 +0900 | [diff] [blame] | 381 | EXPECT_EQ(2U, dns.queries().size()); // OUCH! |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 382 | ASSERT_TRUE(result == nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | TEST_F(ResolverTest, BinderSerialization) { |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 386 | using android::net::IDnsResolver; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 387 | std::vector<int> params_offsets = { |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 388 | IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY, |
| 389 | IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD, |
| 390 | IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES, |
| 391 | IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES, |
| 392 | IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC, |
| 393 | IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT, |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 394 | }; |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 395 | const int size = static_cast<int>(params_offsets.size()); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 396 | EXPECT_EQ(size, IDnsResolver::RESOLVER_PARAMS_COUNT); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 397 | std::sort(params_offsets.begin(), params_offsets.end()); |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 398 | for (int i = 0; i < size; ++i) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 399 | EXPECT_EQ(params_offsets[i], i); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | TEST_F(ResolverTest, GetHostByName_Binder) { |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 404 | using android::net::IDnsResolver; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 405 | |
| 406 | std::vector<std::string> domains = { "example.com" }; |
| 407 | std::vector<std::unique_ptr<test::DNSResponder>> dns; |
| 408 | std::vector<std::string> servers; |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 409 | std::vector<DnsResponderClient::Mapping> mappings; |
| 410 | ASSERT_NO_FATAL_FAILURE(mDnsClient.SetupMappings(1, domains, &mappings)); |
| 411 | ASSERT_NO_FATAL_FAILURE(mDnsClient.SetupDNSServers(4, mappings, &dns, &servers)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 412 | ASSERT_EQ(1U, mappings.size()); |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 413 | const DnsResponderClient::Mapping& mapping = mappings[0]; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 414 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 415 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, domains, kDefaultParams)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 416 | |
| 417 | const hostent* result = gethostbyname(mapping.host.c_str()); |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 418 | const size_t total_queries = |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 419 | std::accumulate(dns.begin(), dns.end(), 0, [&mapping](size_t total, auto& d) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 420 | return total + GetNumQueriesForType(*d, ns_type::ns_t_a, mapping.entry.c_str()); |
| 421 | }); |
| 422 | |
| 423 | EXPECT_LE(1U, total_queries); |
| 424 | ASSERT_FALSE(result == nullptr); |
| 425 | ASSERT_EQ(4, result->h_length); |
| 426 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 427 | EXPECT_EQ(mapping.ip4, ToString(result)); |
| 428 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
| 429 | |
| 430 | std::vector<std::string> res_servers; |
| 431 | std::vector<std::string> res_domains; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 432 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 433 | res_params res_params; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 434 | std::vector<ResolverStats> res_stats; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 435 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 436 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, &res_domains, |
| 437 | &res_tls_servers, &res_params, &res_stats, |
| 438 | &wait_for_pending_req_timeout_count)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 439 | EXPECT_EQ(servers.size(), res_servers.size()); |
| 440 | EXPECT_EQ(domains.size(), res_domains.size()); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 441 | EXPECT_EQ(0U, res_tls_servers.size()); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 442 | ASSERT_EQ(static_cast<size_t>(IDnsResolver::RESOLVER_PARAMS_COUNT), kDefaultParams.size()); |
| 443 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY], |
| 444 | res_params.sample_validity); |
| 445 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD], |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 446 | res_params.success_threshold); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 447 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES], res_params.min_samples); |
| 448 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES], res_params.max_samples); |
| 449 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC], |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 450 | res_params.base_timeout_msec); |
| 451 | EXPECT_EQ(servers.size(), res_stats.size()); |
| 452 | |
Bernie Innocenti | 0c067e4 | 2019-04-01 15:33:16 +0900 | [diff] [blame] | 453 | EXPECT_THAT(res_servers, testing::UnorderedElementsAreArray(servers)); |
| 454 | EXPECT_THAT(res_domains, testing::UnorderedElementsAreArray(domains)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | TEST_F(ResolverTest, GetAddrInfo) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 458 | constexpr char listen_addr[] = "127.0.0.4"; |
| 459 | constexpr char listen_addr2[] = "127.0.0.5"; |
| 460 | constexpr char host_name[] = "howdy.example.com."; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 461 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 462 | const std::vector<DnsRecord> records = { |
| 463 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 464 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 465 | }; |
| 466 | test::DNSResponder dns(listen_addr); |
| 467 | test::DNSResponder dns2(listen_addr2); |
| 468 | StartDns(dns, records); |
| 469 | StartDns(dns2, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 470 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 471 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork({listen_addr})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 472 | dns.clearQueries(); |
| 473 | dns2.clearQueries(); |
| 474 | |
| 475 | ScopedAddrinfo result = safe_getaddrinfo("howdy", nullptr, nullptr); |
| 476 | EXPECT_TRUE(result != nullptr); |
| 477 | size_t found = GetNumQueries(dns, host_name); |
| 478 | EXPECT_LE(1U, found); |
| 479 | // Could be A or AAAA |
| 480 | std::string result_str = ToString(result); |
| 481 | EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4") |
| 482 | << ", result_str='" << result_str << "'"; |
| 483 | |
| 484 | // Verify that the name is cached. |
| 485 | size_t old_found = found; |
| 486 | result = safe_getaddrinfo("howdy", nullptr, nullptr); |
| 487 | EXPECT_TRUE(result != nullptr); |
| 488 | found = GetNumQueries(dns, host_name); |
| 489 | EXPECT_LE(1U, found); |
| 490 | EXPECT_EQ(old_found, found); |
| 491 | result_str = ToString(result); |
| 492 | EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4") |
| 493 | << result_str; |
| 494 | |
| 495 | // Change the DNS resolver, ensure that queries are still cached. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 496 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork({listen_addr2})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 497 | dns.clearQueries(); |
| 498 | dns2.clearQueries(); |
| 499 | |
| 500 | result = safe_getaddrinfo("howdy", nullptr, nullptr); |
| 501 | EXPECT_TRUE(result != nullptr); |
| 502 | found = GetNumQueries(dns, host_name); |
| 503 | size_t found2 = GetNumQueries(dns2, host_name); |
| 504 | EXPECT_EQ(0U, found); |
| 505 | EXPECT_LE(0U, found2); |
| 506 | |
| 507 | // Could be A or AAAA |
| 508 | result_str = ToString(result); |
| 509 | EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4") |
| 510 | << ", result_str='" << result_str << "'"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | TEST_F(ResolverTest, GetAddrInfoV4) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 514 | test::DNSResponder dns; |
| 515 | StartDns(dns, {{kHelloExampleCom, ns_type::ns_t_a, "1.2.3.5"}}); |
| 516 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 517 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 518 | const addrinfo hints = {.ai_family = AF_INET}; |
| 519 | ScopedAddrinfo result = safe_getaddrinfo("hello", nullptr, &hints); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 520 | EXPECT_TRUE(result != nullptr); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 521 | EXPECT_EQ(1U, GetNumQueries(dns, kHelloExampleCom)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 522 | EXPECT_EQ("1.2.3.5", ToString(result)); |
| 523 | } |
| 524 | |
| 525 | TEST_F(ResolverTest, GetAddrInfo_localhost) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 526 | // Add a dummy nameserver which shouldn't receive any queries |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 527 | test::DNSResponder dns; |
| 528 | StartDns(dns, {}); |
| 529 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 530 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 531 | ScopedAddrinfo result = safe_getaddrinfo(kLocalHost, nullptr, nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 532 | EXPECT_TRUE(result != nullptr); |
| 533 | // Expect no DNS queries; localhost is resolved via /etc/hosts |
| 534 | EXPECT_TRUE(dns.queries().empty()) << dns.dumpQueries(); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 535 | EXPECT_EQ(kLocalHostAddr, ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 536 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 537 | result = safe_getaddrinfo(kIp6LocalHost, nullptr, nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 538 | EXPECT_TRUE(result != nullptr); |
| 539 | // Expect no DNS queries; ip6-localhost is resolved via /etc/hosts |
| 540 | EXPECT_TRUE(dns.queries().empty()) << dns.dumpQueries(); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 541 | EXPECT_EQ(kIp6LocalHostAddr, ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 542 | } |
| 543 | |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 544 | TEST_F(ResolverTest, GetAddrInfo_InvalidSocketType) { |
| 545 | test::DNSResponder dns; |
| 546 | StartDns(dns, {{kHelloExampleCom, ns_type::ns_t_a, "1.2.3.5"}}); |
| 547 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 548 | |
| 549 | // TODO: Test other invalid socket types. |
| 550 | const addrinfo hints = { |
| 551 | .ai_family = AF_UNSPEC, |
Hungming Chen | 3264747 | 2019-07-05 14:04:51 +0800 | [diff] [blame] | 552 | .ai_protocol = ANY, |
Luke Huang | d8ac475 | 2019-06-18 17:05:47 +0800 | [diff] [blame] | 553 | .ai_socktype = SOCK_PACKET, |
| 554 | }; |
| 555 | addrinfo* result = nullptr; |
| 556 | // This is a valid hint, but the query won't be sent because the socket type is |
| 557 | // not supported. |
| 558 | EXPECT_EQ(EAI_NODATA, getaddrinfo("hello", nullptr, &hints, &result)); |
| 559 | ScopedAddrinfo result_cleanup(result); |
| 560 | EXPECT_EQ(nullptr, result); |
| 561 | } |
| 562 | |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 563 | // Verify if the resolver correctly handle multiple queries simultaneously |
| 564 | // step 1: set dns server#1 into deferred responding mode. |
| 565 | // step 2: thread#1 query "hello.example.com." --> resolver send query to server#1. |
| 566 | // step 3: thread#2 query "hello.example.com." --> resolver hold the request and wait for |
| 567 | // response of previous pending query sent by thread#1. |
| 568 | // step 4: thread#3 query "konbanha.example.com." --> resolver send query to server#3. Server |
| 569 | // respond to resolver immediately. |
| 570 | // step 5: check if server#1 get 1 query by thread#1, server#2 get 0 query, server#3 get 1 query. |
| 571 | // step 6: resume dns server#1 to respond dns query in step#2. |
| 572 | // step 7: thread#1 and #2 should get returned from DNS query after step#6. Also, check the |
| 573 | // number of queries in server#2 is 0 to ensure thread#2 does not wake up unexpectedly |
| 574 | // before signaled by thread#1. |
| 575 | TEST_F(ResolverTest, GetAddrInfoV4_deferred_resp) { |
| 576 | const char* listen_addr1 = "127.0.0.9"; |
| 577 | const char* listen_addr2 = "127.0.0.10"; |
| 578 | const char* listen_addr3 = "127.0.0.11"; |
| 579 | const char* listen_srv = "53"; |
| 580 | const char* host_name_deferred = "hello.example.com."; |
| 581 | const char* host_name_normal = "konbanha.example.com."; |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 582 | test::DNSResponder dns1(listen_addr1, listen_srv, ns_rcode::ns_r_servfail); |
| 583 | test::DNSResponder dns2(listen_addr2, listen_srv, ns_rcode::ns_r_servfail); |
| 584 | test::DNSResponder dns3(listen_addr3, listen_srv, ns_rcode::ns_r_servfail); |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 585 | dns1.addMapping(host_name_deferred, ns_type::ns_t_a, "1.2.3.4"); |
| 586 | dns2.addMapping(host_name_deferred, ns_type::ns_t_a, "1.2.3.4"); |
| 587 | dns3.addMapping(host_name_normal, ns_type::ns_t_a, "1.2.3.5"); |
| 588 | ASSERT_TRUE(dns1.startServer()); |
| 589 | ASSERT_TRUE(dns2.startServer()); |
| 590 | ASSERT_TRUE(dns3.startServer()); |
| 591 | const std::vector<std::string> servers_for_t1 = {listen_addr1}; |
| 592 | const std::vector<std::string> servers_for_t2 = {listen_addr2}; |
| 593 | const std::vector<std::string> servers_for_t3 = {listen_addr3}; |
| 594 | addrinfo hints = {.ai_family = AF_INET}; |
| 595 | const std::vector<int> params = {300, 25, 8, 8, 5000}; |
| 596 | bool t3_task_done = false; |
| 597 | |
| 598 | dns1.setDeferredResp(true); |
| 599 | std::thread t1([&, this]() { |
| 600 | ASSERT_TRUE( |
| 601 | mDnsClient.SetResolversForNetwork(servers_for_t1, kDefaultSearchDomains, params)); |
| 602 | ScopedAddrinfo result = safe_getaddrinfo(host_name_deferred, nullptr, &hints); |
| 603 | // t3's dns query should got returned first |
| 604 | EXPECT_TRUE(t3_task_done); |
| 605 | EXPECT_EQ(1U, GetNumQueries(dns1, host_name_deferred)); |
| 606 | EXPECT_TRUE(result != nullptr); |
| 607 | EXPECT_EQ("1.2.3.4", ToString(result)); |
| 608 | }); |
| 609 | |
| 610 | // ensuring t1 and t2 handler functions are processed in order |
| 611 | usleep(100 * 1000); |
| 612 | std::thread t2([&, this]() { |
| 613 | ASSERT_TRUE( |
| 614 | mDnsClient.SetResolversForNetwork(servers_for_t2, kDefaultSearchDomains, params)); |
| 615 | ScopedAddrinfo result = safe_getaddrinfo(host_name_deferred, nullptr, &hints); |
| 616 | EXPECT_TRUE(t3_task_done); |
| 617 | EXPECT_EQ(0U, GetNumQueries(dns2, host_name_deferred)); |
| 618 | EXPECT_TRUE(result != nullptr); |
| 619 | EXPECT_EQ("1.2.3.4", ToString(result)); |
| 620 | |
| 621 | std::vector<std::string> res_servers; |
| 622 | std::vector<std::string> res_domains; |
| 623 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 624 | res_params res_params; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 625 | std::vector<ResolverStats> res_stats; |
| 626 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 627 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, |
| 628 | &res_domains, &res_tls_servers, &res_params, &res_stats, |
| 629 | &wait_for_pending_req_timeout_count)); |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 630 | EXPECT_EQ(0, wait_for_pending_req_timeout_count); |
| 631 | }); |
| 632 | |
| 633 | // ensuring t2 and t3 handler functions are processed in order |
| 634 | usleep(100 * 1000); |
| 635 | std::thread t3([&, this]() { |
| 636 | ASSERT_TRUE( |
| 637 | mDnsClient.SetResolversForNetwork(servers_for_t3, kDefaultSearchDomains, params)); |
| 638 | ScopedAddrinfo result = safe_getaddrinfo(host_name_normal, nullptr, &hints); |
| 639 | EXPECT_EQ(1U, GetNumQueries(dns1, host_name_deferred)); |
| 640 | EXPECT_EQ(0U, GetNumQueries(dns2, host_name_deferred)); |
| 641 | EXPECT_EQ(1U, GetNumQueries(dns3, host_name_normal)); |
| 642 | EXPECT_TRUE(result != nullptr); |
| 643 | EXPECT_EQ("1.2.3.5", ToString(result)); |
| 644 | |
| 645 | t3_task_done = true; |
| 646 | dns1.setDeferredResp(false); |
| 647 | }); |
| 648 | t3.join(); |
| 649 | t1.join(); |
| 650 | t2.join(); |
| 651 | } |
| 652 | |
lifr | 4e4a2e0 | 2019-01-29 16:53:51 +0800 | [diff] [blame] | 653 | TEST_F(ResolverTest, GetAddrInfo_cnames) { |
| 654 | constexpr char host_name[] = "host.example.com."; |
| 655 | test::DNSResponder dns; |
| 656 | const std::vector<DnsRecord> records = { |
| 657 | {kHelloExampleCom, ns_type::ns_t_cname, "a.example.com."}, |
| 658 | {"a.example.com.", ns_type::ns_t_cname, "b.example.com."}, |
| 659 | {"b.example.com.", ns_type::ns_t_cname, "c.example.com."}, |
| 660 | {"c.example.com.", ns_type::ns_t_cname, "d.example.com."}, |
| 661 | {"d.example.com.", ns_type::ns_t_cname, "e.example.com."}, |
| 662 | {"e.example.com.", ns_type::ns_t_cname, host_name}, |
| 663 | {host_name, ns_type::ns_t_a, "1.2.3.3"}, |
| 664 | {host_name, ns_type::ns_t_aaaa, "2001:db8::42"}, |
| 665 | }; |
| 666 | StartDns(dns, records); |
| 667 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 668 | |
| 669 | addrinfo hints = {.ai_family = AF_INET}; |
| 670 | ScopedAddrinfo result = safe_getaddrinfo("hello", nullptr, &hints); |
| 671 | EXPECT_TRUE(result != nullptr); |
| 672 | EXPECT_EQ("1.2.3.3", ToString(result)); |
| 673 | |
| 674 | dns.clearQueries(); |
| 675 | hints = {.ai_family = AF_INET6}; |
| 676 | result = safe_getaddrinfo("hello", nullptr, &hints); |
| 677 | EXPECT_TRUE(result != nullptr); |
| 678 | EXPECT_EQ("2001:db8::42", ToString(result)); |
| 679 | } |
| 680 | |
| 681 | TEST_F(ResolverTest, GetAddrInfo_cnamesNoIpAddress) { |
| 682 | test::DNSResponder dns; |
| 683 | const std::vector<DnsRecord> records = { |
| 684 | {kHelloExampleCom, ns_type::ns_t_cname, "a.example.com."}, |
| 685 | }; |
| 686 | StartDns(dns, records); |
| 687 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 688 | |
| 689 | addrinfo hints = {.ai_family = AF_INET}; |
| 690 | ScopedAddrinfo result = safe_getaddrinfo("hello", nullptr, &hints); |
| 691 | EXPECT_TRUE(result == nullptr); |
| 692 | |
| 693 | dns.clearQueries(); |
| 694 | hints = {.ai_family = AF_INET6}; |
| 695 | result = safe_getaddrinfo("hello", nullptr, &hints); |
| 696 | EXPECT_TRUE(result == nullptr); |
| 697 | } |
| 698 | |
| 699 | TEST_F(ResolverTest, GetAddrInfo_cnamesIllegalRdata) { |
| 700 | test::DNSResponder dns; |
| 701 | const std::vector<DnsRecord> records = { |
| 702 | {kHelloExampleCom, ns_type::ns_t_cname, ".!#?"}, |
| 703 | }; |
| 704 | StartDns(dns, records); |
| 705 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 706 | |
| 707 | addrinfo hints = {.ai_family = AF_INET}; |
| 708 | ScopedAddrinfo result = safe_getaddrinfo("hello", nullptr, &hints); |
| 709 | EXPECT_TRUE(result == nullptr); |
| 710 | |
| 711 | dns.clearQueries(); |
| 712 | hints = {.ai_family = AF_INET6}; |
| 713 | result = safe_getaddrinfo("hello", nullptr, &hints); |
| 714 | EXPECT_TRUE(result == nullptr); |
| 715 | } |
| 716 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 717 | TEST_F(ResolverTest, MultidomainResolution) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 718 | constexpr char host_name[] = "nihao.example2.com."; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 719 | std::vector<std::string> searchDomains = { "example1.com", "example2.com", "example3.com" }; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 720 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 721 | test::DNSResponder dns("127.0.0.6"); |
| 722 | StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.3"}}); |
| 723 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork({"127.0.0.6"}, searchDomains)); |
| 724 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 725 | const hostent* result = gethostbyname("nihao"); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 726 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 727 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, host_name)); |
| 728 | ASSERT_FALSE(result == nullptr); |
| 729 | ASSERT_EQ(4, result->h_length); |
| 730 | ASSERT_FALSE(result->h_addr_list[0] == nullptr); |
| 731 | EXPECT_EQ("1.2.3.3", ToString(result)); |
| 732 | EXPECT_TRUE(result->h_addr_list[1] == nullptr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | TEST_F(ResolverTest, GetAddrInfoV6_numeric) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 736 | constexpr char host_name[] = "ohayou.example.com."; |
| 737 | constexpr char numeric_addr[] = "fe80::1%lo"; |
| 738 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 739 | test::DNSResponder dns; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 740 | dns.setResponseProbability(0.0); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 741 | StartDns(dns, {{host_name, ns_type::ns_t_aaaa, "2001:db8::5"}}); |
| 742 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 743 | |
| 744 | addrinfo hints = {.ai_family = AF_INET6}; |
| 745 | ScopedAddrinfo result = safe_getaddrinfo(numeric_addr, nullptr, &hints); |
| 746 | EXPECT_TRUE(result != nullptr); |
| 747 | EXPECT_EQ(numeric_addr, ToString(result)); |
| 748 | EXPECT_TRUE(dns.queries().empty()); // Ensure no DNS queries were sent out |
| 749 | |
| 750 | // Now try a non-numeric hostname query with the AI_NUMERICHOST flag set. |
| 751 | // We should fail without sending out a DNS query. |
| 752 | hints.ai_flags |= AI_NUMERICHOST; |
| 753 | result = safe_getaddrinfo(host_name, nullptr, &hints); |
| 754 | EXPECT_TRUE(result == nullptr); |
| 755 | EXPECT_TRUE(dns.queries().empty()); // Ensure no DNS queries were sent out |
| 756 | } |
| 757 | |
| 758 | TEST_F(ResolverTest, GetAddrInfoV6_failing) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 759 | constexpr char listen_addr0[] = "127.0.0.7"; |
| 760 | constexpr char listen_addr1[] = "127.0.0.8"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 761 | const char* host_name = "ohayou.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 762 | |
| 763 | test::DNSResponder dns0(listen_addr0); |
| 764 | test::DNSResponder dns1(listen_addr1); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 765 | dns0.setResponseProbability(0.0); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 766 | StartDns(dns0, {{host_name, ns_type::ns_t_aaaa, "2001:db8::5"}}); |
| 767 | StartDns(dns1, {{host_name, ns_type::ns_t_aaaa, "2001:db8::6"}}); |
| 768 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 769 | std::vector<std::string> servers = { listen_addr0, listen_addr1 }; |
| 770 | // <sample validity in s> <success threshold in percent> <min samples> <max samples> |
| 771 | int sample_count = 8; |
| 772 | const std::vector<int> params = { 300, 25, sample_count, sample_count }; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 773 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, kDefaultSearchDomains, params)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 774 | |
| 775 | // Repeatedly perform resolutions for non-existing domains until MAXNSSAMPLES resolutions have |
| 776 | // reached the dns0, which is set to fail. No more requests should then arrive at that server |
| 777 | // for the next sample_lifetime seconds. |
| 778 | // TODO: This approach is implementation-dependent, change once metrics reporting is available. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 779 | const addrinfo hints = {.ai_family = AF_INET6}; |
Bernie Innocenti | b102dd2 | 2018-12-04 14:57:48 +0900 | [diff] [blame] | 780 | for (int i = 0; i < sample_count; ++i) { |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 781 | std::string domain = StringPrintf("nonexistent%d", i); |
| 782 | ScopedAddrinfo result = safe_getaddrinfo(domain.c_str(), nullptr, &hints); |
| 783 | } |
| 784 | // Due to 100% errors for all possible samples, the server should be ignored from now on and |
| 785 | // only the second one used for all following queries, until NSSAMPLE_VALIDITY is reached. |
| 786 | dns0.clearQueries(); |
| 787 | dns1.clearQueries(); |
| 788 | ScopedAddrinfo result = safe_getaddrinfo("ohayou", nullptr, &hints); |
| 789 | EXPECT_TRUE(result != nullptr); |
| 790 | EXPECT_EQ(0U, GetNumQueries(dns0, host_name)); |
| 791 | EXPECT_EQ(1U, GetNumQueries(dns1, host_name)); |
| 792 | } |
| 793 | |
| 794 | TEST_F(ResolverTest, GetAddrInfoV6_nonresponsive) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 795 | constexpr char listen_addr0[] = "127.0.0.7"; |
| 796 | constexpr char listen_addr1[] = "127.0.0.8"; |
| 797 | constexpr char listen_srv[] = "53"; |
| 798 | constexpr char host_name1[] = "ohayou.example.com."; |
| 799 | constexpr char host_name2[] = "ciao.example.com."; |
Luke Huang | 483cf33 | 2019-06-03 17:24:51 +0800 | [diff] [blame] | 800 | const std::vector<std::string> defaultSearchDomain = {"example.com"}; |
| 801 | // The minimal timeout is 1000ms, so we can't decrease timeout |
| 802 | // So reduce retry count. |
| 803 | const std::vector<int> reduceRetryParams = { |
| 804 | 300, // sample validity in seconds |
| 805 | 25, // success threshod in percent |
| 806 | 8, 8, // {MIN,MAX}_SAMPLES |
| 807 | 1000, // BASE_TIMEOUT_MSEC |
| 808 | 1, // retry count |
| 809 | }; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 810 | const std::vector<DnsRecord> records0 = { |
| 811 | {host_name1, ns_type::ns_t_aaaa, "2001:db8::5"}, |
| 812 | {host_name2, ns_type::ns_t_aaaa, "2001:db8::5"}, |
| 813 | }; |
| 814 | const std::vector<DnsRecord> records1 = { |
| 815 | {host_name1, ns_type::ns_t_aaaa, "2001:db8::6"}, |
| 816 | {host_name2, ns_type::ns_t_aaaa, "2001:db8::6"}, |
| 817 | }; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 818 | |
| 819 | // dns0 does not respond with 100% probability, while |
| 820 | // dns1 responds normally, at least initially. |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 821 | test::DNSResponder dns0(listen_addr0, listen_srv, static_cast<ns_rcode>(-1)); |
| 822 | test::DNSResponder dns1(listen_addr1, listen_srv, static_cast<ns_rcode>(-1)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 823 | dns0.setResponseProbability(0.0); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 824 | StartDns(dns0, records0); |
| 825 | StartDns(dns1, records1); |
Luke Huang | 483cf33 | 2019-06-03 17:24:51 +0800 | [diff] [blame] | 826 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork({listen_addr0, listen_addr1}, defaultSearchDomain, |
| 827 | reduceRetryParams)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 828 | |
Luke Huang | 483cf33 | 2019-06-03 17:24:51 +0800 | [diff] [blame] | 829 | // Specify ai_socktype to make getaddrinfo will only query 1 time |
| 830 | const addrinfo hints = {.ai_family = AF_INET6, .ai_socktype = SOCK_STREAM}; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 831 | |
| 832 | // dns0 will ignore the request, and we'll fallback to dns1 after the first |
| 833 | // retry. |
| 834 | ScopedAddrinfo result = safe_getaddrinfo(host_name1, nullptr, &hints); |
| 835 | EXPECT_TRUE(result != nullptr); |
| 836 | EXPECT_EQ(1U, GetNumQueries(dns0, host_name1)); |
| 837 | EXPECT_EQ(1U, GetNumQueries(dns1, host_name1)); |
| 838 | |
| 839 | // Now make dns1 also ignore 100% requests... The resolve should alternate |
Luke Huang | 483cf33 | 2019-06-03 17:24:51 +0800 | [diff] [blame] | 840 | // queries between the nameservers and fail |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 841 | dns1.setResponseProbability(0.0); |
| 842 | addrinfo* result2 = nullptr; |
| 843 | EXPECT_EQ(EAI_NODATA, getaddrinfo(host_name2, nullptr, &hints, &result2)); |
| 844 | EXPECT_EQ(nullptr, result2); |
Luke Huang | 483cf33 | 2019-06-03 17:24:51 +0800 | [diff] [blame] | 845 | EXPECT_EQ(1U, GetNumQueries(dns0, host_name2)); |
| 846 | EXPECT_EQ(1U, GetNumQueries(dns1, host_name2)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | TEST_F(ResolverTest, GetAddrInfoV6_concurrent) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 850 | constexpr char listen_addr0[] = "127.0.0.9"; |
| 851 | constexpr char listen_addr1[] = "127.0.0.10"; |
| 852 | constexpr char listen_addr2[] = "127.0.0.11"; |
| 853 | constexpr char host_name[] = "konbanha.example.com."; |
| 854 | |
| 855 | test::DNSResponder dns0(listen_addr0); |
| 856 | test::DNSResponder dns1(listen_addr1); |
| 857 | test::DNSResponder dns2(listen_addr2); |
| 858 | StartDns(dns0, {{host_name, ns_type::ns_t_aaaa, "2001:db8::5"}}); |
| 859 | StartDns(dns1, {{host_name, ns_type::ns_t_aaaa, "2001:db8::6"}}); |
| 860 | StartDns(dns2, {{host_name, ns_type::ns_t_aaaa, "2001:db8::7"}}); |
| 861 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 862 | const std::vector<std::string> servers = { listen_addr0, listen_addr1, listen_addr2 }; |
| 863 | std::vector<std::thread> threads(10); |
| 864 | for (std::thread& thread : threads) { |
| 865 | thread = std::thread([this, &servers]() { |
| 866 | unsigned delay = arc4random_uniform(1*1000*1000); // <= 1s |
| 867 | usleep(delay); |
| 868 | std::vector<std::string> serverSubset; |
| 869 | for (const auto& server : servers) { |
| 870 | if (arc4random_uniform(2)) { |
| 871 | serverSubset.push_back(server); |
| 872 | } |
| 873 | } |
| 874 | if (serverSubset.empty()) serverSubset = servers; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 875 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(serverSubset)); |
| 876 | const addrinfo hints = {.ai_family = AF_INET6}; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 877 | addrinfo* result = nullptr; |
| 878 | int rv = getaddrinfo("konbanha", nullptr, &hints, &result); |
| 879 | EXPECT_EQ(0, rv) << "error [" << rv << "] " << gai_strerror(rv); |
| 880 | if (result) { |
| 881 | freeaddrinfo(result); |
| 882 | result = nullptr; |
| 883 | } |
| 884 | }); |
| 885 | } |
| 886 | for (std::thread& thread : threads) { |
| 887 | thread.join(); |
| 888 | } |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 889 | |
| 890 | std::vector<std::string> res_servers; |
| 891 | std::vector<std::string> res_domains; |
| 892 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 893 | res_params res_params; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 894 | std::vector<ResolverStats> res_stats; |
| 895 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 896 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, &res_domains, |
| 897 | &res_tls_servers, &res_params, &res_stats, |
| 898 | &wait_for_pending_req_timeout_count)); |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 899 | EXPECT_EQ(0, wait_for_pending_req_timeout_count); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 900 | } |
| 901 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 902 | TEST_F(ResolverTest, EmptySetup) { |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 903 | using android::net::IDnsResolver; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 904 | std::vector<std::string> servers; |
| 905 | std::vector<std::string> domains; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 906 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, domains)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 907 | std::vector<std::string> res_servers; |
| 908 | std::vector<std::string> res_domains; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 909 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 910 | res_params res_params; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 911 | std::vector<ResolverStats> res_stats; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 912 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 913 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, &res_domains, |
| 914 | &res_tls_servers, &res_params, &res_stats, |
| 915 | &wait_for_pending_req_timeout_count)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 916 | EXPECT_EQ(0U, res_servers.size()); |
| 917 | EXPECT_EQ(0U, res_domains.size()); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 918 | EXPECT_EQ(0U, res_tls_servers.size()); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 919 | ASSERT_EQ(static_cast<size_t>(IDnsResolver::RESOLVER_PARAMS_COUNT), kDefaultParams.size()); |
| 920 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY], |
| 921 | res_params.sample_validity); |
| 922 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD], |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 923 | res_params.success_threshold); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 924 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES], res_params.min_samples); |
| 925 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES], res_params.max_samples); |
| 926 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC], |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 927 | res_params.base_timeout_msec); |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 928 | EXPECT_EQ(kDefaultParams[IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT], res_params.retry_count); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | TEST_F(ResolverTest, SearchPathChange) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 932 | constexpr char listen_addr[] = "127.0.0.13"; |
| 933 | constexpr char host_name1[] = "test13.domain1.org."; |
| 934 | constexpr char host_name2[] = "test13.domain2.org."; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 935 | std::vector<std::string> servers = { listen_addr }; |
| 936 | std::vector<std::string> domains = { "domain1.org" }; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 937 | |
| 938 | const std::vector<DnsRecord> records = { |
| 939 | {host_name1, ns_type::ns_t_aaaa, "2001:db8::13"}, |
| 940 | {host_name2, ns_type::ns_t_aaaa, "2001:db8::1:13"}, |
| 941 | }; |
| 942 | test::DNSResponder dns(listen_addr); |
| 943 | StartDns(dns, records); |
| 944 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, domains)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 945 | |
| 946 | const addrinfo hints = {.ai_family = AF_INET6}; |
| 947 | ScopedAddrinfo result = safe_getaddrinfo("test13", nullptr, &hints); |
| 948 | EXPECT_TRUE(result != nullptr); |
| 949 | EXPECT_EQ(1U, dns.queries().size()); |
| 950 | EXPECT_EQ(1U, GetNumQueries(dns, host_name1)); |
| 951 | EXPECT_EQ("2001:db8::13", ToString(result)); |
| 952 | |
| 953 | // Test that changing the domain search path on its own works. |
| 954 | domains = { "domain2.org" }; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 955 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, domains)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 956 | dns.clearQueries(); |
| 957 | |
| 958 | result = safe_getaddrinfo("test13", nullptr, &hints); |
| 959 | EXPECT_TRUE(result != nullptr); |
| 960 | EXPECT_EQ(1U, dns.queries().size()); |
| 961 | EXPECT_EQ(1U, GetNumQueries(dns, host_name2)); |
| 962 | EXPECT_EQ("2001:db8::1:13", ToString(result)); |
| 963 | } |
| 964 | |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 965 | namespace { |
| 966 | |
| 967 | std::vector<std::string> getResolverDomains(android::net::IDnsResolver* dnsResolverService, |
| 968 | unsigned netId) { |
| 969 | std::vector<std::string> res_servers; |
| 970 | std::vector<std::string> res_domains; |
| 971 | std::vector<std::string> res_tls_servers; |
| 972 | res_params res_params; |
| 973 | std::vector<ResolverStats> res_stats; |
| 974 | int wait_for_pending_req_timeout_count; |
| 975 | GetResolverInfo(dnsResolverService, netId, &res_servers, &res_domains, &res_tls_servers, |
| 976 | &res_params, &res_stats, &wait_for_pending_req_timeout_count); |
| 977 | return res_domains; |
| 978 | } |
| 979 | |
| 980 | } // namespace |
| 981 | |
| 982 | TEST_F(ResolverTest, SearchPathPrune) { |
| 983 | constexpr size_t DUPLICATED_DOMAIN_NUM = 3; |
| 984 | constexpr char listen_addr[] = "127.0.0.13"; |
| 985 | constexpr char domian_name1[] = "domain13.org."; |
| 986 | constexpr char domian_name2[] = "domain14.org."; |
| 987 | constexpr char host_name1[] = "test13.domain13.org."; |
| 988 | constexpr char host_name2[] = "test14.domain14.org."; |
| 989 | std::vector<std::string> servers = {listen_addr}; |
| 990 | |
| 991 | std::vector<std::string> testDomains1; |
| 992 | std::vector<std::string> testDomains2; |
| 993 | // Domain length should be <= 255 |
| 994 | // Max number of domains in search path is 6 |
| 995 | for (size_t i = 0; i < MAXDNSRCH + 1; i++) { |
| 996 | // Fill up with invalid domain |
| 997 | testDomains1.push_back(std::string(300, i + '0')); |
| 998 | // Fill up with valid but duplicated domain |
| 999 | testDomains2.push_back(StringPrintf("domain%zu.org", i % DUPLICATED_DOMAIN_NUM)); |
| 1000 | } |
| 1001 | |
| 1002 | // Add valid domain used for query. |
| 1003 | testDomains1.push_back(domian_name1); |
| 1004 | |
| 1005 | // Add valid domain twice used for query. |
| 1006 | testDomains2.push_back(domian_name2); |
| 1007 | testDomains2.push_back(domian_name2); |
| 1008 | |
| 1009 | const std::vector<DnsRecord> records = { |
| 1010 | {host_name1, ns_type::ns_t_aaaa, "2001:db8::13"}, |
| 1011 | {host_name2, ns_type::ns_t_aaaa, "2001:db8::1:13"}, |
| 1012 | }; |
| 1013 | test::DNSResponder dns(listen_addr); |
| 1014 | StartDns(dns, records); |
| 1015 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, testDomains1)); |
| 1016 | |
| 1017 | const addrinfo hints = {.ai_family = AF_INET6}; |
| 1018 | ScopedAddrinfo result = safe_getaddrinfo("test13", nullptr, &hints); |
| 1019 | |
| 1020 | EXPECT_TRUE(result != nullptr); |
| 1021 | |
| 1022 | EXPECT_EQ(1U, dns.queries().size()); |
| 1023 | EXPECT_EQ(1U, GetNumQueries(dns, host_name1)); |
| 1024 | EXPECT_EQ("2001:db8::13", ToString(result)); |
| 1025 | |
| 1026 | const auto& res_domains1 = getResolverDomains(mDnsClient.resolvService(), TEST_NETID); |
| 1027 | // Expect 1 valid domain, invalid domains are removed. |
| 1028 | ASSERT_EQ(1U, res_domains1.size()); |
| 1029 | EXPECT_EQ(domian_name1, res_domains1[0]); |
| 1030 | |
| 1031 | dns.clearQueries(); |
| 1032 | |
| 1033 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, testDomains2)); |
| 1034 | |
| 1035 | result = safe_getaddrinfo("test14", nullptr, &hints); |
| 1036 | EXPECT_TRUE(result != nullptr); |
| 1037 | |
| 1038 | // (3 domains * 2 retries) + 1 success query = 7 |
| 1039 | EXPECT_EQ(7U, dns.queries().size()); |
| 1040 | EXPECT_EQ(1U, GetNumQueries(dns, host_name2)); |
| 1041 | EXPECT_EQ("2001:db8::1:13", ToString(result)); |
| 1042 | |
| 1043 | const auto& res_domains2 = getResolverDomains(mDnsClient.resolvService(), TEST_NETID); |
| 1044 | // Expect 4 valid domain, duplicate domains are removed. |
| 1045 | EXPECT_EQ(DUPLICATED_DOMAIN_NUM + 1U, res_domains2.size()); |
| 1046 | EXPECT_THAT( |
| 1047 | std::vector<std::string>({"domain0.org", "domain1.org", "domain2.org", domian_name2}), |
| 1048 | testing::ElementsAreArray(res_domains2)); |
| 1049 | } |
| 1050 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1051 | static std::string base64Encode(const std::vector<uint8_t>& input) { |
| 1052 | size_t out_len; |
| 1053 | EXPECT_EQ(1, EVP_EncodedLength(&out_len, input.size())); |
| 1054 | // out_len includes the trailing NULL. |
| 1055 | uint8_t output_bytes[out_len]; |
| 1056 | EXPECT_EQ(out_len - 1, EVP_EncodeBlock(output_bytes, input.data(), input.size())); |
| 1057 | return std::string(reinterpret_cast<char*>(output_bytes)); |
| 1058 | } |
| 1059 | |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1060 | // If we move this function to dns_responder_client, it will complicate the dependency need of |
| 1061 | // dns_tls_frontend.h. |
| 1062 | static void setupTlsServers(const std::vector<std::string>& servers, |
| 1063 | std::vector<std::unique_ptr<test::DnsTlsFrontend>>* tls, |
| 1064 | std::vector<std::string>* fingerprints) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1065 | constexpr char listen_udp[] = "53"; |
| 1066 | constexpr char listen_tls[] = "853"; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1067 | |
| 1068 | for (const auto& server : servers) { |
| 1069 | auto t = std::make_unique<test::DnsTlsFrontend>(server, listen_tls, server, listen_udp); |
| 1070 | t = std::make_unique<test::DnsTlsFrontend>(server, listen_tls, server, listen_udp); |
| 1071 | t->startServer(); |
| 1072 | fingerprints->push_back(base64Encode(t->fingerprint())); |
| 1073 | tls->push_back(std::move(t)); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1077 | TEST_F(ResolverTest, MaxServerPrune_Binder) { |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1078 | std::vector<std::string> domains; |
| 1079 | std::vector<std::unique_ptr<test::DNSResponder>> dns; |
| 1080 | std::vector<std::unique_ptr<test::DnsTlsFrontend>> tls; |
| 1081 | std::vector<std::string> servers; |
| 1082 | std::vector<std::string> fingerprints; |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1083 | std::vector<DnsResponderClient::Mapping> mappings; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1084 | |
| 1085 | for (unsigned i = 0; i < MAXDNSRCH + 1; i++) { |
| 1086 | domains.push_back(StringPrintf("example%u.com", i)); |
| 1087 | } |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1088 | ASSERT_NO_FATAL_FAILURE(mDnsClient.SetupMappings(1, domains, &mappings)); |
| 1089 | ASSERT_NO_FATAL_FAILURE(mDnsClient.SetupDNSServers(MAXNS + 1, mappings, &dns, &servers)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1090 | ASSERT_NO_FATAL_FAILURE(setupTlsServers(servers, &tls, &fingerprints)); |
| 1091 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1092 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, domains, kDefaultParams, "", fingerprints)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1093 | |
Mike Yu | 383855b | 2019-01-15 17:53:27 +0800 | [diff] [blame] | 1094 | // If the private DNS validation hasn't completed yet before backend DNS servers stop, |
| 1095 | // TLS servers will get stuck in handleOneRequest(), which causes this test stuck in |
| 1096 | // ~DnsTlsFrontend() because the TLS server loop threads can't be terminated. |
| 1097 | // So, wait for private DNS validation done before stopping backend DNS servers. |
| 1098 | for (int i = 0; i < MAXNS; i++) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 1099 | LOG(INFO) << "Waiting for private DNS validation on " << tls[i]->listen_address() << "."; |
Mike Yu | 383855b | 2019-01-15 17:53:27 +0800 | [diff] [blame] | 1100 | EXPECT_TRUE(tls[i]->waitForQueries(1, 5000)); |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 1101 | LOG(INFO) << "private DNS validation on " << tls[i]->listen_address() << " done."; |
Mike Yu | 383855b | 2019-01-15 17:53:27 +0800 | [diff] [blame] | 1102 | } |
| 1103 | |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1104 | std::vector<std::string> res_servers; |
| 1105 | std::vector<std::string> res_domains; |
| 1106 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 1107 | res_params res_params; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1108 | std::vector<ResolverStats> res_stats; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1109 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 1110 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, &res_domains, |
| 1111 | &res_tls_servers, &res_params, &res_stats, |
| 1112 | &wait_for_pending_req_timeout_count)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1113 | |
| 1114 | // Check the size of the stats and its contents. |
| 1115 | EXPECT_EQ(static_cast<size_t>(MAXNS), res_servers.size()); |
| 1116 | EXPECT_EQ(static_cast<size_t>(MAXNS), res_tls_servers.size()); |
| 1117 | EXPECT_EQ(static_cast<size_t>(MAXDNSRCH), res_domains.size()); |
| 1118 | EXPECT_TRUE(std::equal(servers.begin(), servers.begin() + MAXNS, res_servers.begin())); |
| 1119 | EXPECT_TRUE(std::equal(servers.begin(), servers.begin() + MAXNS, res_tls_servers.begin())); |
| 1120 | EXPECT_TRUE(std::equal(domains.begin(), domains.begin() + MAXDNSRCH, res_domains.begin())); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | TEST_F(ResolverTest, ResolverStats) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1124 | constexpr char listen_addr1[] = "127.0.0.4"; |
| 1125 | constexpr char listen_addr2[] = "127.0.0.5"; |
| 1126 | constexpr char listen_addr3[] = "127.0.0.6"; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1127 | |
| 1128 | // Set server 1 timeout. |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 1129 | test::DNSResponder dns1(listen_addr1, "53", static_cast<ns_rcode>(-1)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1130 | dns1.setResponseProbability(0.0); |
| 1131 | ASSERT_TRUE(dns1.startServer()); |
| 1132 | |
| 1133 | // Set server 2 responding server failure. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1134 | test::DNSResponder dns2(listen_addr2); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1135 | dns2.setResponseProbability(0.0); |
| 1136 | ASSERT_TRUE(dns2.startServer()); |
| 1137 | |
| 1138 | // Set server 3 workable. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1139 | test::DNSResponder dns3(listen_addr3); |
| 1140 | dns3.addMapping(kHelloExampleCom, ns_type::ns_t_a, "1.2.3.4"); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1141 | ASSERT_TRUE(dns3.startServer()); |
| 1142 | |
| 1143 | std::vector<std::string> servers = {listen_addr1, listen_addr2, listen_addr3}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1144 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1145 | |
| 1146 | dns3.clearQueries(); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1147 | const addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1148 | ScopedAddrinfo result = safe_getaddrinfo("hello", nullptr, &hints); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1149 | size_t found = GetNumQueries(dns3, kHelloExampleCom); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1150 | EXPECT_LE(1U, found); |
| 1151 | std::string result_str = ToString(result); |
| 1152 | EXPECT_TRUE(result_str == "1.2.3.4") << ", result_str='" << result_str << "'"; |
| 1153 | |
| 1154 | std::vector<std::string> res_servers; |
| 1155 | std::vector<std::string> res_domains; |
| 1156 | std::vector<std::string> res_tls_servers; |
Bernie Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 1157 | res_params res_params; |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1158 | std::vector<ResolverStats> res_stats; |
Ken Chen | 92bed61 | 2018-12-22 21:46:55 +0800 | [diff] [blame] | 1159 | int wait_for_pending_req_timeout_count; |
Luke Huang | fde8248 | 2019-06-04 01:04:53 +0800 | [diff] [blame] | 1160 | ASSERT_TRUE(GetResolverInfo(mDnsClient.resolvService(), TEST_NETID, &res_servers, &res_domains, |
| 1161 | &res_tls_servers, &res_params, &res_stats, |
| 1162 | &wait_for_pending_req_timeout_count)); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1163 | |
| 1164 | EXPECT_EQ(1, res_stats[0].timeouts); |
| 1165 | EXPECT_EQ(1, res_stats[1].errors); |
| 1166 | EXPECT_EQ(1, res_stats[2].successes); |
Mike Yu | 0a1c53d | 2018-11-26 13:26:21 +0900 | [diff] [blame] | 1167 | } |
| 1168 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1169 | // Test what happens if the specified TLS server is nonexistent. |
| 1170 | TEST_F(ResolverTest, GetHostByName_TlsMissing) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1171 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1172 | constexpr char host_name[] = "tlsmissing.example.com."; |
| 1173 | |
| 1174 | test::DNSResponder dns; |
| 1175 | StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.3"}}); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1176 | std::vector<std::string> servers = { listen_addr }; |
| 1177 | |
| 1178 | // There's nothing listening on this address, so validation will either fail or |
| 1179 | /// hang. Either way, queries will continue to flow to the DNSResponder. |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1180 | ASSERT_TRUE( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1181 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1182 | |
| 1183 | const hostent* result; |
| 1184 | |
| 1185 | result = gethostbyname("tlsmissing"); |
| 1186 | ASSERT_FALSE(result == nullptr); |
| 1187 | EXPECT_EQ("1.2.3.3", ToString(result)); |
| 1188 | |
| 1189 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1190 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | // Test what happens if the specified TLS server replies with garbage. |
| 1194 | TEST_F(ResolverTest, GetHostByName_TlsBroken) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1195 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1196 | constexpr char host_name1[] = "tlsbroken1.example.com."; |
| 1197 | constexpr char host_name2[] = "tlsbroken2.example.com."; |
| 1198 | const std::vector<DnsRecord> records = { |
| 1199 | {host_name1, ns_type::ns_t_a, "1.2.3.1"}, |
| 1200 | {host_name2, ns_type::ns_t_a, "1.2.3.2"}, |
| 1201 | }; |
| 1202 | |
| 1203 | test::DNSResponder dns; |
| 1204 | StartDns(dns, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1205 | std::vector<std::string> servers = { listen_addr }; |
| 1206 | |
| 1207 | // Bind the specified private DNS socket but don't respond to any client sockets yet. |
| 1208 | int s = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); |
| 1209 | ASSERT_TRUE(s >= 0); |
| 1210 | struct sockaddr_in tlsServer = { |
| 1211 | .sin_family = AF_INET, |
| 1212 | .sin_port = htons(853), |
| 1213 | }; |
| 1214 | ASSERT_TRUE(inet_pton(AF_INET, listen_addr, &tlsServer.sin_addr)); |
| 1215 | ASSERT_TRUE(enableSockopt(s, SOL_SOCKET, SO_REUSEPORT).ok()); |
| 1216 | ASSERT_TRUE(enableSockopt(s, SOL_SOCKET, SO_REUSEADDR).ok()); |
| 1217 | ASSERT_FALSE(bind(s, reinterpret_cast<struct sockaddr*>(&tlsServer), sizeof(tlsServer))); |
| 1218 | ASSERT_FALSE(listen(s, 1)); |
| 1219 | |
| 1220 | // Trigger TLS validation. |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1221 | ASSERT_TRUE( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1222 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1223 | |
| 1224 | struct sockaddr_storage cliaddr; |
| 1225 | socklen_t sin_size = sizeof(cliaddr); |
| 1226 | int new_fd = accept4(s, reinterpret_cast<struct sockaddr *>(&cliaddr), &sin_size, SOCK_CLOEXEC); |
| 1227 | ASSERT_TRUE(new_fd > 0); |
| 1228 | |
| 1229 | // We've received the new file descriptor but not written to it or closed, so the |
| 1230 | // validation is still pending. Queries should still flow correctly because the |
| 1231 | // server is not used until validation succeeds. |
| 1232 | const hostent* result; |
| 1233 | result = gethostbyname("tlsbroken1"); |
| 1234 | ASSERT_FALSE(result == nullptr); |
| 1235 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1236 | |
| 1237 | // Now we cause the validation to fail. |
| 1238 | std::string garbage = "definitely not a valid TLS ServerHello"; |
| 1239 | write(new_fd, garbage.data(), garbage.size()); |
| 1240 | close(new_fd); |
| 1241 | |
| 1242 | // Validation failure shouldn't interfere with lookups, because lookups won't be sent |
| 1243 | // to the TLS server unless validation succeeds. |
| 1244 | result = gethostbyname("tlsbroken2"); |
| 1245 | ASSERT_FALSE(result == nullptr); |
| 1246 | EXPECT_EQ("1.2.3.2", ToString(result)); |
| 1247 | |
| 1248 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1249 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1250 | close(s); |
| 1251 | } |
| 1252 | |
| 1253 | TEST_F(ResolverTest, GetHostByName_Tls) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1254 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1255 | constexpr char listen_udp[] = "53"; |
| 1256 | constexpr char listen_tls[] = "853"; |
| 1257 | constexpr char host_name1[] = "tls1.example.com."; |
| 1258 | constexpr char host_name2[] = "tls2.example.com."; |
| 1259 | constexpr char host_name3[] = "tls3.example.com."; |
| 1260 | const std::vector<DnsRecord> records = { |
| 1261 | {host_name1, ns_type::ns_t_a, "1.2.3.1"}, |
| 1262 | {host_name2, ns_type::ns_t_a, "1.2.3.2"}, |
| 1263 | {host_name3, ns_type::ns_t_a, "1.2.3.3"}, |
| 1264 | }; |
| 1265 | |
| 1266 | test::DNSResponder dns; |
| 1267 | StartDns(dns, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1268 | std::vector<std::string> servers = { listen_addr }; |
| 1269 | |
| 1270 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1271 | ASSERT_TRUE(tls.startServer()); |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1272 | ASSERT_TRUE( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1273 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1274 | |
| 1275 | const hostent* result; |
| 1276 | |
| 1277 | // Wait for validation to complete. |
| 1278 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1279 | |
| 1280 | result = gethostbyname("tls1"); |
| 1281 | ASSERT_FALSE(result == nullptr); |
| 1282 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1283 | |
| 1284 | // Wait for query to get counted. |
| 1285 | EXPECT_TRUE(tls.waitForQueries(2, 5000)); |
| 1286 | |
| 1287 | // Stop the TLS server. Since we're in opportunistic mode, queries will |
| 1288 | // fall back to the locally-assigned (clear text) nameservers. |
| 1289 | tls.stopServer(); |
| 1290 | |
| 1291 | dns.clearQueries(); |
| 1292 | result = gethostbyname("tls2"); |
| 1293 | EXPECT_FALSE(result == nullptr); |
| 1294 | EXPECT_EQ("1.2.3.2", ToString(result)); |
| 1295 | const auto queries = dns.queries(); |
| 1296 | EXPECT_EQ(1U, queries.size()); |
| 1297 | EXPECT_EQ("tls2.example.com.", queries[0].first); |
| 1298 | EXPECT_EQ(ns_t_a, queries[0].second); |
| 1299 | |
| 1300 | // Reset the resolvers without enabling TLS. Queries should still be routed |
| 1301 | // to the UDP endpoint. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1302 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1303 | |
| 1304 | result = gethostbyname("tls3"); |
| 1305 | ASSERT_FALSE(result == nullptr); |
| 1306 | EXPECT_EQ("1.2.3.3", ToString(result)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | TEST_F(ResolverTest, GetHostByName_TlsFingerprint) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1310 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1311 | constexpr char listen_udp[] = "53"; |
| 1312 | constexpr char listen_tls[] = "853"; |
| 1313 | test::DNSResponder dns; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1314 | ASSERT_TRUE(dns.startServer()); |
| 1315 | for (int chain_length = 1; chain_length <= 3; ++chain_length) { |
| 1316 | std::string host_name = StringPrintf("tlsfingerprint%d.example.com.", chain_length); |
| 1317 | dns.addMapping(host_name, ns_type::ns_t_a, "1.2.3.1"); |
| 1318 | std::vector<std::string> servers = { listen_addr }; |
| 1319 | |
| 1320 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1321 | tls.set_chain_length(chain_length); |
| 1322 | ASSERT_TRUE(tls.startServer()); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1323 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1324 | "", {base64Encode(tls.fingerprint())})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1325 | |
| 1326 | const hostent* result; |
| 1327 | |
| 1328 | // Wait for validation to complete. |
| 1329 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1330 | |
| 1331 | result = gethostbyname(StringPrintf("tlsfingerprint%d", chain_length).c_str()); |
| 1332 | EXPECT_FALSE(result == nullptr); |
| 1333 | if (result) { |
| 1334 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1335 | |
| 1336 | // Wait for query to get counted. |
| 1337 | EXPECT_TRUE(tls.waitForQueries(2, 5000)); |
| 1338 | } |
| 1339 | |
| 1340 | // Clear TLS bit to ensure revalidation. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1341 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1342 | tls.stopServer(); |
| 1343 | } |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | TEST_F(ResolverTest, GetHostByName_BadTlsFingerprint) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1347 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1348 | constexpr char listen_udp[] = "53"; |
| 1349 | constexpr char listen_tls[] = "853"; |
| 1350 | constexpr char host_name[] = "badtlsfingerprint.example.com."; |
| 1351 | |
| 1352 | test::DNSResponder dns; |
| 1353 | StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.1"}}); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1354 | std::vector<std::string> servers = { listen_addr }; |
| 1355 | |
| 1356 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1357 | ASSERT_TRUE(tls.startServer()); |
| 1358 | std::vector<uint8_t> bad_fingerprint = tls.fingerprint(); |
| 1359 | bad_fingerprint[5] += 1; // Corrupt the fingerprint. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1360 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1361 | {base64Encode(bad_fingerprint)})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1362 | |
| 1363 | // The initial validation should fail at the fingerprint check before |
| 1364 | // issuing a query. |
| 1365 | EXPECT_FALSE(tls.waitForQueries(1, 500)); |
| 1366 | |
| 1367 | // A fingerprint was provided and failed to match, so the query should fail. |
| 1368 | EXPECT_EQ(nullptr, gethostbyname("badtlsfingerprint")); |
| 1369 | |
| 1370 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1371 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | // Test that we can pass two different fingerprints, and connection succeeds as long as |
| 1375 | // at least one of them matches the server. |
| 1376 | TEST_F(ResolverTest, GetHostByName_TwoTlsFingerprints) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1377 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1378 | constexpr char listen_udp[] = "53"; |
| 1379 | constexpr char listen_tls[] = "853"; |
| 1380 | constexpr char host_name[] = "twotlsfingerprints.example.com."; |
| 1381 | |
| 1382 | test::DNSResponder dns; |
| 1383 | StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.1"}}); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1384 | std::vector<std::string> servers = { listen_addr }; |
| 1385 | |
| 1386 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1387 | ASSERT_TRUE(tls.startServer()); |
| 1388 | std::vector<uint8_t> bad_fingerprint = tls.fingerprint(); |
| 1389 | bad_fingerprint[5] += 1; // Corrupt the fingerprint. |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1390 | ASSERT_TRUE(mDnsClient.SetResolversWithTls( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1391 | servers, kDefaultSearchDomains, kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1392 | {base64Encode(bad_fingerprint), base64Encode(tls.fingerprint())})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1393 | |
| 1394 | const hostent* result; |
| 1395 | |
| 1396 | // Wait for validation to complete. |
| 1397 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1398 | |
| 1399 | result = gethostbyname("twotlsfingerprints"); |
| 1400 | ASSERT_FALSE(result == nullptr); |
| 1401 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1402 | |
| 1403 | // Wait for query to get counted. |
| 1404 | EXPECT_TRUE(tls.waitForQueries(2, 5000)); |
| 1405 | |
| 1406 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1407 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | TEST_F(ResolverTest, GetHostByName_TlsFingerprintGoesBad) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1411 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1412 | constexpr char listen_udp[] = "53"; |
| 1413 | constexpr char listen_tls[] = "853"; |
| 1414 | constexpr char host_name1[] = "tlsfingerprintgoesbad1.example.com."; |
| 1415 | constexpr char host_name2[] = "tlsfingerprintgoesbad2.example.com."; |
| 1416 | const std::vector<DnsRecord> records = { |
| 1417 | {host_name1, ns_type::ns_t_a, "1.2.3.1"}, |
| 1418 | {host_name2, ns_type::ns_t_a, "1.2.3.2"}, |
| 1419 | }; |
| 1420 | |
| 1421 | test::DNSResponder dns; |
| 1422 | StartDns(dns, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1423 | std::vector<std::string> servers = { listen_addr }; |
| 1424 | |
| 1425 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1426 | ASSERT_TRUE(tls.startServer()); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1427 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1428 | {base64Encode(tls.fingerprint())})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1429 | |
| 1430 | const hostent* result; |
| 1431 | |
| 1432 | // Wait for validation to complete. |
| 1433 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1434 | |
| 1435 | result = gethostbyname("tlsfingerprintgoesbad1"); |
| 1436 | ASSERT_FALSE(result == nullptr); |
| 1437 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1438 | |
| 1439 | // Wait for query to get counted. |
| 1440 | EXPECT_TRUE(tls.waitForQueries(2, 5000)); |
| 1441 | |
| 1442 | // Restart the TLS server. This will generate a new certificate whose fingerprint |
| 1443 | // no longer matches the stored fingerprint. |
| 1444 | tls.stopServer(); |
| 1445 | tls.startServer(); |
| 1446 | |
| 1447 | result = gethostbyname("tlsfingerprintgoesbad2"); |
| 1448 | ASSERT_TRUE(result == nullptr); |
| 1449 | EXPECT_EQ(HOST_NOT_FOUND, h_errno); |
| 1450 | |
| 1451 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1452 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | TEST_F(ResolverTest, GetHostByName_TlsFailover) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1456 | constexpr char listen_addr1[] = "127.0.0.3"; |
| 1457 | constexpr char listen_addr2[] = "127.0.0.4"; |
| 1458 | constexpr char listen_udp[] = "53"; |
| 1459 | constexpr char listen_tls[] = "853"; |
| 1460 | constexpr char host_name1[] = "tlsfailover1.example.com."; |
| 1461 | constexpr char host_name2[] = "tlsfailover2.example.com."; |
| 1462 | const std::vector<DnsRecord> records1 = { |
| 1463 | {host_name1, ns_type::ns_t_a, "1.2.3.1"}, |
| 1464 | {host_name2, ns_type::ns_t_a, "1.2.3.2"}, |
| 1465 | }; |
| 1466 | const std::vector<DnsRecord> records2 = { |
| 1467 | {host_name1, ns_type::ns_t_a, "1.2.3.3"}, |
| 1468 | {host_name2, ns_type::ns_t_a, "1.2.3.4"}, |
| 1469 | }; |
| 1470 | |
| 1471 | test::DNSResponder dns1(listen_addr1); |
| 1472 | test::DNSResponder dns2(listen_addr2); |
| 1473 | StartDns(dns1, records1); |
| 1474 | StartDns(dns2, records2); |
| 1475 | |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1476 | std::vector<std::string> servers = { listen_addr1, listen_addr2 }; |
| 1477 | |
| 1478 | test::DnsTlsFrontend tls1(listen_addr1, listen_tls, listen_addr1, listen_udp); |
| 1479 | test::DnsTlsFrontend tls2(listen_addr2, listen_tls, listen_addr2, listen_udp); |
| 1480 | ASSERT_TRUE(tls1.startServer()); |
| 1481 | ASSERT_TRUE(tls2.startServer()); |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1482 | ASSERT_TRUE(mDnsClient.SetResolversWithTls( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1483 | servers, kDefaultSearchDomains, kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1484 | {base64Encode(tls1.fingerprint()), base64Encode(tls2.fingerprint())})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1485 | |
| 1486 | const hostent* result; |
| 1487 | |
| 1488 | // Wait for validation to complete. |
| 1489 | EXPECT_TRUE(tls1.waitForQueries(1, 5000)); |
| 1490 | EXPECT_TRUE(tls2.waitForQueries(1, 5000)); |
| 1491 | |
| 1492 | result = gethostbyname("tlsfailover1"); |
| 1493 | ASSERT_FALSE(result == nullptr); |
| 1494 | EXPECT_EQ("1.2.3.1", ToString(result)); |
| 1495 | |
| 1496 | // Wait for query to get counted. |
| 1497 | EXPECT_TRUE(tls1.waitForQueries(2, 5000)); |
| 1498 | // No new queries should have reached tls2. |
| 1499 | EXPECT_EQ(1, tls2.queries()); |
| 1500 | |
| 1501 | // Stop tls1. Subsequent queries should attempt to reach tls1, fail, and retry to tls2. |
| 1502 | tls1.stopServer(); |
| 1503 | |
| 1504 | result = gethostbyname("tlsfailover2"); |
| 1505 | EXPECT_EQ("1.2.3.4", ToString(result)); |
| 1506 | |
| 1507 | // Wait for query to get counted. |
| 1508 | EXPECT_TRUE(tls2.waitForQueries(2, 5000)); |
| 1509 | |
| 1510 | // No additional queries should have reached the insecure servers. |
| 1511 | EXPECT_EQ(2U, dns1.queries().size()); |
| 1512 | EXPECT_EQ(2U, dns2.queries().size()); |
| 1513 | |
| 1514 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1515 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | TEST_F(ResolverTest, GetHostByName_BadTlsName) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1519 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1520 | constexpr char listen_udp[] = "53"; |
| 1521 | constexpr char listen_tls[] = "853"; |
| 1522 | constexpr char host_name[] = "badtlsname.example.com."; |
| 1523 | |
| 1524 | test::DNSResponder dns; |
| 1525 | StartDns(dns, {{host_name, ns_type::ns_t_a, "1.2.3.1"}}); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1526 | std::vector<std::string> servers = { listen_addr }; |
| 1527 | |
| 1528 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1529 | ASSERT_TRUE(tls.startServer()); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1530 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1531 | "www.example.com", {})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1532 | |
| 1533 | // The TLS server's certificate doesn't chain to a known CA, and a nonempty name was specified, |
| 1534 | // so the client should fail the TLS handshake before ever issuing a query. |
| 1535 | EXPECT_FALSE(tls.waitForQueries(1, 500)); |
| 1536 | |
| 1537 | // The query should fail hard, because a name was specified. |
| 1538 | EXPECT_EQ(nullptr, gethostbyname("badtlsname")); |
| 1539 | |
| 1540 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1541 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | TEST_F(ResolverTest, GetAddrInfo_Tls) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1545 | constexpr char listen_addr[] = "127.0.0.3"; |
| 1546 | constexpr char listen_udp[] = "53"; |
| 1547 | constexpr char listen_tls[] = "853"; |
| 1548 | constexpr char host_name[] = "addrinfotls.example.com."; |
| 1549 | const std::vector<DnsRecord> records = { |
| 1550 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1551 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1552 | }; |
| 1553 | |
| 1554 | test::DNSResponder dns; |
| 1555 | StartDns(dns, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1556 | std::vector<std::string> servers = { listen_addr }; |
| 1557 | |
| 1558 | test::DnsTlsFrontend tls(listen_addr, listen_tls, listen_addr, listen_udp); |
| 1559 | ASSERT_TRUE(tls.startServer()); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1560 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1561 | {base64Encode(tls.fingerprint())})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1562 | |
| 1563 | // Wait for validation to complete. |
| 1564 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1565 | |
| 1566 | dns.clearQueries(); |
| 1567 | ScopedAddrinfo result = safe_getaddrinfo("addrinfotls", nullptr, nullptr); |
| 1568 | EXPECT_TRUE(result != nullptr); |
| 1569 | size_t found = GetNumQueries(dns, host_name); |
| 1570 | EXPECT_LE(1U, found); |
| 1571 | // Could be A or AAAA |
| 1572 | std::string result_str = ToString(result); |
| 1573 | EXPECT_TRUE(result_str == "1.2.3.4" || result_str == "::1.2.3.4") |
| 1574 | << ", result_str='" << result_str << "'"; |
| 1575 | // Wait for both A and AAAA queries to get counted. |
| 1576 | EXPECT_TRUE(tls.waitForQueries(3, 5000)); |
| 1577 | |
| 1578 | // Clear TLS bit. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1579 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | TEST_F(ResolverTest, TlsBypass) { |
| 1583 | const char OFF[] = "off"; |
| 1584 | const char OPPORTUNISTIC[] = "opportunistic"; |
| 1585 | const char STRICT[] = "strict"; |
| 1586 | |
| 1587 | const char GETHOSTBYNAME[] = "gethostbyname"; |
| 1588 | const char GETADDRINFO[] = "getaddrinfo"; |
| 1589 | const char GETADDRINFOFORNET[] = "getaddrinfofornet"; |
| 1590 | |
| 1591 | const unsigned BYPASS_NETID = NETID_USE_LOCAL_NAMESERVERS | TEST_NETID; |
| 1592 | |
Luke Huang | 579ddc7 | 2019-06-14 00:59:39 +0800 | [diff] [blame] | 1593 | const std::vector<uint8_t> NOOP_FINGERPRINT(android::netdutils::SHA256_SIZE, 0U); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1594 | |
| 1595 | const char ADDR4[] = "192.0.2.1"; |
| 1596 | const char ADDR6[] = "2001:db8::1"; |
| 1597 | |
| 1598 | const char cleartext_addr[] = "127.0.0.53"; |
| 1599 | const char cleartext_port[] = "53"; |
| 1600 | const char tls_port[] = "853"; |
| 1601 | const std::vector<std::string> servers = { cleartext_addr }; |
| 1602 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1603 | test::DNSResponder dns(cleartext_addr); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1604 | ASSERT_TRUE(dns.startServer()); |
| 1605 | |
| 1606 | test::DnsTlsFrontend tls(cleartext_addr, tls_port, cleartext_addr, cleartext_port); |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 1607 | ASSERT_TRUE(tls.startServer()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1608 | |
| 1609 | struct TestConfig { |
| 1610 | const std::string mode; |
| 1611 | const bool withWorkingTLS; |
| 1612 | const std::string method; |
| 1613 | |
| 1614 | std::string asHostName() const { |
| 1615 | return StringPrintf("%s.%s.%s.", |
| 1616 | mode.c_str(), |
| 1617 | withWorkingTLS ? "tlsOn" : "tlsOff", |
| 1618 | method.c_str()); |
| 1619 | } |
| 1620 | } testConfigs[]{ |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1621 | {OFF, true, GETHOSTBYNAME}, |
| 1622 | {OPPORTUNISTIC, true, GETHOSTBYNAME}, |
| 1623 | {STRICT, true, GETHOSTBYNAME}, |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1624 | {OFF, true, GETADDRINFO}, |
| 1625 | {OPPORTUNISTIC, true, GETADDRINFO}, |
| 1626 | {STRICT, true, GETADDRINFO}, |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1627 | {OFF, true, GETADDRINFOFORNET}, |
| 1628 | {OPPORTUNISTIC, true, GETADDRINFOFORNET}, |
| 1629 | {STRICT, true, GETADDRINFOFORNET}, |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 1630 | {OFF, false, GETHOSTBYNAME}, |
| 1631 | {OPPORTUNISTIC, false, GETHOSTBYNAME}, |
| 1632 | {STRICT, false, GETHOSTBYNAME}, |
| 1633 | {OFF, false, GETADDRINFO}, |
| 1634 | {OPPORTUNISTIC, false, GETADDRINFO}, |
| 1635 | {STRICT, false, GETADDRINFO}, |
| 1636 | {OFF, false, GETADDRINFOFORNET}, |
| 1637 | {OPPORTUNISTIC, false, GETADDRINFOFORNET}, |
| 1638 | {STRICT, false, GETADDRINFOFORNET}, |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1639 | }; |
| 1640 | |
| 1641 | for (const auto& config : testConfigs) { |
| 1642 | const std::string testHostName = config.asHostName(); |
| 1643 | SCOPED_TRACE(testHostName); |
| 1644 | |
| 1645 | // Don't tempt test bugs due to caching. |
| 1646 | const char* host_name = testHostName.c_str(); |
| 1647 | dns.addMapping(host_name, ns_type::ns_t_a, ADDR4); |
| 1648 | dns.addMapping(host_name, ns_type::ns_t_aaaa, ADDR6); |
| 1649 | |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 1650 | if (config.withWorkingTLS) { |
| 1651 | if (!tls.running()) { |
| 1652 | ASSERT_TRUE(tls.startServer()); |
| 1653 | } |
| 1654 | } else { |
| 1655 | if (tls.running()) { |
| 1656 | ASSERT_TRUE(tls.stopServer()); |
| 1657 | } |
| 1658 | } |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1659 | |
| 1660 | if (config.mode == OFF) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1661 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers, kDefaultSearchDomains, |
| 1662 | kDefaultParams)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1663 | } else if (config.mode == OPPORTUNISTIC) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1664 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, |
| 1665 | kDefaultParams, "", {})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1666 | // Wait for validation to complete. |
| 1667 | if (config.withWorkingTLS) EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 1668 | } else if (config.mode == STRICT) { |
| 1669 | // We use the existence of fingerprints to trigger strict mode, |
| 1670 | // rather than hostname validation. |
| 1671 | const auto& fingerprint = |
| 1672 | (config.withWorkingTLS) ? tls.fingerprint() : NOOP_FINGERPRINT; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1673 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, |
| 1674 | kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1675 | {base64Encode(fingerprint)})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1676 | // Wait for validation to complete. |
| 1677 | if (config.withWorkingTLS) EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1678 | } |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 1679 | tls.clearQueries(); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1680 | |
| 1681 | const hostent* h_result = nullptr; |
| 1682 | ScopedAddrinfo ai_result; |
| 1683 | |
| 1684 | if (config.method == GETHOSTBYNAME) { |
| 1685 | ASSERT_EQ(0, setNetworkForResolv(BYPASS_NETID)); |
| 1686 | h_result = gethostbyname(host_name); |
| 1687 | |
| 1688 | EXPECT_EQ(1U, GetNumQueriesForType(dns, ns_type::ns_t_a, host_name)); |
| 1689 | ASSERT_FALSE(h_result == nullptr); |
| 1690 | ASSERT_EQ(4, h_result->h_length); |
| 1691 | ASSERT_FALSE(h_result->h_addr_list[0] == nullptr); |
| 1692 | EXPECT_EQ(ADDR4, ToString(h_result)); |
| 1693 | EXPECT_TRUE(h_result->h_addr_list[1] == nullptr); |
| 1694 | } else if (config.method == GETADDRINFO) { |
| 1695 | ASSERT_EQ(0, setNetworkForResolv(BYPASS_NETID)); |
| 1696 | ai_result = safe_getaddrinfo(host_name, nullptr, nullptr); |
| 1697 | EXPECT_TRUE(ai_result != nullptr); |
| 1698 | |
| 1699 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 1700 | // Could be A or AAAA |
| 1701 | const std::string result_str = ToString(ai_result); |
| 1702 | EXPECT_TRUE(result_str == ADDR4 || result_str == ADDR6) |
| 1703 | << ", result_str='" << result_str << "'"; |
| 1704 | } else if (config.method == GETADDRINFOFORNET) { |
| 1705 | addrinfo* raw_ai_result = nullptr; |
| 1706 | EXPECT_EQ(0, android_getaddrinfofornet(host_name, /*servname=*/nullptr, |
| 1707 | /*hints=*/nullptr, BYPASS_NETID, MARK_UNSET, |
| 1708 | &raw_ai_result)); |
| 1709 | ai_result.reset(raw_ai_result); |
| 1710 | |
| 1711 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 1712 | // Could be A or AAAA |
| 1713 | const std::string result_str = ToString(ai_result); |
| 1714 | EXPECT_TRUE(result_str == ADDR4 || result_str == ADDR6) |
| 1715 | << ", result_str='" << result_str << "'"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1716 | } |
| 1717 | |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 1718 | EXPECT_EQ(0, tls.queries()); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1719 | |
| 1720 | // Clear per-process resolv netid. |
| 1721 | ASSERT_EQ(0, setNetworkForResolv(NETID_UNSET)); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1722 | dns.clearQueries(); |
| 1723 | } |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | TEST_F(ResolverTest, StrictMode_NoTlsServers) { |
Luke Huang | 579ddc7 | 2019-06-14 00:59:39 +0800 | [diff] [blame] | 1727 | const std::vector<uint8_t> NOOP_FINGERPRINT(android::netdutils::SHA256_SIZE, 0U); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1728 | constexpr char cleartext_addr[] = "127.0.0.53"; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1729 | const std::vector<std::string> servers = { cleartext_addr }; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1730 | constexpr char host_name[] = "strictmode.notlsips.example.com."; |
| 1731 | const std::vector<DnsRecord> records = { |
| 1732 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1733 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1734 | }; |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1735 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1736 | test::DNSResponder dns(cleartext_addr); |
| 1737 | StartDns(dns, records); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1738 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1739 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, {}, |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 1740 | "", {base64Encode(NOOP_FINGERPRINT)})); |
Ken Chen | b9fa206 | 2018-11-13 21:51:13 +0800 | [diff] [blame] | 1741 | |
| 1742 | addrinfo* ai_result = nullptr; |
| 1743 | EXPECT_NE(0, getaddrinfo(host_name, nullptr, nullptr, &ai_result)); |
| 1744 | EXPECT_EQ(0U, GetNumQueries(dns, host_name)); |
| 1745 | } |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1746 | |
| 1747 | namespace { |
| 1748 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 1749 | int getAsyncResponse(int fd, int* rcode, uint8_t* buf, int bufLen) { |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1750 | struct pollfd wait_fd[1]; |
| 1751 | wait_fd[0].fd = fd; |
| 1752 | wait_fd[0].events = POLLIN; |
| 1753 | short revents; |
| 1754 | int ret; |
| 1755 | |
| 1756 | ret = poll(wait_fd, 1, -1); |
| 1757 | revents = wait_fd[0].revents; |
| 1758 | if (revents & POLLIN) { |
| 1759 | int n = resNetworkResult(fd, rcode, buf, bufLen); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1760 | // Verify that resNetworkResult() closed the fd |
| 1761 | char dummy; |
| 1762 | EXPECT_EQ(-1, read(fd, &dummy, sizeof dummy)); |
| 1763 | EXPECT_EQ(EBADF, errno); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1764 | return n; |
| 1765 | } |
| 1766 | return -1; |
| 1767 | } |
| 1768 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 1769 | std::string toString(uint8_t* buf, int bufLen, int ipType) { |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1770 | ns_msg handle; |
| 1771 | int ancount, n = 0; |
| 1772 | ns_rr rr; |
| 1773 | |
| 1774 | if (ns_initparse((const uint8_t*) buf, bufLen, &handle) >= 0) { |
| 1775 | ancount = ns_msg_count(handle, ns_s_an); |
| 1776 | if (ns_parserr(&handle, ns_s_an, n, &rr) == 0) { |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 1777 | const uint8_t* rdata = ns_rr_rdata(rr); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1778 | char buffer[INET6_ADDRSTRLEN]; |
| 1779 | if (inet_ntop(ipType, (const char*) rdata, buffer, sizeof(buffer))) { |
| 1780 | return buffer; |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | return ""; |
| 1785 | } |
| 1786 | |
| 1787 | int dns_open_proxy() { |
| 1788 | int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); |
| 1789 | if (s == -1) { |
| 1790 | return -1; |
| 1791 | } |
| 1792 | const int one = 1; |
| 1793 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 1794 | |
| 1795 | static const struct sockaddr_un proxy_addr = { |
| 1796 | .sun_family = AF_UNIX, |
| 1797 | .sun_path = "/dev/socket/dnsproxyd", |
| 1798 | }; |
| 1799 | |
| 1800 | if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != |
| 1801 | 0) { |
| 1802 | close(s); |
| 1803 | return -1; |
| 1804 | } |
| 1805 | |
| 1806 | return s; |
| 1807 | } |
| 1808 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1809 | void expectAnswersValid(int fd, int ipType, const std::string& expectedAnswer) { |
| 1810 | int rcode = -1; |
| 1811 | uint8_t buf[MAXPACKET] = {}; |
| 1812 | |
| 1813 | int res = getAsyncResponse(fd, &rcode, buf, MAXPACKET); |
| 1814 | EXPECT_GT(res, 0); |
| 1815 | EXPECT_EQ(expectedAnswer, toString(buf, res, ipType)); |
| 1816 | } |
| 1817 | |
| 1818 | void expectAnswersNotValid(int fd, int expectedErrno) { |
| 1819 | int rcode = -1; |
| 1820 | uint8_t buf[MAXPACKET] = {}; |
| 1821 | |
| 1822 | int res = getAsyncResponse(fd, &rcode, buf, MAXPACKET); |
| 1823 | EXPECT_EQ(expectedErrno, res); |
| 1824 | } |
| 1825 | |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1826 | } // namespace |
| 1827 | |
| 1828 | TEST_F(ResolverTest, Async_NormalQueryV4V6) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1829 | constexpr char listen_addr[] = "127.0.0.4"; |
| 1830 | constexpr char host_name[] = "howdy.example.com."; |
| 1831 | const std::vector<DnsRecord> records = { |
| 1832 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1833 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1834 | }; |
| 1835 | |
| 1836 | test::DNSResponder dns(listen_addr); |
| 1837 | StartDns(dns, records); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1838 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1839 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1840 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1841 | int fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
| 1842 | int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1843 | EXPECT_TRUE(fd1 != -1); |
| 1844 | EXPECT_TRUE(fd2 != -1); |
| 1845 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 1846 | uint8_t buf[MAXPACKET] = {}; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1847 | int rcode; |
| 1848 | int res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); |
| 1849 | EXPECT_GT(res, 0); |
| 1850 | EXPECT_EQ("::1.2.3.4", toString(buf, res, AF_INET6)); |
| 1851 | |
| 1852 | res = getAsyncResponse(fd1, &rcode, buf, MAXPACKET); |
| 1853 | EXPECT_GT(res, 0); |
| 1854 | EXPECT_EQ("1.2.3.4", toString(buf, res, AF_INET)); |
| 1855 | |
| 1856 | EXPECT_EQ(2U, GetNumQueries(dns, host_name)); |
| 1857 | |
| 1858 | // Re-query verify cache works |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1859 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
| 1860 | fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1861 | |
| 1862 | EXPECT_TRUE(fd1 != -1); |
| 1863 | EXPECT_TRUE(fd2 != -1); |
| 1864 | |
| 1865 | res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); |
| 1866 | EXPECT_GT(res, 0); |
| 1867 | EXPECT_EQ("::1.2.3.4", toString(buf, res, AF_INET6)); |
| 1868 | |
| 1869 | res = getAsyncResponse(fd1, &rcode, buf, MAXPACKET); |
| 1870 | EXPECT_GT(res, 0); |
| 1871 | EXPECT_EQ("1.2.3.4", toString(buf, res, AF_INET)); |
| 1872 | |
| 1873 | EXPECT_EQ(2U, GetNumQueries(dns, host_name)); |
| 1874 | } |
| 1875 | |
| 1876 | TEST_F(ResolverTest, Async_BadQuery) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1877 | constexpr char listen_addr[] = "127.0.0.4"; |
| 1878 | constexpr char host_name[] = "howdy.example.com."; |
| 1879 | const std::vector<DnsRecord> records = { |
| 1880 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1881 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1882 | }; |
| 1883 | |
| 1884 | test::DNSResponder dns(listen_addr); |
| 1885 | StartDns(dns, records); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1886 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1887 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1888 | |
| 1889 | static struct { |
| 1890 | int fd; |
| 1891 | const char* dname; |
| 1892 | const int queryType; |
| 1893 | const int expectRcode; |
| 1894 | } kTestData[] = { |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1895 | {-1, "", ns_t_aaaa, 0}, |
| 1896 | {-1, "as65ass46", ns_t_aaaa, 0}, |
| 1897 | {-1, "454564564564", ns_t_aaaa, 0}, |
| 1898 | {-1, "h645235", ns_t_a, 0}, |
| 1899 | {-1, "www.google.com", ns_t_a, 0}, |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1900 | }; |
| 1901 | |
| 1902 | for (auto& td : kTestData) { |
| 1903 | SCOPED_TRACE(td.dname); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1904 | td.fd = resNetworkQuery(TEST_NETID, td.dname, ns_c_in, td.queryType, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1905 | EXPECT_TRUE(td.fd != -1); |
| 1906 | } |
| 1907 | |
| 1908 | // dns_responder return empty resp(packet only contains query part) with no error currently |
| 1909 | for (const auto& td : kTestData) { |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 1910 | uint8_t buf[MAXPACKET] = {}; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1911 | int rcode; |
| 1912 | SCOPED_TRACE(td.dname); |
| 1913 | int res = getAsyncResponse(td.fd, &rcode, buf, MAXPACKET); |
| 1914 | EXPECT_GT(res, 0); |
| 1915 | EXPECT_EQ(rcode, td.expectRcode); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | TEST_F(ResolverTest, Async_EmptyAnswer) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1920 | constexpr char listen_addr[] = "127.0.0.4"; |
| 1921 | constexpr char host_name[] = "howdy.example.com."; |
| 1922 | const std::vector<DnsRecord> records = { |
| 1923 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1924 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1925 | }; |
| 1926 | |
| 1927 | test::DNSResponder dns(listen_addr); |
| 1928 | StartDns(dns, records); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1929 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1930 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1931 | |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1932 | // TODO: Disable retry to make this test explicit. |
| 1933 | auto& cv = dns.getCv(); |
| 1934 | auto& cvMutex = dns.getCvMutex(); |
| 1935 | int fd1; |
| 1936 | // Wait on the condition variable to ensure that the DNS server has handled our first query. |
| 1937 | { |
| 1938 | std::unique_lock lk(cvMutex); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1939 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1940 | EXPECT_TRUE(fd1 != -1); |
| 1941 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lk, std::chrono::seconds(1))); |
| 1942 | } |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1943 | |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1944 | dns.setResponseProbability(0.0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1945 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1946 | int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1947 | EXPECT_TRUE(fd2 != -1); |
| 1948 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1949 | int fd3 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1950 | EXPECT_TRUE(fd3 != -1); |
| 1951 | |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1952 | uint8_t buf[MAXPACKET] = {}; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1953 | int rcode; |
| 1954 | |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1955 | // expect no response |
| 1956 | int res = getAsyncResponse(fd3, &rcode, buf, MAXPACKET); |
| 1957 | EXPECT_EQ(-ETIMEDOUT, res); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1958 | |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1959 | // expect no response |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1960 | memset(buf, 0, MAXPACKET); |
Luke Huang | 9c264bb | 2018-12-18 16:44:41 +0800 | [diff] [blame] | 1961 | res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); |
| 1962 | EXPECT_EQ(-ETIMEDOUT, res); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1963 | |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1964 | dns.setResponseProbability(1.0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1965 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 1966 | int fd4 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1967 | EXPECT_TRUE(fd4 != -1); |
| 1968 | |
| 1969 | memset(buf, 0, MAXPACKET); |
| 1970 | res = getAsyncResponse(fd4, &rcode, buf, MAXPACKET); |
| 1971 | EXPECT_GT(res, 0); |
| 1972 | EXPECT_EQ("1.2.3.4", toString(buf, res, AF_INET)); |
| 1973 | |
| 1974 | memset(buf, 0, MAXPACKET); |
| 1975 | res = getAsyncResponse(fd1, &rcode, buf, MAXPACKET); |
| 1976 | EXPECT_GT(res, 0); |
| 1977 | EXPECT_EQ("::1.2.3.4", toString(buf, res, AF_INET6)); |
| 1978 | } |
| 1979 | |
| 1980 | TEST_F(ResolverTest, Async_MalformedQuery) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1981 | constexpr char listen_addr[] = "127.0.0.4"; |
| 1982 | constexpr char host_name[] = "howdy.example.com."; |
| 1983 | const std::vector<DnsRecord> records = { |
| 1984 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 1985 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 1986 | }; |
| 1987 | |
| 1988 | test::DNSResponder dns(listen_addr); |
| 1989 | StartDns(dns, records); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1990 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1991 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1992 | |
| 1993 | int fd = dns_open_proxy(); |
| 1994 | EXPECT_TRUE(fd > 0); |
| 1995 | |
| 1996 | const std::string badMsg = "16-52512#"; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 1997 | static const struct { |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 1998 | const std::string cmd; |
| 1999 | const int expectErr; |
| 2000 | } kTestData[] = { |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2001 | // Too few arguments |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2002 | {"resnsend " + badMsg + '\0', -EINVAL}, |
| 2003 | // Bad netId |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2004 | {"resnsend badnetId 0 " + badMsg + '\0', -EINVAL}, |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2005 | // Bad raw data |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2006 | {"resnsend " + std::to_string(TEST_NETID) + " 0 " + badMsg + '\0', -EILSEQ}, |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2007 | }; |
| 2008 | |
| 2009 | for (unsigned int i = 0; i < std::size(kTestData); i++) { |
| 2010 | auto& td = kTestData[i]; |
| 2011 | SCOPED_TRACE(td.cmd); |
| 2012 | ssize_t rc = TEMP_FAILURE_RETRY(write(fd, td.cmd.c_str(), td.cmd.size())); |
| 2013 | EXPECT_EQ(rc, static_cast<ssize_t>(td.cmd.size())); |
| 2014 | |
| 2015 | int32_t tmp; |
| 2016 | rc = TEMP_FAILURE_RETRY(read(fd, &tmp, sizeof(tmp))); |
| 2017 | EXPECT_TRUE(rc > 0); |
| 2018 | EXPECT_EQ(static_cast<int>(ntohl(tmp)), td.expectErr); |
| 2019 | } |
| 2020 | // Normal query with answer buffer |
| 2021 | // This is raw data of query "howdy.example.com" type 1 class 1 |
| 2022 | std::string query = "81sBAAABAAAAAAAABWhvd2R5B2V4YW1wbGUDY29tAAABAAE="; |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2023 | std::string cmd = "resnsend " + std::to_string(TEST_NETID) + " 0 " + query + '\0'; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2024 | ssize_t rc = TEMP_FAILURE_RETRY(write(fd, cmd.c_str(), cmd.size())); |
| 2025 | EXPECT_EQ(rc, static_cast<ssize_t>(cmd.size())); |
| 2026 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2027 | uint8_t smallBuf[1] = {}; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2028 | int rcode; |
| 2029 | rc = getAsyncResponse(fd, &rcode, smallBuf, 1); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2030 | EXPECT_EQ(-EMSGSIZE, rc); |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2031 | |
| 2032 | // Do the normal test with large buffer again |
| 2033 | fd = dns_open_proxy(); |
| 2034 | EXPECT_TRUE(fd > 0); |
| 2035 | rc = TEMP_FAILURE_RETRY(write(fd, cmd.c_str(), cmd.size())); |
| 2036 | EXPECT_EQ(rc, static_cast<ssize_t>(cmd.size())); |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2037 | uint8_t buf[MAXPACKET] = {}; |
Luke Huang | 94b10b9 | 2018-11-21 20:13:38 +0800 | [diff] [blame] | 2038 | rc = getAsyncResponse(fd, &rcode, buf, MAXPACKET); |
| 2039 | EXPECT_EQ("1.2.3.4", toString(buf, rc, AF_INET)); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2040 | } |
| 2041 | |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2042 | TEST_F(ResolverTest, Async_CacheFlags) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2043 | constexpr char listen_addr[] = "127.0.0.4"; |
| 2044 | constexpr char host_name[] = "howdy.example.com."; |
Luke Huang | 40f5f49 | 2019-05-09 19:37:35 +0800 | [diff] [blame] | 2045 | constexpr char another_host_name[] = "howdy.example2.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2046 | const std::vector<DnsRecord> records = { |
| 2047 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2048 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
Luke Huang | 40f5f49 | 2019-05-09 19:37:35 +0800 | [diff] [blame] | 2049 | {another_host_name, ns_type::ns_t_a, "1.2.3.5"}, |
| 2050 | {another_host_name, ns_type::ns_t_aaaa, "::1.2.3.5"}, |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2051 | }; |
| 2052 | |
| 2053 | test::DNSResponder dns(listen_addr); |
| 2054 | StartDns(dns, records); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2055 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2056 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2057 | |
| 2058 | // ANDROID_RESOLV_NO_CACHE_STORE |
| 2059 | int fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2060 | ANDROID_RESOLV_NO_CACHE_STORE); |
| 2061 | EXPECT_TRUE(fd1 != -1); |
| 2062 | int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2063 | ANDROID_RESOLV_NO_CACHE_STORE); |
| 2064 | EXPECT_TRUE(fd2 != -1); |
| 2065 | int fd3 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2066 | ANDROID_RESOLV_NO_CACHE_STORE); |
| 2067 | EXPECT_TRUE(fd3 != -1); |
| 2068 | |
| 2069 | expectAnswersValid(fd3, AF_INET, "1.2.3.4"); |
| 2070 | expectAnswersValid(fd2, AF_INET, "1.2.3.4"); |
| 2071 | expectAnswersValid(fd1, AF_INET, "1.2.3.4"); |
| 2072 | |
| 2073 | // No cache exists, expect 3 queries |
| 2074 | EXPECT_EQ(3U, GetNumQueries(dns, host_name)); |
| 2075 | |
| 2076 | // Re-query and cache |
| 2077 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
| 2078 | |
| 2079 | EXPECT_TRUE(fd1 != -1); |
| 2080 | |
| 2081 | expectAnswersValid(fd1, AF_INET, "1.2.3.4"); |
| 2082 | |
| 2083 | // Now we have cache, expect 4 queries |
| 2084 | EXPECT_EQ(4U, GetNumQueries(dns, host_name)); |
| 2085 | |
| 2086 | // ANDROID_RESOLV_NO_CACHE_LOOKUP |
| 2087 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2088 | ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 2089 | fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2090 | ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 2091 | |
| 2092 | EXPECT_TRUE(fd1 != -1); |
| 2093 | EXPECT_TRUE(fd2 != -1); |
| 2094 | |
| 2095 | expectAnswersValid(fd2, AF_INET, "1.2.3.4"); |
| 2096 | expectAnswersValid(fd1, AF_INET, "1.2.3.4"); |
| 2097 | |
| 2098 | // Skip cache, expect 6 queries |
| 2099 | EXPECT_EQ(6U, GetNumQueries(dns, host_name)); |
| 2100 | |
| 2101 | // Re-query verify cache works |
| 2102 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2103 | ANDROID_RESOLV_NO_CACHE_STORE); |
| 2104 | EXPECT_TRUE(fd1 != -1); |
| 2105 | expectAnswersValid(fd1, AF_INET, "1.2.3.4"); |
| 2106 | |
| 2107 | // Cache hits, expect still 6 queries |
| 2108 | EXPECT_EQ(6U, GetNumQueries(dns, host_name)); |
Luke Huang | a1d7418 | 2019-03-19 17:30:36 +0800 | [diff] [blame] | 2109 | |
| 2110 | // Start to verify if ANDROID_RESOLV_NO_CACHE_LOOKUP does write response into cache |
| 2111 | dns.clearQueries(); |
| 2112 | |
| 2113 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, |
| 2114 | ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 2115 | fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, |
| 2116 | ANDROID_RESOLV_NO_CACHE_LOOKUP); |
| 2117 | |
| 2118 | EXPECT_TRUE(fd1 != -1); |
| 2119 | EXPECT_TRUE(fd2 != -1); |
| 2120 | |
| 2121 | expectAnswersValid(fd2, AF_INET6, "::1.2.3.4"); |
| 2122 | expectAnswersValid(fd1, AF_INET6, "::1.2.3.4"); |
| 2123 | |
| 2124 | // Skip cache, expect 2 queries |
| 2125 | EXPECT_EQ(2U, GetNumQueries(dns, host_name)); |
| 2126 | |
| 2127 | // Re-query without flags |
| 2128 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
| 2129 | fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
| 2130 | |
| 2131 | EXPECT_TRUE(fd1 != -1); |
| 2132 | EXPECT_TRUE(fd2 != -1); |
| 2133 | |
| 2134 | expectAnswersValid(fd2, AF_INET6, "::1.2.3.4"); |
| 2135 | expectAnswersValid(fd1, AF_INET6, "::1.2.3.4"); |
| 2136 | |
| 2137 | // Cache hits, expect still 2 queries |
| 2138 | EXPECT_EQ(2U, GetNumQueries(dns, host_name)); |
Luke Huang | 40f5f49 | 2019-05-09 19:37:35 +0800 | [diff] [blame] | 2139 | |
| 2140 | // Test both ANDROID_RESOLV_NO_CACHE_STORE and ANDROID_RESOLV_NO_CACHE_LOOKUP are set |
| 2141 | dns.clearQueries(); |
| 2142 | |
| 2143 | // Make sure that the cache of "howdy.example2.com" exists. |
| 2144 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example2.com", ns_c_in, ns_t_aaaa, 0); |
| 2145 | EXPECT_TRUE(fd1 != -1); |
| 2146 | expectAnswersValid(fd1, AF_INET6, "::1.2.3.5"); |
| 2147 | EXPECT_EQ(1U, GetNumQueries(dns, another_host_name)); |
| 2148 | |
| 2149 | // Re-query with testFlags |
| 2150 | const int testFlag = ANDROID_RESOLV_NO_CACHE_STORE | ANDROID_RESOLV_NO_CACHE_LOOKUP; |
| 2151 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example2.com", ns_c_in, ns_t_aaaa, testFlag); |
| 2152 | EXPECT_TRUE(fd1 != -1); |
| 2153 | expectAnswersValid(fd1, AF_INET6, "::1.2.3.5"); |
| 2154 | // Expect cache lookup is skipped. |
| 2155 | EXPECT_EQ(2U, GetNumQueries(dns, another_host_name)); |
| 2156 | |
| 2157 | // Do another query with testFlags |
| 2158 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example2.com", ns_c_in, ns_t_a, testFlag); |
| 2159 | EXPECT_TRUE(fd1 != -1); |
| 2160 | expectAnswersValid(fd1, AF_INET, "1.2.3.5"); |
| 2161 | // Expect cache lookup is skipped. |
| 2162 | EXPECT_EQ(3U, GetNumQueries(dns, another_host_name)); |
| 2163 | |
| 2164 | // Re-query with no flags |
| 2165 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example2.com", ns_c_in, ns_t_a, 0); |
| 2166 | EXPECT_TRUE(fd1 != -1); |
| 2167 | expectAnswersValid(fd1, AF_INET, "1.2.3.5"); |
| 2168 | // Expect no cache hit because cache storing is also skipped in previous query. |
| 2169 | EXPECT_EQ(4U, GetNumQueries(dns, another_host_name)); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | TEST_F(ResolverTest, Async_NoRetryFlag) { |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2173 | constexpr char listen_addr0[] = "127.0.0.4"; |
| 2174 | constexpr char listen_addr1[] = "127.0.0.6"; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2175 | constexpr char host_name[] = "howdy.example.com."; |
| 2176 | const std::vector<DnsRecord> records = { |
| 2177 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2178 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 2179 | }; |
| 2180 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2181 | test::DNSResponder dns0(listen_addr0); |
| 2182 | test::DNSResponder dns1(listen_addr1); |
| 2183 | StartDns(dns0, records); |
| 2184 | StartDns(dns1, records); |
| 2185 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork({listen_addr0, listen_addr1})); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2186 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2187 | dns0.clearQueries(); |
| 2188 | dns1.clearQueries(); |
| 2189 | |
| 2190 | dns0.setResponseProbability(0.0); |
| 2191 | dns1.setResponseProbability(0.0); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2192 | |
| 2193 | int fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, |
| 2194 | ANDROID_RESOLV_NO_RETRY); |
| 2195 | EXPECT_TRUE(fd1 != -1); |
| 2196 | |
| 2197 | int fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, |
| 2198 | ANDROID_RESOLV_NO_RETRY); |
| 2199 | EXPECT_TRUE(fd2 != -1); |
| 2200 | |
| 2201 | // expect no response |
| 2202 | expectAnswersNotValid(fd1, -ETIMEDOUT); |
| 2203 | expectAnswersNotValid(fd2, -ETIMEDOUT); |
| 2204 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2205 | // No retry case, expect total 2 queries. The server is selected randomly. |
| 2206 | EXPECT_EQ(2U, GetNumQueries(dns0, host_name) + GetNumQueries(dns1, host_name)); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2207 | |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2208 | dns0.clearQueries(); |
| 2209 | dns1.clearQueries(); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2210 | |
| 2211 | fd1 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_a, 0); |
| 2212 | EXPECT_TRUE(fd1 != -1); |
| 2213 | |
| 2214 | fd2 = resNetworkQuery(TEST_NETID, "howdy.example.com", ns_c_in, ns_t_aaaa, 0); |
| 2215 | EXPECT_TRUE(fd2 != -1); |
| 2216 | |
| 2217 | // expect no response |
| 2218 | expectAnswersNotValid(fd1, -ETIMEDOUT); |
| 2219 | expectAnswersNotValid(fd2, -ETIMEDOUT); |
| 2220 | |
| 2221 | // Retry case, expect 4 queries |
Luke Huang | 70931aa | 2019-01-31 11:57:41 +0800 | [diff] [blame] | 2222 | EXPECT_EQ(4U, GetNumQueries(dns0, host_name)); |
| 2223 | EXPECT_EQ(4U, GetNumQueries(dns1, host_name)); |
| 2224 | } |
| 2225 | |
| 2226 | TEST_F(ResolverTest, Async_VerifyQueryID) { |
| 2227 | constexpr char listen_addr[] = "127.0.0.4"; |
| 2228 | constexpr char host_name[] = "howdy.example.com."; |
| 2229 | const std::vector<DnsRecord> records = { |
| 2230 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2231 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 2232 | }; |
| 2233 | |
| 2234 | test::DNSResponder dns(listen_addr); |
| 2235 | StartDns(dns, records); |
| 2236 | std::vector<std::string> servers = {listen_addr}; |
| 2237 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
| 2238 | |
| 2239 | const uint8_t queryBuf1[] = { |
| 2240 | /* Header */ |
| 2241 | 0x55, 0x66, /* Transaction ID */ |
| 2242 | 0x01, 0x00, /* Flags */ |
| 2243 | 0x00, 0x01, /* Questions */ |
| 2244 | 0x00, 0x00, /* Answer RRs */ |
| 2245 | 0x00, 0x00, /* Authority RRs */ |
| 2246 | 0x00, 0x00, /* Additional RRs */ |
| 2247 | /* Queries */ |
| 2248 | 0x05, 0x68, 0x6f, 0x77, 0x64, 0x79, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, |
| 2249 | 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */ |
| 2250 | 0x00, 0x01, /* Type */ |
| 2251 | 0x00, 0x01 /* Class */ |
| 2252 | }; |
| 2253 | |
| 2254 | int fd = resNetworkSend(TEST_NETID, queryBuf1, sizeof(queryBuf1), 0); |
| 2255 | EXPECT_TRUE(fd != -1); |
| 2256 | |
| 2257 | uint8_t buf[MAXPACKET] = {}; |
| 2258 | int rcode; |
| 2259 | |
| 2260 | int res = getAsyncResponse(fd, &rcode, buf, MAXPACKET); |
| 2261 | EXPECT_GT(res, 0); |
| 2262 | EXPECT_EQ("1.2.3.4", toString(buf, res, AF_INET)); |
| 2263 | |
| 2264 | auto hp = reinterpret_cast<HEADER*>(buf); |
| 2265 | EXPECT_EQ(21862U, htons(hp->id)); |
| 2266 | |
| 2267 | EXPECT_EQ(1U, GetNumQueries(dns, host_name)); |
| 2268 | |
| 2269 | const uint8_t queryBuf2[] = { |
| 2270 | /* Header */ |
| 2271 | 0x00, 0x53, /* Transaction ID */ |
| 2272 | 0x01, 0x00, /* Flags */ |
| 2273 | 0x00, 0x01, /* Questions */ |
| 2274 | 0x00, 0x00, /* Answer RRs */ |
| 2275 | 0x00, 0x00, /* Authority RRs */ |
| 2276 | 0x00, 0x00, /* Additional RRs */ |
| 2277 | /* Queries */ |
| 2278 | 0x05, 0x68, 0x6f, 0x77, 0x64, 0x79, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, |
| 2279 | 0x03, 0x63, 0x6f, 0x6d, 0x00, /* Name */ |
| 2280 | 0x00, 0x01, /* Type */ |
| 2281 | 0x00, 0x01 /* Class */ |
| 2282 | }; |
| 2283 | |
| 2284 | // Re-query verify cache works and query id is correct |
| 2285 | fd = resNetworkSend(TEST_NETID, queryBuf2, sizeof(queryBuf2), 0); |
| 2286 | |
| 2287 | EXPECT_TRUE(fd != -1); |
| 2288 | |
| 2289 | res = getAsyncResponse(fd, &rcode, buf, MAXPACKET); |
| 2290 | EXPECT_GT(res, 0); |
| 2291 | EXPECT_EQ("1.2.3.4", toString(buf, res, AF_INET)); |
| 2292 | |
| 2293 | EXPECT_EQ(0x0053U, htons(hp->id)); |
| 2294 | |
| 2295 | EXPECT_EQ(1U, GetNumQueries(dns, host_name)); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 2296 | } |
| 2297 | |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2298 | // This test checks that the resolver should not generate the request containing OPT RR when using |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2299 | // cleartext DNS. If we query the DNS server not supporting EDNS0 and it reponds with |
| 2300 | // FORMERR_ON_EDNS, we will fallback to no EDNS0 and try again. If the server does no response, we |
| 2301 | // won't retry so that we get no answer. |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2302 | TEST_F(ResolverTest, BrokenEdns) { |
| 2303 | typedef test::DNSResponder::Edns Edns; |
| 2304 | enum ExpectResult { EXPECT_FAILURE, EXPECT_SUCCESS }; |
| 2305 | |
| 2306 | const char OFF[] = "off"; |
| 2307 | const char OPPORTUNISTIC_UDP[] = "opportunistic_udp"; |
| 2308 | const char OPPORTUNISTIC_TLS[] = "opportunistic_tls"; |
| 2309 | const char STRICT[] = "strict"; |
| 2310 | const char GETHOSTBYNAME[] = "gethostbyname"; |
| 2311 | const char GETADDRINFO[] = "getaddrinfo"; |
Luke Huang | 579ddc7 | 2019-06-14 00:59:39 +0800 | [diff] [blame] | 2312 | const std::vector<uint8_t> NOOP_FINGERPRINT(android::netdutils::SHA256_SIZE, 0U); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2313 | const char ADDR4[] = "192.0.2.1"; |
| 2314 | const char CLEARTEXT_ADDR[] = "127.0.0.53"; |
| 2315 | const char CLEARTEXT_PORT[] = "53"; |
| 2316 | const char TLS_PORT[] = "853"; |
| 2317 | const std::vector<std::string> servers = { CLEARTEXT_ADDR }; |
| 2318 | |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 2319 | test::DNSResponder dns(CLEARTEXT_ADDR, CLEARTEXT_PORT, ns_rcode::ns_r_servfail); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2320 | ASSERT_TRUE(dns.startServer()); |
| 2321 | |
| 2322 | test::DnsTlsFrontend tls(CLEARTEXT_ADDR, TLS_PORT, CLEARTEXT_ADDR, CLEARTEXT_PORT); |
| 2323 | |
| 2324 | static const struct TestConfig { |
| 2325 | std::string mode; |
| 2326 | std::string method; |
| 2327 | Edns edns; |
| 2328 | ExpectResult expectResult; |
| 2329 | |
| 2330 | std::string asHostName() const { |
| 2331 | const char* ednsString; |
| 2332 | switch (edns) { |
| 2333 | case Edns::ON: |
| 2334 | ednsString = "ednsOn"; |
| 2335 | break; |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2336 | case Edns::FORMERR_ON_EDNS: |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2337 | ednsString = "ednsFormerr"; |
| 2338 | break; |
| 2339 | case Edns::DROP: |
| 2340 | ednsString = "ednsDrop"; |
| 2341 | break; |
| 2342 | default: |
| 2343 | ednsString = ""; |
| 2344 | break; |
| 2345 | } |
| 2346 | return StringPrintf("%s.%s.%s.", mode.c_str(), method.c_str(), ednsString); |
| 2347 | } |
| 2348 | } testConfigs[] = { |
| 2349 | // In OPPORTUNISTIC_TLS, we get no answer if the DNS server supports TLS but not EDNS0. |
| 2350 | // Could such server exist? if so, we might need to fallback to query cleartext DNS. |
| 2351 | // Another thing is that {OPPORTUNISTIC_TLS, Edns::DROP} and {STRICT, Edns::DROP} are |
| 2352 | // commented out since TLS timeout is not configurable. |
| 2353 | // TODO: Uncomment them after TLS timeout is configurable. |
| 2354 | {OFF, GETHOSTBYNAME, Edns::ON, EXPECT_SUCCESS}, |
| 2355 | {OPPORTUNISTIC_UDP, GETHOSTBYNAME, Edns::ON, EXPECT_SUCCESS}, |
| 2356 | {OPPORTUNISTIC_TLS, GETHOSTBYNAME, Edns::ON, EXPECT_SUCCESS}, |
| 2357 | {STRICT, GETHOSTBYNAME, Edns::ON, EXPECT_SUCCESS}, |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2358 | {OFF, GETHOSTBYNAME, Edns::FORMERR_ON_EDNS, EXPECT_SUCCESS}, |
| 2359 | {OPPORTUNISTIC_UDP, GETHOSTBYNAME, Edns::FORMERR_ON_EDNS, EXPECT_SUCCESS}, |
| 2360 | {OPPORTUNISTIC_TLS, GETHOSTBYNAME, Edns::FORMERR_ON_EDNS, EXPECT_FAILURE}, |
| 2361 | {STRICT, GETHOSTBYNAME, Edns::FORMERR_ON_EDNS, EXPECT_FAILURE}, |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2362 | {OFF, GETHOSTBYNAME, Edns::DROP, EXPECT_SUCCESS}, |
| 2363 | {OPPORTUNISTIC_UDP, GETHOSTBYNAME, Edns::DROP, EXPECT_SUCCESS}, |
| 2364 | //{OPPORTUNISTIC_TLS, GETHOSTBYNAME, Edns::DROP, EXPECT_FAILURE}, |
| 2365 | //{STRICT, GETHOSTBYNAME, Edns::DROP, EXPECT_FAILURE}, |
| 2366 | {OFF, GETADDRINFO, Edns::ON, EXPECT_SUCCESS}, |
| 2367 | {OPPORTUNISTIC_UDP, GETADDRINFO, Edns::ON, EXPECT_SUCCESS}, |
| 2368 | {OPPORTUNISTIC_TLS, GETADDRINFO, Edns::ON, EXPECT_SUCCESS}, |
| 2369 | {STRICT, GETADDRINFO, Edns::ON, EXPECT_SUCCESS}, |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2370 | {OFF, GETADDRINFO, Edns::FORMERR_ON_EDNS, EXPECT_SUCCESS}, |
| 2371 | {OPPORTUNISTIC_UDP, GETADDRINFO, Edns::FORMERR_ON_EDNS, EXPECT_SUCCESS}, |
| 2372 | {OPPORTUNISTIC_TLS, GETADDRINFO, Edns::FORMERR_ON_EDNS, EXPECT_FAILURE}, |
| 2373 | {STRICT, GETADDRINFO, Edns::FORMERR_ON_EDNS, EXPECT_FAILURE}, |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2374 | {OFF, GETADDRINFO, Edns::DROP, EXPECT_SUCCESS}, |
| 2375 | {OPPORTUNISTIC_UDP, GETADDRINFO, Edns::DROP, EXPECT_SUCCESS}, |
| 2376 | //{OPPORTUNISTIC_TLS, GETADDRINFO, Edns::DROP, EXPECT_FAILURE}, |
| 2377 | //{STRICT, GETADDRINFO, Edns::DROP, EXPECT_FAILURE}, |
| 2378 | }; |
| 2379 | |
| 2380 | for (const auto& config : testConfigs) { |
| 2381 | const std::string testHostName = config.asHostName(); |
| 2382 | SCOPED_TRACE(testHostName); |
| 2383 | |
| 2384 | const char* host_name = testHostName.c_str(); |
| 2385 | dns.addMapping(host_name, ns_type::ns_t_a, ADDR4); |
| 2386 | dns.setEdns(config.edns); |
| 2387 | |
| 2388 | if (config.mode == OFF) { |
Mike Yu | 0f20cdd | 2019-06-11 11:52:37 +0800 | [diff] [blame] | 2389 | if (tls.running()) { |
| 2390 | ASSERT_TRUE(tls.stopServer()); |
| 2391 | } |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2392 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2393 | } else if (config.mode == OPPORTUNISTIC_UDP) { |
Mike Yu | 0f20cdd | 2019-06-11 11:52:37 +0800 | [diff] [blame] | 2394 | if (tls.running()) { |
| 2395 | ASSERT_TRUE(tls.stopServer()); |
| 2396 | } |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2397 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, |
| 2398 | kDefaultParams, "", {})); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2399 | } else if (config.mode == OPPORTUNISTIC_TLS) { |
Mike Yu | 0f20cdd | 2019-06-11 11:52:37 +0800 | [diff] [blame] | 2400 | if (!tls.running()) { |
| 2401 | ASSERT_TRUE(tls.startServer()); |
| 2402 | } |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2403 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, |
| 2404 | kDefaultParams, "", {})); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2405 | // Wait for validation to complete. |
| 2406 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 2407 | } else if (config.mode == STRICT) { |
Mike Yu | 0f20cdd | 2019-06-11 11:52:37 +0800 | [diff] [blame] | 2408 | if (!tls.running()) { |
| 2409 | ASSERT_TRUE(tls.startServer()); |
| 2410 | } |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2411 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, |
| 2412 | kDefaultParams, "", |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 2413 | {base64Encode(tls.fingerprint())})); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2414 | // Wait for validation to complete. |
| 2415 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 2416 | } |
| 2417 | |
| 2418 | if (config.method == GETHOSTBYNAME) { |
| 2419 | const hostent* h_result = gethostbyname(host_name); |
| 2420 | if (config.expectResult == EXPECT_SUCCESS) { |
| 2421 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 2422 | ASSERT_TRUE(h_result != nullptr); |
| 2423 | ASSERT_EQ(4, h_result->h_length); |
| 2424 | ASSERT_FALSE(h_result->h_addr_list[0] == nullptr); |
| 2425 | EXPECT_EQ(ADDR4, ToString(h_result)); |
| 2426 | EXPECT_TRUE(h_result->h_addr_list[1] == nullptr); |
| 2427 | } else { |
| 2428 | EXPECT_EQ(0U, GetNumQueriesForType(dns, ns_type::ns_t_a, host_name)); |
| 2429 | ASSERT_TRUE(h_result == nullptr); |
| 2430 | ASSERT_EQ(HOST_NOT_FOUND, h_errno); |
| 2431 | } |
| 2432 | } else if (config.method == GETADDRINFO) { |
| 2433 | ScopedAddrinfo ai_result; |
| 2434 | addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; |
| 2435 | ai_result = safe_getaddrinfo(host_name, nullptr, &hints); |
| 2436 | if (config.expectResult == EXPECT_SUCCESS) { |
| 2437 | EXPECT_TRUE(ai_result != nullptr); |
| 2438 | EXPECT_EQ(1U, GetNumQueries(dns, host_name)); |
| 2439 | const std::string result_str = ToString(ai_result); |
| 2440 | EXPECT_EQ(ADDR4, result_str); |
| 2441 | } else { |
| 2442 | EXPECT_TRUE(ai_result == nullptr); |
| 2443 | EXPECT_EQ(0U, GetNumQueries(dns, host_name)); |
| 2444 | } |
| 2445 | } else { |
| 2446 | FAIL() << "Unsupported query method: " << config.method; |
| 2447 | } |
| 2448 | |
Mike Yu | dd4ac2d | 2019-05-31 16:52:11 +0800 | [diff] [blame] | 2449 | tls.clearQueries(); |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2450 | dns.clearQueries(); |
| 2451 | } |
Mike Yu | 4f3747b | 2018-12-02 17:54:29 +0900 | [diff] [blame] | 2452 | } |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2453 | |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2454 | // DNS-over-TLS validation success, but server does not respond to TLS query after a while. |
| 2455 | // Resolver should have a reasonable number of retries instead of spinning forever. We don't have |
| 2456 | // an efficient way to know if resolver is stuck in an infinite loop. However, test case will be |
| 2457 | // failed due to timeout. |
| 2458 | TEST_F(ResolverTest, UnstableTls) { |
| 2459 | const char CLEARTEXT_ADDR[] = "127.0.0.53"; |
| 2460 | const char CLEARTEXT_PORT[] = "53"; |
| 2461 | const char TLS_PORT[] = "853"; |
| 2462 | const char* host_name1 = "nonexistent1.example.com."; |
| 2463 | const char* host_name2 = "nonexistent2.example.com."; |
| 2464 | const std::vector<std::string> servers = {CLEARTEXT_ADDR}; |
| 2465 | |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 2466 | test::DNSResponder dns(CLEARTEXT_ADDR, CLEARTEXT_PORT, ns_rcode::ns_r_servfail); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2467 | ASSERT_TRUE(dns.startServer()); |
| 2468 | dns.setEdns(test::DNSResponder::Edns::FORMERR_ON_EDNS); |
| 2469 | test::DnsTlsFrontend tls(CLEARTEXT_ADDR, TLS_PORT, CLEARTEXT_ADDR, CLEARTEXT_PORT); |
| 2470 | ASSERT_TRUE(tls.startServer()); |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 2471 | ASSERT_TRUE( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2472 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2473 | // Wait for validation complete. |
| 2474 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 2475 | // Shutdown TLS server to get an error. It's similar to no response case but without waiting. |
| 2476 | tls.stopServer(); |
| 2477 | |
| 2478 | const hostent* h_result = gethostbyname(host_name1); |
| 2479 | EXPECT_EQ(1U, GetNumQueries(dns, host_name1)); |
| 2480 | ASSERT_TRUE(h_result == nullptr); |
| 2481 | ASSERT_EQ(HOST_NOT_FOUND, h_errno); |
| 2482 | |
| 2483 | addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; |
| 2484 | ScopedAddrinfo ai_result = safe_getaddrinfo(host_name2, nullptr, &hints); |
| 2485 | EXPECT_TRUE(ai_result == nullptr); |
| 2486 | EXPECT_EQ(1U, GetNumQueries(dns, host_name2)); |
| 2487 | } |
| 2488 | |
| 2489 | // DNS-over-TLS validation success, but server does not respond to TLS query after a while. |
| 2490 | // Moreover, server responds RCODE=FORMERR even on non-EDNS query. |
| 2491 | TEST_F(ResolverTest, BogusDnsServer) { |
| 2492 | const char CLEARTEXT_ADDR[] = "127.0.0.53"; |
| 2493 | const char CLEARTEXT_PORT[] = "53"; |
| 2494 | const char TLS_PORT[] = "853"; |
| 2495 | const char* host_name1 = "nonexistent1.example.com."; |
| 2496 | const char* host_name2 = "nonexistent2.example.com."; |
| 2497 | const std::vector<std::string> servers = {CLEARTEXT_ADDR}; |
| 2498 | |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 2499 | test::DNSResponder dns(CLEARTEXT_ADDR, CLEARTEXT_PORT, ns_rcode::ns_r_servfail); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2500 | ASSERT_TRUE(dns.startServer()); |
| 2501 | test::DnsTlsFrontend tls(CLEARTEXT_ADDR, TLS_PORT, CLEARTEXT_ADDR, CLEARTEXT_PORT); |
| 2502 | ASSERT_TRUE(tls.startServer()); |
Bernie Innocenti | a5c7b7b | 2019-01-17 21:28:24 +0900 | [diff] [blame] | 2503 | ASSERT_TRUE( |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2504 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
Ken Chen | 0a01553 | 2019-01-02 14:59:38 +0800 | [diff] [blame] | 2505 | // Wait for validation complete. |
| 2506 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 2507 | // Shutdown TLS server to get an error. It's similar to no response case but without waiting. |
| 2508 | tls.stopServer(); |
| 2509 | dns.setEdns(test::DNSResponder::Edns::FORMERR_UNCOND); |
| 2510 | |
| 2511 | const hostent* h_result = gethostbyname(host_name1); |
| 2512 | EXPECT_EQ(0U, GetNumQueries(dns, host_name1)); |
| 2513 | ASSERT_TRUE(h_result == nullptr); |
| 2514 | ASSERT_EQ(HOST_NOT_FOUND, h_errno); |
| 2515 | |
| 2516 | addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; |
| 2517 | ScopedAddrinfo ai_result = safe_getaddrinfo(host_name2, nullptr, &hints); |
| 2518 | EXPECT_TRUE(ai_result == nullptr); |
| 2519 | EXPECT_EQ(0U, GetNumQueries(dns, host_name2)); |
| 2520 | } |
| 2521 | |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2522 | TEST_F(ResolverTest, GetAddrInfo_Dns64Synthesize) { |
| 2523 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2524 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2525 | constexpr char host_name[] = "v4only.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2526 | const std::vector<DnsRecord> records = { |
| 2527 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2528 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2529 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2530 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2531 | test::DNSResponder dns(listen_addr); |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2532 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2533 | |
| 2534 | std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2535 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2536 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2537 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2538 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2539 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2540 | |
| 2541 | // hints are necessary in order to let netd know which type of addresses the caller is |
| 2542 | // interested in. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2543 | const addrinfo hints = {.ai_family = AF_UNSPEC}; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2544 | ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, &hints); |
| 2545 | EXPECT_TRUE(result != nullptr); |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2546 | // TODO: BUG: there should only be two queries, one AAAA (which returns no records) and one A |
| 2547 | // (which returns 1.2.3.4). But there is an extra AAAA. |
| 2548 | EXPECT_EQ(3U, GetNumQueries(dns, host_name)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2549 | |
| 2550 | std::string result_str = ToString(result); |
| 2551 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 2552 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2553 | // Stopping NAT64 prefix discovery disables synthesis. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2554 | EXPECT_TRUE(mDnsClient.resolvService()->stopPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2555 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_NOT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2556 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2557 | dns.clearQueries(); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2558 | |
| 2559 | result = safe_getaddrinfo("v4only", nullptr, &hints); |
| 2560 | EXPECT_TRUE(result != nullptr); |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2561 | // TODO: BUG: there should only be one query, an AAAA (which returns no records), because the |
| 2562 | // A is already cached. But there is an extra AAAA. |
| 2563 | EXPECT_EQ(2U, GetNumQueries(dns, host_name)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2564 | |
| 2565 | result_str = ToString(result); |
| 2566 | EXPECT_EQ(result_str, "1.2.3.4"); |
| 2567 | } |
| 2568 | |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2569 | TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecified) { |
| 2570 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2571 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2572 | constexpr char host_name[] = "v4only.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2573 | const std::vector<DnsRecord> records = { |
| 2574 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2575 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2576 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2577 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2578 | test::DNSResponder dns(listen_addr); |
| 2579 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2580 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2581 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2582 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2583 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2584 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2585 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2586 | |
| 2587 | // Ensure to synthesize AAAA if AF_INET6 is specified, and not to synthesize AAAA |
| 2588 | // in AF_INET case. |
| 2589 | addrinfo hints; |
| 2590 | memset(&hints, 0, sizeof(hints)); |
| 2591 | hints.ai_family = AF_INET6; |
| 2592 | ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, &hints); |
| 2593 | EXPECT_TRUE(result != nullptr); |
| 2594 | std::string result_str = ToString(result); |
| 2595 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 2596 | |
| 2597 | hints.ai_family = AF_INET; |
| 2598 | result = safe_getaddrinfo("v4only", nullptr, &hints); |
| 2599 | EXPECT_TRUE(result != nullptr); |
| 2600 | EXPECT_LE(2U, GetNumQueries(dns, host_name)); |
| 2601 | result_str = ToString(result); |
| 2602 | EXPECT_EQ(result_str, "1.2.3.4"); |
| 2603 | } |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2604 | |
| 2605 | TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedV6) { |
| 2606 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2607 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2608 | constexpr char host_name[] = "v4v6.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2609 | const std::vector<DnsRecord> records = { |
| 2610 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2611 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2612 | {host_name, ns_type::ns_t_aaaa, "2001:db8::1.2.3.4"}, |
| 2613 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2614 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2615 | test::DNSResponder dns(listen_addr); |
| 2616 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2617 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2618 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2619 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2620 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2621 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2622 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2623 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2624 | const addrinfo hints = {.ai_family = AF_UNSPEC}; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2625 | ScopedAddrinfo result = safe_getaddrinfo("v4v6", nullptr, &hints); |
| 2626 | EXPECT_TRUE(result != nullptr); |
| 2627 | EXPECT_LE(2U, GetNumQueries(dns, host_name)); |
| 2628 | |
| 2629 | // In AF_UNSPEC case, do not synthesize AAAA if there's at least one AAAA answer. |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2630 | const std::vector<std::string> result_strs = ToStrings(result); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2631 | for (const auto& str : result_strs) { |
| 2632 | EXPECT_TRUE(str == "1.2.3.4" || str == "2001:db8::102:304") |
| 2633 | << ", result_str='" << str << "'"; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedNoV6) { |
| 2638 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2639 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2640 | constexpr char host_name[] = "v4v6.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2641 | const std::vector<DnsRecord> records = { |
| 2642 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2643 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2644 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2645 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2646 | test::DNSResponder dns(listen_addr); |
| 2647 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2648 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2649 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2650 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2651 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2652 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2653 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2654 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2655 | const addrinfo hints = {.ai_family = AF_UNSPEC}; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2656 | ScopedAddrinfo result = safe_getaddrinfo("v4v6", nullptr, &hints); |
| 2657 | EXPECT_TRUE(result != nullptr); |
| 2658 | EXPECT_LE(2U, GetNumQueries(dns, host_name)); |
| 2659 | |
| 2660 | // In AF_UNSPEC case, synthesize AAAA if there's no AAAA answer. |
| 2661 | std::string result_str = ToString(result); |
| 2662 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 2663 | } |
| 2664 | |
| 2665 | TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecialUseIPv4Addresses) { |
| 2666 | constexpr char THIS_NETWORK[] = "this_network"; |
| 2667 | constexpr char LOOPBACK[] = "loopback"; |
| 2668 | constexpr char LINK_LOCAL[] = "link_local"; |
| 2669 | constexpr char MULTICAST[] = "multicast"; |
| 2670 | constexpr char LIMITED_BROADCAST[] = "limited_broadcast"; |
| 2671 | |
| 2672 | constexpr char ADDR_THIS_NETWORK[] = "0.0.0.1"; |
| 2673 | constexpr char ADDR_LOOPBACK[] = "127.0.0.1"; |
| 2674 | constexpr char ADDR_LINK_LOCAL[] = "169.254.0.1"; |
| 2675 | constexpr char ADDR_MULTICAST[] = "224.0.0.1"; |
| 2676 | constexpr char ADDR_LIMITED_BROADCAST[] = "255.255.255.255"; |
| 2677 | |
| 2678 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2679 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2680 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2681 | test::DNSResponder dns(listen_addr); |
| 2682 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::"}}); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2683 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2684 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2685 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2686 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2687 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2688 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2689 | |
| 2690 | static const struct TestConfig { |
| 2691 | std::string name; |
| 2692 | std::string addr; |
| 2693 | |
| 2694 | std::string asHostName() const { return StringPrintf("%s.example.com.", name.c_str()); } |
| 2695 | } testConfigs[]{ |
| 2696 | {THIS_NETWORK, ADDR_THIS_NETWORK}, |
| 2697 | {LOOPBACK, ADDR_LOOPBACK}, |
| 2698 | {LINK_LOCAL, ADDR_LINK_LOCAL}, |
| 2699 | {MULTICAST, ADDR_MULTICAST}, |
| 2700 | {LIMITED_BROADCAST, ADDR_LIMITED_BROADCAST} |
| 2701 | }; |
| 2702 | |
| 2703 | for (const auto& config : testConfigs) { |
| 2704 | const std::string testHostName = config.asHostName(); |
| 2705 | SCOPED_TRACE(testHostName); |
| 2706 | |
| 2707 | const char* host_name = testHostName.c_str(); |
| 2708 | dns.addMapping(host_name, ns_type::ns_t_a, config.addr.c_str()); |
| 2709 | |
| 2710 | addrinfo hints; |
| 2711 | memset(&hints, 0, sizeof(hints)); |
| 2712 | hints.ai_family = AF_INET6; |
| 2713 | ScopedAddrinfo result = safe_getaddrinfo(config.name.c_str(), nullptr, &hints); |
| 2714 | // In AF_INET6 case, don't return IPv4 answers |
| 2715 | EXPECT_TRUE(result == nullptr); |
| 2716 | EXPECT_LE(2U, GetNumQueries(dns, host_name)); |
| 2717 | dns.clearQueries(); |
| 2718 | |
| 2719 | memset(&hints, 0, sizeof(hints)); |
| 2720 | hints.ai_family = AF_UNSPEC; |
| 2721 | result = safe_getaddrinfo(config.name.c_str(), nullptr, &hints); |
| 2722 | EXPECT_TRUE(result != nullptr); |
| 2723 | // Expect IPv6 query only. IPv4 answer has been cached in previous query. |
| 2724 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 2725 | // In AF_UNSPEC case, don't synthesize special use IPv4 address. |
| 2726 | std::string result_str = ToString(result); |
| 2727 | EXPECT_EQ(result_str, config.addr.c_str()); |
| 2728 | dns.clearQueries(); |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | TEST_F(ResolverTest, GetAddrInfo_Dns64QueryWithNullArgumentHints) { |
| 2733 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2734 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2735 | constexpr char host_name[] = "v4only.example.com."; |
| 2736 | constexpr char host_name2[] = "v4v6.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2737 | const std::vector<DnsRecord> records = { |
| 2738 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2739 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 2740 | {host_name2, ns_type::ns_t_a, "1.2.3.4"}, |
| 2741 | {host_name2, ns_type::ns_t_aaaa, "2001:db8::1.2.3.4"}, |
| 2742 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2743 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2744 | test::DNSResponder dns(listen_addr); |
| 2745 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2746 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2747 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2748 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2749 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2750 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2751 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2752 | |
| 2753 | // Assign argument hints of getaddrinfo() as null is equivalent to set ai_family AF_UNSPEC. |
| 2754 | // In AF_UNSPEC case, synthesize AAAA if there has A answer only. |
| 2755 | ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, nullptr); |
| 2756 | EXPECT_TRUE(result != nullptr); |
| 2757 | EXPECT_LE(2U, GetNumQueries(dns, host_name)); |
| 2758 | std::string result_str = ToString(result); |
| 2759 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 2760 | dns.clearQueries(); |
| 2761 | |
| 2762 | // In AF_UNSPEC case, do not synthesize AAAA if there's at least one AAAA answer. |
| 2763 | result = safe_getaddrinfo("v4v6", nullptr, nullptr); |
| 2764 | EXPECT_TRUE(result != nullptr); |
| 2765 | EXPECT_LE(2U, GetNumQueries(dns, host_name2)); |
| 2766 | std::vector<std::string> result_strs = ToStrings(result); |
| 2767 | for (const auto& str : result_strs) { |
| 2768 | EXPECT_TRUE(str == "1.2.3.4" || str == "2001:db8::102:304") |
| 2769 | << ", result_str='" << str << "'"; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | TEST_F(ResolverTest, GetAddrInfo_Dns64QueryNullArgumentNode) { |
| 2774 | constexpr char ADDR_ANYADDR_V4[] = "0.0.0.0"; |
| 2775 | constexpr char ADDR_ANYADDR_V6[] = "::"; |
| 2776 | constexpr char ADDR_LOCALHOST_V4[] = "127.0.0.1"; |
| 2777 | constexpr char ADDR_LOCALHOST_V6[] = "::1"; |
| 2778 | |
| 2779 | constexpr char PORT_NAME_HTTP[] = "http"; |
| 2780 | constexpr char PORT_NUMBER_HTTP[] = "80"; |
| 2781 | |
| 2782 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2783 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2784 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2785 | test::DNSResponder dns(listen_addr); |
| 2786 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::"}}); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2787 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2788 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2789 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2790 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2791 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2792 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2793 | |
| 2794 | // If node is null, return address is listed by libc/getaddrinfo.c as follows. |
| 2795 | // - passive socket -> anyaddr (0.0.0.0 or ::) |
| 2796 | // - non-passive socket -> localhost (127.0.0.1 or ::1) |
| 2797 | static const struct TestConfig { |
| 2798 | int flag; |
| 2799 | std::string addr_v4; |
| 2800 | std::string addr_v6; |
| 2801 | |
| 2802 | std::string asParameters() const { |
| 2803 | return StringPrintf("flag=%d, addr_v4=%s, addr_v6=%s", flag, addr_v4.c_str(), |
| 2804 | addr_v6.c_str()); |
| 2805 | } |
| 2806 | } testConfigs[]{ |
| 2807 | {0 /* non-passive */, ADDR_LOCALHOST_V4, ADDR_LOCALHOST_V6}, |
| 2808 | {AI_PASSIVE, ADDR_ANYADDR_V4, ADDR_ANYADDR_V6} |
| 2809 | }; |
| 2810 | |
| 2811 | for (const auto& config : testConfigs) { |
| 2812 | SCOPED_TRACE(config.asParameters()); |
| 2813 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2814 | addrinfo hints = { |
| 2815 | .ai_family = AF_UNSPEC, // any address family |
| 2816 | .ai_socktype = 0, // any type |
| 2817 | .ai_protocol = 0, // any protocol |
| 2818 | .ai_flags = config.flag, |
| 2819 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2820 | |
| 2821 | // Assign hostname as null and service as port name. |
| 2822 | ScopedAddrinfo result = safe_getaddrinfo(nullptr, PORT_NAME_HTTP, &hints); |
| 2823 | ASSERT_TRUE(result != nullptr); |
| 2824 | |
| 2825 | // Can't be synthesized because it should not get into Netd. |
| 2826 | std::vector<std::string> result_strs = ToStrings(result); |
| 2827 | for (const auto& str : result_strs) { |
| 2828 | EXPECT_TRUE(str == config.addr_v4 || str == config.addr_v6) |
| 2829 | << ", result_str='" << str << "'"; |
| 2830 | } |
| 2831 | |
| 2832 | // Assign hostname as null and service as numeric port number. |
| 2833 | hints.ai_flags = config.flag | AI_NUMERICSERV; |
| 2834 | result = safe_getaddrinfo(nullptr, PORT_NUMBER_HTTP, &hints); |
| 2835 | ASSERT_TRUE(result != nullptr); |
| 2836 | |
| 2837 | // Can't be synthesized because it should not get into Netd. |
| 2838 | result_strs = ToStrings(result); |
| 2839 | for (const auto& str : result_strs) { |
| 2840 | EXPECT_TRUE(str == config.addr_v4 || str == config.addr_v6) |
| 2841 | << ", result_str='" << str << "'"; |
| 2842 | } |
| 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | TEST_F(ResolverTest, GetHostByAddr_ReverseDnsQueryWithHavingNat64Prefix) { |
| 2847 | struct hostent* result = nullptr; |
| 2848 | struct in_addr v4addr; |
| 2849 | struct in6_addr v6addr; |
| 2850 | |
| 2851 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2852 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2853 | constexpr char ptr_name[] = "v4v6.example.com."; |
| 2854 | // PTR record for IPv4 address 1.2.3.4 |
| 2855 | constexpr char ptr_addr_v4[] = "4.3.2.1.in-addr.arpa."; |
| 2856 | // PTR record for IPv6 address 2001:db8::102:304 |
| 2857 | constexpr char ptr_addr_v6[] = |
| 2858 | "4.0.3.0.2.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2859 | const std::vector<DnsRecord> records = { |
| 2860 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2861 | {ptr_addr_v4, ns_type::ns_t_ptr, ptr_name}, |
| 2862 | {ptr_addr_v6, ns_type::ns_t_ptr, ptr_name}, |
| 2863 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2864 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2865 | test::DNSResponder dns(listen_addr); |
| 2866 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2867 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2868 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2869 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2870 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2871 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2872 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2873 | |
| 2874 | // Reverse IPv4 DNS query. Prefix should have no effect on it. |
| 2875 | inet_pton(AF_INET, "1.2.3.4", &v4addr); |
| 2876 | result = gethostbyaddr(&v4addr, sizeof(v4addr), AF_INET); |
| 2877 | ASSERT_TRUE(result != nullptr); |
| 2878 | std::string result_str = result->h_name ? result->h_name : "null"; |
| 2879 | EXPECT_EQ(result_str, "v4v6.example.com"); |
| 2880 | |
| 2881 | // Reverse IPv6 DNS query. Prefix should have no effect on it. |
| 2882 | inet_pton(AF_INET6, "2001:db8::102:304", &v6addr); |
| 2883 | result = gethostbyaddr(&v6addr, sizeof(v6addr), AF_INET6); |
| 2884 | ASSERT_TRUE(result != nullptr); |
| 2885 | result_str = result->h_name ? result->h_name : "null"; |
| 2886 | EXPECT_EQ(result_str, "v4v6.example.com"); |
| 2887 | } |
| 2888 | |
| 2889 | TEST_F(ResolverTest, GetHostByAddr_ReverseDns64Query) { |
| 2890 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2891 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2892 | constexpr char ptr_name[] = "v4only.example.com."; |
| 2893 | // PTR record for IPv4 address 1.2.3.4 |
| 2894 | constexpr char ptr_addr_v4[] = "4.3.2.1.in-addr.arpa."; |
| 2895 | // PTR record for IPv6 address 64:ff9b::1.2.3.4 |
| 2896 | constexpr char ptr_addr_v6_nomapping[] = |
| 2897 | "4.0.3.0.2.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.9.f.f.4.6.0.0.ip6.arpa."; |
| 2898 | constexpr char ptr_name_v6_synthesis[] = "v6synthesis.example.com."; |
| 2899 | // PTR record for IPv6 address 64:ff9b::5.6.7.8 |
| 2900 | constexpr char ptr_addr_v6_synthesis[] = |
| 2901 | "8.0.7.0.6.0.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.9.f.f.4.6.0.0.ip6.arpa."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2902 | const std::vector<DnsRecord> records = { |
| 2903 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 2904 | {ptr_addr_v4, ns_type::ns_t_ptr, ptr_name}, |
| 2905 | {ptr_addr_v6_synthesis, ns_type::ns_t_ptr, ptr_name_v6_synthesis}, |
| 2906 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2907 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2908 | test::DNSResponder dns(listen_addr); |
| 2909 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2910 | // "ptr_addr_v6_nomapping" is not mapped in DNS server |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2911 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2912 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2913 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2914 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2915 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2916 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2917 | |
| 2918 | // Synthesized PTR record doesn't exist on DNS server |
| 2919 | // Reverse IPv6 DNS64 query while DNS server doesn't have an answer for synthesized address. |
| 2920 | // After querying synthesized address failed, expect that prefix is removed from IPv6 |
| 2921 | // synthesized address and do reverse IPv4 query instead. |
| 2922 | struct in6_addr v6addr; |
| 2923 | inet_pton(AF_INET6, "64:ff9b::1.2.3.4", &v6addr); |
| 2924 | struct hostent* result = gethostbyaddr(&v6addr, sizeof(v6addr), AF_INET6); |
| 2925 | ASSERT_TRUE(result != nullptr); |
| 2926 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v6_nomapping)); // PTR record not exist |
| 2927 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v4)); // PTR record exist |
| 2928 | std::string result_str = result->h_name ? result->h_name : "null"; |
| 2929 | EXPECT_EQ(result_str, "v4only.example.com"); |
| 2930 | // Check that return address has been mapped from IPv4 to IPv6 address because Netd |
| 2931 | // removes NAT64 prefix and does IPv4 DNS reverse lookup in this case. Then, Netd |
| 2932 | // fakes the return IPv4 address as original queried IPv6 address. |
| 2933 | result_str = ToString(result); |
| 2934 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 2935 | dns.clearQueries(); |
| 2936 | |
| 2937 | // Synthesized PTR record exists on DNS server |
| 2938 | // Reverse IPv6 DNS64 query while DNS server has an answer for synthesized address. |
| 2939 | // Expect to Netd pass through synthesized address for DNS queries. |
| 2940 | inet_pton(AF_INET6, "64:ff9b::5.6.7.8", &v6addr); |
| 2941 | result = gethostbyaddr(&v6addr, sizeof(v6addr), AF_INET6); |
| 2942 | ASSERT_TRUE(result != nullptr); |
| 2943 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v6_synthesis)); |
| 2944 | result_str = result->h_name ? result->h_name : "null"; |
| 2945 | EXPECT_EQ(result_str, "v6synthesis.example.com"); |
| 2946 | } |
| 2947 | |
| 2948 | TEST_F(ResolverTest, GetHostByAddr_ReverseDns64QueryFromHostFile) { |
| 2949 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 2950 | constexpr char host_name[] = "localhost"; |
| 2951 | // The address is synthesized by prefix64:localhost. |
| 2952 | constexpr char host_addr[] = "64:ff9b::7f00:1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2953 | constexpr char listen_addr[] = "::1"; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2954 | |
| 2955 | test::DNSResponder dns(listen_addr); |
| 2956 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}}); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2957 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 2958 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2959 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 2960 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 2961 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 2962 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 2963 | |
| 2964 | // Using synthesized "localhost" address to be a trick for resolving host name |
| 2965 | // from host file /etc/hosts and "localhost" is the only name in /etc/hosts. Note that this is |
| 2966 | // not realistic: the code never synthesizes AAAA records for addresses in 127.0.0.0/8. |
| 2967 | struct in6_addr v6addr; |
| 2968 | inet_pton(AF_INET6, host_addr, &v6addr); |
| 2969 | struct hostent* result = gethostbyaddr(&v6addr, sizeof(v6addr), AF_INET6); |
| 2970 | ASSERT_TRUE(result != nullptr); |
| 2971 | // Expect no DNS queries; localhost is resolved via /etc/hosts. |
| 2972 | EXPECT_EQ(0U, GetNumQueries(dns, host_name)); |
| 2973 | |
| 2974 | ASSERT_EQ(sizeof(in6_addr), (unsigned) result->h_length); |
| 2975 | ASSERT_EQ(AF_INET6, result->h_addrtype); |
| 2976 | std::string result_str = ToString(result); |
| 2977 | EXPECT_EQ(result_str, host_addr); |
| 2978 | result_str = result->h_name ? result->h_name : "null"; |
| 2979 | EXPECT_EQ(result_str, host_name); |
| 2980 | } |
| 2981 | |
Hungming Chen | 9e6185a | 2019-06-04 16:09:19 +0800 | [diff] [blame] | 2982 | TEST_F(ResolverTest, GetHostByAddr_cnamesClasslessReverseDelegation) { |
| 2983 | // IPv4 addresses in the subnet with notation '/' or '-'. |
| 2984 | constexpr char addr_slash[] = "192.0.2.1"; |
| 2985 | constexpr char addr_hyphen[] = "192.0.3.1"; |
| 2986 | |
| 2987 | // Used to verify DNS reverse query for classless reverse lookup zone. See detail in RFC 2317 |
| 2988 | // section 4. |
| 2989 | const static std::vector<DnsRecord> records = { |
| 2990 | // The records for reverse querying "192.0.2.1" in the subnet with notation '/'. |
| 2991 | {"1.2.0.192.in-addr.arpa.", ns_type::ns_t_cname, "1.0/25.2.0.192.in-addr.arpa."}, |
| 2992 | {"1.0/25.2.0.192.in-addr.arpa.", ns_type::ns_t_ptr, kHelloExampleCom}, |
| 2993 | |
| 2994 | // The records for reverse querying "192.0.3.1" in the subnet with notation '-'. |
| 2995 | {"1.3.0.192.in-addr.arpa.", ns_type::ns_t_cname, "1.0-127.3.0.192.in-addr.arpa."}, |
| 2996 | {"1.0-127.3.0.192.in-addr.arpa.", ns_type::ns_t_ptr, kHelloExampleCom}, |
| 2997 | }; |
| 2998 | |
| 2999 | test::DNSResponder dns; |
| 3000 | StartDns(dns, records); |
| 3001 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 3002 | |
| 3003 | for (const auto& address : {addr_slash, addr_hyphen}) { |
| 3004 | SCOPED_TRACE(address); |
| 3005 | |
| 3006 | in_addr v4addr; |
| 3007 | ASSERT_TRUE(inet_pton(AF_INET, address, &v4addr)); |
| 3008 | hostent* result = gethostbyaddr(&v4addr, sizeof(v4addr), AF_INET); |
| 3009 | ASSERT_TRUE(result != nullptr); |
| 3010 | EXPECT_STREQ("hello.example.com", result->h_name); |
| 3011 | } |
| 3012 | } |
| 3013 | |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3014 | TEST_F(ResolverTest, GetNameInfo_ReverseDnsQueryWithHavingNat64Prefix) { |
| 3015 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3016 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3017 | constexpr char ptr_name[] = "v4v6.example.com."; |
| 3018 | // PTR record for IPv4 address 1.2.3.4 |
| 3019 | constexpr char ptr_addr_v4[] = "4.3.2.1.in-addr.arpa."; |
| 3020 | // PTR record for IPv6 address 2001:db8::102:304 |
| 3021 | constexpr char ptr_addr_v6[] = |
| 3022 | "4.0.3.0.2.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3023 | const std::vector<DnsRecord> records = { |
| 3024 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 3025 | {ptr_addr_v4, ns_type::ns_t_ptr, ptr_name}, |
| 3026 | {ptr_addr_v6, ns_type::ns_t_ptr, ptr_name}, |
| 3027 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3028 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3029 | test::DNSResponder dns(listen_addr); |
| 3030 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3031 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3032 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3033 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3034 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3035 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3036 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3037 | |
| 3038 | static const struct TestConfig { |
| 3039 | int flag; |
| 3040 | int family; |
| 3041 | std::string addr; |
| 3042 | std::string host; |
| 3043 | |
| 3044 | std::string asParameters() const { |
| 3045 | return StringPrintf("flag=%d, family=%d, addr=%s, host=%s", flag, family, addr.c_str(), |
| 3046 | host.c_str()); |
| 3047 | } |
| 3048 | } testConfigs[]{ |
| 3049 | {NI_NAMEREQD, AF_INET, "1.2.3.4", "v4v6.example.com"}, |
| 3050 | {NI_NUMERICHOST, AF_INET, "1.2.3.4", "1.2.3.4"}, |
| 3051 | {0, AF_INET, "1.2.3.4", "v4v6.example.com"}, |
| 3052 | {0, AF_INET, "5.6.7.8", "5.6.7.8"}, // unmapped |
| 3053 | {NI_NAMEREQD, AF_INET6, "2001:db8::102:304", "v4v6.example.com"}, |
| 3054 | {NI_NUMERICHOST, AF_INET6, "2001:db8::102:304", "2001:db8::102:304"}, |
| 3055 | {0, AF_INET6, "2001:db8::102:304", "v4v6.example.com"}, |
| 3056 | {0, AF_INET6, "2001:db8::506:708", "2001:db8::506:708"}, // unmapped |
| 3057 | }; |
| 3058 | |
| 3059 | // Reverse IPv4/IPv6 DNS query. Prefix should have no effect on it. |
| 3060 | for (const auto& config : testConfigs) { |
| 3061 | SCOPED_TRACE(config.asParameters()); |
| 3062 | |
| 3063 | int rv; |
| 3064 | char host[NI_MAXHOST]; |
| 3065 | struct sockaddr_in sin; |
| 3066 | struct sockaddr_in6 sin6; |
| 3067 | if (config.family == AF_INET) { |
| 3068 | memset(&sin, 0, sizeof(sin)); |
| 3069 | sin.sin_family = AF_INET; |
| 3070 | inet_pton(AF_INET, config.addr.c_str(), &sin.sin_addr); |
| 3071 | rv = getnameinfo((const struct sockaddr*) &sin, sizeof(sin), host, sizeof(host), |
| 3072 | nullptr, 0, config.flag); |
| 3073 | if (config.flag == NI_NAMEREQD) EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v4)); |
| 3074 | } else if (config.family == AF_INET6) { |
| 3075 | memset(&sin6, 0, sizeof(sin6)); |
| 3076 | sin6.sin6_family = AF_INET6; |
| 3077 | inet_pton(AF_INET6, config.addr.c_str(), &sin6.sin6_addr); |
| 3078 | rv = getnameinfo((const struct sockaddr*) &sin6, sizeof(sin6), host, sizeof(host), |
| 3079 | nullptr, 0, config.flag); |
| 3080 | if (config.flag == NI_NAMEREQD) EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v6)); |
| 3081 | } |
| 3082 | ASSERT_EQ(0, rv); |
| 3083 | std::string result_str = host; |
| 3084 | EXPECT_EQ(result_str, config.host); |
| 3085 | dns.clearQueries(); |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | TEST_F(ResolverTest, GetNameInfo_ReverseDns64Query) { |
| 3090 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3091 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3092 | constexpr char ptr_name[] = "v4only.example.com."; |
| 3093 | // PTR record for IPv4 address 1.2.3.4 |
| 3094 | constexpr char ptr_addr_v4[] = "4.3.2.1.in-addr.arpa."; |
| 3095 | // PTR record for IPv6 address 64:ff9b::1.2.3.4 |
| 3096 | constexpr char ptr_addr_v6_nomapping[] = |
| 3097 | "4.0.3.0.2.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.9.f.f.4.6.0.0.ip6.arpa."; |
| 3098 | constexpr char ptr_name_v6_synthesis[] = "v6synthesis.example.com."; |
| 3099 | // PTR record for IPv6 address 64:ff9b::5.6.7.8 |
| 3100 | constexpr char ptr_addr_v6_synthesis[] = |
| 3101 | "8.0.7.0.6.0.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.b.9.f.f.4.6.0.0.ip6.arpa."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3102 | const std::vector<DnsRecord> records = { |
| 3103 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 3104 | {ptr_addr_v4, ns_type::ns_t_ptr, ptr_name}, |
| 3105 | {ptr_addr_v6_synthesis, ns_type::ns_t_ptr, ptr_name_v6_synthesis}, |
| 3106 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3107 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3108 | test::DNSResponder dns(listen_addr); |
| 3109 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3110 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3111 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3112 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3113 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3114 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3115 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3116 | |
| 3117 | static const struct TestConfig { |
| 3118 | bool hasSynthesizedPtrRecord; |
| 3119 | int flag; |
| 3120 | std::string addr; |
| 3121 | std::string host; |
| 3122 | |
| 3123 | std::string asParameters() const { |
| 3124 | return StringPrintf("hasSynthesizedPtrRecord=%d, flag=%d, addr=%s, host=%s", |
| 3125 | hasSynthesizedPtrRecord, flag, addr.c_str(), host.c_str()); |
| 3126 | } |
| 3127 | } testConfigs[]{ |
| 3128 | {false, NI_NAMEREQD, "64:ff9b::102:304", "v4only.example.com"}, |
| 3129 | {false, NI_NUMERICHOST, "64:ff9b::102:304", "64:ff9b::102:304"}, |
| 3130 | {false, 0, "64:ff9b::102:304", "v4only.example.com"}, |
| 3131 | {true, NI_NAMEREQD, "64:ff9b::506:708", "v6synthesis.example.com"}, |
| 3132 | {true, NI_NUMERICHOST, "64:ff9b::506:708", "64:ff9b::506:708"}, |
| 3133 | {true, 0, "64:ff9b::506:708", "v6synthesis.example.com"} |
| 3134 | }; |
| 3135 | |
| 3136 | // hasSynthesizedPtrRecord = false |
| 3137 | // Synthesized PTR record doesn't exist on DNS server |
| 3138 | // Reverse IPv6 DNS64 query while DNS server doesn't have an answer for synthesized address. |
| 3139 | // After querying synthesized address failed, expect that prefix is removed from IPv6 |
| 3140 | // synthesized address and do reverse IPv4 query instead. |
| 3141 | // |
| 3142 | // hasSynthesizedPtrRecord = true |
| 3143 | // Synthesized PTR record exists on DNS server |
| 3144 | // Reverse IPv6 DNS64 query while DNS server has an answer for synthesized address. |
| 3145 | // Expect to just pass through synthesized address for DNS queries. |
| 3146 | for (const auto& config : testConfigs) { |
| 3147 | SCOPED_TRACE(config.asParameters()); |
| 3148 | |
| 3149 | char host[NI_MAXHOST]; |
| 3150 | struct sockaddr_in6 sin6; |
| 3151 | memset(&sin6, 0, sizeof(sin6)); |
| 3152 | sin6.sin6_family = AF_INET6; |
| 3153 | inet_pton(AF_INET6, config.addr.c_str(), &sin6.sin6_addr); |
| 3154 | int rv = getnameinfo((const struct sockaddr*) &sin6, sizeof(sin6), host, sizeof(host), |
| 3155 | nullptr, 0, config.flag); |
| 3156 | ASSERT_EQ(0, rv); |
| 3157 | if (config.flag == NI_NAMEREQD) { |
| 3158 | if (config.hasSynthesizedPtrRecord) { |
| 3159 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v6_synthesis)); |
| 3160 | } else { |
| 3161 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v6_nomapping)); // PTR record not exist. |
| 3162 | EXPECT_LE(1U, GetNumQueries(dns, ptr_addr_v4)); // PTR record exist. |
| 3163 | } |
| 3164 | } |
| 3165 | std::string result_str = host; |
| 3166 | EXPECT_EQ(result_str, config.host); |
| 3167 | dns.clearQueries(); |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | TEST_F(ResolverTest, GetNameInfo_ReverseDns64QueryFromHostFile) { |
| 3172 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3173 | constexpr char host_name[] = "localhost"; |
| 3174 | // The address is synthesized by prefix64:localhost. |
| 3175 | constexpr char host_addr[] = "64:ff9b::7f00:1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3176 | constexpr char listen_addr[] = "::1"; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3177 | |
| 3178 | test::DNSResponder dns(listen_addr); |
| 3179 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}}); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3180 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3181 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3182 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3183 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3184 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3185 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3186 | |
| 3187 | // Using synthesized "localhost" address to be a trick for resolving host name |
| 3188 | // from host file /etc/hosts and "localhost" is the only name in /etc/hosts. Note that this is |
| 3189 | // not realistic: the code never synthesizes AAAA records for addresses in 127.0.0.0/8. |
| 3190 | char host[NI_MAXHOST]; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3191 | struct sockaddr_in6 sin6 = {.sin6_family = AF_INET6}; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3192 | inet_pton(AF_INET6, host_addr, &sin6.sin6_addr); |
| 3193 | int rv = getnameinfo((const struct sockaddr*) &sin6, sizeof(sin6), host, sizeof(host), nullptr, |
| 3194 | 0, NI_NAMEREQD); |
| 3195 | ASSERT_EQ(0, rv); |
| 3196 | // Expect no DNS queries; localhost is resolved via /etc/hosts. |
| 3197 | EXPECT_EQ(0U, GetNumQueries(dns, host_name)); |
| 3198 | |
| 3199 | std::string result_str = host; |
| 3200 | EXPECT_EQ(result_str, host_name); |
| 3201 | } |
| 3202 | |
Hungming Chen | 9e6185a | 2019-06-04 16:09:19 +0800 | [diff] [blame] | 3203 | TEST_F(ResolverTest, GetNameInfo_cnamesClasslessReverseDelegation) { |
| 3204 | // IPv4 addresses in the subnet with notation '/' or '-'. |
| 3205 | constexpr char addr_slash[] = "192.0.2.1"; |
| 3206 | constexpr char addr_hyphen[] = "192.0.3.1"; |
| 3207 | |
| 3208 | // Used to verify DNS reverse query for classless reverse lookup zone. See detail in RFC 2317 |
| 3209 | // section 4. |
| 3210 | const static std::vector<DnsRecord> records = { |
| 3211 | // The records for reverse querying "192.0.2.1" in the subnet with notation '/'. |
| 3212 | {"1.2.0.192.in-addr.arpa.", ns_type::ns_t_cname, "1.0/25.2.0.192.in-addr.arpa."}, |
| 3213 | {"1.0/25.2.0.192.in-addr.arpa.", ns_type::ns_t_ptr, kHelloExampleCom}, |
| 3214 | |
| 3215 | // The records for reverse querying "192.0.3.1" in the subnet with notation '-'. |
| 3216 | {"1.3.0.192.in-addr.arpa.", ns_type::ns_t_cname, "1.0-127.3.0.192.in-addr.arpa."}, |
| 3217 | {"1.0-127.3.0.192.in-addr.arpa.", ns_type::ns_t_ptr, kHelloExampleCom}, |
| 3218 | }; |
| 3219 | |
| 3220 | test::DNSResponder dns; |
| 3221 | StartDns(dns, records); |
| 3222 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork()); |
| 3223 | |
| 3224 | for (const auto& address : {addr_slash, addr_hyphen}) { |
| 3225 | SCOPED_TRACE(address); |
| 3226 | |
| 3227 | char host[NI_MAXHOST]; |
| 3228 | sockaddr_in sin = {.sin_family = AF_INET}; |
| 3229 | ASSERT_TRUE(inet_pton(AF_INET, address, &sin.sin_addr)); |
| 3230 | int rv = getnameinfo((const sockaddr*)&sin, sizeof(sin), host, sizeof(host), nullptr, 0, |
| 3231 | NI_NAMEREQD); |
| 3232 | ASSERT_EQ(0, rv); |
| 3233 | EXPECT_STREQ("hello.example.com", host); |
| 3234 | } |
| 3235 | } |
| 3236 | |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3237 | TEST_F(ResolverTest, GetHostByName2_Dns64Synthesize) { |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3238 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3239 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3240 | constexpr char host_name[] = "ipv4only.example.com."; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3241 | const std::vector<DnsRecord> records = { |
| 3242 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 3243 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 3244 | }; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3245 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3246 | test::DNSResponder dns(listen_addr); |
| 3247 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3248 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3249 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3250 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3251 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3252 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3253 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3254 | |
| 3255 | // Query an IPv4-only hostname. Expect that gets a synthesized address. |
| 3256 | struct hostent* result = gethostbyname2("ipv4only", AF_INET6); |
| 3257 | ASSERT_TRUE(result != nullptr); |
| 3258 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 3259 | std::string result_str = ToString(result); |
| 3260 | EXPECT_EQ(result_str, "64:ff9b::102:304"); |
| 3261 | } |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3262 | |
| 3263 | TEST_F(ResolverTest, GetHostByName2_DnsQueryWithHavingNat64Prefix) { |
| 3264 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3265 | constexpr char host_name[] = "v4v6.example.com."; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3266 | constexpr char listen_addr[] = "::1"; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3267 | const std::vector<DnsRecord> records = { |
| 3268 | {dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}, |
| 3269 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 3270 | {host_name, ns_type::ns_t_aaaa, "2001:db8::1.2.3.4"}, |
| 3271 | }; |
| 3272 | |
| 3273 | test::DNSResponder dns(listen_addr); |
| 3274 | StartDns(dns, records); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3275 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3276 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3277 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3278 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3279 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3280 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3281 | |
| 3282 | // IPv4 DNS query. Prefix should have no effect on it. |
| 3283 | struct hostent* result = gethostbyname2("v4v6", AF_INET); |
| 3284 | ASSERT_TRUE(result != nullptr); |
| 3285 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 3286 | std::string result_str = ToString(result); |
| 3287 | EXPECT_EQ(result_str, "1.2.3.4"); |
| 3288 | dns.clearQueries(); |
| 3289 | |
| 3290 | // IPv6 DNS query. Prefix should have no effect on it. |
| 3291 | result = gethostbyname2("v4v6", AF_INET6); |
| 3292 | ASSERT_TRUE(result != nullptr); |
| 3293 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 3294 | result_str = ToString(result); |
| 3295 | EXPECT_EQ(result_str, "2001:db8::102:304"); |
| 3296 | } |
| 3297 | |
| 3298 | TEST_F(ResolverTest, GetHostByName2_Dns64QuerySpecialUseIPv4Addresses) { |
| 3299 | constexpr char THIS_NETWORK[] = "this_network"; |
| 3300 | constexpr char LOOPBACK[] = "loopback"; |
| 3301 | constexpr char LINK_LOCAL[] = "link_local"; |
| 3302 | constexpr char MULTICAST[] = "multicast"; |
| 3303 | constexpr char LIMITED_BROADCAST[] = "limited_broadcast"; |
| 3304 | |
| 3305 | constexpr char ADDR_THIS_NETWORK[] = "0.0.0.1"; |
| 3306 | constexpr char ADDR_LOOPBACK[] = "127.0.0.1"; |
| 3307 | constexpr char ADDR_LINK_LOCAL[] = "169.254.0.1"; |
| 3308 | constexpr char ADDR_MULTICAST[] = "224.0.0.1"; |
| 3309 | constexpr char ADDR_LIMITED_BROADCAST[] = "255.255.255.255"; |
| 3310 | |
| 3311 | constexpr char listen_addr[] = "::1"; |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3312 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3313 | |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3314 | test::DNSResponder dns(listen_addr); |
| 3315 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::"}}); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3316 | const std::vector<std::string> servers = {listen_addr}; |
Xiao Ma | 09b7102 | 2018-12-11 17:56:32 +0900 | [diff] [blame] | 3317 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3318 | |
Lorenzo Colitti | d39345c | 2019-02-18 10:59:29 +0900 | [diff] [blame] | 3319 | // Start NAT64 prefix discovery and wait for it to complete. |
Luke Huang | 5bd827c | 2019-03-14 16:10:04 +0800 | [diff] [blame] | 3320 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3321 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
nuccachen | a26cc2a | 2018-07-17 18:07:23 +0800 | [diff] [blame] | 3322 | |
| 3323 | static const struct TestConfig { |
| 3324 | std::string name; |
| 3325 | std::string addr; |
| 3326 | |
| 3327 | std::string asHostName() const { |
| 3328 | return StringPrintf("%s.example.com.", |
| 3329 | name.c_str()); |
| 3330 | } |
| 3331 | } testConfigs[]{ |
| 3332 | {THIS_NETWORK, ADDR_THIS_NETWORK}, |
| 3333 | {LOOPBACK, ADDR_LOOPBACK}, |
| 3334 | {LINK_LOCAL, ADDR_LINK_LOCAL}, |
| 3335 | {MULTICAST, ADDR_MULTICAST}, |
| 3336 | {LIMITED_BROADCAST, ADDR_LIMITED_BROADCAST} |
| 3337 | }; |
| 3338 | |
| 3339 | for (const auto& config : testConfigs) { |
| 3340 | const std::string testHostName = config.asHostName(); |
| 3341 | SCOPED_TRACE(testHostName); |
| 3342 | |
| 3343 | const char* host_name = testHostName.c_str(); |
| 3344 | dns.addMapping(host_name, ns_type::ns_t_a, config.addr.c_str()); |
| 3345 | |
| 3346 | struct hostent* result = gethostbyname2(config.name.c_str(), AF_INET6); |
| 3347 | EXPECT_LE(1U, GetNumQueries(dns, host_name)); |
| 3348 | |
| 3349 | // In AF_INET6 case, don't synthesize special use IPv4 address. |
| 3350 | // Expect to have no answer |
| 3351 | EXPECT_EQ(nullptr, result); |
| 3352 | |
| 3353 | dns.clearQueries(); |
| 3354 | } |
Bernie Innocenti | 2fef2b8 | 2018-12-20 15:56:40 +0900 | [diff] [blame] | 3355 | } |
Mike Yu | f14e1a9 | 2019-05-10 13:54:58 +0800 | [diff] [blame] | 3356 | |
| 3357 | TEST_F(ResolverTest, PrefixDiscoveryBypassTls) { |
| 3358 | constexpr char listen_addr[] = "::1"; |
| 3359 | constexpr char cleartext_port[] = "53"; |
| 3360 | constexpr char tls_port[] = "853"; |
| 3361 | constexpr char dns64_name[] = "ipv4only.arpa."; |
| 3362 | const std::vector<std::string> servers = {listen_addr}; |
| 3363 | |
| 3364 | test::DNSResponder dns(listen_addr); |
| 3365 | StartDns(dns, {{dns64_name, ns_type::ns_t_aaaa, "64:ff9b::192.0.0.170"}}); |
| 3366 | test::DnsTlsFrontend tls(listen_addr, tls_port, listen_addr, cleartext_port); |
| 3367 | ASSERT_TRUE(tls.startServer()); |
| 3368 | |
| 3369 | // Setup OPPORTUNISTIC mode and wait for the validation complete. |
| 3370 | ASSERT_TRUE( |
| 3371 | mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", {})); |
| 3372 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 3373 | tls.clearQueries(); |
| 3374 | |
| 3375 | // Start NAT64 prefix discovery and wait for it complete. |
| 3376 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3377 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
Mike Yu | f14e1a9 | 2019-05-10 13:54:58 +0800 | [diff] [blame] | 3378 | |
| 3379 | // Verify it bypassed TLS even though there's a TLS server available. |
| 3380 | EXPECT_EQ(0, tls.queries()); |
| 3381 | EXPECT_EQ(1U, GetNumQueries(dns, dns64_name)); |
| 3382 | |
| 3383 | // Restart the testing network to reset the cache. |
| 3384 | mDnsClient.TearDown(); |
| 3385 | mDnsClient.SetUp(); |
| 3386 | dns.clearQueries(); |
| 3387 | |
| 3388 | // Setup STRICT mode and wait for the validation complete. |
| 3389 | ASSERT_TRUE(mDnsClient.SetResolversWithTls(servers, kDefaultSearchDomains, kDefaultParams, "", |
| 3390 | {base64Encode(tls.fingerprint())})); |
| 3391 | EXPECT_TRUE(tls.waitForQueries(1, 5000)); |
| 3392 | tls.clearQueries(); |
| 3393 | |
| 3394 | // Start NAT64 prefix discovery and wait for it to complete. |
| 3395 | EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 3396 | EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); |
Mike Yu | f14e1a9 | 2019-05-10 13:54:58 +0800 | [diff] [blame] | 3397 | |
| 3398 | // Verify it bypassed TLS despite STRICT mode. |
| 3399 | EXPECT_EQ(0, tls.queries()); |
| 3400 | EXPECT_EQ(1U, GetNumQueries(dns, dns64_name)); |
| 3401 | } |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3402 | |
| 3403 | namespace { |
| 3404 | |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3405 | class ScopedSetNetworkForProcess { |
| 3406 | public: |
| 3407 | explicit ScopedSetNetworkForProcess(unsigned netId) { |
| 3408 | mStoredNetId = getNetworkForProcess(); |
| 3409 | if (netId == mStoredNetId) return; |
| 3410 | EXPECT_EQ(0, setNetworkForProcess(netId)); |
| 3411 | } |
| 3412 | ~ScopedSetNetworkForProcess() { EXPECT_EQ(0, setNetworkForProcess(mStoredNetId)); } |
| 3413 | |
| 3414 | private: |
| 3415 | unsigned mStoredNetId; |
| 3416 | }; |
| 3417 | |
| 3418 | class ScopedSetNetworkForResolv { |
| 3419 | public: |
| 3420 | explicit ScopedSetNetworkForResolv(unsigned netId) { EXPECT_EQ(0, setNetworkForResolv(netId)); } |
| 3421 | ~ScopedSetNetworkForResolv() { EXPECT_EQ(0, setNetworkForResolv(NETID_UNSET)); } |
| 3422 | }; |
| 3423 | |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3424 | void sendCommand(int fd, const std::string& cmd) { |
| 3425 | ssize_t rc = TEMP_FAILURE_RETRY(write(fd, cmd.c_str(), cmd.size() + 1)); |
| 3426 | EXPECT_EQ(rc, static_cast<ssize_t>(cmd.size() + 1)); |
| 3427 | } |
| 3428 | |
| 3429 | int32_t readBE32(int fd) { |
| 3430 | int32_t tmp; |
| 3431 | int n = TEMP_FAILURE_RETRY(read(fd, &tmp, sizeof(tmp))); |
| 3432 | EXPECT_TRUE(n > 0); |
| 3433 | return ntohl(tmp); |
| 3434 | } |
| 3435 | |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3436 | int readResponseCode(int fd) { |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3437 | char buf[4]; |
| 3438 | int n = TEMP_FAILURE_RETRY(read(fd, &buf, sizeof(buf))); |
| 3439 | EXPECT_TRUE(n > 0); |
| 3440 | // The format of response code is that 4 bytes for the code & null. |
| 3441 | buf[3] = '\0'; |
| 3442 | int result; |
| 3443 | EXPECT_TRUE(ParseInt(buf, &result)); |
| 3444 | return result; |
| 3445 | } |
| 3446 | |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3447 | bool checkAndClearUseLocalNameserversFlag(unsigned* netid) { |
| 3448 | if (netid == nullptr || ((*netid) & NETID_USE_LOCAL_NAMESERVERS) == 0) { |
| 3449 | return false; |
| 3450 | } |
| 3451 | *netid = (*netid) & ~NETID_USE_LOCAL_NAMESERVERS; |
| 3452 | return true; |
| 3453 | } |
| 3454 | |
| 3455 | android::net::UidRangeParcel makeUidRangeParcel(int start, int stop) { |
| 3456 | android::net::UidRangeParcel res; |
| 3457 | res.start = start; |
| 3458 | res.stop = stop; |
| 3459 | |
| 3460 | return res; |
| 3461 | } |
| 3462 | |
| 3463 | void expectNetIdWithLocalNameserversFlag(unsigned netId) { |
| 3464 | unsigned dnsNetId = 0; |
| 3465 | EXPECT_EQ(0, getNetworkForDns(&dnsNetId)); |
| 3466 | EXPECT_TRUE(checkAndClearUseLocalNameserversFlag(&dnsNetId)); |
| 3467 | EXPECT_EQ(netId, static_cast<unsigned>(dnsNetId)); |
| 3468 | } |
| 3469 | |
| 3470 | void expectDnsNetIdEquals(unsigned netId) { |
| 3471 | unsigned dnsNetId = 0; |
| 3472 | EXPECT_EQ(0, getNetworkForDns(&dnsNetId)); |
| 3473 | EXPECT_EQ(netId, static_cast<unsigned>(dnsNetId)); |
| 3474 | } |
| 3475 | |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3476 | void expectDnsNetIdIsDefaultNetwork(INetd* netdService) { |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3477 | int currentNetid; |
| 3478 | EXPECT_TRUE(netdService->networkGetDefault(¤tNetid).isOk()); |
| 3479 | expectDnsNetIdEquals(currentNetid); |
| 3480 | } |
| 3481 | |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3482 | void expectDnsNetIdWithVpn(INetd* netdService, unsigned vpnNetId, unsigned expectedNetId) { |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3483 | EXPECT_TRUE(netdService->networkCreateVpn(vpnNetId, false /* secure */).isOk()); |
| 3484 | uid_t uid = getuid(); |
| 3485 | // Add uid to VPN |
| 3486 | EXPECT_TRUE(netdService->networkAddUidRanges(vpnNetId, {makeUidRangeParcel(uid, uid)}).isOk()); |
| 3487 | expectDnsNetIdEquals(expectedNetId); |
| 3488 | EXPECT_TRUE(netdService->networkDestroy(vpnNetId).isOk()); |
| 3489 | } |
| 3490 | |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3491 | } // namespace |
| 3492 | |
| 3493 | TEST_F(ResolverTest, getDnsNetId) { |
| 3494 | // We've called setNetworkForProcess in SetupOemNetwork, so reset to default first. |
| 3495 | setNetworkForProcess(NETID_UNSET); |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3496 | |
| 3497 | expectDnsNetIdIsDefaultNetwork(mDnsClient.netdService()); |
| 3498 | expectDnsNetIdWithVpn(mDnsClient.netdService(), TEST_VPN_NETID, TEST_VPN_NETID); |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3499 | |
| 3500 | // Test with setNetworkForProcess |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3501 | { |
| 3502 | ScopedSetNetworkForProcess scopedSetNetworkForProcess(TEST_NETID); |
| 3503 | expectDnsNetIdEquals(TEST_NETID); |
| 3504 | } |
| 3505 | |
| 3506 | // Test with setNetworkForProcess with NETID_USE_LOCAL_NAMESERVERS |
| 3507 | { |
| 3508 | ScopedSetNetworkForProcess scopedSetNetworkForProcess(TEST_NETID | |
| 3509 | NETID_USE_LOCAL_NAMESERVERS); |
| 3510 | expectNetIdWithLocalNameserversFlag(TEST_NETID); |
| 3511 | } |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3512 | |
| 3513 | // Test with setNetworkForResolv |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3514 | { |
| 3515 | ScopedSetNetworkForResolv scopedSetNetworkForResolv(TEST_NETID); |
| 3516 | expectDnsNetIdEquals(TEST_NETID); |
| 3517 | } |
| 3518 | |
| 3519 | // Test with setNetworkForResolv with NETID_USE_LOCAL_NAMESERVERS |
| 3520 | { |
| 3521 | ScopedSetNetworkForResolv scopedSetNetworkForResolv(TEST_NETID | |
| 3522 | NETID_USE_LOCAL_NAMESERVERS); |
| 3523 | expectNetIdWithLocalNameserversFlag(TEST_NETID); |
| 3524 | } |
| 3525 | |
| 3526 | // Test with setNetworkForResolv under bypassable vpn |
| 3527 | { |
| 3528 | ScopedSetNetworkForResolv scopedSetNetworkForResolv(TEST_NETID); |
| 3529 | expectDnsNetIdWithVpn(mDnsClient.netdService(), TEST_VPN_NETID, TEST_NETID); |
| 3530 | } |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3531 | |
| 3532 | // Create socket connected to DnsProxyListener |
| 3533 | int fd = dns_open_proxy(); |
| 3534 | EXPECT_TRUE(fd > 0); |
| 3535 | unique_fd ufd(fd); |
| 3536 | |
| 3537 | // Test command with wrong netId |
| 3538 | sendCommand(fd, "getdnsnetid abc"); |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3539 | EXPECT_EQ(ResponseCode::DnsProxyQueryResult, readResponseCode(fd)); |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3540 | EXPECT_EQ(-EINVAL, readBE32(fd)); |
| 3541 | |
| 3542 | // Test unsupported command |
| 3543 | sendCommand(fd, "getdnsnetidNotSupported"); |
| 3544 | // Keep in sync with FrameworkListener.cpp (500, "Command not recognized") |
Luke Huang | 0d592bc | 2019-05-25 18:24:03 +0800 | [diff] [blame] | 3545 | EXPECT_EQ(500, readResponseCode(fd)); |
Luke Huang | 9807e6b | 2019-05-20 16:17:12 +0800 | [diff] [blame] | 3546 | } |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3547 | |
| 3548 | TEST_F(ResolverTest, BlockDnsQueryWithUidRule) { |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3549 | // This test relies on blocking traffic on loopback, which xt_qtaguid does not do. |
| 3550 | // See aosp/358413 and b/34444781 for why. |
| 3551 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 3552 | |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3553 | constexpr char listen_addr1[] = "127.0.0.4"; |
| 3554 | constexpr char listen_addr2[] = "::1"; |
| 3555 | constexpr char host_name[] = "howdy.example.com."; |
| 3556 | const std::vector<DnsRecord> records = { |
| 3557 | {host_name, ns_type::ns_t_a, "1.2.3.4"}, |
| 3558 | {host_name, ns_type::ns_t_aaaa, "::1.2.3.4"}, |
| 3559 | }; |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3560 | INetd* netdService = mDnsClient.netdService(); |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3561 | |
| 3562 | test::DNSResponder dns1(listen_addr1); |
| 3563 | test::DNSResponder dns2(listen_addr2); |
| 3564 | StartDns(dns1, records); |
| 3565 | StartDns(dns2, records); |
| 3566 | |
| 3567 | std::vector<std::string> servers = {listen_addr1, listen_addr2}; |
| 3568 | ASSERT_TRUE(mDnsClient.SetResolversForNetwork(servers)); |
| 3569 | dns1.clearQueries(); |
| 3570 | dns2.clearQueries(); |
| 3571 | |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3572 | // Add drop rule for TEST_UID. Also enable the standby chain because it might not be enabled. |
| 3573 | // Unfortunately we cannot use FIREWALL_CHAIN_NONE, or custom iptables rules, for this purpose |
| 3574 | // because netd calls fchown() on the DNS query sockets, and "iptables -m owner" matches the |
| 3575 | // UID of the socket creator, not the UID set by fchown(). |
| 3576 | // |
| 3577 | // TODO: migrate FIREWALL_CHAIN_NONE to eBPF as well. |
| 3578 | EXPECT_TRUE(netdService->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, true).isOk()); |
| 3579 | EXPECT_TRUE(netdService |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3580 | ->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, TEST_UID, |
| 3581 | INetd::FIREWALL_RULE_DENY) |
| 3582 | .isOk()); |
| 3583 | |
| 3584 | // Save uid |
| 3585 | int suid = getuid(); |
| 3586 | |
| 3587 | // Switch to TEST_UID |
| 3588 | EXPECT_TRUE(seteuid(TEST_UID) == 0); |
| 3589 | |
| 3590 | // Dns Query |
| 3591 | int fd1 = resNetworkQuery(TEST_NETID, host_name, ns_c_in, ns_t_a, 0); |
| 3592 | int fd2 = resNetworkQuery(TEST_NETID, host_name, ns_c_in, ns_t_aaaa, 0); |
| 3593 | EXPECT_TRUE(fd1 != -1); |
| 3594 | EXPECT_TRUE(fd2 != -1); |
| 3595 | |
| 3596 | uint8_t buf[MAXPACKET] = {}; |
| 3597 | int rcode; |
| 3598 | int res = getAsyncResponse(fd2, &rcode, buf, MAXPACKET); |
| 3599 | EXPECT_EQ(-ECONNREFUSED, res); |
| 3600 | |
| 3601 | memset(buf, 0, MAXPACKET); |
| 3602 | res = getAsyncResponse(fd1, &rcode, buf, MAXPACKET); |
| 3603 | EXPECT_EQ(-ECONNREFUSED, res); |
| 3604 | |
| 3605 | // Restore uid |
| 3606 | EXPECT_TRUE(seteuid(suid) == 0); |
| 3607 | |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3608 | // Remove drop rule for TEST_UID, and disable the standby chain. |
| 3609 | EXPECT_TRUE(netdService |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3610 | ->firewallSetUidRule(INetd::FIREWALL_CHAIN_STANDBY, TEST_UID, |
| 3611 | INetd::FIREWALL_RULE_ALLOW) |
| 3612 | .isOk()); |
Lorenzo Colitti | eb9aeec | 2019-06-26 19:53:55 +0900 | [diff] [blame] | 3613 | EXPECT_TRUE(netdService->firewallEnableChildChain(INetd::FIREWALL_CHAIN_STANDBY, false).isOk()); |
Sehee Park | 2c11878 | 2019-05-07 13:02:45 +0900 | [diff] [blame] | 3614 | } |