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