xref: /aosp_15_r20/external/cronet/net/dns/host_resolver_manager_unittest.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_HOST_RESOLVER_MANAGER_UNITTEST_H_
6 #define NET_DNS_HOST_RESOLVER_MANAGER_UNITTEST_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/scoped_refptr.h"
14 #include "base/test/test_mock_time_task_runner.h"
15 #include "net/base/completion_once_callback.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_export.h"
18 #include "net/dns/dns_client.h"
19 #include "net/dns/dns_test_util.h"
20 #include "net/dns/host_cache.h"
21 #include "net/dns/host_resolver_dns_task.h"
22 #include "net/dns/host_resolver_manager.h"
23 #include "net/dns/test_dns_config_service.h"
24 #include "net/log/net_log_with_source.h"
25 #include "net/test/test_with_task_environment.h"
26 
27 namespace net {
28 
29 class MockHostResolverProc;
30 
31 class HostResolverManagerTest : public TestWithTaskEnvironment {
32  public:
33   static const int kDefaultPort = 80;
34 
35   explicit HostResolverManagerTest(
36       base::test::TaskEnvironment::TimeSource time_source =
37           base::test::TaskEnvironment::TimeSource::SYSTEM_TIME);
38 
39   ~HostResolverManagerTest() override;
40 
41   void CreateResolver(bool check_ipv6_on_wifi = true);
42 
43   virtual void DestroyResolver();
44 
45   // This HostResolverManager will only allow 1 outstanding resolve at a time
46   // and perform no retries.
47   void CreateSerialResolver(bool check_ipv6_on_wifi = true,
48                             bool ipv6_reachable = true,
49                             bool is_async = false);
50 
51   void StaleAllowedFromIpTest(bool is_async);
52   void LocalOnlyFromIpTest(bool is_async);
53   void ChangePriorityTest(bool is_async);
54   void AbortOnlyExistingRequestsOnIPAddressChangeTest(bool is_async);
55   void FlushCacheOnIPAddressChangeTest(bool is_async);
56   void AbortOnIPAddressChangedTest(bool is_async);
57   void NumericIPv6AddressTest(bool is_async);
58   void NumericIPv6AddressWithSchemeTest(bool is_async);
59   void LocalhostIPV4IPV6LookupTest(bool is_async);
60   void IPv4AddressLiteralInIPv6OnlyNetworkTest(bool is_async);
61   void IPv4AddressLiteralInIPv6OnlyNetworkPort443Test(bool is_async);
62   void IPv4AddressLiteralInIPv6OnlyNetworkNoDns64Test(bool is_async);
63   void IPv4AddressLiteralInIPv6OnlyNetworkBadAddressTest(bool is_async);
64 
65  protected:
66   // testing::Test implementation:
67   void SetUp() override;
68   void TearDown() override;
69 
70   void CreateResolverWithLimitsAndParams(
71       size_t max_concurrent_resolves,
72       const HostResolverSystemTask::Params& params,
73       bool ipv6_reachable,
74       bool check_ipv6_on_wifi,
75       bool is_async = false);
76 
77   virtual HostResolver::ManagerOptions DefaultOptions();
78 
79   virtual void CreateResolverWithOptionsAndParams(
80       HostResolver::ManagerOptions options,
81       const HostResolverSystemTask::Params& params,
82       bool ipv6_reachable,
83       bool is_async = false,
84       bool ipv4_reachable = true);
85 
86   // Friendship is not inherited, so use proxies to access those.
87   size_t num_running_dispatcher_jobs() const;
88 
89   void set_allow_fallback_to_systemtask(bool allow_fallback_to_systemtask);
90 
maximum_insecure_dns_task_failures()91   static unsigned maximum_insecure_dns_task_failures() {
92     return DnsClient::kMaxInsecureFallbackFailures;
93   }
94 
95   int StartIPv6ReachabilityCheck(
96       const NetLogWithSource& net_log,
97       raw_ptr<ClientSocketFactory> client_socket_factory,
98       CompletionOnceCallback callback);
99 
100   bool GetLastIpv6ProbeResult();
101 
102   void PopulateCache(const HostCache::Key& key, IPEndPoint endpoint);
103 
104   const std::pair<const HostCache::Key, HostCache::Entry>* GetCacheHit(
105       const HostCache::Key& key);
106 
107   void MakeCacheStale();
108 
109   IPEndPoint CreateExpected(const std::string& ip_literal, uint16_t port);
110 
111   scoped_refptr<MockHostResolverProc> proc_;
112   std::unique_ptr<HostResolverManager> resolver_;
113   std::unique_ptr<URLRequestContext> request_context_;
114   std::unique_ptr<ResolveContext> resolve_context_;
115 };
116 
117 // Specialized fixture for tests of DnsTask.
118 class HostResolverManagerDnsTest : public HostResolverManagerTest {
119  public:
120   explicit HostResolverManagerDnsTest(
121       base::test::TaskEnvironment::TimeSource time_source =
122           base::test::TaskEnvironment::TimeSource::MOCK_TIME);
123 
124   ~HostResolverManagerDnsTest() override;
125 
126   void DestroyResolver() override;
127 
128   // Note that this clears `mock_dns_client_`.
129   void SetDnsClient(std::unique_ptr<DnsClient> dns_client);
130 
131   void Ipv6UnreachableTest(bool is_async);
132   void Ipv6UnreachableInvalidConfigTest(bool is_async);
133 
134  protected:
135   void TearDown() override;
136 
137   // HostResolverManagerTest implementation:
138   HostResolver::ManagerOptions DefaultOptions() override;
139 
140   void CreateResolverWithOptionsAndParams(
141       HostResolver::ManagerOptions options,
142       const HostResolverSystemTask::Params& params,
143       bool ipv6_reachable,
144       bool is_async = false,
145       bool ipv4_reachable = true) override;
146 
147   // Call after CreateResolver() to update the resolver with a new MockDnsClient
148   // using`config` and `rules`.
149   void UseMockDnsClient(const DnsConfig& config, MockDnsClientRuleList rules);
150 
151   static MockDnsClientRuleList CreateDefaultDnsRules();
152 
153   // Adds a rule to `rules`.
154   static void AddDnsRule(MockDnsClientRuleList* rules,
155                          const std::string& prefix,
156                          uint16_t qtype,
157                          MockDnsClientRule::ResultType result_type,
158                          bool delay);
159 
160   static void AddDnsRule(MockDnsClientRuleList* rules,
161                          const std::string& prefix,
162                          uint16_t qtype,
163                          const IPAddress& result_ip,
164                          bool delay);
165 
166   static void AddDnsRule(MockDnsClientRuleList* rules,
167                          const std::string& prefix,
168                          uint16_t qtype,
169                          IPAddress result_ip,
170                          std::string cannonname,
171                          bool delay);
172 
173   static void AddDnsRule(MockDnsClientRuleList* rules,
174                          const std::string& prefix,
175                          uint16_t qtype,
176                          DnsResponse dns_test_response,
177                          bool delay);
178 
179   static void AddSecureDnsRule(MockDnsClientRuleList* rules,
180                                const std::string& prefix,
181                                uint16_t qtype,
182                                MockDnsClientRule::ResultType result_type,
183                                bool delay);
184 
185   void ChangeDnsConfig(const DnsConfig& config);
186 
187   void InvalidateDnsConfig();
188 
189   void SetInitialDnsConfig(const DnsConfig& config);
190 
191   void TriggerInsecureFailureCondition();
192 
193   scoped_refptr<base::TestMockTimeTaskRunner> notifier_task_runner_;
194   raw_ptr<TestDnsConfigService, DanglingUntriaged> config_service_;
195   std::unique_ptr<SystemDnsConfigChangeNotifier> notifier_;
196 
197   // Owned by `resolver_`.
198   raw_ptr<MockDnsClient> mock_dns_client_ = nullptr;
199 };
200 
201 }  // namespace net
202 
203 #endif  // NET_DNS_HOST_RESOLVER_MANAGER_UNITTEST_H_
204