File: mock_host_resolver.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (758 lines) | stat: -rw-r--r-- 29,404 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// 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 <set>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_piece_forward.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/optional.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(base::StringPiece 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,
      public base::SupportsWeakPtr<MockHostResolverBase> {
 private:
  class RequestImpl;
  class ProbeRequestImpl;
  class MdnsListenerImpl;

 public:
  class RuleResolver {
   public:
    struct RuleKey {
      struct WildcardScheme : absl::monostate {};
      struct NoScheme : absl::monostate {};
      using Scheme = std::string;

      RuleKey();
      ~RuleKey();

      RuleKey(const RuleKey&);
      RuleKey& operator=(const RuleKey&);
      RuleKey(RuleKey&&);
      RuleKey& operator=(RuleKey&&);

      auto GetTuple() const {
        return std::tie(scheme, hostname_pattern, port, query_type,
                        query_source);
      }

      bool operator<(const RuleKey& other) const {
        return GetTuple() < other.GetTuple();
      }

      // If `WildcardScheme`, scheme is wildcard and any query will match,
      // whether made with url::SchemeHostPort or HostPortPair. If `NoScheme`,
      // queries will only match if made using HostPortPair. Else, queries will
      // only match if made using url::SchemeHostPort with matching scheme
      // value.
      absl::variant<WildcardScheme, NoScheme, Scheme> scheme = WildcardScheme();

      // Pattern matched via `base::MatchPattern()`.
      std::string hostname_pattern = "*";

      // `nullopt` represents wildcard and all queries will match.
      absl::optional<uint16_t> port;
      absl::optional<DnsQueryType> query_type;
      absl::optional<HostResolverSource> query_source;
    };

    struct RuleResult {
      RuleResult();
      explicit RuleResult(
          std::vector<HostResolverEndpointResult> endpoints,
          std::set<std::string> aliases = std::set<std::string>());

      ~RuleResult();

      RuleResult(const RuleResult&);
      RuleResult& operator=(const RuleResult&);
      RuleResult(RuleResult&&);
      RuleResult& operator=(RuleResult&&);

      std::vector<HostResolverEndpointResult> endpoints;
      std::set<std::string> aliases;
    };

    using ErrorResult = Error;
    using RuleResultOrError = absl::variant<RuleResult, ErrorResult>;

    // If `default_result` is nullopt, every resolve must match an added rule.
    explicit RuleResolver(
        absl::optional<RuleResultOrError> default_result = absl::nullopt);
    ~RuleResolver();

    RuleResolver(const RuleResolver&);
    RuleResolver& operator=(const RuleResolver&);
    RuleResolver(RuleResolver&&);
    RuleResolver& operator=(RuleResolver&&);

    const RuleResultOrError& Resolve(const Host& request_endpoint,
                                     DnsQueryTypeSet request_types,
                                     HostResolverSource request_source) const;

    void ClearRules();

    static RuleResultOrError GetLocalhostResult();

    void AddRule(RuleKey key, RuleResultOrError result);
    void AddRule(RuleKey key, base::StringPiece ip_literal);

    void AddRule(base::StringPiece hostname_pattern, RuleResultOrError result);
    void AddRule(base::StringPiece hostname_pattern,
                 base::StringPiece ip_literal);

    void AddRule(base::StringPiece hostname_pattern, Error error);

    // Legacy rule creation. Only for compatibility with tests written for use
    // with RuleBasedHostResolverProc. New code should use the AddRule() calls
    // above.
    void AddIPLiteralRule(base::StringPiece hostname_pattern,
                          base::StringPiece ip_literal,
                          base::StringPiece canonical_name);
    void AddIPLiteralRuleWithDnsAliases(base::StringPiece hostname_pattern,
                                        base::StringPiece ip_literal,
                                        std::vector<std::string> dns_aliases);
    void AddIPLiteralRuleWithDnsAliases(base::StringPiece hostname_pattern,
                                        base::StringPiece ip_literal,
                                        std::set<std::string> dns_aliases);
    void AddSimulatedFailure(base::StringPiece hostname_pattern);
    void AddSimulatedTimeoutFailure(base::StringPiece hostname_pattern);
    void AddRuleWithFlags(base::StringPiece host_pattern,
                          base::StringPiece ip_literal,
                          HostResolverFlags flags,
                          std::vector<std::string> dns_aliases = {});

   private:
    std::map<RuleKey, RuleResultOrError> rules_;
    absl::optional<RuleResultOrError> default_result_;
  };

  using RequestMap = std::map<size_t, RequestImpl*>;

  // A set of states in MockHostResolver. This is used to observe the internal
  // state variables after destructing a MockHostResolver.
  class State : public base::RefCounted<State> {
   public:
    State();

    bool has_pending_requests() const { return !requests_.empty(); }
    bool IsDohProbeRunning() const { return !!doh_probe_request_; }
    size_t num_resolve() const { return num_resolve_; }
    size_t num_resolve_from_cache() const { return num_resolve_from_cache_; }
    size_t num_non_local_resolves() const { return num_non_local_resolves_; }

    RequestMap& mutable_requests() { return requests_; }
    void IncrementNumResolve() { ++num_resolve_; }
    void IncrementNumResolveFromCache() { ++num_resolve_from_cache_; }
    void IncrementNumNonLocalResolves() { ++num_non_local_resolves_; }
    void ClearDohProbeRequest() { doh_probe_request_ = nullptr; }
    void ClearDohProbeRequestIfMatching(ProbeRequestImpl* request) {
      if (request == doh_probe_request_) {
        doh_probe_request_ = nullptr;
      }
    }
    void set_doh_probe_request(ProbeRequestImpl* request) {
      DCHECK(request);
      DCHECK(!doh_probe_request_);
      doh_probe_request_ = request;
    }

   private:
    friend class RefCounted<State>;

    ~State();

    // Maintain non-owning pointers to outstanding requests and listeners to
    // allow completing/notifying them. The objects are owned by callers, and
    // should be removed from |this| on destruction by calling DetachRequest()
    // or RemoveCancelledListener().
    RequestMap requests_;
    raw_ptr<ProbeRequestImpl> doh_probe_request_ = nullptr;
    size_t num_resolve_ = 0;
    size_t num_resolve_from_cache_ = 0;
    size_t num_non_local_resolves_ = 0;
  };

  MockHostResolverBase(const MockHostResolverBase&) = delete;
  MockHostResolverBase& operator=(const MockHostResolverBase&) = delete;

  ~MockHostResolverBase() override;

  RuleResolver* rules() { return &rule_resolver_; }

  scoped_refptr<const State> state() const { return state_; }

  // Controls whether resolutions complete synchronously or asynchronously.
  void set_synchronous_mode(bool is_synchronous) {
    synchronous_mode_ = is_synchronous;
  }

  // Asynchronous requests are automatically resolved by default.
  // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and
  // ResolveAllPending() must be explicitly invoked to resolve all requests
  // that are pending.
  void set_ondemand_mode(bool is_ondemand) {
    ondemand_mode_ = is_ondemand;
  }

  // HostResolver methods:
  void OnShutdown() override;
  std::unique_ptr<ResolveHostRequest> CreateRequest(
      url::SchemeHostPort host,
      NetworkAnonymizationKey network_anonymization_key,
      NetLogWithSource net_log,
      absl::optional<ResolveHostParameters> optional_parameters) override;
  std::unique_ptr<ResolveHostRequest> CreateRequest(
      const HostPortPair& host,
      const NetworkAnonymizationKey& network_anonymization_key,
      const NetLogWithSource& net_log,
      const absl::optional<ResolveHostParameters>& optional_parameters)
      override;
  std::unique_ptr<ProbeRequest> CreateDohProbeRequest() override;
  std::unique_ptr<MdnsListener> CreateMdnsListener(
      const HostPortPair& host,
      DnsQueryType query_type) override;
  HostCache* GetHostCache() override;
  void SetRequestContext(URLRequestContext* request_context) override {}

  // Preloads the cache with what would currently be the result of a request
  // with the given parameters. Returns the net error of the cached result.
  int LoadIntoCache(
      absl::variant<url::SchemeHostPort, HostPortPair> endpoint,
      const NetworkAnonymizationKey& network_anonymization_key,
      const absl::optional<ResolveHostParameters>& optional_parameters);
  int LoadIntoCache(
      const Host& endpoint,
      const NetworkAnonymizationKey& network_anonymization_key,
      const absl::optional<ResolveHostParameters>& optional_parameters);

  // Returns true if there are pending requests that can be resolved by invoking
  // ResolveAllPending().
  bool has_pending_requests() const { return state_->has_pending_requests(); }

  // Resolves all pending requests. It is only valid to invoke this if
  // set_ondemand_mode was set before. The requests are resolved asynchronously,
  // after this call returns.
  void ResolveAllPending();

  // Each request is assigned an ID when started and stored with the resolver
  // for async resolution, starting with 1. IDs are not reused. Once a request
  // completes, it is destroyed, and can no longer be accessed.

  // Returns the ID of the most recently started still-active request. Zero if
  // no requests are currently active.
  size_t last_id();

  // Resolve request stored in |requests_|. Pass rv to callback.
  void ResolveNow(size_t id);

  // Detach cancelled request.
  void DetachRequest(size_t id);

  // Returns the hostname of the request with the given id.
  base::StringPiece request_host(size_t id);

  // Returns the priority of the request with the given id.
  RequestPriority request_priority(size_t id);

  // Returns NetworkAnonymizationKey of the request with the given id.
  const NetworkAnonymizationKey& request_network_anonymization_key(size_t id);

  // Like ResolveNow, but doesn't take an ID. DCHECKs if there's more than one
  // pending request.
  void ResolveOnlyRequestNow();

  // The number of times that Resolve() has been called.
  size_t num_resolve() const { return state_->num_resolve(); }

  // The number of times that ResolveFromCache() has been called.
  size_t num_resolve_from_cache() const {
    return state_->num_resolve_from_cache();
  }

  // The number of times resolve was attempted non-locally.
  size_t num_non_local_resolves() const {
    return state_->num_non_local_resolves();
  }

  // Returns the RequestPriority of the last call to Resolve() (or
  // DEFAULT_PRIORITY if Resolve() hasn't been called yet).
  RequestPriority last_request_priority() const {
    return last_request_priority_;
  }

  // Returns the NetworkAnonymizationKey passed in to the last call to Resolve()
  // (or absl::nullopt if Resolve() hasn't been called yet).
  const absl::optional<NetworkAnonymizationKey>&
  last_request_network_anonymization_key() {
    return last_request_network_anonymization_key_;
  }

  // Returns the SecureDnsPolicy of the last call to Resolve() (or
  // absl::nullopt if Resolve() hasn't been called yet).
  SecureDnsPolicy last_secure_dns_policy() const {
    return last_secure_dns_policy_;
  }

  bool IsDohProbeRunning() const { return state_->IsDohProbeRunning(); }

  void TriggerMdnsListeners(const HostPortPair& host,
                            DnsQueryType query_type,
                            MdnsListenerUpdateType update_type,
                            const IPEndPoint& address_result);
  void TriggerMdnsListeners(const HostPortPair& host,
                            DnsQueryType query_type,
                            MdnsListenerUpdateType update_type,
                            const std::vector<std::string>& text_result);
  void TriggerMdnsListeners(const HostPortPair& host,
                            DnsQueryType query_type,
                            MdnsListenerUpdateType update_type,
                            const HostPortPair& host_result);
  void TriggerMdnsListeners(const HostPortPair& host,
                            DnsQueryType query_type,
                            MdnsListenerUpdateType update_type);

  void set_tick_clock(const base::TickClock* tick_clock) {
    tick_clock_ = tick_clock;
  }

 private:
  friend class MockHostResolver;
  friend class MockCachingHostResolver;
  friend class MockHostResolverFactory;

  // Returns the request with the given id.
  RequestImpl* request(size_t id);

  // If > 0, |cache_invalidation_num| is the number of times a cached entry can
  // be read before it invalidates itself. Useful to force cache expiration
  // scenarios.
  MockHostResolverBase(bool use_caching,
                       int cache_invalidation_num,
                       RuleResolver rule_resolver);

  // Handle resolution for |request|. Expected to be called only the RequestImpl
  // object itself.
  int Resolve(RequestImpl* request);

  // Resolve as IP or from |cache_| return cached error or
  // DNS_CACHE_MISS if failed.
  int 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,
      absl::optional<HostCache::EntryStaleness>* out_stale_info);
  int DoSynchronousResolution(RequestImpl& request);

  void AddListener(MdnsListenerImpl* listener);
  void RemoveCancelledListener(MdnsListenerImpl* listener);

  RequestPriority last_request_priority_ = DEFAULT_PRIORITY;
  absl::optional<NetworkAnonymizationKey>
      last_request_network_anonymization_key_;
  SecureDnsPolicy last_secure_dns_policy_ = SecureDnsPolicy::kAllow;
  bool synchronous_mode_ = false;
  bool ondemand_mode_ = false;
  RuleResolver rule_resolver_;
  std::unique_ptr<HostCache> cache_;

  const int initial_cache_invalidation_num_;
  std::map<HostCache::Key, int> cache_invalidation_nums_;

  std::set<MdnsListenerImpl*> listeners_;

  size_t next_request_id_ = 1;

  raw_ptr<const base::TickClock> tick_clock_;

  scoped_refptr<State> state_;

  THREAD_CHECKER(thread_checker_);
};

class MockHostResolver : public MockHostResolverBase {
 public:
  explicit MockHostResolver(absl::optional<RuleResolver::RuleResultOrError>
                                default_result = absl::nullopt)
      : MockHostResolverBase(/*use_caching=*/false,
                             /*cache_invalidation_num=*/0,
                             RuleResolver(std::move(default_result))) {}
  ~MockHostResolver() override = default;
};

// 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 {
 public:
  // If > 0, |cache_invalidation_num| is the number of times a cached entry can
  // be read before it invalidates itself. Useful to force cache expiration
  // scenarios.
  explicit MockCachingHostResolver(
      int cache_invalidation_num = 0,
      absl::optional<RuleResolver::RuleResultOrError> default_result =
          absl::nullopt)
      : MockHostResolverBase(/*use_caching=*/true,
                             cache_invalidation_num,
                             RuleResolver(std::move(default_result))) {}
  ~MockCachingHostResolver() override = default;
};

// 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 {
 public:
  explicit MockHostResolverFactory(MockHostResolverBase::RuleResolver rules =
                                       MockHostResolverBase::RuleResolver(),
                                   bool use_caching = false,
                                   int cache_invalidation_num = 0);

  MockHostResolverFactory(const MockHostResolverFactory&) = delete;
  MockHostResolverFactory& operator=(const MockHostResolverFactory&) = delete;

  ~MockHostResolverFactory() override;

  std::unique_ptr<HostResolver> CreateResolver(
      HostResolverManager* manager,
      base::StringPiece host_mapping_rules,
      bool enable_caching) override;
  std::unique_ptr<HostResolver> CreateStandaloneResolver(
      NetLog* net_log,
      const HostResolver::ManagerOptions& options,
      base::StringPiece host_mapping_rules,
      bool enable_caching) override;

 private:
  const MockHostResolverBase::RuleResolver rules_;
  const bool use_caching_;
  const int cache_invalidation_num_;
};

// 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 {
 public:
  // If `allow_fallback` is false, no Proc fallback is allowed except to
  // `previous`.
  explicit RuleBasedHostResolverProc(scoped_refptr<HostResolverProc> previous,
                                     bool allow_fallback = true);

  // Any hostname matching the given pattern will be replaced with the given
  // |ip_literal|.
  void AddRule(base::StringPiece host_pattern, base::StringPiece ip_literal);

  // Same as AddRule(), but further restricts to |address_family|.
  void AddRuleForAddressFamily(base::StringPiece host_pattern,
                               AddressFamily address_family,
                               base::StringPiece ip_literal);

  void AddRuleWithFlags(base::StringPiece host_pattern,
                        base::StringPiece ip_literal,
                        HostResolverFlags flags,
                        std::vector<std::string> dns_aliases = {});

  // Same as AddRule(), but the replacement is expected to be an IPv4 or IPv6
  // literal. This can be used in place of AddRule() to bypass the system's
  // host resolver (the address list will be constructed manually).
  // If |canonical_name| is non-empty, it is copied to the resulting AddressList
  // but does not impact DNS resolution.
  // |ip_literal| can be a single IP address like "192.168.1.1" or a comma
  // separated list of IP addresses, like "::1,192:168.1.2".
  void AddIPLiteralRule(base::StringPiece host_pattern,
                        base::StringPiece ip_literal,
                        base::StringPiece canonical_name);

  // Same as AddIPLiteralRule, but with a parameter allowing multiple DNS
  // aliases, such as CNAME aliases, instead of only the canonical name. While
  // a simulation using HostResolverProc to obtain more than a single DNS alias
  // is currently unrealistic, this capability is useful for clients of
  // MockHostResolver who need to be able to obtain aliases and can be
  // agnostic about how the host resolution took place, as the alternative,
  // MockDnsClient, is not currently hooked up to MockHostResolver.
  void AddIPLiteralRuleWithDnsAliases(base::StringPiece host_pattern,
                                      base::StringPiece ip_literal,
                                      std::vector<std::string> dns_aliases);

  void AddRuleWithLatency(base::StringPiece host_pattern,
                          base::StringPiece replacement,
                          int latency_ms);

  // Make sure that |host| will not be re-mapped or even processed by underlying
  // host resolver procedures. It can also be a pattern.
  void AllowDirectLookup(base::StringPiece host);

  // Simulate a lookup failure for |host| (it also can be a pattern).
  void AddSimulatedFailure(
      base::StringPiece host,
      HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY);

  // Simulate a lookup timeout failure for |host| (it also can be a pattern).
  void AddSimulatedTimeoutFailure(
      base::StringPiece host,
      HostResolverFlags flags = HOST_RESOLVER_LOOPBACK_ONLY);

  // Deletes all the rules that have been added.
  void ClearRules();

  // Causes method calls that add or delete rules to assert.
  // TODO(jam): once this class isn't used by tests that use an out of process
  // network service, remove this method and make Rule private.
  void DisableModifications();

  // HostResolverProc methods:
  int Resolve(const std::string& host,
              AddressFamily address_family,
              HostResolverFlags host_resolver_flags,
              AddressList* addrlist,
              int* os_error) override;

  struct Rule {
    // TODO(https://crbug.com/1298106) Deduplicate this enum's definition.
    enum ResolverType {
      kResolverTypeFail,
      kResolverTypeFailTimeout,
      // TODO(mmenke): Is it really reasonable for a "mock" host resolver to
      // fall back to the system resolver?
      kResolverTypeSystem,
      kResolverTypeIPLiteral,
    };

    Rule(ResolverType resolver_type,
         base::StringPiece host_pattern,
         AddressFamily address_family,
         HostResolverFlags host_resolver_flags,
         base::StringPiece replacement,
         std::vector<std::string> dns_aliases,
         int latency_ms);
    Rule(const Rule& other);
    ~Rule();

    ResolverType resolver_type;
    std::string host_pattern;
    AddressFamily address_family;
    HostResolverFlags host_resolver_flags;
    std::string replacement;
    std::vector<std::string> dns_aliases;
    int latency_ms;  // In milliseconds.
  };

  typedef std::list<Rule> RuleList;

  RuleList GetRules();

  // Returns the number of calls to Resolve() where |host| matched
  // |host_pattern|.
  size_t NumResolvesForHostPattern(base::StringPiece host_pattern);

 private:
  ~RuleBasedHostResolverProc() override;

  void AddRuleInternal(const Rule& rule);

  RuleList rules_ GUARDED_BY(rule_lock_);

  // Tracks the number of calls to Resolve() where |host| matches a rule's host
  // pattern.
  std::map<base::StringPiece, size_t> num_resolves_per_host_pattern_
      GUARDED_BY(rule_lock_);

  // Must be obtained before writing to or reading from |rules_|.
  base::Lock rule_lock_;

  // Whether changes are allowed.
  bool modifications_allowed_ = true;
};

// 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 {
 public:
  // A set of states in HangingHostResolver. This is used to observe the
  // internal state variables after destructing a MockHostResolver.
  class State : public base::RefCounted<State> {
   public:
    State();

    int num_cancellations() const { return num_cancellations_; }

    void IncrementNumCancellations() { ++num_cancellations_; }

   private:
    friend class RefCounted<State>;

    ~State();

    int num_cancellations_ = 0;
  };

  HangingHostResolver();
  ~HangingHostResolver() override;
  void OnShutdown() override;
  std::unique_ptr<ResolveHostRequest> CreateRequest(
      url::SchemeHostPort host,
      NetworkAnonymizationKey network_anonymization_key,
      NetLogWithSource net_log,
      absl::optional<ResolveHostParameters> optional_parameters) override;
  std::unique_ptr<ResolveHostRequest> CreateRequest(
      const HostPortPair& host,
      const NetworkAnonymizationKey& network_anonymization_key,
      const NetLogWithSource& net_log,
      const absl::optional<ResolveHostParameters>& optional_parameters)
      override;

  std::unique_ptr<ProbeRequest> CreateDohProbeRequest() override;

  void SetRequestContext(URLRequestContext* url_request_context) override;

  // Use to detect cancellations since there's otherwise no externally-visible
  // differentiation between a cancelled and a hung task.
  int num_cancellations() const { return state_->num_cancellations(); }

  // Return the corresponding values passed to the most recent call to
  // CreateRequest()
  const HostPortPair& last_host() const { return last_host_; }
  const NetworkAnonymizationKey& last_network_anonymization_key() const {
    return last_network_anonymization_key_;
  }

  const scoped_refptr<const State> state() const { return state_; }

 private:
  class RequestImpl;
  class ProbeRequestImpl;

  HostPortPair last_host_;
  NetworkAnonymizationKey last_network_anonymization_key_;

  scoped_refptr<State> state_;
  bool shutting_down_ = false;
  base::WeakPtrFactory<HangingHostResolver> weak_ptr_factory_{this};
};

// 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 {
 public:
  ScopedDefaultHostResolverProc();
  explicit ScopedDefaultHostResolverProc(HostResolverProc* proc);

  ~ScopedDefaultHostResolverProc();

  void Init(HostResolverProc* proc);

 private:
  scoped_refptr<HostResolverProc> current_proc_;
  scoped_refptr<HostResolverProc> previous_proc_;
};

}  // namespace net

#endif  // NET_DNS_MOCK_HOST_RESOLVER_H_