// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_DNS_MOCK_HOST_RESOLVER_H_ #define NET_DNS_MOCK_HOST_RESOLVER_H_ #include <stddef.h> #include <stdint.h> #include <list> #include <map> #include <memory> #include <optional> #include <set> #include <string> #include <string_view> #include <vector> #include "base/memory/raw_ptr.h" #include "base/memory/weak_ptr.h" #include "base/synchronization/lock.h" #include "base/synchronization/waitable_event.h" #include "base/thread_annotations.h" #include "base/threading/thread_checker.h" #include "net/base/address_family.h" #include "net/base/address_list.h" #include "net/base/completion_once_callback.h" #include "net/base/host_port_pair.h" #include "net/base/net_errors.h" #include "net/base/network_anonymization_key.h" #include "net/dns/host_resolver.h" #include "net/dns/host_resolver_proc.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/secure_dns_policy.h" #include "net/log/net_log_with_source.h" #include "third_party/abseil-cpp/absl/types/variant.h" #include "url/scheme_host_port.h" namespace base { class TickClock; } // namespace base namespace net { class HostCache; class IPEndPoint; class URLRequestContext; // Fills `ip_endpoints` with a socket address for `host_list` which should be a // comma-separated list of IPv4 or IPv6 literal(s) without enclosing brackets. int ParseAddressList(std::string_view host_list, std::vector<net::IPEndPoint>* ip_endpoints); // In most cases, it is important that unit tests avoid relying on making actual // DNS queries since the resulting tests can be flaky, especially if the network // is unreliable for some reason. To simplify writing tests that avoid making // actual DNS queries, pass a MockHostResolver as the HostResolver dependency. // The socket addresses returned can be configured using the // MockHostResolverBase::RuleResolver: // // host_resolver->rules()->AddRule("foo.com", "1.2.3.4"); // host_resolver->rules()->AddRule("bar.com", "2.3.4.5"); // // The above rules define a static mapping from hostnames to IP address // literals. The first parameter to AddRule specifies a host pattern to match // against, and the second parameter indicates what IP address should be used to // replace the given hostname. So, the following is also supported: // // host_mapper->AddRule("*.com", "127.0.0.1"); // // For more advanced matching, the first parameter may be replaced with a // MockHostResolverBase::RuleResolver::RuleKey. For more advanced responses, the // second parameter may be replaced with a // MockHostResolverBase::RuleResolver::RuleResultOrError. // // MockHostResolvers may optionally be created with a default result: // // MockHostResolver(ERR_NAME_NOT_RESOLVED); // MockHostResolver(AddressList(ip_endpoint)); // MockHostResolver(MockHostResolverBase::RuleResolver::GetLocalhostResult()); // // If no default result is given, every resolve request must match a configured // rule, otherwise DCHECKs will fire. // Base class shared by MockHostResolver and MockCachingHostResolver. class MockHostResolverBase : public HostResolver { … }; class MockHostResolver : public MockHostResolverBase { … }; // Same as MockHostResolver, except internally it uses a host-cache. // // Note that tests are advised to use MockHostResolver instead, since it is // more predictable. (MockHostResolver also can be put into synchronous // operation mode in case that is what you needed from the caching version). class MockCachingHostResolver : public MockHostResolverBase { … }; // Factory that will always create and return Mock(Caching)HostResolvers. // // The default behavior is to create a non-caching mock, even if the tested code // requests caching enabled (via the |enable_caching| parameter in the creation // methods). A caching mock will only be created if both |use_caching| is set on // factory construction and |enable_caching| is set in the creation method. class MockHostResolverFactory : public HostResolver::Factory { … }; // RuleBasedHostResolverProc applies a set of rules to map a host string to // a replacement host string. It then uses the system host resolver to return // a socket address. Generally the replacement should be an IPv4 literal so // there is no network dependency. // // RuleBasedHostResolverProc is thread-safe, to a limited degree. Rules can be // added or removed on any thread. class RuleBasedHostResolverProc : public HostResolverProc { … }; // Create rules that map all requests to localhost. scoped_refptr<RuleBasedHostResolverProc> CreateCatchAllHostResolverProc(); // HangingHostResolver never completes its |Resolve| request. As LOCAL_ONLY // requests are not allowed to complete asynchronously, they will always result // in |ERR_DNS_CACHE_MISS|. class HangingHostResolver : public HostResolver { … }; // This class sets the default HostResolverProc for a particular scope. The // chain of resolver procs starting at |proc| is placed in front of any existing // default resolver proc(s). This means that if multiple // ScopedDefaultHostResolverProcs are declared, then resolving will start with // the procs given to the last-allocated one, then fall back to the procs given // to the previously-allocated one, and so forth. // // NOTE: Only use this as a catch-all safety net. Individual tests should use // MockHostResolver. class ScopedDefaultHostResolverProc { … }; } // namespace net #endif // NET_DNS_MOCK_HOST_RESOLVER_H_