blob: b1cfeed4892c3cbefd188593704451258fe22193 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/host_resolver_manager.h"
#include <algorithm>
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind_test_util.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "base/timer/mock_timer.h"
#include "base/values.h"
#include "build/build_config.h"
#include "net/base/address_list.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/mock_network_change_notifier.h"
#include "net/base/net_errors.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_config.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/dns_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/dns/mock_mdns_client.h"
#include "net/dns/mock_mdns_socket_factory.h"
#include "net/dns/test_dns_config_service.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_source_type.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log.h"
#include "net/log/test_net_log_util.h"
#include "net/socket/socket_test_util.h"
#include "net/test/gtest_util.h"
#include "net/test/test_with_task_environment.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(ENABLE_MDNS)
#include "net/dns/mdns_client_impl.h"
#endif // BUILDFLAG(ENABLE_MDNS)
using net::test::IsError;
using net::test::IsOk;
using ::testing::_;
using ::testing::Between;
using ::testing::ByMove;
using ::testing::NotNull;
using ::testing::Return;
namespace net {
namespace {
const size_t kMaxJobs = 10u;
const size_t kMaxRetryAttempts = 4u;
ProcTaskParams DefaultParams(HostResolverProc* resolver_proc) {
return ProcTaskParams(resolver_proc, kMaxRetryAttempts);
}
// A HostResolverProc that pushes each host mapped into a list and allows
// waiting for a specific number of requests. Unlike RuleBasedHostResolverProc
// it never calls SystemHostResolverCall. By default resolves all hostnames to
// "127.0.0.1". After AddRule(), it resolves only names explicitly specified.
class MockHostResolverProc : public HostResolverProc {
public:
struct ResolveKey {
ResolveKey(const std::string& hostname,
AddressFamily address_family,
HostResolverFlags flags)
: hostname(hostname), address_family(address_family), flags(flags) {}
bool operator<(const ResolveKey& other) const {
return std::tie(address_family, hostname, flags) <
std::tie(other.address_family, other.hostname, other.flags);
}
std::string hostname;
AddressFamily address_family;
HostResolverFlags flags;
};
typedef std::vector<ResolveKey> CaptureList;
MockHostResolverProc()
: HostResolverProc(nullptr),
num_requests_waiting_(0),
num_slots_available_(0),
requests_waiting_(&lock_),
slots_available_(&lock_) {}
// Waits until |count| calls to |Resolve| are blocked. Returns false when
// timed out.
bool WaitFor(unsigned count) {
base::AutoLock lock(lock_);
base::Time start_time = base::Time::Now();
while (num_requests_waiting_ < count) {
requests_waiting_.TimedWait(TestTimeouts::action_timeout());
if (base::Time::Now() > start_time + TestTimeouts::action_timeout())
return false;
}
return true;
}
// Signals |count| waiting calls to |Resolve|. First come first served.
void SignalMultiple(unsigned count) {
base::AutoLock lock(lock_);
num_slots_available_ += count;
slots_available_.Broadcast();
}
// Signals all waiting calls to |Resolve|. Beware of races.
void SignalAll() {
base::AutoLock lock(lock_);
num_slots_available_ = num_requests_waiting_;
slots_available_.Broadcast();
}
void AddRule(const std::string& hostname,
AddressFamily family,
const AddressList& result,
HostResolverFlags flags = 0) {
base::AutoLock lock(lock_);
rules_[ResolveKey(hostname, family, flags)] = result;
}
void AddRule(const std::string& hostname,
AddressFamily family,
const std::string& ip_list,
HostResolverFlags flags = 0,
const std::string& canonical_name = "") {
AddressList result;
int rv = ParseAddressList(ip_list, canonical_name, &result);
DCHECK_EQ(OK, rv);
AddRule(hostname, family, result, flags);
}
void AddRuleForAllFamilies(const std::string& hostname,
const std::string& ip_list,
HostResolverFlags flags = 0,
const std::string& canonical_name = "") {
AddressList result;
int rv = ParseAddressList(ip_list, canonical_name, &result);
DCHECK_EQ(OK, rv);
AddRule(hostname, ADDRESS_FAMILY_UNSPECIFIED, result, flags);
AddRule(hostname, ADDRESS_FAMILY_IPV4, result, flags);
AddRule(hostname, ADDRESS_FAMILY_IPV6, result, flags);
}
int Resolve(const std::string& hostname,
AddressFamily address_family,
HostResolverFlags host_resolver_flags,
AddressList* addrlist,
int* os_error) override {
base::AutoLock lock(lock_);
capture_list_.push_back(
ResolveKey(hostname, address_family, host_resolver_flags));
++num_requests_waiting_;
requests_waiting_.Broadcast();
{
base::ScopedAllowBaseSyncPrimitivesForTesting
scoped_allow_base_sync_primitives;
while (!num_slots_available_)
slots_available_.Wait();
}
DCHECK_GT(num_requests_waiting_, 0u);
--num_slots_available_;
--num_requests_waiting_;
if (rules_.empty()) {
int rv = ParseAddressList("127.0.0.1", std::string(), addrlist);
DCHECK_EQ(OK, rv);
return OK;
}
// Ignore HOST_RESOLVER_SYSTEM_ONLY, since it should have no impact on
// whether a rule matches. It should only affect cache lookups.
ResolveKey key(hostname, address_family,
host_resolver_flags & ~HOST_RESOLVER_SYSTEM_ONLY);
if (rules_.count(key) == 0)
return ERR_NAME_NOT_RESOLVED;
*addrlist = rules_[key];
return OK;
}
CaptureList GetCaptureList() const {
CaptureList copy;
{
base::AutoLock lock(lock_);
copy = capture_list_;
}
return copy;
}
bool HasBlockedRequests() const {
base::AutoLock lock(lock_);
return num_requests_waiting_ > num_slots_available_;
}
protected:
~MockHostResolverProc() override = default;
private:
mutable base::Lock lock_;
std::map<ResolveKey, AddressList> rules_;
CaptureList capture_list_;
unsigned num_requests_waiting_;
unsigned num_slots_available_;
base::ConditionVariable requests_waiting_;
base::ConditionVariable slots_available_;
DISALLOW_COPY_AND_ASSIGN(MockHostResolverProc);
};
class ResolveHostResponseHelper {
public:
using Callback =
base::OnceCallback<void(CompletionOnceCallback completion_callback,
int error)>;
ResolveHostResponseHelper() {}
explicit ResolveHostResponseHelper(
std::unique_ptr<HostResolverManager::CancellableRequest> request)
: request_(std::move(request)) {
result_error_ = request_->Start(base::BindOnce(
&ResolveHostResponseHelper::OnComplete, base::Unretained(this)));
}
ResolveHostResponseHelper(
std::unique_ptr<HostResolverManager::CancellableRequest> request,
Callback custom_callback)
: request_(std::move(request)) {
result_error_ = request_->Start(
base::BindOnce(std::move(custom_callback),
base::BindOnce(&ResolveHostResponseHelper::OnComplete,
base::Unretained(this))));
}
bool complete() const { return result_error_ != ERR_IO_PENDING; }
int result_error() {
WaitForCompletion();
return result_error_;
}
HostResolverManager::CancellableRequest* request() { return request_.get(); }
void CancelRequest() {
DCHECK(request_);
DCHECK(!complete());
request_ = nullptr;
}
void OnComplete(int error) {
DCHECK(!complete());
result_error_ = error;
run_loop_.Quit();
}
private:
void WaitForCompletion() {
DCHECK(request_);
if (complete()) {
return;
}
run_loop_.Run();
DCHECK(complete());
}
std::unique_ptr<HostResolverManager::CancellableRequest> request_;
int result_error_ = ERR_IO_PENDING;
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(ResolveHostResponseHelper);
};
// Using LookupAttemptHostResolverProc simulate very long lookups, and control
// which attempt resolves the host.
class LookupAttemptHostResolverProc : public HostResolverProc {
public:
LookupAttemptHostResolverProc(HostResolverProc* previous,
int attempt_number_to_resolve,
int total_attempts)
: HostResolverProc(previous),
attempt_number_to_resolve_(attempt_number_to_resolve),
current_attempt_number_(0),
total_attempts_(total_attempts),
total_attempts_resolved_(0),
resolved_attempt_number_(0),
num_attempts_waiting_(0),
all_done_(&lock_),
blocked_attempt_signal_(&lock_) {}
// Test harness will wait for all attempts to finish before checking the
// results.
void WaitForAllAttemptsToFinish() {
base::AutoLock auto_lock(lock_);
while (total_attempts_resolved_ != total_attempts_) {
all_done_.Wait();
}
}
void WaitForNAttemptsToBeBlocked(int n) {
base::AutoLock auto_lock(lock_);
while (num_attempts_waiting_ < n) {
blocked_attempt_signal_.Wait();
}
}
// All attempts will wait for an attempt to resolve the host.
void WaitForAnAttemptToComplete() {
{
base::AutoLock auto_lock(lock_);
base::ScopedAllowBaseSyncPrimitivesForTesting
scoped_allow_base_sync_primitives;
while (resolved_attempt_number_ == 0)
all_done_.Wait();
}
all_done_.Broadcast(); // Tell all waiting attempts to proceed.
}
// Returns the number of attempts that have finished the Resolve() method.
int GetTotalAttemptsResolved() {
base::AutoLock auto_lock(lock_);
return total_attempts_resolved_;
}
// Sets the resolved attempt number and unblocks waiting
// attempts.
void SetResolvedAttemptNumber(int n) {
base::AutoLock auto_lock(lock_);
EXPECT_EQ(0, resolved_attempt_number_);
resolved_attempt_number_ = n;
all_done_.Broadcast();
}
// HostResolverProc methods.
int Resolve(const std::string& host,
AddressFamily address_family,
HostResolverFlags host_resolver_flags,
AddressList* addrlist,
int* os_error) override {
bool wait_for_right_attempt_to_complete = true;
{
base::AutoLock auto_lock(lock_);
++current_attempt_number_;
++num_attempts_waiting_;
if (current_attempt_number_ == attempt_number_to_resolve_) {
resolved_attempt_number_ = current_attempt_number_;
wait_for_right_attempt_to_complete = false;
}
}
blocked_attempt_signal_.Broadcast();
if (wait_for_right_attempt_to_complete)
// Wait for the attempt_number_to_resolve_ attempt to resolve.
WaitForAnAttemptToComplete();
int result = ResolveUsingPrevious(host, address_family, host_resolver_flags,
addrlist, os_error);
{
base::AutoLock auto_lock(lock_);
++total_attempts_resolved_;
--num_attempts_waiting_;
}
all_done_.Broadcast(); // Tell all attempts to proceed.
// Since any negative number is considered a network error, with -1 having
// special meaning (ERR_IO_PENDING). We could return the attempt that has
// resolved the host as a negative number. For example, if attempt number 3
// resolves the host, then this method returns -4.
if (result == OK)
return -1 - resolved_attempt_number_;
else
return result;
}
protected:
~LookupAttemptHostResolverProc() override = default;
private:
int attempt_number_to_resolve_;
int current_attempt_number_; // Incremented whenever Resolve is called.
int total_attempts_;
int total_attempts_resolved_;
int resolved_attempt_number_;
int num_attempts_waiting_;
// All attempts wait for right attempt to be resolve.
base::Lock lock_;
base::ConditionVariable all_done_;
base::ConditionVariable blocked_attempt_signal_;
};
// TestHostResolverManager's sole purpose is to mock the IPv6 reachability test.
// By default, this pretends that IPv6 is globally reachable.
// This class is necessary so unit tests run the same on dual-stack machines as
// well as IPv4 only machines.
class TestHostResolverManager : public HostResolverManager {
public:
TestHostResolverManager(const HostResolver::ManagerOptions& options,
SystemDnsConfigChangeNotifier* notifier,
NetLog* net_log,
bool ipv6_reachable = true)
: HostResolverManager(options, notifier, net_log),
ipv6_reachable_(ipv6_reachable) {}
~TestHostResolverManager() override = default;
private:
const bool ipv6_reachable_;
bool IsGloballyReachable(const IPAddress& dest,
const NetLogWithSource& net_log) override {
return ipv6_reachable_;
}
};
bool HasAddress(const IPAddress& search_address, const AddressList& addresses) {
for (const auto& address : addresses) {
if (search_address == address.address())
return true;
}
return false;
}
void TestBothLoopbackIPs(const std::string& host) {
AddressList addresses;
EXPECT_TRUE(ResolveLocalHostname(host, &addresses));
EXPECT_EQ(2u, addresses.size());
EXPECT_TRUE(HasAddress(IPAddress::IPv4Localhost(), addresses));
EXPECT_TRUE(HasAddress(IPAddress::IPv6Localhost(), addresses));
}
void TestIPv6LoopbackOnly(const std::string& host) {
AddressList addresses;
EXPECT_TRUE(ResolveLocalHostname(host, &addresses));
EXPECT_EQ(1u, addresses.size());
EXPECT_TRUE(HasAddress(IPAddress::IPv6Localhost(), addresses));
}
} // namespace
class HostResolverManagerTest : public TestWithTaskEnvironment {
public:
static const int kDefaultPort = 80;
HostResolverManagerTest() : proc_(new MockHostResolverProc()) {}
void CreateResolver(bool check_ipv6_on_wifi = true) {
CreateResolverWithLimitsAndParams(kMaxJobs, DefaultParams(proc_.get()),
true /* ipv6_reachable */,
check_ipv6_on_wifi);
}
void DestroyResolver() {
if (!resolver_)
return;
if (host_cache_)
resolver_->RemoveHostCacheInvalidator(host_cache_->invalidator());
resolver_ = nullptr;
}
// This HostResolverManager will only allow 1 outstanding resolve at a time
// and perform no retries.
void CreateSerialResolver(bool check_ipv6_on_wifi = true) {
ProcTaskParams params = DefaultParams(proc_.get());
params.max_retry_attempts = 0u;
CreateResolverWithLimitsAndParams(1u, params, true /* ipv6_reachable */,
check_ipv6_on_wifi);
}
protected:
// testing::Test implementation:
void SetUp() override {
host_cache_ = HostCache::CreateDefaultCache();
CreateResolver();
request_context_ = std::make_unique<TestURLRequestContext>();
}
void TearDown() override {
if (resolver_) {
EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests());
if (host_cache_)
resolver_->RemoveHostCacheInvalidator(host_cache_->invalidator());
resolver_.reset();
}
EXPECT_FALSE(proc_->HasBlockedRequests());
}
void CreateResolverWithLimitsAndParams(size_t max_concurrent_resolves,
const ProcTaskParams& params,
bool ipv6_reachable,
bool check_ipv6_on_wifi) {
HostResolver::ManagerOptions options = DefaultOptions();
options.max_concurrent_resolves = max_concurrent_resolves;
options.check_ipv6_on_wifi = check_ipv6_on_wifi;
CreateResolverWithOptionsAndParams(std::move(options), params,
ipv6_reachable);
}
virtual HostResolver::ManagerOptions DefaultOptions() {
HostResolver::ManagerOptions options;
options.max_concurrent_resolves = kMaxJobs;
options.max_system_retry_attempts = kMaxRetryAttempts;
return options;
}
virtual void CreateResolverWithOptionsAndParams(
HostResolver::ManagerOptions options,
const ProcTaskParams& params,
bool ipv6_reachable) {
// Use HostResolverManagerDnsTest if enabling DNS client.
DCHECK(!options.insecure_dns_client_enabled);
DestroyResolver();
resolver_ = std::make_unique<TestHostResolverManager>(
options, nullptr /* notifier */, nullptr /* net_log */, ipv6_reachable);
resolver_->set_proc_params_for_test(params);
if (host_cache_)
resolver_->AddHostCacheInvalidator(host_cache_->invalidator());
}
// Friendship is not inherited, so use proxies to access those.
size_t num_running_dispatcher_jobs() const {
DCHECK(resolver_.get());
return resolver_->num_running_dispatcher_jobs_for_tests();
}
void set_allow_fallback_to_proctask(bool allow_fallback_to_proctask) {
DCHECK(resolver_.get());
resolver_->allow_fallback_to_proctask_ = allow_fallback_to_proctask;
}
static unsigned maximum_insecure_dns_task_failures() {
return DnsClient::kMaxInsecureFallbackFailures;
}
bool IsIPv6Reachable(const NetLogWithSource& net_log) {
return resolver_->IsIPv6Reachable(net_log);
}
void PopulateCache(const HostCache::Key& key, IPEndPoint endpoint) {
resolver_->CacheResult(host_cache_.get(), key,
HostCache::Entry(OK, AddressList(endpoint),
HostCache::Entry::SOURCE_UNKNOWN),
base::TimeDelta::FromSeconds(1));
}
const std::pair<const HostCache::Key, HostCache::Entry>* GetCacheHit(
const HostCache::Key& key) {
DCHECK(host_cache_);
return host_cache_->LookupStale(key, base::TimeTicks(), nullptr,
false /* ignore_secure */);
}
void MakeCacheStale() {
DCHECK(host_cache_);
host_cache_->Invalidate();
}
IPEndPoint CreateExpected(const std::string& ip_literal, uint16_t port) {
IPAddress ip;
bool result = ip.AssignFromIPLiteral(ip_literal);
DCHECK(result);
return IPEndPoint(ip, port);
}
scoped_refptr<MockHostResolverProc> proc_;
std::unique_ptr<HostResolverManager> resolver_;
std::unique_ptr<URLRequestContext> request_context_;
std::unique_ptr<HostCache> host_cache_;
};
TEST_F(HostResolverManagerTest, AsynchronousLookup) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.42", 80)));
EXPECT_FALSE(response.request()->GetStaleInfo());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
const std::pair<const HostCache::Key, HostCache::Entry>* cache_result =
GetCacheHit(HostCache::Key("just.testing", DnsQueryType::UNSPECIFIED,
0 /* host_resolver_flags */,
HostResolverSource::ANY));
EXPECT_TRUE(cache_result);
}
TEST_F(HostResolverManagerTest, JobsClearedOnCompletion) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_EQ(1u, resolver_->num_jobs_for_testing());
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_EQ(0u, resolver_->num_jobs_for_testing());
}
TEST_F(HostResolverManagerTest, JobsClearedOnCompletion_MultipleRequests) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response1(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper response2(resolver_->CreateRequest(
HostPortPair("just.testing", 85), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_EQ(1u, resolver_->num_jobs_for_testing());
EXPECT_THAT(response1.result_error(), IsOk());
EXPECT_THAT(response2.result_error(), IsOk());
EXPECT_EQ(0u, resolver_->num_jobs_for_testing());
}
TEST_F(HostResolverManagerTest, JobsClearedOnCompletion_Failure) {
proc_->AddRuleForAllFamilies(std::string(),
"0.0.0.1"); // Default to failures.
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_EQ(1u, resolver_->num_jobs_for_testing());
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_EQ(0u, resolver_->num_jobs_for_testing());
}
TEST_F(HostResolverManagerTest, JobsClearedOnCompletion_Abort) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_EQ(1u, resolver_->num_jobs_for_testing());
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
proc_->SignalMultiple(1u);
EXPECT_THAT(response.result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_EQ(0u, resolver_->num_jobs_for_testing());
}
TEST_F(HostResolverManagerTest, DnsQueryType) {
proc_->AddRule("host", ADDRESS_FAMILY_IPV4, "192.168.1.20");
proc_->AddRule("host", ADDRESS_FAMILY_IPV6, "::5");
HostResolver::ResolveHostParameters parameters;
parameters.dns_query_type = DnsQueryType::A;
ResolveHostResponseHelper v4_response(resolver_->CreateRequest(
HostPortPair("host", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
parameters.dns_query_type = DnsQueryType::AAAA;
ResolveHostResponseHelper v6_response(resolver_->CreateRequest(
HostPortPair("host", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
proc_->SignalMultiple(2u);
EXPECT_THAT(v4_response.result_error(), IsOk());
EXPECT_THAT(v4_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.20", 80)));
EXPECT_THAT(v6_response.result_error(), IsOk());
EXPECT_THAT(v6_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::5", 80)));
}
TEST_F(HostResolverManagerTest, LocalhostIPV4IPV6Lookup) {
HostResolver::ResolveHostParameters parameters;
parameters.dns_query_type = DnsQueryType::A;
ResolveHostResponseHelper v6_v4_response(resolver_->CreateRequest(
HostPortPair("localhost6", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_v4_response.result_error(), IsOk());
EXPECT_THAT(v6_v4_response.request()->GetAddressResults().value().endpoints(),
testing::IsEmpty());
parameters.dns_query_type = DnsQueryType::AAAA;
ResolveHostResponseHelper v6_v6_response(resolver_->CreateRequest(
HostPortPair("localhost6", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_v6_response.result_error(), IsOk());
EXPECT_THAT(v6_v6_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::1", 80)));
ResolveHostResponseHelper v6_unsp_response(resolver_->CreateRequest(
HostPortPair("localhost6", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_unsp_response.result_error(), IsOk());
EXPECT_THAT(
v6_unsp_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::1", 80)));
parameters.dns_query_type = DnsQueryType::A;
ResolveHostResponseHelper v4_v4_response(resolver_->CreateRequest(
HostPortPair("localhost", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_v4_response.result_error(), IsOk());
EXPECT_THAT(v4_v4_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("127.0.0.1", 80)));
parameters.dns_query_type = DnsQueryType::AAAA;
ResolveHostResponseHelper v4_v6_response(resolver_->CreateRequest(
HostPortPair("localhost", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_v6_response.result_error(), IsOk());
EXPECT_THAT(v4_v6_response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::1", 80)));
ResolveHostResponseHelper v4_unsp_response(resolver_->CreateRequest(
HostPortPair("localhost", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_unsp_response.result_error(), IsOk());
EXPECT_THAT(
v4_unsp_response.request()->GetAddressResults().value().endpoints(),
testing::UnorderedElementsAre(CreateExpected("127.0.0.1", 80),
CreateExpected("::1", 80)));
}
TEST_F(HostResolverManagerTest, ResolveIPLiteralWithHostResolverSystemOnly) {
const char kIpLiteral[] = "178.78.32.1";
// Add a mapping to tell if the resolver proc was called (if it was called,
// then the result will be the remapped value. Otherwise it will be the IP
// literal).
proc_->AddRuleForAllFamilies(kIpLiteral, "183.45.32.1");
HostResolver::ResolveHostParameters parameters;
parameters.source = HostResolverSource::SYSTEM;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair(kIpLiteral, 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
// IP literal resolution is expected to take precedence over source, so the
// result is expected to be the input IP, not the result IP from the proc rule
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected(kIpLiteral, 80)));
EXPECT_FALSE(response.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, EmptyListMeansNameNotResolved) {
proc_->AddRuleForAllFamilies("just.testing", "");
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
EXPECT_FALSE(response.request()->GetStaleInfo());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
}
TEST_F(HostResolverManagerTest, FailedAsynchronousLookup) {
proc_->AddRuleForAllFamilies(std::string(),
"0.0.0.1"); // Default to failures.
proc_->SignalMultiple(1u);
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
EXPECT_FALSE(response.request()->GetStaleInfo());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
// Also test that the error is not cached.
const std::pair<const HostCache::Key, HostCache::Entry>* cache_result =
GetCacheHit(HostCache::Key("just.testing", DnsQueryType::UNSPECIFIED,
0 /* host_resolver_flags */,
HostResolverSource::ANY));
EXPECT_FALSE(cache_result);
}
TEST_F(HostResolverManagerTest, AbortedAsynchronousLookup) {
ResolveHostResponseHelper response0(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ASSERT_FALSE(response0.complete());
ASSERT_TRUE(proc_->WaitFor(1u));
// Resolver is destroyed while job is running on WorkerPool.
DestroyResolver();
proc_->SignalAll();
// To ensure there was no spurious callback, complete with a new resolver.
CreateResolver();
ResolveHostResponseHelper response1(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
proc_->SignalMultiple(2u);
EXPECT_THAT(response1.result_error(), IsOk());
// This request was canceled.
EXPECT_FALSE(response0.complete());
}
TEST_F(HostResolverManagerTest, NumericIPv4Address) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("127.1.2.3", 5555), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("127.1.2.3", 5555)));
}
TEST_F(HostResolverManagerTest, NumericIPv6Address) {
// Resolve a plain IPv6 address. Don't worry about [brackets], because
// the caller should have removed them.
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("2001:db8::1", 5555), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("2001:db8::1", 5555)));
}
TEST_F(HostResolverManagerTest, EmptyHost) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair(std::string(), 5555), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
}
TEST_F(HostResolverManagerTest, EmptyDotsHost) {
for (int i = 0; i < 16; ++i) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair(std::string(i, '.'), 5555), NetLogWithSource(),
base::nullopt, request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
}
}
TEST_F(HostResolverManagerTest, LongHost) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair(std::string(4097, 'a'), 5555), NetLogWithSource(),
base::nullopt, request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
}
TEST_F(HostResolverManagerTest, DeDupeRequests) {
// Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is
// blocked, these should all pile up until we signal it.
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 82), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 83), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
for (auto& response : responses) {
ASSERT_FALSE(response->complete());
}
proc_->SignalMultiple(2u); // One for "a", one for "b".
for (auto& response : responses) {
EXPECT_THAT(response->result_error(), IsOk());
}
}
TEST_F(HostResolverManagerTest, CancelMultipleRequests) {
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 82), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 83), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
for (auto& response : responses) {
ASSERT_FALSE(response->complete());
}
// Cancel everything except request for requests[3] ("a", 82).
responses[0]->CancelRequest();
responses[1]->CancelRequest();
responses[2]->CancelRequest();
responses[4]->CancelRequest();
proc_->SignalMultiple(2u); // One for "a", one for "b".
EXPECT_THAT(responses[3]->result_error(), IsOk());
EXPECT_FALSE(responses[0]->complete());
EXPECT_FALSE(responses[1]->complete());
EXPECT_FALSE(responses[2]->complete());
EXPECT_FALSE(responses[4]->complete());
}
TEST_F(HostResolverManagerTest, CanceledRequestsReleaseJobSlots) {
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
// Fill up the dispatcher and queue.
for (unsigned i = 0; i < kMaxJobs + 1; ++i) {
std::string hostname = "a_";
hostname[1] = 'a' + i;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair(hostname, 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
ASSERT_FALSE(responses.back()->complete());
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair(hostname, 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
ASSERT_FALSE(responses.back()->complete());
}
ASSERT_TRUE(proc_->WaitFor(kMaxJobs));
// Cancel all but last two.
for (unsigned i = 0; i < responses.size() - 2; ++i) {
responses[i]->CancelRequest();
}
ASSERT_TRUE(proc_->WaitFor(kMaxJobs + 1));
proc_->SignalAll();
size_t num_requests = responses.size();
EXPECT_THAT(responses[num_requests - 1]->result_error(), IsOk());
EXPECT_THAT(responses[num_requests - 2]->result_error(), IsOk());
for (unsigned i = 0; i < num_requests - 2; ++i) {
EXPECT_FALSE(responses[i]->complete());
}
}
TEST_F(HostResolverManagerTest, CancelWithinCallback) {
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
for (auto& response : responses) {
// Cancelling request is required to complete first, so that it can
// attempt to cancel the others. This test assumes all jobs are
// completed in order.
DCHECK(!response->complete());
response->CancelRequest();
}
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper cancelling_response(
resolver_->CreateRequest(HostPortPair("a", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 82), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
proc_->SignalMultiple(2u); // One for "a". One for "finalrequest".
EXPECT_THAT(cancelling_response.result_error(), IsOk());
ResolveHostResponseHelper final_response(resolver_->CreateRequest(
HostPortPair("finalrequest", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(final_response.result_error(), IsOk());
for (auto& response : responses) {
EXPECT_FALSE(response->complete());
}
}
TEST_F(HostResolverManagerTest, DeleteWithinCallback) {
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
for (auto& response : responses) {
// Deleting request is required to be first, so the other requests
// will still be running to be deleted. This test assumes that the
// Jobs will be Aborted in order and the requests in order within the
// jobs.
DCHECK(!response->complete());
}
DestroyResolver();
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper deleting_response(
resolver_->CreateRequest(HostPortPair("a", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
// Start additional requests to be cancelled as part of the first's deletion.
// Assumes all requests for a job are handled in order so that the deleting
// request will run first and cancel the rest.
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 82), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
proc_->SignalMultiple(3u);
EXPECT_THAT(deleting_response.result_error(), IsOk());
base::RunLoop().RunUntilIdle();
for (auto& response : responses) {
EXPECT_FALSE(response->complete());
}
}
TEST_F(HostResolverManagerTest, DeleteWithinAbortedCallback) {
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
ResolveHostResponseHelper::Callback custom_callback =
base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
for (auto& response : responses) {
// Deleting request is required to be first, so the other requests
// will still be running to be deleted. This test assumes that the
// Jobs will be Aborted in order and the requests in order within
// the jobs.
DCHECK(!response->complete());
}
DestroyResolver();
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper deleting_response(
resolver_->CreateRequest(HostPortPair("a", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 81), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 82), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 83), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
// Wait for all calls to queue up, trigger abort via IP address change, then
// signal all the queued requests to let them all try to finish.
EXPECT_TRUE(proc_->WaitFor(2u));
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
proc_->SignalAll();
EXPECT_THAT(deleting_response.result_error(), IsError(ERR_NETWORK_CHANGED));
base::RunLoop().RunUntilIdle();
for (auto& response : responses) {
EXPECT_FALSE(response->complete());
}
}
TEST_F(HostResolverManagerTest, StartWithinCallback) {
std::unique_ptr<ResolveHostResponseHelper> new_response;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
new_response = std::make_unique<ResolveHostResponseHelper>(
resolver_->CreateRequest(
HostPortPair("new", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper starting_response(
resolver_->CreateRequest(HostPortPair("a", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
proc_->SignalMultiple(2u); // One for "a". One for "new".
EXPECT_THAT(starting_response.result_error(), IsOk());
EXPECT_THAT(new_response->result_error(), IsOk());
}
TEST_F(HostResolverManagerTest, StartWithinEvictionCallback) {
CreateSerialResolver();
resolver_->SetMaxQueuedJobsForTesting(2);
std::unique_ptr<ResolveHostResponseHelper> new_response;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
new_response = std::make_unique<ResolveHostResponseHelper>(
resolver_->CreateRequest(
HostPortPair("new", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper initial_response(resolver_->CreateRequest(
HostPortPair("initial", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper evictee1_response(
resolver_->CreateRequest(HostPortPair("evictee1", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
ResolveHostResponseHelper evictee2_response(resolver_->CreateRequest(
HostPortPair("evictee2", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
// Now one running request ("initial") and two queued requests ("evictee1" and
// "evictee2"). Any further requests will cause evictions.
ResolveHostResponseHelper evictor_response(resolver_->CreateRequest(
HostPortPair("evictor", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(evictee1_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
// "new" should evict "evictee2"
EXPECT_THAT(evictee2_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
proc_->SignalMultiple(3u);
EXPECT_THAT(initial_response.result_error(), IsOk());
EXPECT_THAT(evictor_response.result_error(), IsOk());
EXPECT_THAT(new_response->result_error(), IsOk());
}
// Test where we start a new request within an eviction callback that itself
// evicts the first evictor.
TEST_F(HostResolverManagerTest, StartWithinEvictionCallback_DoubleEviction) {
CreateSerialResolver();
resolver_->SetMaxQueuedJobsForTesting(1);
std::unique_ptr<ResolveHostResponseHelper> new_response;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
new_response = std::make_unique<ResolveHostResponseHelper>(
resolver_->CreateRequest(
HostPortPair("new", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper initial_response(resolver_->CreateRequest(
HostPortPair("initial", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper evictee_response(
resolver_->CreateRequest(HostPortPair("evictee", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
// Now one running request ("initial") and one queued requests ("evictee").
// Any further requests will cause evictions.
ResolveHostResponseHelper evictor_response(resolver_->CreateRequest(
HostPortPair("evictor", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(evictee_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
// "new" should evict "evictor"
EXPECT_THAT(evictor_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
proc_->SignalMultiple(2u);
EXPECT_THAT(initial_response.result_error(), IsOk());
EXPECT_THAT(new_response->result_error(), IsOk());
}
TEST_F(HostResolverManagerTest, StartWithinEvictionCallback_SameRequest) {
CreateSerialResolver();
resolver_->SetMaxQueuedJobsForTesting(2);
std::unique_ptr<ResolveHostResponseHelper> new_response;
auto custom_callback = base::BindLambdaForTesting(
[&](CompletionOnceCallback completion_callback, int error) {
new_response = std::make_unique<ResolveHostResponseHelper>(
resolver_->CreateRequest(
HostPortPair("evictor", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
std::move(completion_callback).Run(error);
});
ResolveHostResponseHelper initial_response(resolver_->CreateRequest(
HostPortPair("initial", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper evictee_response(
resolver_->CreateRequest(HostPortPair("evictee", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
std::move(custom_callback));
ResolveHostResponseHelper additional_response(resolver_->CreateRequest(
HostPortPair("additional", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
// Now one running request ("initial") and two queued requests ("evictee" and
// "additional"). Any further requests will cause evictions.
ResolveHostResponseHelper evictor_response(resolver_->CreateRequest(
HostPortPair("evictor", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(evictee_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
// Second "evictor" should be joined with the first and not evict "additional"
// Only 3 proc requests because both "evictor" requests are combined.
proc_->SignalMultiple(3u);
EXPECT_THAT(initial_response.result_error(), IsOk());
EXPECT_THAT(additional_response.result_error(), IsOk());
EXPECT_THAT(evictor_response.result_error(), IsOk());
EXPECT_THAT(new_response->result_error(), IsOk());
}
TEST_F(HostResolverManagerTest, BypassCache) {
proc_->SignalMultiple(2u);
ResolveHostResponseHelper initial_response(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(initial_response.result_error(), IsOk());
EXPECT_EQ(1u, proc_->GetCaptureList().size());
ResolveHostResponseHelper cached_response(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(cached_response.result_error(), IsOk());
// Expect no increase to calls to |proc_| because result was cached.
EXPECT_EQ(1u, proc_->GetCaptureList().size());
HostResolver::ResolveHostParameters parameters;
parameters.cache_usage =
HostResolver::ResolveHostParameters::CacheUsage::DISALLOWED;
ResolveHostResponseHelper cache_bypassed_response(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(cache_bypassed_response.result_error(), IsOk());
// Expect call to |proc_| because cache was bypassed.
EXPECT_EQ(2u, proc_->GetCaptureList().size());
}
// Test that IP address changes flush the cache but initial DNS config reads
// do not.
TEST_F(HostResolverManagerTest, FlushCacheOnIPAddressChange) {
proc_->SignalMultiple(2u); // One before the flush, one after.
ResolveHostResponseHelper initial_response(resolver_->CreateRequest(
HostPortPair("host1", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(initial_response.result_error(), IsOk());
EXPECT_EQ(1u, proc_->GetCaptureList().size());
ResolveHostResponseHelper cached_response(resolver_->CreateRequest(
HostPortPair("host1", 75), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(cached_response.result_error(), IsOk());
EXPECT_EQ(1u, proc_->GetCaptureList().size()); // No expected increase.
// Verify initial DNS config read does not flush cache.
NetworkChangeNotifier::NotifyObserversOfInitialDNSConfigReadForTests();
ResolveHostResponseHelper unflushed_response(resolver_->CreateRequest(
HostPortPair("host1", 75), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(unflushed_response.result_error(), IsOk());
EXPECT_EQ(1u, proc_->GetCaptureList().size()); // No expected increase.
// Flush cache by triggering an IP address change.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::RunLoop().RunUntilIdle(); // Notification happens async.
// Resolve "host1" again -- this time it won't be served from cache, so it
// will complete asynchronously.
ResolveHostResponseHelper flushed_response(resolver_->CreateRequest(
HostPortPair("host1", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(flushed_response.result_error(), IsOk());
EXPECT_EQ(2u, proc_->GetCaptureList().size()); // Expected increase.
}
// Test that IP address changes send ERR_NETWORK_CHANGED to pending requests.
TEST_F(HostResolverManagerTest, AbortOnIPAddressChanged) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("host1", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ASSERT_FALSE(response.complete());
ASSERT_TRUE(proc_->WaitFor(1u));
// Triggering an IP address change.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::RunLoop().RunUntilIdle(); // Notification happens async.
proc_->SignalAll();
EXPECT_THAT(response.result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_FALSE(response.request()->GetAddressResults());
EXPECT_EQ(0u, host_cache_->size());
}
// Test that initial DNS config read signals do not abort pending requests.
TEST_F(HostResolverManagerTest, DontAbortOnInitialDNSConfigRead) {
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("host1", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ASSERT_FALSE(response.complete());
ASSERT_TRUE(proc_->WaitFor(1u));
// Triggering initial DNS config read signal.
NetworkChangeNotifier::NotifyObserversOfInitialDNSConfigReadForTests();
base::RunLoop().RunUntilIdle(); // Notification happens async.
proc_->SignalAll();
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_TRUE(response.request()->GetAddressResults());
}
// Obey pool constraints after IP address has changed.
TEST_F(HostResolverManagerTest, ObeyPoolConstraintsAfterIPAddressChange) {
// Runs at most one job at a time.
CreateSerialResolver();
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("a", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("b", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("c", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get())));
for (auto& response : responses) {
ASSERT_FALSE(response->complete());
}
ASSERT_TRUE(proc_->WaitFor(1u));
// Triggering an IP address change.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::RunLoop().RunUntilIdle(); // Notification happens async.
proc_->SignalMultiple(3u); // Let the false-start go so that we can catch it.
// Requests should complete one at a time, with the first failing.
EXPECT_THAT(responses[0]->result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_EQ(1u, num_running_dispatcher_jobs());
EXPECT_FALSE(responses[1]->complete());
EXPECT_FALSE(responses[2]->complete());
EXPECT_THAT(responses[1]->result_error(), IsOk());
EXPECT_EQ(1u, num_running_dispatcher_jobs());
EXPECT_FALSE(responses[2]->complete());
EXPECT_THAT(responses[2]->result_error(), IsOk());
}
// Tests that a new Request made from the callback of a previously aborted one
// will not be aborted.
TEST_F(HostResolverManagerTest, AbortOnlyExistingRequestsOnIPAddressChange) {
auto custom_callback_template = base::BindLambdaForTesting(
[&](const HostPortPair& next_host,
std::unique_ptr<ResolveHostResponseHelper>* next_response,
CompletionOnceCallback completion_callback, int error) {
*next_response = std::make_unique<ResolveHostResponseHelper>(
resolver_->CreateRequest(next_host, NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()));
std::move(completion_callback).Run(error);
});
std::vector<std::unique_ptr<ResolveHostResponseHelper>> next_responses(3);
ResolveHostResponseHelper response0(
resolver_->CreateRequest(HostPortPair("bbb", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
base::BindOnce(custom_callback_template, HostPortPair("zzz", 80),
&next_responses[0]));
ResolveHostResponseHelper response1(
resolver_->CreateRequest(HostPortPair("eee", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
base::BindOnce(custom_callback_template, HostPortPair("aaa", 80),
&next_responses[1]));
ResolveHostResponseHelper response2(
resolver_->CreateRequest(HostPortPair("ccc", 80), NetLogWithSource(),
base::nullopt, request_context_.get(),
host_cache_.get()),
base::BindOnce(custom_callback_template, HostPortPair("eee", 80),
&next_responses[2]));
// Wait until all are blocked;
ASSERT_TRUE(proc_->WaitFor(3u));
// Trigger an IP address change.
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
// This should abort all running jobs.
base::RunLoop().RunUntilIdle();
EXPECT_THAT(response0.result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_THAT(response1.result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_THAT(response2.result_error(), IsError(ERR_NETWORK_CHANGED));
EXPECT_FALSE(next_responses[0]->complete());
EXPECT_FALSE(next_responses[1]->complete());
EXPECT_FALSE(next_responses[2]->complete());
// Unblock all calls to proc.
proc_->SignalMultiple(6u);
// Run until the re-started requests finish.
EXPECT_THAT(next_responses[0]->result_error(), IsOk());
EXPECT_THAT(next_responses[1]->result_error(), IsOk());
EXPECT_THAT(next_responses[2]->result_error(), IsOk());
// Verify that results of aborted Jobs were not cached.
EXPECT_EQ(6u, proc_->GetCaptureList().size());
EXPECT_EQ(3u, host_cache_->size());
}
// Tests that when the maximum threads is set to 1, requests are dequeued
// in order of priority.
TEST_F(HostResolverManagerTest, HigherPriorityRequestsStartedFirst) {
CreateSerialResolver();
HostResolver::ResolveHostParameters low_priority;
low_priority.initial_priority = LOW;
HostResolver::ResolveHostParameters medium_priority;
medium_priority.initial_priority = MEDIUM;
HostResolver::ResolveHostParameters highest_priority;
highest_priority.initial_priority = HIGHEST;
// Note that at this point the MockHostResolverProc is blocked, so any
// requests we make will not complete.
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req0", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req1", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req2", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req3", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req4", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req5", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req6", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req5", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
for (const auto& response : responses) {
ASSERT_FALSE(response->complete());
}
// Unblock the resolver thread so the requests can run.
proc_->SignalMultiple(responses.size()); // More than needed.
// Wait for all the requests to complete successfully.
for (auto& response : responses) {
EXPECT_THAT(response->result_error(), IsOk());
}
// Since we have restricted to a single concurrent thread in the jobpool,
// the requests should complete in order of priority (with the exception
// of the first request, which gets started right away, since there is
// nothing outstanding).
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(7u, capture_list.size());
EXPECT_EQ("req0", capture_list[0].hostname);
EXPECT_EQ("req4", capture_list[1].hostname);
EXPECT_EQ("req5", capture_list[2].hostname);
EXPECT_EQ("req1", capture_list[3].hostname);
EXPECT_EQ("req2", capture_list[4].hostname);
EXPECT_EQ("req3", capture_list[5].hostname);
EXPECT_EQ("req6", capture_list[6].hostname);
}
// Test that changing a job's priority affects the dequeueing order.
TEST_F(HostResolverManagerTest, ChangePriority) {
CreateSerialResolver();
HostResolver::ResolveHostParameters lowest_priority;
lowest_priority.initial_priority = LOWEST;
HostResolver::ResolveHostParameters low_priority;
low_priority.initial_priority = LOW;
HostResolver::ResolveHostParameters medium_priority;
medium_priority.initial_priority = MEDIUM;
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req0", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req1", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req2", 80), NetLogWithSource(), lowest_priority,
request_context_.get(), host_cache_.get())));
// req0 starts immediately; without ChangePriority, req1 and then req2 should
// run.
for (const auto& response : responses) {
ASSERT_FALSE(response->complete());
}
// Changing req2 to HIGHEST should make it run before req1.
// (It can't run before req0, since req0 started immediately.)
responses[2]->request()->ChangeRequestPriority(HIGHEST);
// Let all 3 requests finish.
proc_->SignalMultiple(3u);
for (auto& response : responses) {
EXPECT_THAT(response->result_error(), IsOk());
}
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(3u, capture_list.size());
EXPECT_EQ("req0", capture_list[0].hostname);
EXPECT_EQ("req2", capture_list[1].hostname);
EXPECT_EQ("req1", capture_list[2].hostname);
}
// Try cancelling a job which has not started yet.
TEST_F(HostResolverManagerTest, CancelPendingRequest) {
CreateSerialResolver();
HostResolver::ResolveHostParameters lowest_priority;
lowest_priority.initial_priority = LOWEST;
HostResolver::ResolveHostParameters low_priority;
low_priority.initial_priority = LOW;
HostResolver::ResolveHostParameters medium_priority;
medium_priority.initial_priority = MEDIUM;
HostResolver::ResolveHostParameters highest_priority;
highest_priority.initial_priority = HIGHEST;
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req0", 80), NetLogWithSource(), lowest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req1", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req2", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req3", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req4", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req5", 80), NetLogWithSource(), lowest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req6", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
// Cancel some requests
responses[1]->CancelRequest();
responses[4]->CancelRequest();
responses[5]->CancelRequest();
// Unblock the resolver thread so the requests can run.
proc_->SignalMultiple(responses.size()); // More than needed.
// Let everything try to finish.
base::RunLoop().RunUntilIdle();
// Wait for all the requests to complete succesfully.
EXPECT_THAT(responses[0]->result_error(), IsOk());
EXPECT_THAT(responses[2]->result_error(), IsOk());
EXPECT_THAT(responses[3]->result_error(), IsOk());
EXPECT_THAT(responses[6]->result_error(), IsOk());
// Cancelled requests shouldn't complete.
EXPECT_FALSE(responses[1]->complete());
EXPECT_FALSE(responses[4]->complete());
EXPECT_FALSE(responses[5]->complete());
// Verify that they called out the the resolver proc (which runs on the
// resolver thread) in the expected order.
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(4u, capture_list.size());
EXPECT_EQ("req0", capture_list[0].hostname);
EXPECT_EQ("req2", capture_list[1].hostname);
EXPECT_EQ("req6", capture_list[2].hostname);
EXPECT_EQ("req3", capture_list[3].hostname);
}
// Test that when too many requests are enqueued, old ones start to be aborted.
TEST_F(HostResolverManagerTest, QueueOverflow) {
CreateSerialResolver();
// Allow only 3 queued jobs.
const size_t kMaxPendingJobs = 3u;
resolver_->SetMaxQueuedJobsForTesting(kMaxPendingJobs);
HostResolver::ResolveHostParameters lowest_priority;
lowest_priority.initial_priority = LOWEST;
HostResolver::ResolveHostParameters low_priority;
low_priority.initial_priority = LOW;
HostResolver::ResolveHostParameters medium_priority;
medium_priority.initial_priority = MEDIUM;
HostResolver::ResolveHostParameters highest_priority;
highest_priority.initial_priority = HIGHEST;
// Note that at this point the MockHostResolverProc is blocked, so any
// requests we make will not complete.
std::vector<std::unique_ptr<ResolveHostResponseHelper>> responses;
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req0", 80), NetLogWithSource(), lowest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req1", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req2", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req3", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
// At this point, there are 3 enqueued jobs (and one "running" job).
// Insertion of subsequent requests will cause evictions.
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req4", 80), NetLogWithSource(), low_priority,
request_context_.get(), host_cache_.get())));
EXPECT_THAT(responses[4]->result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE)); // Evicts self.
EXPECT_FALSE(responses[4]->request()->GetAddressResults());
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req5", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
EXPECT_THAT(responses[2]->result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
EXPECT_FALSE(responses[2]->request()->GetAddressResults());
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req6", 80), NetLogWithSource(), highest_priority,
request_context_.get(), host_cache_.get())));
EXPECT_THAT(responses[3]->result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
EXPECT_FALSE(responses[3]->request()->GetAddressResults());
responses.emplace_back(
std::make_unique<ResolveHostResponseHelper>(resolver_->CreateRequest(
HostPortPair("req7", 80), NetLogWithSource(), medium_priority,
request_context_.get(), host_cache_.get())));
EXPECT_THAT(responses[5]->result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
EXPECT_FALSE(responses[5]->request()->GetAddressResults());
// Unblock the resolver thread so the requests can run.
proc_->SignalMultiple(4u);
// The rest should succeed.
EXPECT_THAT(responses[0]->result_error(), IsOk());
EXPECT_TRUE(responses[0]->request()->GetAddressResults());
EXPECT_THAT(responses[1]->result_error(), IsOk());
EXPECT_TRUE(responses[1]->request()->GetAddressResults());
EXPECT_THAT(responses[6]->result_error(), IsOk());
EXPECT_TRUE(responses[6]->request()->GetAddressResults());
EXPECT_THAT(responses[7]->result_error(), IsOk());
EXPECT_TRUE(responses[7]->request()->GetAddressResults());
// Verify that they called out the the resolver proc (which runs on the
// resolver thread) in the expected order.
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(4u, capture_list.size());
EXPECT_EQ("req0", capture_list[0].hostname);
EXPECT_EQ("req1", capture_list[1].hostname);
EXPECT_EQ("req6", capture_list[2].hostname);
EXPECT_EQ("req7", capture_list[3].hostname);
// Verify that the evicted (incomplete) requests were not cached.
EXPECT_EQ(4u, host_cache_->size());
for (size_t i = 0; i < responses.size(); ++i) {
EXPECT_TRUE(responses[i]->complete()) << i;
}
}
// Tests that jobs can self-evict by setting the max queue to 0.
TEST_F(HostResolverManagerTest, QueueOverflow_SelfEvict) {
CreateSerialResolver();
resolver_->SetMaxQueuedJobsForTesting(0);
// Note that at this point the MockHostResolverProc is blocked, so any
// requests we make will not complete.
ResolveHostResponseHelper run_response(resolver_->CreateRequest(
HostPortPair("run", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper evict_response(resolver_->CreateRequest(
HostPortPair("req1", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(evict_response.result_error(),
IsError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
EXPECT_FALSE(evict_response.request()->GetAddressResults());
proc_->SignalMultiple(1u);
EXPECT_THAT(run_response.result_error(), IsOk());
EXPECT_TRUE(run_response.request()->GetAddressResults());
}
// Make sure that the dns query type parameter is respected when raw IPs are
// passed in.
TEST_F(HostResolverManagerTest, AddressFamilyWithRawIPs) {
HostResolver::ResolveHostParameters v4_parameters;
v4_parameters.dns_query_type = DnsQueryType::A;
HostResolver::ResolveHostParameters v6_parameters;
v6_parameters.dns_query_type = DnsQueryType::AAAA;
ResolveHostResponseHelper v4_v4_request(resolver_->CreateRequest(
HostPortPair("127.0.0.1", 80), NetLogWithSource(), v4_parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_v4_request.result_error(), IsOk());
EXPECT_THAT(v4_v4_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("127.0.0.1", 80)));
ResolveHostResponseHelper v4_v6_request(resolver_->CreateRequest(
HostPortPair("127.0.0.1", 80), NetLogWithSource(), v6_parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_v6_request.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
ResolveHostResponseHelper v4_unsp_request(resolver_->CreateRequest(
HostPortPair("127.0.0.1", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v4_unsp_request.result_error(), IsOk());
EXPECT_THAT(
v4_unsp_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("127.0.0.1", 80)));
ResolveHostResponseHelper v6_v4_request(resolver_->CreateRequest(
HostPortPair("::1", 80), NetLogWithSource(), v4_parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_v4_request.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
ResolveHostResponseHelper v6_v6_request(resolver_->CreateRequest(
HostPortPair("::1", 80), NetLogWithSource(), v6_parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_v6_request.result_error(), IsOk());
EXPECT_THAT(v6_v6_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::1", 80)));
ResolveHostResponseHelper v6_unsp_request(resolver_->CreateRequest(
HostPortPair("::1", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(v6_unsp_request.result_error(), IsOk());
EXPECT_THAT(
v6_unsp_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("::1", 80)));
}
TEST_F(HostResolverManagerTest, LocalOnly_FromCache) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u); // Need only one.
HostResolver::ResolveHostParameters source_none_parameters;
source_none_parameters.source = HostResolverSource::LOCAL_ONLY;
// First NONE query expected to complete synchronously with a cache miss.
ResolveHostResponseHelper cache_miss_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(cache_miss_request.complete());
EXPECT_THAT(cache_miss_request.result_error(), IsError(ERR_DNS_CACHE_MISS));
EXPECT_FALSE(cache_miss_request.request()->GetAddressResults());
EXPECT_FALSE(cache_miss_request.request()->GetStaleInfo());
// Normal query to populate the cache.
ResolveHostResponseHelper normal_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(normal_request.result_error(), IsOk());
EXPECT_FALSE(normal_request.request()->GetStaleInfo());
// Second NONE query expected to complete synchronously with cache hit.
ResolveHostResponseHelper cache_hit_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(cache_hit_request.complete());
EXPECT_THAT(cache_hit_request.result_error(), IsOk());
EXPECT_THAT(
cache_hit_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.42", 80)));
EXPECT_FALSE(cache_hit_request.request()->GetStaleInfo().value().is_stale());
}
TEST_F(HostResolverManagerTest, LocalOnly_StaleEntry) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u); // Need only one.
HostResolver::ResolveHostParameters source_none_parameters;
source_none_parameters.source = HostResolverSource::LOCAL_ONLY;
// First NONE query expected to complete synchronously with a cache miss.
ResolveHostResponseHelper cache_miss_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(cache_miss_request.complete());
EXPECT_THAT(cache_miss_request.result_error(), IsError(ERR_DNS_CACHE_MISS));
EXPECT_FALSE(cache_miss_request.request()->GetAddressResults());
EXPECT_FALSE(cache_miss_request.request()->GetStaleInfo());
// Normal query to populate the cache.
ResolveHostResponseHelper normal_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(normal_request.result_error(), IsOk());
EXPECT_FALSE(normal_request.request()->GetStaleInfo());
MakeCacheStale();
// Second NONE query still expected to complete synchronously with cache miss.
ResolveHostResponseHelper stale_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(stale_request.complete());
EXPECT_THAT(stale_request.result_error(), IsError(ERR_DNS_CACHE_MISS));
EXPECT_FALSE(stale_request.request()->GetAddressResults());
EXPECT_FALSE(stale_request.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, LocalOnly_FromIp) {
HostResolver::ResolveHostParameters source_none_parameters;
source_none_parameters.source = HostResolverSource::LOCAL_ONLY;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("1.2.3.4", 56), NetLogWithSource(), source_none_parameters,
request_context_.get(), host_cache_.get()));
// Expected to resolve synchronously.
EXPECT_TRUE(response.complete());
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("1.2.3.4", 56)));
EXPECT_FALSE(response.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, LocalOnly_InvalidName) {
proc_->AddRuleForAllFamilies("foo,bar.com", "192.168.1.42");
HostResolver::ResolveHostParameters source_none_parameters;
source_none_parameters.source = HostResolverSource::LOCAL_ONLY;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("foo,bar.com", 57), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
// Expected to fail synchronously.
EXPECT_TRUE(response.complete());
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
EXPECT_FALSE(response.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, LocalOnly_InvalidLocalhost) {
HostResolver::ResolveHostParameters source_none_parameters;
source_none_parameters.source = HostResolverSource::LOCAL_ONLY;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("foo,bar.localhost", 58), NetLogWithSource(),
source_none_parameters, request_context_.get(), host_cache_.get()));
// Expected to fail synchronously.
EXPECT_TRUE(response.complete());
EXPECT_THAT(response.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
EXPECT_FALSE(response.request()->GetAddressResults());
EXPECT_FALSE(response.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, StaleAllowed) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u); // Need only one.
HostResolver::ResolveHostParameters stale_allowed_parameters;
stale_allowed_parameters.source = HostResolverSource::LOCAL_ONLY;
stale_allowed_parameters.cache_usage =
HostResolver::ResolveHostParameters::CacheUsage::STALE_ALLOWED;
// First query expected to complete synchronously as a cache miss.
ResolveHostResponseHelper cache_miss_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(),
stale_allowed_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(cache_miss_request.complete());
EXPECT_THAT(cache_miss_request.result_error(), IsError(ERR_DNS_CACHE_MISS));
EXPECT_FALSE(cache_miss_request.request()->GetAddressResults());
EXPECT_FALSE(cache_miss_request.request()->GetStaleInfo());
// Normal query to populate cache
ResolveHostResponseHelper normal_request(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(normal_request.result_error(), IsOk());
EXPECT_FALSE(normal_request.request()->GetStaleInfo());
MakeCacheStale();
// Second NONE query expected to get a stale cache hit.
ResolveHostResponseHelper stale_request(resolver_->CreateRequest(
HostPortPair("just.testing", 84), NetLogWithSource(),
stale_allowed_parameters, request_context_.get(), host_cache_.get()));
EXPECT_TRUE(stale_request.complete());
EXPECT_THAT(stale_request.result_error(), IsOk());
EXPECT_THAT(stale_request.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.42", 84)));
EXPECT_TRUE(stale_request.request()->GetStaleInfo().value().is_stale());
}
TEST_F(HostResolverManagerTest, StaleAllowed_NonLocal) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.2.42");
proc_->SignalMultiple(1u); // Need only one.
HostResolver::ResolveHostParameters stale_allowed_parameters;
stale_allowed_parameters.cache_usage =
HostResolver::ResolveHostParameters::CacheUsage::STALE_ALLOWED;
// Normal non-local resolves should still work normally with the STALE_ALLOWED
// parameter, and there should be no stale info.
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 85), NetLogWithSource(),
stale_allowed_parameters, request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.2.42", 85)));
EXPECT_FALSE(response.request()->GetStaleInfo());
}
TEST_F(HostResolverManagerTest, StaleAllowed_FromIp) {
HostResolver::ResolveHostParameters stale_allowed_parameters;
stale_allowed_parameters.cache_usage =
HostResolver::ResolveHostParameters::CacheUsage::STALE_ALLOWED;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("1.2.3.4", 57), NetLogWithSource(), stale_allowed_parameters,
request_context_.get(), host_cache_.get()));
// Expected to resolve synchronously without stale info.
EXPECT_TRUE(response.complete());
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("1.2.3.4", 57)));
EXPECT_FALSE(response.request()->GetStaleInfo());
}
// TODO(mgersh): add a test case for errors with positive TTL after
// https://crbug.com/115051 is fixed.
// Test the retry attempts simulating host resolver proc that takes too long.
TEST_F(HostResolverManagerTest, MultipleAttempts) {
// Total number of attempts would be 3 and we want the 3rd attempt to resolve
// the host. First and second attempt will be forced to wait until they get
// word that a resolution has completed. The 3rd resolution attempt will try
// to get done ASAP, and won't wait.
int kAttemptNumberToResolve = 3;
int kTotalAttempts = 3;
// Add a little bit of extra fudge to the delay to allow reasonable
// flexibility for time > vs >= etc. We don't need to fail the test if we
// retry at t=6001 instead of t=6000.
base::TimeDelta kSleepFudgeFactor = base::TimeDelta::FromMilliseconds(1);
scoped_refptr<LookupAttemptHostResolverProc> resolver_proc(
new LookupAttemptHostResolverProc(nullptr, kAttemptNumberToResolve,
kTotalAttempts));
ProcTaskParams params = DefaultParams(resolver_proc.get());
base::TimeDelta unresponsive_delay = params.unresponsive_delay;
int retry_factor = params.retry_factor;
CreateResolverWithLimitsAndParams(kMaxJobs, params, true /* ipv6_reachable */,
true /* check_ipv6_on_wifi */);
// Override the current thread task runner, so we can simulate the passage of
// time and avoid any actual sleeps.
auto test_task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
base::ScopedClosureRunner task_runner_override_scoped_cleanup =
base::ThreadTaskRunnerHandle::OverrideForTesting(test_task_runner);
// Resolve "host1".
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("host1", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_FALSE(response.complete());
resolver_proc->WaitForNAttemptsToBeBlocked(1);
EXPECT_FALSE(response.complete());
test_task_runner->FastForwardBy(unresponsive_delay + kSleepFudgeFactor);
resolver_proc->WaitForNAttemptsToBeBlocked(2);
EXPECT_FALSE(response.complete());
test_task_runner->FastForwardBy(unresponsive_delay * retry_factor +
kSleepFudgeFactor);
resolver_proc->WaitForAllAttemptsToFinish();
test_task_runner->RunUntilIdle();
// Resolve returns -4 to indicate that 3rd attempt has resolved the host.
// Since we're using a TestMockTimeTaskRunner, the RunLoop stuff in
// result_error() will fail if it actually has to wait, but unless there's an
// error, the result should be immediately ready by this point.
EXPECT_EQ(-4, response.result_error());
// We should be done with retries, but make sure none erroneously happen.
test_task_runner->FastForwardUntilNoTasksRemain();
EXPECT_EQ(resolver_proc->GetTotalAttemptsResolved(), kTotalAttempts);
}
// Regression test for https://crbug.com/976948.
//
// Tests that when the maximum number of retries is set to
// |HostResolver::ManagerOptions::kDefaultRetryAttempts| the
// number of retries used is 4 rather than something higher.
TEST_F(HostResolverManagerTest, DefaultMaxRetryAttempts) {
auto test_task_runner = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
base::ScopedClosureRunner task_runner_override_scoped_cleanup =
base::ThreadTaskRunnerHandle::OverrideForTesting(test_task_runner);
// Instantiate a ResolverProc that will block all incoming requests.
auto resolver_proc = base::MakeRefCounted<LookupAttemptHostResolverProc>(
nullptr, std::numeric_limits<size_t>::max(),
std::numeric_limits<size_t>::max());
// This corresponds to kDefaultMaxRetryAttempts in
// ProcTaskParams::ProcTaskParams(). The correspondence is verified below,
// since that symbol is not exported.
const size_t expected_max_retries = 4;
// Use the special value |ManagerOptions::kDefaultRetryAttempts|, which is
// expected to translate into |expected_num_retries|.
ASSERT_NE(HostResolver::ManagerOptions::kDefaultRetryAttempts,
expected_max_retries);
ProcTaskParams params(resolver_proc.get(),
HostResolver::ManagerOptions::kDefaultRetryAttempts);
ASSERT_EQ(params.max_retry_attempts, expected_max_retries);
CreateResolverWithLimitsAndParams(kMaxJobs, params,
false /* ipv6_reachable */,
false /* check_ipv6_on_wifi */);
// Resolve "host1". The resolver proc will hang all requests so this
// resolution should remain stalled until calling SetResolvedAttemptNumber().
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("host1", 70), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_FALSE(response.complete());
// Simulate running the main thread (network task runner) for a long
// time. Because none of the attempts posted to worker pool can complete, this
// should cause all of the retry attempts to get posted, according to the
// exponential backoff schedule.
test_task_runner->FastForwardBy(base::TimeDelta::FromMinutes(20));
// Unblock the resolver proc, then wait for all the worker pool and main
// thread tasks to complete. Note that the call to SetResolvedAttemptNumber(1)
// will cause all the blocked resolver procs tasks fail with -2.
resolver_proc->SetResolvedAttemptNumber(1);
const int kExpectedError = -2;
base::ThreadPoolInstance::Get()->FlushForTesting();
test_task_runner->RunUntilIdle();
ASSERT_TRUE(response.complete());
EXPECT_EQ(kExpectedError, response.result_error());
// Ensure that the original attempt was executed on the worker pool, as well
// as the maximum number of allowed retries, and no more.
EXPECT_EQ(static_cast<int>(expected_max_retries + 1),
resolver_proc->GetTotalAttemptsResolved());
}
// If a host resolves to a list that includes 127.0.53.53, this is treated as
// an error. 127.0.53.53 is a localhost address, however it has been given a
// special significance by ICANN to help surface name collision resulting from
// the new gTLDs.
TEST_F(HostResolverManagerTest, NameCollisionIcann) {
proc_->AddRuleForAllFamilies("single", "127.0.53.53");
proc_->AddRuleForAllFamilies("multiple", "127.0.0.1,127.0.53.53");
proc_->AddRuleForAllFamilies("ipv6", "::127.0.53.53");
proc_->AddRuleForAllFamilies("not_reserved1", "53.53.0.127");
proc_->AddRuleForAllFamilies("not_reserved2", "127.0.53.54");
proc_->AddRuleForAllFamilies("not_reserved3", "10.0.53.53");
proc_->SignalMultiple(6u);
ResolveHostResponseHelper single_response(resolver_->CreateRequest(
HostPortPair("single", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(single_response.result_error(),
IsError(ERR_ICANN_NAME_COLLISION));
EXPECT_FALSE(single_response.request()->GetAddressResults());
// ERR_ICANN_NAME_COLLISION is cached like any other error, using a fixed TTL
// for failed entries from proc-based resolver. That said, the fixed TTL is 0,
// so it should never be cached.
const std::pair<const HostCache::Key, HostCache::Entry>* cache_result =
GetCacheHit(HostCache::Key("single", DnsQueryType::UNSPECIFIED,
0 /* host_resolver_flags */,
HostResolverSource::ANY));
EXPECT_FALSE(cache_result);
ResolveHostResponseHelper multiple_response(resolver_->CreateRequest(
HostPortPair("multiple", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(multiple_response.result_error(),
IsError(ERR_ICANN_NAME_COLLISION));
// Resolving an IP literal of 127.0.53.53 however is allowed.
ResolveHostResponseHelper literal_response(resolver_->CreateRequest(
HostPortPair("127.0.53.53", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(literal_response.result_error(), IsOk());
// Moreover the address should not be recognized when embedded in an IPv6
// address.
ResolveHostResponseHelper ipv6_response(resolver_->CreateRequest(
HostPortPair("127.0.53.53", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(ipv6_response.result_error(), IsOk());
// Try some other IPs which are similar, but NOT an exact match on
// 127.0.53.53.
ResolveHostResponseHelper similar_response1(resolver_->CreateRequest(
HostPortPair("not_reserved1", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(similar_response1.result_error(), IsOk());
ResolveHostResponseHelper similar_response2(resolver_->CreateRequest(
HostPortPair("not_reserved2", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(similar_response2.result_error(), IsOk());
ResolveHostResponseHelper similar_response3(resolver_->CreateRequest(
HostPortPair("not_reserved3", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(similar_response3.result_error(), IsOk());
}
TEST_F(HostResolverManagerTest, IsIPv6Reachable) {
// The real HostResolverManager is needed since TestHostResolverManager will
// bypass the IPv6 reachability tests.
DestroyResolver();
host_cache_ = nullptr;
resolver_ = std::make_unique<HostResolverManager>(
DefaultOptions(), nullptr /* system_dns_config_notifier */,
nullptr /* net_log */);
// Verify that two consecutive calls return the same value.
TestNetLog test_net_log;
NetLogWithSource net_log =
NetLogWithSource::Make(&test_net_log, NetLogSourceType::NONE);
bool result1 = IsIPv6Reachable(net_log);
bool result2 = IsIPv6Reachable(net_log);
EXPECT_EQ(result1, result2);
// Filter reachability check events and verify that there are two of them.
auto probe_event_list = test_net_log.GetEntriesWithType(
NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK);
ASSERT_EQ(2U, probe_event_list.size());
// Verify that the first request was not cached and the second one was.
EXPECT_FALSE(GetBooleanValueFromParams(probe_event_list[0], "cached"));
EXPECT_TRUE(GetBooleanValueFromParams(probe_event_list[1], "cached"));
}
TEST_F(HostResolverManagerTest, IncludeCanonicalName) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42",
HOST_RESOLVER_CANONNAME, "canon.name");
proc_->SignalMultiple(2u);
HostResolver::ResolveHostParameters parameters;
parameters.include_canonical_name = true;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper response_no_flag(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.42", 80)));
EXPECT_EQ("canon.name",
response.request()->GetAddressResults().value().canonical_name());
EXPECT_THAT(response_no_flag.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
}
TEST_F(HostResolverManagerTest, LoopbackOnly) {
proc_->AddRuleForAllFamilies("otherlocal", "127.0.0.1",
HOST_RESOLVER_LOOPBACK_ONLY);
proc_->SignalMultiple(2u);
HostResolver::ResolveHostParameters parameters;
parameters.loopback_only = true;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("otherlocal", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
ResolveHostResponseHelper response_no_flag(resolver_->CreateRequest(
HostPortPair("otherlocal", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_THAT(response.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("127.0.0.1", 80)));
EXPECT_THAT(response_no_flag.result_error(), IsError(ERR_NAME_NOT_RESOLVED));
}
TEST_F(HostResolverManagerTest, IsSpeculative) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
HostResolver::ResolveHostParameters parameters;
parameters.is_speculative = true;
ResolveHostResponseHelper response(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), parameters,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response.result_error(), IsOk());
EXPECT_FALSE(response.request()->GetAddressResults());
ASSERT_EQ(1u, proc_->GetCaptureList().size());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
// Reresolve without the |is_speculative| flag should immediately return from
// cache.
ResolveHostResponseHelper response2(resolver_->CreateRequest(
HostPortPair("just.testing", 80), NetLogWithSource(), base::nullopt,
request_context_.get(), host_cache_.get()));
EXPECT_THAT(response2.result_error(), IsOk());
EXPECT_THAT(response2.request()->GetAddressResults().value().endpoints(),
testing::ElementsAre(CreateExpected("192.168.1.42", 80)));
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
EXPECT_EQ(1u, proc_->GetCaptureList().size()); // No increase.
}
#if BUILDFLAG(ENABLE_MDNS)
const uint8_t kMdnsResponseA[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No questions (for simplicity)
0x00, 0x01, // 1 RR (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// "myhello.local."
0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', 0x05, 'l', 'o', 'c', 'a', 'l',
0x00,
0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, 0x00, 0x10, // TTL is 16 (seconds)
0x00, 0x04, // RDLENGTH is 4 bytes.
0x01, 0x02, 0x03, 0x04, // 1.2.3.4
};
const uint8_t kMdnsResponseA2[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No questions (for simplicity)
0x00, 0x01, // 1 RR (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// "myhello.local."
0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', 0x05, 'l', 'o', 'c', 'a', 'l',
0x00,
0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, 0x00, 0x10, // TTL is 16 (seconds)
0x00, 0x04, // RDLENGTH is 4 bytes.
0x05, 0x06, 0x07, 0x08, // 5.6.7.8
};
const uint8_t kMdnsResponseA2Goodbye[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No questions (for simplicity)
0x00, 0x01, // 1 RR (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// "myhello.local."
0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', 0x05, 'l', 'o', 'c', 'a', 'l',
0x00,
0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, 0x00, 0x00, // TTL is 0 (signaling "goodbye" removal of result)
0x00, 0x04, // RDLENGTH is 4 bytes.
0x05, 0x06, 0x07, 0x08, // 5.6.7.8
};
const uint8_t kMdnsResponseAAAA[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No questions (for simplicity)
0x00, 0x01, // 1 RR (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// "myhello.local."
0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', 0x05, 'l', 'o', 'c', 'a', 'l',
0x00,
0x00, 0x1C, // TYPE is AAAA.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, 0x00, 0x10, // TTL is 16 (seconds)
0x00, 0x10, // RDLENGTH is 16 bytes.
// 000a:0000:0000:0000:0001:0002:0003:0004
0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,
0x00, 0x03, 0x00, 0x04};
// An MDNS response indicating that the responder owns the hostname, but the
// specific requested type (AAAA) does not exist because the responder only has
// A addresses.
const uint8_t kMdnsResponseNsec[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x81, 0x80, // Standard query response, RA, no error
0x00, 0x00, // No questions (for simplicity)
0x00, 0x01, // 1 RR (answers)
0x00, 0x00, // 0 authority RRs
0x00, 0x00, // 0 additional RRs
// "myhello.local."
0x07, 'm', 'y', 'h', 'e', 'l', 'l', 'o', 0x05, 'l', 'o', 'c', 'a', 'l',
0x00,
0x00, 0x2f, // TYPE is NSEC.
0x00, 0x01, // CLASS is IN.
0x00, 0x00, 0x00, 0x10, // TTL is 16 (seconds)
0x00, 0x06, // RDLENGTH is 6 bytes.
0xc0, 0x0c, // Next Domain Name (always pointer back to name in MDNS)
0x00, // Bitmap block number (always 0 in MDNS)
0x02, // Bitmap length is 2
0x00, 0x08