Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 17 | #ifdef NDEBUG |
| 18 | #undef NDEBUG |
| 19 | #endif |
| 20 | |
Bernie Innocenti | bb74c58 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 21 | #include <netdb.h> |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 22 | |
Bernie Innocenti | bb74c58 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <vector> |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 25 | |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 26 | #include <android-base/strings.h> |
| 27 | #include <android/net/IDnsResolver.h> |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 29 | #include <binder/IServiceManager.h> |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 30 | #include <gmock/gmock-matchers.h> |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 31 | #include <gtest/gtest.h> |
Mike Yu | 40ea2ad | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 32 | #include <netdutils/Stopwatch.h> |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 33 | |
| 34 | #include "tests/dns_metrics_listener/base_metrics_listener.h" |
| 35 | #include "tests/dns_metrics_listener/test_metrics.h" |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 36 | |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 37 | #include "ResolverStats.h" |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 38 | #include "dns_responder.h" |
| 39 | #include "dns_responder_client.h" |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 40 | |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 41 | namespace binder = android::binder; |
| 42 | |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 43 | using android::IBinder; |
| 44 | using android::IServiceManager; |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 45 | using android::ProcessState; |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 46 | using android::sp; |
| 47 | using android::String16; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 48 | using android::String8; |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 49 | using android::net::IDnsResolver; |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 50 | using android::net::ResolverParamsParcel; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 51 | using android::net::ResolverStats; |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 52 | using android::net::metrics::INetdEventListener; |
| 53 | using android::net::metrics::TestOnDnsEvent; |
Mike Yu | 40ea2ad | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 54 | using android::netdutils::Stopwatch; |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 55 | |
| 56 | // TODO: make this dynamic and stop depending on implementation details. |
waynema | e930259 | 2019-08-15 14:50:58 +0800 | [diff] [blame] | 57 | // Sync from TEST_NETID in dns_responder_client.cpp as resolv_integration_test.cpp does. |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 58 | constexpr int TEST_NETID = 30; |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 59 | |
| 60 | class DnsResolverBinderTest : public ::testing::Test { |
| 61 | public: |
| 62 | DnsResolverBinderTest() { |
| 63 | sp<IServiceManager> sm = android::defaultServiceManager(); |
| 64 | sp<IBinder> binder = sm->getService(String16("dnsresolver")); |
| 65 | if (binder != nullptr) { |
| 66 | mDnsResolver = android::interface_cast<IDnsResolver>(binder); |
| 67 | } |
Bernie Innocenti | 45d9baa | 2019-06-19 16:28:05 +0900 | [diff] [blame] | 68 | // This could happen when the test isn't running as root, or if netd isn't running. |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 69 | assert(nullptr != mDnsResolver.get()); |
| 70 | // Create cache for test |
| 71 | mDnsResolver->createNetworkCache(TEST_NETID); |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 74 | ~DnsResolverBinderTest() { |
| 75 | // Destroy cache for test |
| 76 | mDnsResolver->destroyNetworkCache(TEST_NETID); |
| 77 | } |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 78 | |
| 79 | protected: |
| 80 | sp<IDnsResolver> mDnsResolver; |
| 81 | }; |
| 82 | |
| 83 | class TimedOperation : public Stopwatch { |
| 84 | public: |
| 85 | explicit TimedOperation(const std::string& name) : mName(name) {} |
Bernie Innocenti | bb74c58 | 2019-05-20 16:34:16 +0900 | [diff] [blame] | 86 | virtual ~TimedOperation() { |
| 87 | std::cerr << " " << mName << ": " << timeTakenUs() << "us" << std::endl; |
| 88 | } |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | std::string mName; |
| 92 | }; |
| 93 | |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 94 | namespace { |
| 95 | |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 96 | // TODO: Convert tests to ResolverParamsParcel and delete this stub. |
| 97 | ResolverParamsParcel makeResolverParamsParcel(int netId, const std::vector<int>& params, |
| 98 | const std::vector<std::string>& servers, |
| 99 | const std::vector<std::string>& domains, |
| 100 | const std::string& tlsHostname, |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 101 | const std::vector<std::string>& tlsServers) { |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 102 | using android::net::IDnsResolver; |
| 103 | ResolverParamsParcel paramsParcel; |
| 104 | |
| 105 | paramsParcel.netId = netId; |
| 106 | paramsParcel.sampleValiditySeconds = params[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY]; |
| 107 | paramsParcel.successThreshold = params[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD]; |
| 108 | paramsParcel.minSamples = params[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES]; |
| 109 | paramsParcel.maxSamples = params[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES]; |
| 110 | if (params.size() > IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC) { |
| 111 | paramsParcel.baseTimeoutMsec = params[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC]; |
| 112 | } else { |
| 113 | paramsParcel.baseTimeoutMsec = 0; |
| 114 | } |
| 115 | if (params.size() > IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT) { |
| 116 | paramsParcel.retryCount = params[IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT]; |
| 117 | } else { |
| 118 | paramsParcel.retryCount = 0; |
| 119 | } |
| 120 | paramsParcel.servers = servers; |
| 121 | paramsParcel.domains = domains; |
| 122 | paramsParcel.tlsName = tlsHostname; |
| 123 | paramsParcel.tlsServers = tlsServers; |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 124 | paramsParcel.tlsFingerprints = {}; |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 125 | |
| 126 | return paramsParcel; |
| 127 | } |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 128 | |
| 129 | } // namespace |
| 130 | |
Luke Huang | 403c044 | 2019-02-25 15:33:42 +0800 | [diff] [blame] | 131 | TEST_F(DnsResolverBinderTest, IsAlive) { |
| 132 | TimedOperation t("isAlive RPC"); |
| 133 | bool isAlive = false; |
| 134 | mDnsResolver->isAlive(&isAlive); |
| 135 | ASSERT_TRUE(isAlive); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Hungming Chen | 01dcb41 | 2019-05-31 17:46:49 +0800 | [diff] [blame] | 138 | TEST_F(DnsResolverBinderTest, RegisterEventListener_NullListener) { |
| 139 | android::binder::Status status = mDnsResolver->registerEventListener( |
| 140 | android::interface_cast<INetdEventListener>(nullptr)); |
| 141 | ASSERT_FALSE(status.isOk()); |
| 142 | ASSERT_EQ(EINVAL, status.serviceSpecificErrorCode()); |
| 143 | } |
| 144 | |
| 145 | TEST_F(DnsResolverBinderTest, RegisterEventListener_DuplicateSubscription) { |
Hungming Chen | e8f970c | 2019-04-10 17:34:06 +0800 | [diff] [blame] | 146 | class DummyListener : public android::net::metrics::BaseMetricsListener {}; |
Hungming Chen | 01dcb41 | 2019-05-31 17:46:49 +0800 | [diff] [blame] | 147 | |
| 148 | // Expect to subscribe successfully. |
| 149 | android::sp<DummyListener> dummyListener = new DummyListener(); |
| 150 | android::binder::Status status = mDnsResolver->registerEventListener( |
| 151 | android::interface_cast<INetdEventListener>(dummyListener)); |
| 152 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 153 | |
| 154 | // Expect to subscribe failed with registered listener instance. |
| 155 | status = mDnsResolver->registerEventListener( |
| 156 | android::interface_cast<INetdEventListener>(dummyListener)); |
| 157 | ASSERT_FALSE(status.isOk()); |
| 158 | ASSERT_EQ(EEXIST, status.serviceSpecificErrorCode()); |
| 159 | } |
| 160 | |
waynema | e930259 | 2019-08-15 14:50:58 +0800 | [diff] [blame] | 161 | // TODO: Move this test to resolv_integration_test.cpp |
Hungming Chen | 01dcb41 | 2019-05-31 17:46:49 +0800 | [diff] [blame] | 162 | TEST_F(DnsResolverBinderTest, RegisterEventListener_onDnsEvent) { |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 163 | // The test configs are used to trigger expected events. The expected results are defined in |
| 164 | // expectedResults. |
| 165 | static const struct TestConfig { |
| 166 | std::string hostname; |
| 167 | int returnCode; |
| 168 | } testConfigs[] = { |
| 169 | {"hi", 0 /*success*/}, |
| 170 | {"nonexistent", EAI_NODATA}, |
| 171 | }; |
| 172 | |
| 173 | // The expected results define expected event content for test verification. |
| 174 | static const std::vector<TestOnDnsEvent::TestResult> expectedResults = { |
| 175 | {TEST_NETID, INetdEventListener::EVENT_GETADDRINFO, 0 /*success*/, 1, "hi", "1.2.3.4"}, |
| 176 | {TEST_NETID, INetdEventListener::EVENT_GETADDRINFO, EAI_NODATA, 0, "nonexistent", ""}, |
| 177 | }; |
| 178 | |
| 179 | // Start the Binder thread pool. |
| 180 | // TODO: Consider doing this once if there has another event listener unit test. |
| 181 | android::ProcessState::self()->startThreadPool(); |
| 182 | |
| 183 | // Setup network. |
| 184 | // TODO: Setup device configuration and DNS responser server as resolver test does. |
| 185 | // Currently, leave DNS related configuration in this test because only it needs DNS |
| 186 | // client-server testing environment. |
| 187 | DnsResponderClient dnsClient; |
| 188 | dnsClient.SetUp(); |
| 189 | |
| 190 | // Setup DNS responder server. |
| 191 | constexpr char listen_addr[] = "127.0.0.3"; |
| 192 | constexpr char listen_srv[] = "53"; |
Mike Yu | fc125e4 | 2019-05-15 20:41:28 +0800 | [diff] [blame] | 193 | test::DNSResponder dns(listen_addr, listen_srv, ns_rcode::ns_r_servfail); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 194 | dns.addMapping("hi.example.com.", ns_type::ns_t_a, "1.2.3.4"); |
| 195 | ASSERT_TRUE(dns.startServer()); |
| 196 | |
| 197 | // Setup DNS configuration. |
| 198 | const std::vector<std::string> test_servers = {listen_addr}; |
| 199 | std::vector<std::string> test_domains = {"example.com"}; |
| 200 | std::vector<int> test_params = {300 /*sample_validity*/, 25 /*success_threshold*/, |
| 201 | 8 /*min_samples*/, 8 /*max_samples*/}; |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 202 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 203 | ASSERT_TRUE(dnsClient.SetResolversForNetwork(test_servers, test_domains, test_params)); |
| 204 | dns.clearQueries(); |
| 205 | |
| 206 | // Register event listener. |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 207 | android::sp<TestOnDnsEvent> testOnDnsEvent = new TestOnDnsEvent(expectedResults); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 208 | android::binder::Status status = mDnsResolver->registerEventListener( |
| 209 | android::interface_cast<INetdEventListener>(testOnDnsEvent)); |
| 210 | ASSERT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 211 | |
| 212 | // DNS queries. |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 213 | // Once all expected events of expectedResults are received by the listener, the unit test will |
| 214 | // be notified. Otherwise, notified with a timeout expired failure. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 215 | auto& cv = testOnDnsEvent->getCv(); |
| 216 | auto& cvMutex = testOnDnsEvent->getCvMutex(); |
| 217 | { |
| 218 | std::unique_lock lock(cvMutex); |
| 219 | |
| 220 | for (const auto& config : testConfigs) { |
| 221 | SCOPED_TRACE(config.hostname); |
| 222 | |
| 223 | addrinfo* result = nullptr; |
| 224 | addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; |
| 225 | int status = getaddrinfo(config.hostname.c_str(), nullptr, &hints, &result); |
| 226 | EXPECT_EQ(config.returnCode, status); |
| 227 | |
| 228 | if (result) freeaddrinfo(result); |
| 229 | } |
| 230 | |
| 231 | // Wait for receiving expected events. |
| 232 | EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2))); |
| 233 | } |
| 234 | |
| 235 | // Verify that all testcases are passed. |
| 236 | EXPECT_TRUE(testOnDnsEvent->isVerified()); |
| 237 | |
| 238 | dnsClient.TearDown(); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 239 | } |
| 240 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 241 | // TODO: Need to test more than one server cases. |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 242 | TEST_F(DnsResolverBinderTest, SetResolverConfiguration_Tls) { |
| 243 | const std::vector<std::string> LOCALLY_ASSIGNED_DNS{"8.8.8.8", "2001:4860:4860::8888"}; |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 244 | static const std::vector<std::string> valid_v4_addr = {"192.0.2.1"}; |
| 245 | static const std::vector<std::string> valid_v6_addr = {"2001:db8::2"}; |
| 246 | static const std::vector<std::string> invalid_v4_addr = {"192.0.*.5"}; |
| 247 | static const std::vector<std::string> invalid_v6_addr = {"2001:dg8::6"}; |
| 248 | constexpr char valid_tls_name[] = "example.com"; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 249 | std::vector<int> test_params = {300, 25, 8, 8}; |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 250 | // We enumerate valid and invalid v4/v6 address, and several different TLS names |
| 251 | // to be the input data and verify the binder status. |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 252 | static const struct TestData { |
| 253 | const std::vector<std::string> servers; |
| 254 | const std::string tlsName; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 255 | const int expectedReturnCode; |
| 256 | } kTlsTestData[] = { |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 257 | {valid_v4_addr, valid_tls_name, 0}, |
| 258 | {valid_v4_addr, "host.com", 0}, |
| 259 | {valid_v4_addr, "@@@@", 0}, |
| 260 | {valid_v4_addr, "", 0}, |
| 261 | {valid_v6_addr, valid_tls_name, 0}, |
| 262 | {valid_v6_addr, "host.com", 0}, |
| 263 | {valid_v6_addr, "@@@@", 0}, |
| 264 | {valid_v6_addr, "", 0}, |
| 265 | {invalid_v4_addr, valid_tls_name, EINVAL}, |
| 266 | {invalid_v4_addr, "host.com", EINVAL}, |
| 267 | {invalid_v4_addr, "@@@@", EINVAL}, |
| 268 | {invalid_v4_addr, "", EINVAL}, |
| 269 | {invalid_v6_addr, valid_tls_name, EINVAL}, |
| 270 | {invalid_v6_addr, "host.com", EINVAL}, |
| 271 | {invalid_v6_addr, "@@@@", EINVAL}, |
| 272 | {invalid_v6_addr, "", EINVAL}, |
| 273 | {{}, "", 0}, |
| 274 | {{""}, "", EINVAL}, |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | for (size_t i = 0; i < std::size(kTlsTestData); i++) { |
| 278 | const auto& td = kTlsTestData[i]; |
| 279 | |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 280 | const auto resolverParams = makeResolverParamsParcel( |
| 281 | TEST_NETID, test_params, LOCALLY_ASSIGNED_DNS, {}, td.tlsName, td.servers); |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 282 | binder::Status status = mDnsResolver->setResolverConfiguration(resolverParams); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 283 | |
| 284 | if (td.expectedReturnCode == 0) { |
| 285 | SCOPED_TRACE(String8::format("test case %zu should have passed", i)); |
| 286 | SCOPED_TRACE(status.toString8()); |
| 287 | EXPECT_EQ(0, status.exceptionCode()); |
| 288 | } else { |
| 289 | SCOPED_TRACE(String8::format("test case %zu should have failed", i)); |
| 290 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 291 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 292 | } |
| 293 | } |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST_F(DnsResolverBinderTest, GetResolverInfo) { |
| 297 | std::vector<std::string> servers = {"127.0.0.1", "127.0.0.2"}; |
| 298 | std::vector<std::string> domains = {"example.com"}; |
| 299 | std::vector<int> testParams = { |
| 300 | 300, // sample validity in seconds |
| 301 | 25, // success threshod in percent |
| 302 | 8, 8, // {MIN,MAX}_SAMPLES |
| 303 | 100, // BASE_TIMEOUT_MSEC |
Mike Yu | f7da78c | 2019-04-09 15:40:26 +0800 | [diff] [blame] | 304 | 3, // retry count |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 305 | }; |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 306 | const auto resolverParams = |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 307 | makeResolverParamsParcel(TEST_NETID, testParams, servers, domains, "", {}); |
waynema | 3725518 | 2019-03-18 13:22:56 +0800 | [diff] [blame] | 308 | binder::Status status = mDnsResolver->setResolverConfiguration(resolverParams); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 309 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 310 | |
| 311 | std::vector<std::string> res_servers; |
| 312 | std::vector<std::string> res_domains; |
| 313 | std::vector<std::string> res_tls_servers; |
| 314 | std::vector<int32_t> params32; |
| 315 | std::vector<int32_t> stats32; |
| 316 | std::vector<int32_t> wait_for_pending_req_timeout_count32{0}; |
| 317 | status = mDnsResolver->getResolverInfo(TEST_NETID, &res_servers, &res_domains, &res_tls_servers, |
| 318 | ¶ms32, &stats32, |
| 319 | &wait_for_pending_req_timeout_count32); |
| 320 | |
| 321 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 322 | EXPECT_EQ(servers.size(), res_servers.size()); |
| 323 | EXPECT_EQ(domains.size(), res_domains.size()); |
| 324 | EXPECT_EQ(0U, res_tls_servers.size()); |
| 325 | ASSERT_EQ(static_cast<size_t>(IDnsResolver::RESOLVER_PARAMS_COUNT), testParams.size()); |
| 326 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY], |
| 327 | params32[IDnsResolver::RESOLVER_PARAMS_SAMPLE_VALIDITY]); |
| 328 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD], |
| 329 | params32[IDnsResolver::RESOLVER_PARAMS_SUCCESS_THRESHOLD]); |
| 330 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES], |
| 331 | params32[IDnsResolver::RESOLVER_PARAMS_MIN_SAMPLES]); |
| 332 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES], |
| 333 | params32[IDnsResolver::RESOLVER_PARAMS_MAX_SAMPLES]); |
| 334 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC], |
| 335 | params32[IDnsResolver::RESOLVER_PARAMS_BASE_TIMEOUT_MSEC]); |
Mike Yu | f7da78c | 2019-04-09 15:40:26 +0800 | [diff] [blame] | 336 | EXPECT_EQ(testParams[IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT], |
| 337 | params32[IDnsResolver::RESOLVER_PARAMS_RETRY_COUNT]); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 338 | |
| 339 | std::vector<ResolverStats> stats; |
| 340 | ResolverStats::decodeAll(stats32, &stats); |
| 341 | |
| 342 | EXPECT_EQ(servers.size(), stats.size()); |
| 343 | |
| 344 | EXPECT_THAT(res_servers, testing::UnorderedElementsAreArray(servers)); |
| 345 | EXPECT_THAT(res_domains, testing::UnorderedElementsAreArray(domains)); |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 346 | } |
Mike Yu | 72e359d | 2019-04-09 15:12:48 +0800 | [diff] [blame] | 347 | |
Luke Huang | 9b80e6c | 2019-04-09 17:54:09 +0800 | [diff] [blame] | 348 | TEST_F(DnsResolverBinderTest, CreateDestroyNetworkCache) { |
| 349 | // Must not be the same as TEST_NETID |
| 350 | const int ANOTHER_TEST_NETID = TEST_NETID + 1; |
| 351 | |
| 352 | // Create a new network cache. |
| 353 | EXPECT_TRUE(mDnsResolver->createNetworkCache(ANOTHER_TEST_NETID).isOk()); |
| 354 | |
| 355 | // create it again, expect a EEXIST. |
| 356 | EXPECT_EQ(EEXIST, |
| 357 | mDnsResolver->createNetworkCache(ANOTHER_TEST_NETID).serviceSpecificErrorCode()); |
| 358 | |
| 359 | // destroy it. |
| 360 | EXPECT_TRUE(mDnsResolver->destroyNetworkCache(ANOTHER_TEST_NETID).isOk()); |
| 361 | |
| 362 | // re-create it |
| 363 | EXPECT_TRUE(mDnsResolver->createNetworkCache(ANOTHER_TEST_NETID).isOk()); |
| 364 | |
| 365 | // destroy it. |
| 366 | EXPECT_TRUE(mDnsResolver->destroyNetworkCache(ANOTHER_TEST_NETID).isOk()); |
| 367 | |
| 368 | // re-destroy it |
| 369 | EXPECT_TRUE(mDnsResolver->destroyNetworkCache(ANOTHER_TEST_NETID).isOk()); |
Mike Yu | 40ea2ad | 2019-03-13 17:15:48 +0800 | [diff] [blame] | 370 | } |
Ken Chen | d99022c | 2019-02-20 21:34:14 +0800 | [diff] [blame] | 371 | |
| 372 | TEST_F(DnsResolverBinderTest, setLogSeverity) { |
| 373 | // Expect fail |
| 374 | EXPECT_EQ(EINVAL, mDnsResolver->setLogSeverity(-1).serviceSpecificErrorCode()); |
| 375 | |
| 376 | // Test set different log level |
| 377 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_VERBOSE).isOk()); |
| 378 | |
| 379 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_DEBUG).isOk()); |
| 380 | |
| 381 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_INFO).isOk()); |
| 382 | |
| 383 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_WARNING).isOk()); |
| 384 | |
| 385 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_ERROR).isOk()); |
| 386 | |
| 387 | // Set back to default |
| 388 | EXPECT_TRUE(mDnsResolver->setLogSeverity(IDnsResolver::DNS_RESOLVER_LOG_WARNING).isOk()); |
| 389 | } |