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