This source file includes following definitions.
- DefaultLimits
- DefaultParams
- slots_available_
- WaitFor
- SignalMultiple
- SignalAll
- AddRule
- AddRule
- AddRuleForAllFamilies
- Resolve
- GetCaptureList
- HasBlockedRequests
- AddressListContains
- handle_
- Resolve
- ResolveFromCache
- Cancel
- info
- index
- list
- result
- completed
- pending
- HasAddress
- NumberOfAddresses
- HasOneAddress
- WaitForResult
- OnComplete
- all_done_
- WaitForAllAttemptsToFinish
- WaitForAnAttemptToComplete
- total_attempts_resolved
- resolved_attempt_number
- Resolve
- CreateResolver
- CreateSerialResolver
- SetUp
- TearDown
- CreateResolverWithLimitsAndParams
- CreateRequest
- CreateRequest
- CreateRequest
- CreateRequest
- CreateRequest
- set_handler
- num_running_dispatcher_jobs
- set_fallback_to_proctask
- maximum_dns_failures
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- CreateValidDnsConfig
- SetUp
- CreateResolverWithLimitsAndParams
- AddDnsRule
- ChangeDnsConfig
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "net/dns/host_resolver_impl.h"
#include <algorithm>
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.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/test/test_timeouts.h"
#include "base/time/time.h"
#include "net/base/address_list.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/host_cache.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const size_t kMaxJobs = 10u;
const size_t kMaxRetryAttempts = 4u;
PrioritizedDispatcher::Limits DefaultLimits() {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, kMaxJobs);
return limits;
}
HostResolverImpl::ProcTaskParams DefaultParams(
HostResolverProc* resolver_proc) {
return HostResolverImpl::ProcTaskParams(resolver_proc, kMaxRetryAttempts);
}
class MockHostResolverProc : public HostResolverProc {
public:
struct ResolveKey {
ResolveKey(const std::string& hostname, AddressFamily address_family)
: hostname(hostname), address_family(address_family) {}
bool operator<(const ResolveKey& other) const {
return address_family < other.address_family ||
(address_family == other.address_family && hostname < other.hostname);
}
std::string hostname;
AddressFamily address_family;
};
typedef std::vector<ResolveKey> CaptureList;
MockHostResolverProc()
: HostResolverProc(NULL),
num_requests_waiting_(0),
num_slots_available_(0),
requests_waiting_(&lock_),
slots_available_(&lock_) {
}
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;
}
void SignalMultiple(unsigned count) {
base::AutoLock lock(lock_);
num_slots_available_ += count;
slots_available_.Broadcast();
}
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) {
base::AutoLock lock(lock_);
rules_[ResolveKey(hostname, family)] = result;
}
void AddRule(const std::string& hostname, AddressFamily family,
const std::string& ip_list) {
AddressList result;
int rv = ParseAddressList(ip_list, std::string(), &result);
DCHECK_EQ(OK, rv);
AddRule(hostname, family, result);
}
void AddRuleForAllFamilies(const std::string& hostname,
const std::string& ip_list) {
AddressList result;
int rv = ParseAddressList(ip_list, std::string(), &result);
DCHECK_EQ(OK, rv);
AddRule(hostname, ADDRESS_FAMILY_UNSPECIFIED, result);
AddRule(hostname, ADDRESS_FAMILY_IPV4, result);
AddRule(hostname, ADDRESS_FAMILY_IPV6, result);
}
virtual 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));
++num_requests_waiting_;
requests_waiting_.Broadcast();
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;
}
ResolveKey key(hostname, address_family);
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:
virtual ~MockHostResolverProc() {}
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);
};
bool AddressListContains(const AddressList& list, const std::string& address,
int port) {
IPAddressNumber ip;
bool rv = ParseIPLiteralToNumber(address, &ip);
DCHECK(rv);
return std::find(list.begin(),
list.end(),
IPEndPoint(ip, port)) != list.end();
}
class Request {
public:
struct Handler {
virtual ~Handler() {}
virtual void Handle(Request* request) = 0;
};
Request(const HostResolver::RequestInfo& info,
RequestPriority priority,
size_t index,
HostResolver* resolver,
Handler* handler)
: info_(info),
priority_(priority),
index_(index),
resolver_(resolver),
handler_(handler),
quit_on_complete_(false),
result_(ERR_UNEXPECTED),
handle_(NULL) {}
int Resolve() {
DCHECK(resolver_);
DCHECK(!handle_);
list_ = AddressList();
result_ = resolver_->Resolve(
info_,
priority_,
&list_,
base::Bind(&Request::OnComplete, base::Unretained(this)),
&handle_,
BoundNetLog());
if (!list_.empty())
EXPECT_EQ(OK, result_);
return result_;
}
int ResolveFromCache() {
DCHECK(resolver_);
DCHECK(!handle_);
return resolver_->ResolveFromCache(info_, &list_, BoundNetLog());
}
void Cancel() {
DCHECK(resolver_);
DCHECK(handle_);
resolver_->CancelRequest(handle_);
handle_ = NULL;
}
const HostResolver::RequestInfo& info() const { return info_; }
size_t index() const { return index_; }
const AddressList& list() const { return list_; }
int result() const { return result_; }
bool completed() const { return result_ != ERR_IO_PENDING; }
bool pending() const { return handle_ != NULL; }
bool HasAddress(const std::string& address, int port) const {
return AddressListContains(list_, address, port);
}
unsigned NumberOfAddresses() const {
return list_.size();
}
bool HasOneAddress(const std::string& address, int port) const {
return HasAddress(address, port) && (NumberOfAddresses() == 1u);
}
int WaitForResult() {
if (completed())
return result_;
base::CancelableClosure closure(base::MessageLoop::QuitClosure());
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE, closure.callback(), TestTimeouts::action_max_timeout());
quit_on_complete_ = true;
base::MessageLoop::current()->Run();
bool did_quit = !quit_on_complete_;
quit_on_complete_ = false;
closure.Cancel();
if (did_quit)
return result_;
else
return ERR_UNEXPECTED;
}
private:
void OnComplete(int rv) {
EXPECT_TRUE(pending());
EXPECT_EQ(ERR_IO_PENDING, result_);
EXPECT_NE(ERR_IO_PENDING, rv);
result_ = rv;
handle_ = NULL;
if (!list_.empty()) {
EXPECT_EQ(OK, result_);
EXPECT_EQ(info_.port(), list_.front().port());
}
if (handler_)
handler_->Handle(this);
if (quit_on_complete_) {
base::MessageLoop::current()->Quit();
quit_on_complete_ = false;
}
}
HostResolver::RequestInfo info_;
RequestPriority priority_;
size_t index_;
HostResolver* resolver_;
Handler* handler_;
bool quit_on_complete_;
AddressList list_;
int result_;
HostResolver::RequestHandle handle_;
DISALLOW_COPY_AND_ASSIGN(Request);
};
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),
all_done_(&lock_) {
}
void WaitForAllAttemptsToFinish(const base::TimeDelta& wait_time) {
base::TimeTicks end_time = base::TimeTicks::Now() + wait_time;
{
base::AutoLock auto_lock(lock_);
while (total_attempts_resolved_ != total_attempts_ &&
base::TimeTicks::Now() < end_time) {
all_done_.TimedWait(end_time - base::TimeTicks::Now());
}
}
}
void WaitForAnAttemptToComplete() {
base::TimeDelta wait_time = base::TimeDelta::FromSeconds(60);
base::TimeTicks end_time = base::TimeTicks::Now() + wait_time;
{
base::AutoLock auto_lock(lock_);
while (resolved_attempt_number_ == 0 && base::TimeTicks::Now() < end_time)
all_done_.TimedWait(end_time - base::TimeTicks::Now());
}
all_done_.Broadcast();
}
int total_attempts_resolved() { return total_attempts_resolved_; }
int resolved_attempt_number() { return resolved_attempt_number_; }
virtual 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_;
if (current_attempt_number_ == attempt_number_to_resolve_) {
resolved_attempt_number_ = current_attempt_number_;
wait_for_right_attempt_to_complete = false;
}
}
if (wait_for_right_attempt_to_complete)
WaitForAnAttemptToComplete();
int result = ResolveUsingPrevious(host, address_family, host_resolver_flags,
addrlist, os_error);
{
base::AutoLock auto_lock(lock_);
++total_attempts_resolved_;
}
all_done_.Broadcast();
if (result == OK)
return -1 - resolved_attempt_number_;
else
return result;
}
protected:
virtual ~LookupAttemptHostResolverProc() {}
private:
int attempt_number_to_resolve_;
int current_attempt_number_;
int total_attempts_;
int total_attempts_resolved_;
int resolved_attempt_number_;
base::Lock lock_;
base::ConditionVariable all_done_;
};
}
class HostResolverImplTest : public testing::Test {
public:
static const int kDefaultPort = 80;
HostResolverImplTest() : proc_(new MockHostResolverProc()) {}
void CreateResolver() {
CreateResolverWithLimitsAndParams(DefaultLimits(),
DefaultParams(proc_.get()));
}
void CreateSerialResolver() {
HostResolverImpl::ProcTaskParams params = DefaultParams(proc_.get());
params.max_retry_attempts = 0u;
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
CreateResolverWithLimitsAndParams(limits, params);
}
protected:
struct Handler : public Request::Handler {
virtual ~Handler() {}
Request* CreateRequest(const HostResolver::RequestInfo& info,
RequestPriority priority) {
return test->CreateRequest(info, priority);
}
Request* CreateRequest(const std::string& hostname, int port) {
return test->CreateRequest(hostname, port);
}
Request* CreateRequest(const std::string& hostname) {
return test->CreateRequest(hostname);
}
ScopedVector<Request>& requests() { return test->requests_; }
void DeleteResolver() { test->resolver_.reset(); }
HostResolverImplTest* test;
};
virtual void SetUp() OVERRIDE {
CreateResolver();
}
virtual void TearDown() OVERRIDE {
if (resolver_.get())
EXPECT_EQ(0u, resolver_->num_running_dispatcher_jobs_for_tests());
EXPECT_FALSE(proc_->HasBlockedRequests());
}
virtual void CreateResolverWithLimitsAndParams(
const PrioritizedDispatcher::Limits& limits,
const HostResolverImpl::ProcTaskParams& params) {
resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
limits, params, NULL));
}
Request* CreateRequest(const HostResolver::RequestInfo& info,
RequestPriority priority) {
Request* req = new Request(
info, priority, requests_.size(), resolver_.get(), handler_.get());
requests_.push_back(req);
return req;
}
Request* CreateRequest(const std::string& hostname,
int port,
RequestPriority priority,
AddressFamily family) {
HostResolver::RequestInfo info(HostPortPair(hostname, port));
info.set_address_family(family);
return CreateRequest(info, priority);
}
Request* CreateRequest(const std::string& hostname,
int port,
RequestPriority priority) {
return CreateRequest(hostname, port, priority, ADDRESS_FAMILY_UNSPECIFIED);
}
Request* CreateRequest(const std::string& hostname, int port) {
return CreateRequest(hostname, port, MEDIUM);
}
Request* CreateRequest(const std::string& hostname) {
return CreateRequest(hostname, kDefaultPort);
}
void set_handler(Handler* handler) {
handler_.reset(handler);
handler_->test = this;
}
size_t num_running_dispatcher_jobs() const {
DCHECK(resolver_.get());
return resolver_->num_running_dispatcher_jobs_for_tests();
}
void set_fallback_to_proctask(bool fallback_to_proctask) {
DCHECK(resolver_.get());
resolver_->fallback_to_proctask_ = fallback_to_proctask;
}
static unsigned maximum_dns_failures() {
return HostResolverImpl::kMaximumDnsFailures;
}
scoped_refptr<MockHostResolverProc> proc_;
scoped_ptr<HostResolverImpl> resolver_;
ScopedVector<Request> requests_;
scoped_ptr<Handler> handler_;
};
TEST_F(HostResolverImplTest, AsynchronousLookup) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
Request* req = CreateRequest("just.testing", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
EXPECT_TRUE(req->HasOneAddress("192.168.1.42", 80));
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
}
TEST_F(HostResolverImplTest, EmptyListMeansNameNotResolved) {
proc_->AddRuleForAllFamilies("just.testing", "");
proc_->SignalMultiple(1u);
Request* req = CreateRequest("just.testing", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult());
EXPECT_EQ(0u, req->NumberOfAddresses());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
}
TEST_F(HostResolverImplTest, FailedAsynchronousLookup) {
proc_->AddRuleForAllFamilies(std::string(),
"0.0.0.0");
proc_->SignalMultiple(1u);
Request* req = CreateRequest("just.testing", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult());
EXPECT_EQ("just.testing", proc_->GetCaptureList()[0].hostname);
EXPECT_EQ(ERR_DNS_CACHE_MISS, req->ResolveFromCache());
}
TEST_F(HostResolverImplTest, AbortedAsynchronousLookup) {
Request* req0 = CreateRequest("just.testing", 80);
EXPECT_EQ(ERR_IO_PENDING, req0->Resolve());
EXPECT_TRUE(proc_->WaitFor(1u));
resolver_.reset();
proc_->SignalAll();
CreateResolver();
Request* req1 = CreateRequest("just.testing", 80);
EXPECT_EQ(ERR_IO_PENDING, req1->Resolve());
proc_->SignalMultiple(2u);
EXPECT_EQ(OK, req1->WaitForResult());
EXPECT_FALSE(req0->completed());
}
#if defined(THREAD_SANITIZER)
#define MAYBE_NumericIPv4Address DISABLED_NumericIPv4Address
#else
#define MAYBE_NumericIPv4Address NumericIPv4Address
#endif
TEST_F(HostResolverImplTest, MAYBE_NumericIPv4Address) {
Request* req = CreateRequest("127.1.2.3", 5555);
EXPECT_EQ(OK, req->Resolve());
EXPECT_TRUE(req->HasOneAddress("127.1.2.3", 5555));
}
#if defined(THREAD_SANITIZER)
#define MAYBE_NumericIPv6Address DISABLED_NumericIPv6Address
#else
#define MAYBE_NumericIPv6Address NumericIPv6Address
#endif
TEST_F(HostResolverImplTest, MAYBE_NumericIPv6Address) {
Request* req = CreateRequest("2001:db8::1", 5555);
EXPECT_EQ(OK, req->Resolve());
EXPECT_TRUE(req->HasOneAddress("2001:db8::1", 5555));
}
#if defined(THREAD_SANITIZER)
#define MAYBE_EmptyHost DISABLED_EmptyHost
#else
#define MAYBE_EmptyHost EmptyHost
#endif
TEST_F(HostResolverImplTest, MAYBE_EmptyHost) {
Request* req = CreateRequest(std::string(), 5555);
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve());
}
#if defined(THREAD_SANITIZER)
#define MAYBE_EmptyDotsHost DISABLED_EmptyDotsHost
#else
#define MAYBE_EmptyDotsHost EmptyDotsHost
#endif
TEST_F(HostResolverImplTest, MAYBE_EmptyDotsHost) {
for (int i = 0; i < 16; ++i) {
Request* req = CreateRequest(std::string(i, '.'), 5555);
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve());
}
}
#if defined(THREAD_SANITIZER)
#define MAYBE_LongHost DISABLED_LongHost
#else
#define MAYBE_LongHost LongHost
#endif
TEST_F(HostResolverImplTest, MAYBE_LongHost) {
Request* req = CreateRequest(std::string(4097, 'a'), 5555);
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->Resolve());
}
TEST_F(HostResolverImplTest, DeDupeRequests) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 81)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 82)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve());
proc_->SignalMultiple(2u);
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
}
TEST_F(HostResolverImplTest, CancelMultipleRequests) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 81)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 82)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve());
requests_[0]->Cancel();
requests_[1]->Cancel();
requests_[2]->Cancel();
requests_[4]->Cancel();
proc_->SignalMultiple(2u);
EXPECT_EQ(OK, requests_[3]->WaitForResult());
}
TEST_F(HostResolverImplTest, CanceledRequestsReleaseJobSlots) {
for (unsigned i = 0; i < kMaxJobs + 1; ++i) {
std::string hostname = "a_";
hostname[1] = 'a' + i;
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 81)->Resolve());
}
EXPECT_TRUE(proc_->WaitFor(kMaxJobs));
for (unsigned i = 0; i < requests_.size() - 2; ++i) {
requests_[i]->Cancel();
}
EXPECT_TRUE(proc_->WaitFor(kMaxJobs + 1));
proc_->SignalAll();
size_t num_requests = requests_.size();
EXPECT_EQ(OK, requests_[num_requests - 1]->WaitForResult());
EXPECT_EQ(OK, requests_[num_requests - 2]->result());
}
TEST_F(HostResolverImplTest, CancelWithinCallback) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
if (req->index() == 0) {
requests()[1]->Cancel();
requests()[2]->Cancel();
}
}
};
set_handler(new MyHandler());
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i;
}
proc_->SignalMultiple(2u);
EXPECT_EQ(OK, requests_[0]->WaitForResult());
Request* final_request = CreateRequest("finalrequest", 70);
EXPECT_EQ(ERR_IO_PENDING, final_request->Resolve());
EXPECT_EQ(OK, final_request->WaitForResult());
EXPECT_TRUE(requests_[3]->completed());
}
TEST_F(HostResolverImplTest, DeleteWithinCallback) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
EXPECT_EQ("a", req->info().hostname());
EXPECT_EQ(80, req->info().port());
DeleteResolver();
base::MessageLoop::current()->PostTask(FROM_HERE,
base::MessageLoop::QuitClosure());
}
};
set_handler(new MyHandler());
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i;
}
proc_->SignalMultiple(1u);
base::MessageLoop::current()->Run();
}
TEST_F(HostResolverImplTest, DeleteWithinAbortedCallback) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
EXPECT_EQ("a", req->info().hostname());
EXPECT_EQ(80, req->info().port());
DeleteResolver();
base::MessageLoop::current()->PostTask(FROM_HERE,
base::MessageLoop::QuitClosure());
}
};
set_handler(new MyHandler());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 81)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 82)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b", 83)->Resolve());
EXPECT_TRUE(proc_->WaitFor(1u));
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->Run();
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->result());
EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result());
EXPECT_EQ(ERR_IO_PENDING, requests_[2]->result());
EXPECT_EQ(ERR_IO_PENDING, requests_[3]->result());
proc_->SignalMultiple(requests_.size());
}
TEST_F(HostResolverImplTest, StartWithinCallback) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
if (req->index() == 0) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 70)->Resolve());
}
}
};
set_handler(new MyHandler());
resolver_.reset(new HostResolverImpl(scoped_ptr<HostCache>(),
DefaultLimits(),
DefaultParams(proc_.get()),
NULL));
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80 + i)->Resolve()) << i;
}
proc_->SignalMultiple(2u);
EXPECT_EQ(OK, requests_[0]->WaitForResult());
ASSERT_EQ(5u, requests_.size());
EXPECT_EQ(OK, requests_.back()->WaitForResult());
EXPECT_EQ(2u, proc_->GetCaptureList().size());
}
TEST_F(HostResolverImplTest, BypassCache) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
if (req->index() == 0) {
std::string hostname = req->info().hostname();
EXPECT_EQ(OK, CreateRequest(hostname, 70)->Resolve());
EXPECT_EQ(OK, CreateRequest(hostname, 75)->ResolveFromCache());
HostResolver::RequestInfo info(HostPortPair(hostname, 71));
info.set_allow_cached_response(false);
EXPECT_EQ(ERR_IO_PENDING,
CreateRequest(info, DEFAULT_PRIORITY)->Resolve());
} else if (71 == req->info().port()) {
base::MessageLoop::current()->Quit();
} else {
FAIL() << "Unexpected request";
}
}
};
set_handler(new MyHandler());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a", 80)->Resolve());
proc_->SignalMultiple(3u);
base::MessageLoop::current()->Run();
EXPECT_EQ(2u, proc_->GetCaptureList().size());
}
TEST_F(HostResolverImplTest, FlushCacheOnIPAddressChange) {
proc_->SignalMultiple(2u);
Request* req = CreateRequest("host1", 70);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
req = CreateRequest("host1", 75);
EXPECT_EQ(OK, req->Resolve());
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
req = CreateRequest("host1", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
}
TEST_F(HostResolverImplTest, AbortOnIPAddressChanged) {
Request* req = CreateRequest("host1", 70);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_TRUE(proc_->WaitFor(1u));
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
proc_->SignalAll();
EXPECT_EQ(ERR_NETWORK_CHANGED, req->WaitForResult());
EXPECT_EQ(0u, resolver_->GetHostCache()->size());
}
TEST_F(HostResolverImplTest, ObeyPoolConstraintsAfterIPAddressChange) {
CreateSerialResolver();
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("a")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("b")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("c")->Resolve());
EXPECT_TRUE(proc_->WaitFor(1u));
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
proc_->SignalMultiple(3u);
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult());
EXPECT_EQ(1u, num_running_dispatcher_jobs());
EXPECT_FALSE(requests_[1]->completed());
EXPECT_FALSE(requests_[2]->completed());
EXPECT_EQ(OK, requests_[2]->WaitForResult());
EXPECT_EQ(OK, requests_[1]->result());
}
TEST_F(HostResolverImplTest, AbortOnlyExistingRequestsOnIPAddressChange) {
struct MyHandler : public Handler {
virtual void Handle(Request* req) OVERRIDE {
std::string hostname;
if (req->index() == 0)
hostname = "zzz";
else if (req->index() == 1)
hostname = "aaa";
else if (req->index() == 2)
hostname = "eee";
else
return;
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname)->Resolve()) << hostname;
}
};
set_handler(new MyHandler());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("bbb")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("eee")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ccc")->Resolve());
EXPECT_TRUE(proc_->WaitFor(3u));
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->result());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[1]->result());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->result());
ASSERT_EQ(6u, requests_.size());
proc_->SignalMultiple(requests_.size());
EXPECT_EQ(OK, requests_[3]->WaitForResult());
EXPECT_EQ(OK, requests_[4]->WaitForResult());
EXPECT_EQ(OK, requests_[5]->WaitForResult());
EXPECT_EQ(6u, proc_->GetCaptureList().size());
EXPECT_EQ(3u, resolver_->GetHostCache()->size());
}
TEST_F(HostResolverImplTest, HigherPriorityRequestsStartedFirst) {
CreateSerialResolver();
CreateRequest("req0", 80, LOW);
CreateRequest("req1", 80, MEDIUM);
CreateRequest("req2", 80, MEDIUM);
CreateRequest("req3", 80, LOW);
CreateRequest("req4", 80, HIGHEST);
CreateRequest("req5", 80, LOW);
CreateRequest("req6", 80, LOW);
CreateRequest("req5", 80, HIGHEST);
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
}
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
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_F(HostResolverImplTest, CancelPendingRequest) {
CreateSerialResolver();
CreateRequest("req0", 80, LOWEST);
CreateRequest("req1", 80, HIGHEST);
CreateRequest("req2", 80, MEDIUM);
CreateRequest("req3", 80, LOW);
CreateRequest("req4", 80, HIGHEST);
CreateRequest("req5", 80, LOWEST);
CreateRequest("req6", 80, MEDIUM);
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
}
requests_[1]->Cancel();
requests_[4]->Cancel();
requests_[5]->Cancel();
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < requests_.size(); ++i) {
if (!requests_[i]->pending())
continue;
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
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_F(HostResolverImplTest, QueueOverflow) {
CreateSerialResolver();
const size_t kMaxPendingJobs = 3u;
resolver_->SetMaxQueuedJobs(kMaxPendingJobs);
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req0", 80, LOWEST)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req1", 80, HIGHEST)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req2", 80, MEDIUM)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req3", 80, MEDIUM)->Resolve());
EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE,
CreateRequest("req4", 80, LOW)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req5", 80, MEDIUM)->Resolve());
EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[2]->result());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req6", 80, HIGHEST)->Resolve());
EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[3]->result());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("req7", 80, MEDIUM)->Resolve());
EXPECT_EQ(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE, requests_[5]->result());
proc_->SignalMultiple(4u);
EXPECT_EQ(OK, requests_[7]->WaitForResult());
EXPECT_EQ(OK, requests_[0]->result());
EXPECT_EQ(OK, requests_[1]->result());
EXPECT_EQ(OK, requests_[6]->result());
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);
EXPECT_EQ(4u, resolver_->GetHostCache()->size());
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_TRUE(requests_[i]->completed()) << i;
}
}
TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv4) {
CreateSerialResolver();
proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1");
proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2");
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
}
proc_->SignalMultiple(requests_.size());
for (size_t i = 0u; i < requests_.size(); ++i) {
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(2u, capture_list.size());
EXPECT_EQ("h1", capture_list[0].hostname);
EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[0].address_family);
EXPECT_EQ("h1", capture_list[1].hostname);
EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[1].address_family);
EXPECT_TRUE(requests_[0]->HasOneAddress("1.0.0.1", 80));
EXPECT_TRUE(requests_[1]->HasOneAddress("1.0.0.1", 80));
EXPECT_TRUE(requests_[2]->HasOneAddress("::2", 80));
}
TEST_F(HostResolverImplTest, SetDefaultAddressFamily_IPv6) {
CreateSerialResolver();
proc_->AddRule("h1", ADDRESS_FAMILY_IPV4, "1.0.0.1");
proc_->AddRule("h1", ADDRESS_FAMILY_IPV6, "::2");
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV6);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_UNSPECIFIED);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
CreateRequest("h1", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
for (size_t i = 0; i < requests_.size(); ++i) {
EXPECT_EQ(ERR_IO_PENDING, requests_[i]->Resolve()) << i;
}
proc_->SignalMultiple(requests_.size());
for (size_t i = 0u; i < requests_.size(); ++i) {
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
MockHostResolverProc::CaptureList capture_list = proc_->GetCaptureList();
ASSERT_EQ(2u, capture_list.size());
EXPECT_EQ("h1", capture_list[0].hostname);
EXPECT_EQ(ADDRESS_FAMILY_IPV6, capture_list[0].address_family);
EXPECT_EQ("h1", capture_list[1].hostname);
EXPECT_EQ(ADDRESS_FAMILY_IPV4, capture_list[1].address_family);
EXPECT_TRUE(requests_[0]->HasOneAddress("::2", 80));
EXPECT_TRUE(requests_[1]->HasOneAddress("::2", 80));
EXPECT_TRUE(requests_[2]->HasOneAddress("1.0.0.1", 80));
}
TEST_F(HostResolverImplTest, ResolveFromCache) {
proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42");
proc_->SignalMultiple(1u);
HostResolver::RequestInfo info(HostPortPair("just.testing", 80));
EXPECT_EQ(ERR_DNS_CACHE_MISS,
CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, DEFAULT_PRIORITY)->Resolve());
EXPECT_EQ(OK, requests_[1]->WaitForResult());
EXPECT_EQ(OK, CreateRequest(info, DEFAULT_PRIORITY)->ResolveFromCache());
EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.1.42", 80));
}
TEST_F(HostResolverImplTest, MultipleAttempts) {
int kAttemptNumberToResolve = 3;
int kTotalAttempts = 3;
scoped_refptr<LookupAttemptHostResolverProc> resolver_proc(
new LookupAttemptHostResolverProc(
NULL, kAttemptNumberToResolve, kTotalAttempts));
HostResolverImpl::ProcTaskParams params = DefaultParams(resolver_proc.get());
params.unresponsive_delay = base::TimeDelta::FromMilliseconds(500);
resolver_.reset(
new HostResolverImpl(HostCache::CreateDefaultCache(),
DefaultLimits(),
params,
NULL));
HostResolver::RequestInfo info(HostPortPair("host1", 70));
Request* req = CreateRequest(info, DEFAULT_PRIORITY);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(-4, req->WaitForResult());
resolver_proc->WaitForAllAttemptsToFinish(
base::TimeDelta::FromMilliseconds(60000));
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(resolver_proc->total_attempts_resolved(), kTotalAttempts);
EXPECT_EQ(resolver_proc->resolved_attempt_number(), kAttemptNumberToResolve);
}
DnsConfig CreateValidDnsConfig() {
IPAddressNumber dns_ip;
bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip);
EXPECT_TRUE(rv);
DnsConfig config;
config.nameservers.push_back(IPEndPoint(dns_ip, dns_protocol::kDefaultPort));
EXPECT_TRUE(config.IsValid());
return config;
}
class HostResolverImplDnsTest : public HostResolverImplTest {
public:
HostResolverImplDnsTest() : dns_client_(NULL) {}
protected:
virtual void SetUp() OVERRIDE {
AddDnsRule("nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, false);
AddDnsRule("nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false);
AddDnsRule("ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
AddDnsRule("ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false);
AddDnsRule("4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
AddDnsRule("4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY, false);
AddDnsRule("6ok", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false);
AddDnsRule("6ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK, false);
AddDnsRule("4nx", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
AddDnsRule("4nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL, false);
AddDnsRule("empty", dns_protocol::kTypeA, MockDnsClientRule::EMPTY, false);
AddDnsRule("empty", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY,
false);
AddDnsRule("slow_nx", dns_protocol::kTypeA, MockDnsClientRule::FAIL, true);
AddDnsRule("slow_nx", dns_protocol::kTypeAAAA, MockDnsClientRule::FAIL,
true);
AddDnsRule("4slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true);
AddDnsRule("4slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
false);
AddDnsRule("6slow_ok", dns_protocol::kTypeA, MockDnsClientRule::OK, false);
AddDnsRule("6slow_ok", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
true);
AddDnsRule("4slow_4ok", dns_protocol::kTypeA, MockDnsClientRule::OK, true);
AddDnsRule("4slow_4ok", dns_protocol::kTypeAAAA, MockDnsClientRule::EMPTY,
false);
AddDnsRule("4slow_4timeout", dns_protocol::kTypeA,
MockDnsClientRule::TIMEOUT, true);
AddDnsRule("4slow_4timeout", dns_protocol::kTypeAAAA, MockDnsClientRule::OK,
false);
AddDnsRule("4slow_6timeout", dns_protocol::kTypeA,
MockDnsClientRule::OK, true);
AddDnsRule("4slow_6timeout", dns_protocol::kTypeAAAA,
MockDnsClientRule::TIMEOUT, false);
CreateResolver();
}
virtual void CreateResolverWithLimitsAndParams(
const PrioritizedDispatcher::Limits& limits,
const HostResolverImpl::ProcTaskParams& params) OVERRIDE {
resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
limits,
params,
NULL));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
dns_client_ = new MockDnsClient(DnsConfig(), dns_rules_);
resolver_->SetDnsClient(scoped_ptr<DnsClient>(dns_client_));
}
void AddDnsRule(const std::string& prefix,
uint16 qtype,
MockDnsClientRule::Result result,
bool delay) {
dns_rules_.push_back(MockDnsClientRule(prefix, qtype, result, delay));
}
void ChangeDnsConfig(const DnsConfig& config) {
NetworkChangeNotifier::SetDnsConfig(config);
base::MessageLoop::current()->RunUntilIdle();
}
MockDnsClientRuleList dns_rules_;
MockDnsClient* dns_client_;
};
TEST_F(HostResolverImplDnsTest, DnsTask) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult());
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
for (size_t i = 1; i < requests_.size(); ++i)
EXPECT_NE(ERR_UNEXPECTED, requests_[i]->WaitForResult()) << i;
EXPECT_EQ(OK, requests_[1]->result());
EXPECT_TRUE(requests_[1]->HasOneAddress("127.0.0.1", 80));
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[2]->result());
EXPECT_EQ(OK, requests_[3]->result());
EXPECT_TRUE(requests_[3]->HasOneAddress("192.168.1.102", 80));
}
TEST_F(HostResolverImplDnsTest, NoFallbackToProcTask) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
set_fallback_to_proctask(false);
proc_->AddRuleForAllFamilies("nx_succeed", "192.168.1.102");
ChangeDnsConfig(DnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_succeed", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult());
EXPECT_EQ(OK, requests_[1]->WaitForResult());
EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.1.102", 80));
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_abort", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
resolver_->SetDnsClient(
scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok_fail", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_fail", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[2]->WaitForResult());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[3]->WaitForResult());
EXPECT_EQ(OK, requests_[4]->WaitForResult());
EXPECT_TRUE(requests_[4]->HasOneAddress("127.0.0.1", 80));
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[5]->WaitForResult());
}
TEST_F(HostResolverImplDnsTest, OnDnsTaskFailureAbortedJob) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
CreateResolver();
proc_->SignalMultiple(requests_.size());
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_IO_PENDING, requests_[0]->result());
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
set_fallback_to_proctask(false);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("nx_abort", 80)->Resolve());
CreateResolver();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(ERR_IO_PENDING, requests_[1]->result());
}
TEST_F(HostResolverImplDnsTest, DnsTaskUnspec) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies("4nx", "192.168.1.101");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4ok", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6ok", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4nx", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < requests_.size(); ++i)
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
EXPECT_EQ(1u, requests_[1]->NumberOfAddresses());
EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
EXPECT_EQ(1u, requests_[2]->NumberOfAddresses());
EXPECT_TRUE(requests_[2]->HasAddress("::1", 80));
EXPECT_EQ(1u, requests_[3]->NumberOfAddresses());
EXPECT_TRUE(requests_[3]->HasAddress("192.168.1.101", 80));
}
TEST_F(HostResolverImplDnsTest, ServeFromHosts) {
DnsConfig config = CreateValidDnsConfig();
ChangeDnsConfig(config);
proc_->AddRuleForAllFamilies(std::string(),
std::string());
proc_->SignalMultiple(1u);
Request* req0 = CreateRequest("nx_ipv4", 80);
EXPECT_EQ(ERR_IO_PENDING, req0->Resolve());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req0->WaitForResult());
IPAddressNumber local_ipv4, local_ipv6;
ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4));
ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6));
DnsHosts hosts;
hosts[DnsHostsKey("nx_ipv4", ADDRESS_FAMILY_IPV4)] = local_ipv4;
hosts[DnsHostsKey("nx_ipv6", ADDRESS_FAMILY_IPV6)] = local_ipv6;
hosts[DnsHostsKey("nx_both", ADDRESS_FAMILY_IPV4)] = local_ipv4;
hosts[DnsHostsKey("nx_both", ADDRESS_FAMILY_IPV6)] = local_ipv6;
config.hosts = hosts;
ChangeDnsConfig(config);
Request* req1 = CreateRequest("nx_ipv4", 80);
EXPECT_EQ(OK, req1->Resolve());
EXPECT_TRUE(req1->HasOneAddress("127.0.0.1", 80));
Request* req2 = CreateRequest("nx_ipv6", 80);
EXPECT_EQ(OK, req2->Resolve());
EXPECT_TRUE(req2->HasOneAddress("::1", 80));
Request* req3 = CreateRequest("nx_both", 80);
EXPECT_EQ(OK, req3->Resolve());
EXPECT_TRUE(req3->HasAddress("127.0.0.1", 80) &&
req3->HasAddress("::1", 80));
Request* req4 = CreateRequest("nx_ipv4", 80, MEDIUM, ADDRESS_FAMILY_IPV4);
EXPECT_EQ(OK, req4->Resolve());
EXPECT_TRUE(req4->HasOneAddress("127.0.0.1", 80));
Request* req5 = CreateRequest("nx_ipv6", 80, MEDIUM, ADDRESS_FAMILY_IPV6);
EXPECT_EQ(OK, req5->Resolve());
EXPECT_TRUE(req5->HasOneAddress("::1", 80));
Request* req6 = CreateRequest("nx_IPV4", 80);
EXPECT_EQ(OK, req6->Resolve());
EXPECT_TRUE(req6->HasOneAddress("127.0.0.1", 80));
}
TEST_F(HostResolverImplDnsTest, BypassDnsTask) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies(std::string(),
std::string());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok.local", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok.local.", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("oklocal", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("oklocal.", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < 2; ++i)
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[i]->WaitForResult()) << i;
for (size_t i = 2; i < requests_.size(); ++i)
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
}
TEST_F(HostResolverImplDnsTest, SystemOnlyBypassesDnsTask) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies(std::string(), std::string());
HostResolver::RequestInfo info_bypass(HostPortPair("ok", 80));
info_bypass.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info_bypass, MEDIUM)->Resolve());
HostResolver::RequestInfo info(HostPortPair("ok", 80));
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(info, MEDIUM)->Resolve());
proc_->SignalMultiple(requests_.size());
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, requests_[0]->WaitForResult());
EXPECT_EQ(OK, requests_[1]->WaitForResult());
}
TEST_F(HostResolverImplDnsTest, DisableDnsClientOnPersistentFailure) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies(std::string(),
std::string());
Request* req = CreateRequest("ok_1", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
for (unsigned i = 0; i < maximum_dns_failures(); ++i) {
std::string hostname = base::StringPrintf("nx_%u", i);
proc_->AddRuleForAllFamilies(hostname, "192.168.1.101");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve()) << i;
}
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < requests_.size(); ++i)
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
ASSERT_FALSE(proc_->HasBlockedRequests());
req = CreateRequest("ok_2", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
proc_->SignalMultiple(1u);
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req->WaitForResult());
ChangeDnsConfig(CreateValidDnsConfig());
req = CreateRequest("ok_3", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
}
TEST_F(HostResolverImplDnsTest, DontDisableDnsClientOnSporadicFailure) {
ChangeDnsConfig(CreateValidDnsConfig());
for (unsigned i = 0; i < 40; ++i) {
std::string hostname = (i % 2) == 0 ? base::StringPrintf("nx_%u", i)
: base::StringPrintf("ok_%u", i);
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(hostname, 80)->Resolve()) << i;
}
proc_->SignalMultiple(requests_.size());
for (size_t i = 0; i < requests_.size(); ++i)
EXPECT_EQ(OK, requests_[i]->WaitForResult()) << i;
proc_->AddRuleForAllFamilies(std::string(), std::string());
Request* req = CreateRequest("ok_last", 80);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
}
TEST_F(HostResolverImplDnsTest, DualFamilyLocalhost) {
scoped_refptr<HostResolverProc> proc(new SystemHostResolverProc());
resolver_.reset(new HostResolverImpl(HostCache::CreateDefaultCache(),
DefaultLimits(),
DefaultParams(proc.get()),
NULL));
resolver_->SetDnsClient(
scoped_ptr<DnsClient>(new MockDnsClient(DnsConfig(), dns_rules_)));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
AddressList addrlist;
int rv = proc->Resolve("localhost", ADDRESS_FAMILY_UNSPECIFIED, 0, &addrlist,
NULL);
if (rv != OK)
return;
for (unsigned i = 0; i < addrlist.size(); ++i)
LOG(WARNING) << addrlist[i].ToString();
bool saw_ipv4 = AddressListContains(addrlist, "127.0.0.1", 0);
bool saw_ipv6 = AddressListContains(addrlist, "::1", 0);
if (!saw_ipv4 && !saw_ipv6)
return;
HostResolver::RequestInfo info(HostPortPair("localhost", 80));
info.set_address_family(ADDRESS_FAMILY_UNSPECIFIED);
info.set_host_resolver_flags(HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6);
ChangeDnsConfig(DnsConfig());
Request* req = CreateRequest(info, DEFAULT_PRIORITY);
EXPECT_EQ(ERR_IO_PENDING, req->Resolve());
EXPECT_EQ(OK, req->WaitForResult());
EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80));
EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80));
DnsConfig config = CreateValidDnsConfig();
DnsHosts hosts;
IPAddressNumber local_ipv4, local_ipv6;
ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &local_ipv4));
ASSERT_TRUE(ParseIPLiteralToNumber("::1", &local_ipv6));
if (saw_ipv4)
hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV4)] = local_ipv4;
if (saw_ipv6)
hosts[DnsHostsKey("localhost", ADDRESS_FAMILY_IPV6)] = local_ipv6;
config.hosts = hosts;
ChangeDnsConfig(config);
req = CreateRequest(info, DEFAULT_PRIORITY);
EXPECT_EQ(OK, req->Resolve());
EXPECT_EQ(saw_ipv4, req->HasAddress("127.0.0.1", 80));
EXPECT_EQ(saw_ipv6, req->HasAddress("::1", 80));
}
TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActive) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_IPV4);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
EXPECT_EQ(1u, num_running_dispatcher_jobs());
requests_[0]->Cancel();
}
TEST_F(HostResolverImplDnsTest, CancelWithOneTransactionActiveOnePending) {
CreateSerialResolver();
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
EXPECT_EQ(1u, num_running_dispatcher_jobs());
requests_[0]->Cancel();
}
TEST_F(HostResolverImplDnsTest, CancelWithTwoTransactionsActive) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
EXPECT_EQ(2u, num_running_dispatcher_jobs());
requests_[0]->Cancel();
}
TEST_F(HostResolverImplDnsTest, DeleteWithActiveTransactions) {
CreateResolverWithLimitsAndParams(
PrioritizedDispatcher::Limits(NUM_PRIORITIES, 10u),
DefaultParams(proc_.get()));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
ADDRESS_FAMILY_IPV4)->Resolve());
for (int i = 0; i < 10; ++i) {
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(
base::StringPrintf("ok%i", i))->Resolve());
}
EXPECT_EQ(10u, num_running_dispatcher_jobs());
resolver_.reset();
}
TEST_F(HostResolverImplDnsTest, CancelWithIPv6TransactionActive) {
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("6slow_ok", 80)->Resolve());
EXPECT_EQ(2u, num_running_dispatcher_jobs());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, num_running_dispatcher_jobs());
requests_[0]->Cancel();
}
TEST_F(HostResolverImplDnsTest, CancelWithIPv4TransactionPending) {
set_fallback_to_proctask(false);
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
EXPECT_EQ(2u, num_running_dispatcher_jobs());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, num_running_dispatcher_jobs());
requests_[0]->Cancel();
}
TEST_F(HostResolverImplDnsTest, AAAACompletesFirst) {
set_fallback_to_proctask(false);
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_ok", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4ok", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_4timeout", 80)->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("4slow_6timeout", 80)->Resolve());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(requests_[0]->completed());
EXPECT_FALSE(requests_[1]->completed());
EXPECT_FALSE(requests_[2]->completed());
EXPECT_TRUE(requests_[3]->completed());
EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[3]->result());
EXPECT_EQ(3u, num_running_dispatcher_jobs());
dns_client_->CompleteDelayedTransactions();
EXPECT_TRUE(requests_[0]->completed());
EXPECT_EQ(OK, requests_[0]->result());
EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
EXPECT_TRUE(requests_[1]->completed());
EXPECT_EQ(OK, requests_[1]->result());
EXPECT_EQ(1u, requests_[1]->NumberOfAddresses());
EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
EXPECT_TRUE(requests_[2]->completed());
EXPECT_EQ(ERR_DNS_TIMED_OUT, requests_[2]->result());
}
TEST_F(HostResolverImplDnsTest, SerialResolver) {
CreateSerialResolver();
set_fallback_to_proctask(false);
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80)->Resolve());
EXPECT_EQ(1u, num_running_dispatcher_jobs());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(requests_[0]->completed());
EXPECT_EQ(OK, requests_[0]->result());
EXPECT_EQ(2u, requests_[0]->NumberOfAddresses());
EXPECT_TRUE(requests_[0]->HasAddress("127.0.0.1", 80));
EXPECT_TRUE(requests_[0]->HasAddress("::1", 80));
}
TEST_F(HostResolverImplDnsTest, AAAAStartsAfterOtherJobFinishes) {
CreateResolverWithLimitsAndParams(
PrioritizedDispatcher::Limits(NUM_PRIORITIES, 2),
DefaultParams(proc_.get()));
set_fallback_to_proctask(false);
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok", 80, MEDIUM,
ADDRESS_FAMILY_IPV4)->Resolve());
EXPECT_EQ(ERR_IO_PENDING,
CreateRequest("4slow_ok", 80, MEDIUM)->Resolve());
EXPECT_EQ(2u, num_running_dispatcher_jobs());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, num_running_dispatcher_jobs());
EXPECT_TRUE(requests_[0]->completed());
EXPECT_FALSE(requests_[1]->completed());
dns_client_->CompleteDelayedTransactions();
EXPECT_TRUE(requests_[1]->completed());
EXPECT_EQ(OK, requests_[1]->result());
EXPECT_EQ(2u, requests_[1]->NumberOfAddresses());
EXPECT_TRUE(requests_[1]->HasAddress("127.0.0.1", 80));
EXPECT_TRUE(requests_[1]->HasAddress("::1", 80));
}
TEST_F(HostResolverImplDnsTest, IPv4EmptyFallback) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies("empty_fallback", "192.168.0.1");
proc_->SignalMultiple(1u);
EXPECT_EQ(ERR_IO_PENDING,
CreateRequest("empty_fallback", 80, MEDIUM,
ADDRESS_FAMILY_IPV4)->Resolve());
EXPECT_EQ(OK, requests_[0]->WaitForResult());
EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80));
}
TEST_F(HostResolverImplDnsTest, UnspecEmptyFallback) {
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies("empty_fallback", "192.168.0.1");
proc_->SignalMultiple(1u);
EXPECT_EQ(ERR_IO_PENDING,
CreateRequest("empty_fallback", 80, MEDIUM,
ADDRESS_FAMILY_UNSPECIFIED)->Resolve());
EXPECT_EQ(OK, requests_[0]->WaitForResult());
EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80));
}
TEST_F(HostResolverImplDnsTest, InvalidDnsConfigWithPendingRequests) {
CreateResolverWithLimitsAndParams(
PrioritizedDispatcher::Limits(NUM_PRIORITIES, 3u),
DefaultParams(proc_.get()));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies("slow_nx1", "192.168.0.1");
proc_->AddRuleForAllFamilies("slow_nx2", "192.168.0.2");
proc_->AddRuleForAllFamilies("ok", "192.168.0.3");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx1")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_nx2")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok")->Resolve());
EXPECT_EQ(3u, num_running_dispatcher_jobs());
ChangeDnsConfig(DnsConfig());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[0]->WaitForResult());
EXPECT_EQ(ERR_NETWORK_CHANGED, requests_[1]->WaitForResult());
EXPECT_FALSE(requests_[2]->completed());
proc_->SignalMultiple(1u);
EXPECT_EQ(OK, requests_[2]->WaitForResult());
EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80));
}
TEST_F(HostResolverImplDnsTest,
AutomaticallyDisableDnsClientWithPendingRequests) {
for (size_t limit = 1u; limit < 6u; ++limit) {
CreateResolverWithLimitsAndParams(
PrioritizedDispatcher::Limits(NUM_PRIORITIES, limit),
DefaultParams(proc_.get()));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
for (unsigned i = 0u; i < maximum_dns_failures(); ++i) {
std::string host = base::StringPrintf("nx%u", i);
proc_->AddRuleForAllFamilies(host, "192.168.0.1");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest(host)->Resolve());
}
proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.2");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve());
proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.3");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve());
proc_->AddRuleForAllFamilies("slow_ok3", "192.168.0.4");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok3")->Resolve());
proc_->SignalMultiple(maximum_dns_failures() + 3);
for (size_t i = 0u; i < maximum_dns_failures(); ++i) {
EXPECT_EQ(OK, requests_[i]->WaitForResult());
EXPECT_TRUE(requests_[i]->HasOneAddress("192.168.0.1", 80));
}
EXPECT_EQ(OK, requests_[maximum_dns_failures()]->WaitForResult());
EXPECT_TRUE(requests_[maximum_dns_failures()]->HasOneAddress(
"192.168.0.2", 80));
EXPECT_EQ(OK, requests_[maximum_dns_failures() + 1]->WaitForResult());
EXPECT_TRUE(requests_[maximum_dns_failures() + 1]->HasOneAddress(
"192.168.0.3", 80));
EXPECT_EQ(OK, requests_[maximum_dns_failures() + 2]->WaitForResult());
EXPECT_TRUE(requests_[maximum_dns_failures() + 2]->HasOneAddress(
"192.168.0.4", 80));
requests_.clear();
}
}
TEST_F(HostResolverImplDnsTest, ManuallyDisableDnsClientWithPendingRequests) {
CreateResolverWithLimitsAndParams(
PrioritizedDispatcher::Limits(NUM_PRIORITIES, 3u),
DefaultParams(proc_.get()));
resolver_->SetDefaultAddressFamily(ADDRESS_FAMILY_UNSPECIFIED);
ChangeDnsConfig(CreateValidDnsConfig());
proc_->AddRuleForAllFamilies("slow_ok1", "192.168.0.1");
proc_->AddRuleForAllFamilies("slow_ok2", "192.168.0.2");
proc_->AddRuleForAllFamilies("ok", "192.168.0.3");
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok1")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("slow_ok2")->Resolve());
EXPECT_EQ(ERR_IO_PENDING, CreateRequest("ok")->Resolve());
EXPECT_EQ(3u, num_running_dispatcher_jobs());
resolver_->SetDnsClient(scoped_ptr<DnsClient>());
EXPECT_EQ(3u, num_running_dispatcher_jobs());
proc_->SignalMultiple(3u);
EXPECT_EQ(OK, requests_[0]->WaitForResult());
EXPECT_TRUE(requests_[0]->HasOneAddress("192.168.0.1", 80));
EXPECT_EQ(OK, requests_[1]->WaitForResult());
EXPECT_TRUE(requests_[1]->HasOneAddress("192.168.0.2", 80));
EXPECT_EQ(OK, requests_[2]->WaitForResult());
EXPECT_TRUE(requests_[2]->HasOneAddress("192.168.0.3", 80));
}
}