#include "net/dns/mock_host_resolver.h"
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/pattern.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/types/optional_util.h"
#include "build/build_config.h"
#include "net/base/address_family.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/net_errors.h"
#include "net/base/net_export.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/request_priority.h"
#include "net/base/test_completion_callback.h"
#include "net/dns/dns_alias_utility.h"
#include "net/dns/dns_names_util.h"
#include "net/dns/dns_util.h"
#include "net/dns/host_cache.h"
#include "net/dns/host_resolver.h"
#include "net/dns/host_resolver_manager.h"
#include "net/dns/host_resolver_system_task.h"
#include "net/dns/https_record_rdata.h"
#include "net/dns/public/dns_query_type.h"
#include "net/dns/public/host_resolver_results.h"
#include "net/dns/public/host_resolver_source.h"
#include "net/dns/public/mdns_listener_update_type.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/log/net_log_with_source.h"
#include "net/url_request/url_request_context.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "url/scheme_host_port.h"
#if BUILDFLAG(IS_WIN)
#include "net/base/winsock_init.h"
#endif
namespace net {
namespace {
const unsigned kMaxCacheEntries = …;
const unsigned kCacheEntryTTLSeconds = …;
absl::variant<url::SchemeHostPort, std::string> GetCacheHost(
const HostResolver::Host& endpoint) { … }
std::optional<HostCache::Entry> CreateCacheEntry(
std::string_view canonical_name,
const std::vector<HostResolverEndpointResult>& endpoint_results,
const std::set<std::string>& aliases) { … }
}
int ParseAddressList(std::string_view host_list,
std::vector<net::IPEndPoint>* ip_endpoints) { … }
class MockHostResolverBase::RequestBase { … };
class MockHostResolverBase::RequestImpl
: public RequestBase,
public HostResolver::ResolveHostRequest { … };
class MockHostResolverBase::ServiceEndpointRequestImpl
: public RequestBase,
public HostResolver::ServiceEndpointRequest { … };
class MockHostResolverBase::ProbeRequestImpl
: public HostResolver::ProbeRequest { … };
class MockHostResolverBase::MdnsListenerImpl
: public HostResolver::MdnsListener { … };
MockHostResolverBase::RuleResolver::RuleKey::RuleKey() = default;
MockHostResolverBase::RuleResolver::RuleKey::~RuleKey() = default;
MockHostResolverBase::RuleResolver::RuleKey::RuleKey(const RuleKey&) = default;
MockHostResolverBase::RuleResolver::RuleKey&
MockHostResolverBase::RuleResolver::RuleKey::operator=(const RuleKey&) =
default;
MockHostResolverBase::RuleResolver::RuleKey::RuleKey(RuleKey&&) = default;
MockHostResolverBase::RuleResolver::RuleKey&
MockHostResolverBase::RuleResolver::RuleKey::operator=(RuleKey&&) = default;
MockHostResolverBase::RuleResolver::RuleResult::RuleResult() = default;
MockHostResolverBase::RuleResolver::RuleResult::RuleResult(
std::vector<HostResolverEndpointResult> endpoints,
std::set<std::string> aliases)
: … { … }
MockHostResolverBase::RuleResolver::RuleResult::~RuleResult() = default;
MockHostResolverBase::RuleResolver::RuleResult::RuleResult(const RuleResult&) =
default;
MockHostResolverBase::RuleResolver::RuleResult&
MockHostResolverBase::RuleResolver::RuleResult::operator=(const RuleResult&) =
default;
MockHostResolverBase::RuleResolver::RuleResult::RuleResult(RuleResult&&) =
default;
MockHostResolverBase::RuleResolver::RuleResult&
MockHostResolverBase::RuleResolver::RuleResult::operator=(RuleResult&&) =
default;
MockHostResolverBase::RuleResolver::RuleResolver(
std::optional<RuleResultOrError> default_result)
: … { … }
MockHostResolverBase::RuleResolver::~RuleResolver() = default;
MockHostResolverBase::RuleResolver::RuleResolver(const RuleResolver&) = default;
MockHostResolverBase::RuleResolver&
MockHostResolverBase::RuleResolver::operator=(const RuleResolver&) = default;
MockHostResolverBase::RuleResolver::RuleResolver(RuleResolver&&) = default;
MockHostResolverBase::RuleResolver&
MockHostResolverBase::RuleResolver::operator=(RuleResolver&&) = default;
const MockHostResolverBase::RuleResolver::RuleResultOrError&
MockHostResolverBase::RuleResolver::Resolve(
const Host& request_endpoint,
DnsQueryTypeSet request_types,
HostResolverSource request_source) const { … }
void MockHostResolverBase::RuleResolver::ClearRules() { … }
MockHostResolverBase::RuleResolver::RuleResultOrError
MockHostResolverBase::RuleResolver::GetLocalhostResult() { … }
void MockHostResolverBase::RuleResolver::AddRule(RuleKey key,
RuleResultOrError result) { … }
void MockHostResolverBase::RuleResolver::AddRule(RuleKey key,
std::string_view ip_literal) { … }
void MockHostResolverBase::RuleResolver::AddRule(
std::string_view hostname_pattern,
RuleResultOrError result) { … }
void MockHostResolverBase::RuleResolver::AddRule(
std::string_view hostname_pattern,
std::string_view ip_literal) { … }
void MockHostResolverBase::RuleResolver::AddRule(
std::string_view hostname_pattern,
Error error) { … }
void MockHostResolverBase::RuleResolver::AddIPLiteralRule(
std::string_view hostname_pattern,
std::string_view ip_literal,
std::string_view canonical_name) { … }
void MockHostResolverBase::RuleResolver::AddIPLiteralRuleWithDnsAliases(
std::string_view hostname_pattern,
std::string_view ip_literal,
std::vector<std::string> dns_aliases) { … }
void MockHostResolverBase::RuleResolver::AddIPLiteralRuleWithDnsAliases(
std::string_view hostname_pattern,
std::string_view ip_literal,
std::set<std::string> dns_aliases) { … }
void MockHostResolverBase::RuleResolver::AddSimulatedFailure(
std::string_view hostname_pattern) { … }
void MockHostResolverBase::RuleResolver::AddSimulatedTimeoutFailure(
std::string_view hostname_pattern) { … }
void MockHostResolverBase::RuleResolver::AddRuleWithFlags(
std::string_view host_pattern,
std::string_view ip_literal,
HostResolverFlags ,
std::vector<std::string> dns_aliases) { … }
MockHostResolverBase::State::State() = default;
MockHostResolverBase::State::~State() = default;
MockHostResolverBase::~MockHostResolverBase() { … }
void MockHostResolverBase::OnShutdown() { … }
std::unique_ptr<HostResolver::ResolveHostRequest>
MockHostResolverBase::CreateRequest(
url::SchemeHostPort host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
std::optional<ResolveHostParameters> optional_parameters) { … }
std::unique_ptr<HostResolver::ResolveHostRequest>
MockHostResolverBase::CreateRequest(
const HostPortPair& host,
const NetworkAnonymizationKey& network_anonymization_key,
const NetLogWithSource& source_net_log,
const std::optional<ResolveHostParameters>& optional_parameters) { … }
std::unique_ptr<HostResolver::ServiceEndpointRequest>
MockHostResolverBase::CreateServiceEndpointRequest(
Host host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
ResolveHostParameters parameters) { … }
std::unique_ptr<HostResolver::ProbeRequest>
MockHostResolverBase::CreateDohProbeRequest() { … }
std::unique_ptr<HostResolver::MdnsListener>
MockHostResolverBase::CreateMdnsListener(const HostPortPair& host,
DnsQueryType query_type) { … }
HostCache* MockHostResolverBase::GetHostCache() { … }
int MockHostResolverBase::LoadIntoCache(
absl::variant<url::SchemeHostPort, HostPortPair> endpoint,
const NetworkAnonymizationKey& network_anonymization_key,
const std::optional<ResolveHostParameters>& optional_parameters) { … }
int MockHostResolverBase::LoadIntoCache(
const Host& endpoint,
const NetworkAnonymizationKey& network_anonymization_key,
const std::optional<ResolveHostParameters>& optional_parameters) { … }
void MockHostResolverBase::ResolveAllPending() { … }
size_t MockHostResolverBase::last_id() { … }
void MockHostResolverBase::ResolveNow(size_t id) { … }
void MockHostResolverBase::DetachRequest(size_t id) { … }
std::string_view MockHostResolverBase::request_host(size_t id) { … }
RequestPriority MockHostResolverBase::request_priority(size_t id) { … }
const NetworkAnonymizationKey&
MockHostResolverBase::request_network_anonymization_key(size_t id) { … }
void MockHostResolverBase::ResolveOnlyRequestNow() { … }
void MockHostResolverBase::TriggerMdnsListeners(
const HostPortPair& host,
DnsQueryType query_type,
MdnsListenerUpdateType update_type,
const IPEndPoint& address_result) { … }
void MockHostResolverBase::TriggerMdnsListeners(
const HostPortPair& host,
DnsQueryType query_type,
MdnsListenerUpdateType update_type,
const std::vector<std::string>& text_result) { … }
void MockHostResolverBase::TriggerMdnsListeners(
const HostPortPair& host,
DnsQueryType query_type,
MdnsListenerUpdateType update_type,
const HostPortPair& host_result) { … }
void MockHostResolverBase::TriggerMdnsListeners(
const HostPortPair& host,
DnsQueryType query_type,
MdnsListenerUpdateType update_type) { … }
MockHostResolverBase::RequestBase* MockHostResolverBase::request(size_t id) { … }
MockHostResolverBase::MockHostResolverBase(bool use_caching,
int cache_invalidation_num,
RuleResolver rule_resolver)
: … { … }
int MockHostResolverBase::Resolve(RequestBase* request) { … }
int MockHostResolverBase::ResolveFromIPLiteralOrCache(
const Host& endpoint,
const NetworkAnonymizationKey& network_anonymization_key,
DnsQueryType dns_query_type,
HostResolverFlags flags,
HostResolverSource source,
HostResolver::ResolveHostParameters::CacheUsage cache_usage,
std::vector<HostResolverEndpointResult>* out_endpoints,
std::set<std::string>* out_aliases,
std::optional<HostCache::EntryStaleness>* out_stale_info) { … }
int MockHostResolverBase::DoSynchronousResolution(RequestBase& request) { … }
void MockHostResolverBase::AddListener(MdnsListenerImpl* listener) { … }
void MockHostResolverBase::RemoveCancelledListener(MdnsListenerImpl* listener) { … }
MockHostResolverFactory::MockHostResolverFactory(
MockHostResolverBase::RuleResolver rules,
bool use_caching,
int cache_invalidation_num)
: … { … }
MockHostResolverFactory::~MockHostResolverFactory() = default;
std::unique_ptr<HostResolver> MockHostResolverFactory::CreateResolver(
HostResolverManager* manager,
std::string_view host_mapping_rules,
bool enable_caching) { … }
std::unique_ptr<HostResolver> MockHostResolverFactory::CreateStandaloneResolver(
NetLog* net_log,
const HostResolver::ManagerOptions& options,
std::string_view host_mapping_rules,
bool enable_caching) { … }
RuleBasedHostResolverProc::Rule::Rule(ResolverType resolver_type,
std::string_view host_pattern,
AddressFamily address_family,
HostResolverFlags host_resolver_flags,
std::string_view replacement,
std::vector<std::string> dns_aliases,
int latency_ms)
: … { … }
RuleBasedHostResolverProc::Rule::Rule(const Rule& other) = default;
RuleBasedHostResolverProc::Rule::~Rule() = default;
RuleBasedHostResolverProc::RuleBasedHostResolverProc(
scoped_refptr<HostResolverProc> previous,
bool allow_fallback)
: … { … }
void RuleBasedHostResolverProc::AddRule(std::string_view host_pattern,
std::string_view replacement) { … }
void RuleBasedHostResolverProc::AddRuleForAddressFamily(
std::string_view host_pattern,
AddressFamily address_family,
std::string_view replacement) { … }
void RuleBasedHostResolverProc::AddRuleWithFlags(
std::string_view host_pattern,
std::string_view replacement,
HostResolverFlags flags,
std::vector<std::string> dns_aliases) { … }
void RuleBasedHostResolverProc::AddIPLiteralRule(
std::string_view host_pattern,
std::string_view ip_literal,
std::string_view canonical_name) { … }
void RuleBasedHostResolverProc::AddIPLiteralRuleWithDnsAliases(
std::string_view host_pattern,
std::string_view ip_literal,
std::vector<std::string> dns_aliases) { … }
void RuleBasedHostResolverProc::AddRuleWithLatency(
std::string_view host_pattern,
std::string_view replacement,
int latency_ms) { … }
void RuleBasedHostResolverProc::AllowDirectLookup(
std::string_view host_pattern) { … }
void RuleBasedHostResolverProc::AddSimulatedFailure(
std::string_view host_pattern,
HostResolverFlags flags) { … }
void RuleBasedHostResolverProc::AddSimulatedTimeoutFailure(
std::string_view host_pattern,
HostResolverFlags flags) { … }
void RuleBasedHostResolverProc::ClearRules() { … }
void RuleBasedHostResolverProc::DisableModifications() { … }
RuleBasedHostResolverProc::RuleList RuleBasedHostResolverProc::GetRules() { … }
size_t RuleBasedHostResolverProc::NumResolvesForHostPattern(
std::string_view host_pattern) { … }
int RuleBasedHostResolverProc::Resolve(const std::string& host,
AddressFamily address_family,
HostResolverFlags host_resolver_flags,
AddressList* addrlist,
int* os_error) { … }
RuleBasedHostResolverProc::~RuleBasedHostResolverProc() = default;
void RuleBasedHostResolverProc::AddRuleInternal(const Rule& rule) { … }
scoped_refptr<RuleBasedHostResolverProc> CreateCatchAllHostResolverProc() { … }
class HangingHostResolver::RequestImpl
: public HostResolver::ResolveHostRequest,
public HostResolver::ProbeRequest { … };
HangingHostResolver::State::State() = default;
HangingHostResolver::State::~State() = default;
HangingHostResolver::HangingHostResolver()
: … { … }
HangingHostResolver::~HangingHostResolver() = default;
void HangingHostResolver::OnShutdown() { … }
std::unique_ptr<HostResolver::ResolveHostRequest>
HangingHostResolver::CreateRequest(
url::SchemeHostPort host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
std::optional<ResolveHostParameters> optional_parameters) { … }
std::unique_ptr<HostResolver::ResolveHostRequest>
HangingHostResolver::CreateRequest(
const HostPortPair& host,
const NetworkAnonymizationKey& network_anonymization_key,
const NetLogWithSource& source_net_log,
const std::optional<ResolveHostParameters>& optional_parameters) { … }
std::unique_ptr<HostResolver::ServiceEndpointRequest>
HangingHostResolver::CreateServiceEndpointRequest(
Host host,
NetworkAnonymizationKey network_anonymization_key,
NetLogWithSource net_log,
ResolveHostParameters parameters) { … }
std::unique_ptr<HostResolver::ProbeRequest>
HangingHostResolver::CreateDohProbeRequest() { … }
void HangingHostResolver::SetRequestContext(
URLRequestContext* url_request_context) { … }
ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc() = default;
ScopedDefaultHostResolverProc::ScopedDefaultHostResolverProc(
HostResolverProc* proc) { … }
ScopedDefaultHostResolverProc::~ScopedDefaultHostResolverProc() { … }
void ScopedDefaultHostResolverProc::Init(HostResolverProc* proc) { … }
}