Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 1 | // 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 Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 17 | #include "net/proxy_resolution/proxy_resolve_dns_operation.h" |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 18 | |
| 19 | namespace 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(). |
| 24 | class 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 Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 33 | ProxyResolveDnsOperation operation) override; |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 34 | |
| 35 | void SetError(const std::string& hostname, |
Eric Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 36 | ProxyResolveDnsOperation operation); |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 37 | |
| 38 | void SetResult(const std::string& hostname, |
Eric Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 39 | ProxyResolveDnsOperation operation, |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 40 | std::vector<IPAddress> result); |
| 41 | |
| 42 | void FailAll(); |
| 43 | |
| 44 | unsigned num_resolve() const { return num_resolve_; } |
| 45 | |
| 46 | private: |
Eric Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 47 | using ResultKey = std::pair<std::string, ProxyResolveDnsOperation>; |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 48 | |
| 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. |
| 59 | class 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 Orth | fa19a83 | 2019-02-13 20:04:50 | [diff] [blame] | 68 | ProxyResolveDnsOperation operation) override; |
Eric Orth | 3757467 | 2019-01-31 02:58:08 | [diff] [blame] | 69 | |
| 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_ |