Commit d150cbe6 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

ContextHostResolver: Make tests not depend on local IPv6 probes.

Now the tests always act like IPv6 support was detected locally, by
injecting a bogus cached DNS probe result and mocking out time to
prevent it from expiring.

Previously, the tests would either do only IPv4 lookups, or IPv6 and
IPv4 lookups based on the results of an IPv6 support probe on the local
machine. While this didn't have a huge effect on the tests since none
test IPv6 support, it did affect one of them which directly depended,
and it's generally much better to have tests not behaving differently
based on the environment they're running in.

Bug: None
Change-Id: Ic3f275f059a1cd174fadc9e49c0c43e26ee63252
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1899558Reviewed-by: default avatarEric Orth <ericorth@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712776}
parent 8a8b5463
......@@ -11,6 +11,7 @@
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "net/base/features.h"
#include "net/base/host_port_pair.h"
......@@ -41,12 +42,22 @@ namespace {
const IPEndPoint kEndpoint(IPAddress(1, 2, 3, 4), 100);
}
class ContextHostResolverTest : public TestWithTaskEnvironment {
class ContextHostResolverTest : public ::testing::Test,
public WithTaskEnvironment {
protected:
// Use mock time to prevent the HostResolverManager's injected IPv6 probe
// result from timing out.
ContextHostResolverTest()
: WithTaskEnvironment(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
~ContextHostResolverTest() override = default;
void SetUp() override {
manager_ = std::make_unique<HostResolverManager>(
HostResolver::ManagerOptions(),
nullptr /* system_dns_config_notifier */, nullptr /* net_log */);
manager_->SetLastIPv6ProbeResultForTesting(true);
}
void SetMockDnsRules(MockDnsClientRuleList rules) {
......@@ -511,31 +522,30 @@ TEST_F(ContextHostResolverTest, ResultsAddedToCacheWithNetworkIsolationKey) {
MockDnsClientRule::Result(BuildTestDnsResponse(
"example.com", kEndpoint.address())),
false /* delay */);
rules.emplace_back("example.com", dns_protocol::kTypeAAAA, false /* secure */,
MockDnsClientRule::Result(MockDnsClientRule::EMPTY),
false /* delay */);
SetMockDnsRules(std::move(rules));
auto resolver = std::make_unique<ContextHostResolver>(
manager_.get(), HostCache::CreateDefaultCache());
// Use DnsQueryType::A to avoid running into issues with the IPv6
// availablility check, which affects the cache key used.
HostResolver::ResolveHostParameters parameters;
parameters.dns_query_type = DnsQueryType::A;
std::unique_ptr<HostResolver::ResolveHostRequest> caching_request =
resolver->CreateRequest(HostPortPair("example.com", 103),
kNetworkIsolationKey, NetLogWithSource(),
parameters);
base::nullopt);
TestCompletionCallback caching_callback;
int rv = caching_request->Start(caching_callback.callback());
EXPECT_THAT(caching_callback.GetResult(rv), test::IsOk());
HostCache::Key cache_key("example.com", DnsQueryType::A,
HostCache::Key cache_key("example.com", DnsQueryType::UNSPECIFIED,
0 /* host_resolver_flags */, HostResolverSource::ANY,
kNetworkIsolationKey);
EXPECT_TRUE(
resolver->GetHostCache()->Lookup(cache_key, base::TimeTicks::Now()));
HostCache::Key cache_key_with_empty_nik(
"example.com", DnsQueryType::A, 0 /* host_resolver_flags */,
"example.com", DnsQueryType::UNSPECIFIED, 0 /* host_resolver_flags */,
HostResolverSource::ANY, NetworkIsolationKey());
EXPECT_FALSE(resolver->GetHostCache()->Lookup(cache_key_with_empty_nik,
base::TimeTicks::Now()));
......
......@@ -2823,6 +2823,11 @@ void HostResolverManager::SetDnsClientForTesting(
dns_client_ = std::move(dns_client);
}
void HostResolverManager::SetLastIPv6ProbeResultForTesting(
bool last_ipv6_probe_result) {
SetLastIPv6ProbeResult(last_ipv6_probe_result);
}
void HostResolverManager::SetTaskRunnerForTesting(
scoped_refptr<base::TaskRunner> task_runner) {
proc_task_runner_ = std::move(task_runner);
......@@ -3447,9 +3452,8 @@ bool HostResolverManager::IsIPv6Reachable(const NetLogWithSource& net_log) {
bool cached = true;
if ((tick_clock_->NowTicks() - last_ipv6_probe_time_).InMilliseconds() >
kIPv6ProbePeriodMs) {
last_ipv6_probe_result_ =
IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log);
last_ipv6_probe_time_ = tick_clock_->NowTicks();
SetLastIPv6ProbeResult(
IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log));
cached = false;
}
net_log.AddEvent(
......@@ -3459,6 +3463,11 @@ bool HostResolverManager::IsIPv6Reachable(const NetLogWithSource& net_log) {
return last_ipv6_probe_result_;
}
void HostResolverManager::SetLastIPv6ProbeResult(bool last_ipv6_probe_result) {
last_ipv6_probe_result_ = last_ipv6_probe_result;
last_ipv6_probe_time_ = tick_clock_->NowTicks();
}
bool HostResolverManager::IsGloballyReachable(const IPAddress& dest,
const NetLogWithSource& net_log) {
std::unique_ptr<DatagramClientSocket> socket(
......
......@@ -203,6 +203,11 @@ class NET_EXPORT HostResolverManager
// setting DnsConfig.
void SetDnsClientForTesting(std::unique_ptr<DnsClient> dns_client);
// Sets the last IPv6 probe result for testing. Uses the standard timeout
// duration, so it's up to the test fixture to ensure it doesn't expire by
// mocking time, if expiration would pose a problem.
void SetLastIPv6ProbeResultForTesting(bool last_ipv6_probe_result);
// Allows the tests to catch slots leaking out of the dispatcher. One
// HostResolverManager::Job could occupy multiple PrioritizedDispatcher job
// slots.
......@@ -376,6 +381,9 @@ class NET_EXPORT HostResolverManager
// from the first probe for some time before probing again.
bool IsIPv6Reachable(const NetLogWithSource& net_log);
// Sets |last_ipv6_probe_result_| and updates |last_ipv6_probe_time_|.
void SetLastIPv6ProbeResult(bool last_ipv6_probe_result);
// Attempts to connect a UDP socket to |dest|:53. Virtual for testing.
virtual bool IsGloballyReachable(const IPAddress& dest,
const NetLogWithSource& net_log);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment