blob: cb3e87f09e1b03c9813f430291acadf82e778fa5 [file] [log] [blame]
Eric Orth37574672019-01-31 02:58:081// Copyright 2019 The Chromium Authors. All rights reserved.
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_PROXY_RESOLUTION_MOCK_PROXY_HOST_RESOLVER_H_
6#define NET_PROXY_RESOLUTION_MOCK_PROXY_HOST_RESOLVER_H_
7
8#include <map>
9#include <memory>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "base/callback_forward.h"
15#include "net/base/ip_address.h"
16#include "net/proxy_resolution/proxy_host_resolver.h"
Eric Orthfa19a832019-02-13 20:04:5017#include "net/proxy_resolution/proxy_resolve_dns_operation.h"
Eric Orth37574672019-01-31 02:58:0818
19namespace net {
20
21// Mock of ProxyHostResolver that resolves by default to 127.0.0.1, except for
22// hostnames with more specific results set using SetError() or SetResult().
23// Also allows returning failure for all results with FailAll().
24class MockProxyHostResolver : public ProxyHostResolver {
25 public:
26 // If |synchronous_mode| set to |true|, all results will be returned
27 // synchronously. Otherwise, all results will be asynchronous.
28 explicit MockProxyHostResolver(bool synchronous_mode = false);
29 ~MockProxyHostResolver() override;
30
31 std::unique_ptr<Request> CreateRequest(
32 const std::string& hostname,
Eric Orthfa19a832019-02-13 20:04:5033 ProxyResolveDnsOperation operation) override;
Eric Orth37574672019-01-31 02:58:0834
35 void SetError(const std::string& hostname,
Eric Orthfa19a832019-02-13 20:04:5036 ProxyResolveDnsOperation operation);
Eric Orth37574672019-01-31 02:58:0837
38 void SetResult(const std::string& hostname,
Eric Orthfa19a832019-02-13 20:04:5039 ProxyResolveDnsOperation operation,
Eric Orth37574672019-01-31 02:58:0840 std::vector<IPAddress> result);
41
42 void FailAll();
43
44 unsigned num_resolve() const { return num_resolve_; }
45
46 private:
Eric Orthfa19a832019-02-13 20:04:5047 using ResultKey = std::pair<std::string, ProxyResolveDnsOperation>;
Eric Orth37574672019-01-31 02:58:0848
49 class RequestImpl;
50
51 // Any entry with an empty value signifies an ERR_NAME_NOT_RESOLVED result.
52 std::map<ResultKey, std::vector<IPAddress>> results_;
53 unsigned num_resolve_;
54 bool fail_all_;
55 bool synchronous_mode_;
56};
57
58// Mock of ProxyHostResolver that always hangs until cancelled.
59class HangingProxyHostResolver : public ProxyHostResolver {
60 public:
61 // If not null, |hang_callback| will be invoked whenever a request is started.
62 HangingProxyHostResolver(
63 base::RepeatingClosure hang_callback = base::RepeatingClosure());
64 ~HangingProxyHostResolver() override;
65
66 std::unique_ptr<Request> CreateRequest(
67 const std::string& hostname,
Eric Orthfa19a832019-02-13 20:04:5068 ProxyResolveDnsOperation operation) override;
Eric Orth37574672019-01-31 02:58:0869
70 int num_cancelled_requests() const { return num_cancelled_requests_; }
71
72 void set_hang_callback(base::RepeatingClosure hang_callback) {
73 hang_callback_ = hang_callback;
74 }
75
76 private:
77 class RequestImpl;
78
79 int num_cancelled_requests_;
80 base::RepeatingClosure hang_callback_;
81};
82
83} // namespace net
84
85#endif // NET_PROXY_RESOLUTION_MOCK_PROXY_HOST_RESOLVER_H_