| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_DNS_MOCK_HOST_RESOLVER_H_ | 5 #ifndef NET_DNS_MOCK_HOST_RESOLVER_H_ |
| 6 #define NET_DNS_MOCK_HOST_RESOLVER_H_ | 6 #define NET_DNS_MOCK_HOST_RESOLVER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // re-map one hostname to another as well. | 48 // re-map one hostname to another as well. |
| 49 // | 49 // |
| 50 // By default, MockHostResolvers include a single rule that maps all hosts to | 50 // By default, MockHostResolvers include a single rule that maps all hosts to |
| 51 // 127.0.0.1. | 51 // 127.0.0.1. |
| 52 | 52 |
| 53 // Base class shared by MockHostResolver and MockCachingHostResolver. | 53 // Base class shared by MockHostResolver and MockCachingHostResolver. |
| 54 class MockHostResolverBase : public HostResolver, | 54 class MockHostResolverBase : public HostResolver, |
| 55 public base::SupportsWeakPtr<MockHostResolverBase>, | 55 public base::SupportsWeakPtr<MockHostResolverBase>, |
| 56 public base::NonThreadSafe { | 56 public base::NonThreadSafe { |
| 57 public: | 57 public: |
| 58 virtual ~MockHostResolverBase(); | 58 ~MockHostResolverBase() override; |
| 59 | 59 |
| 60 RuleBasedHostResolverProc* rules() { return rules_.get(); } | 60 RuleBasedHostResolverProc* rules() { return rules_.get(); } |
| 61 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } | 61 void set_rules(RuleBasedHostResolverProc* rules) { rules_ = rules; } |
| 62 | 62 |
| 63 // Controls whether resolutions complete synchronously or asynchronously. | 63 // Controls whether resolutions complete synchronously or asynchronously. |
| 64 void set_synchronous_mode(bool is_synchronous) { | 64 void set_synchronous_mode(bool is_synchronous) { |
| 65 synchronous_mode_ = is_synchronous; | 65 synchronous_mode_ = is_synchronous; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Asynchronous requests are automatically resolved by default. | 68 // Asynchronous requests are automatically resolved by default. |
| 69 // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and | 69 // If set_ondemand_mode() is set then Resolve() returns IO_PENDING and |
| 70 // ResolveAllPending() must be explicitly invoked to resolve all requests | 70 // ResolveAllPending() must be explicitly invoked to resolve all requests |
| 71 // that are pending. | 71 // that are pending. |
| 72 void set_ondemand_mode(bool is_ondemand) { | 72 void set_ondemand_mode(bool is_ondemand) { |
| 73 ondemand_mode_ = is_ondemand; | 73 ondemand_mode_ = is_ondemand; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // HostResolver methods: | 76 // HostResolver methods: |
| 77 virtual int Resolve(const RequestInfo& info, | 77 int Resolve(const RequestInfo& info, |
| 78 RequestPriority priority, | 78 RequestPriority priority, |
| 79 AddressList* addresses, | 79 AddressList* addresses, |
| 80 const CompletionCallback& callback, | 80 const CompletionCallback& callback, |
| 81 RequestHandle* out_req, | 81 RequestHandle* out_req, |
| 82 const BoundNetLog& net_log) override; | 82 const BoundNetLog& net_log) override; |
| 83 virtual int ResolveFromCache(const RequestInfo& info, | 83 int ResolveFromCache(const RequestInfo& info, |
| 84 AddressList* addresses, | 84 AddressList* addresses, |
| 85 const BoundNetLog& net_log) override; | 85 const BoundNetLog& net_log) override; |
| 86 virtual void CancelRequest(RequestHandle req) override; | 86 void CancelRequest(RequestHandle req) override; |
| 87 virtual HostCache* GetHostCache() override; | 87 HostCache* GetHostCache() override; |
| 88 | 88 |
| 89 // Resolves all pending requests. It is only valid to invoke this if | 89 // Resolves all pending requests. It is only valid to invoke this if |
| 90 // set_ondemand_mode was set before. The requests are resolved asynchronously, | 90 // set_ondemand_mode was set before. The requests are resolved asynchronously, |
| 91 // after this call returns. | 91 // after this call returns. |
| 92 void ResolveAllPending(); | 92 void ResolveAllPending(); |
| 93 | 93 |
| 94 // Returns true if there are pending requests that can be resolved by invoking | 94 // Returns true if there are pending requests that can be resolved by invoking |
| 95 // ResolveAllPending(). | 95 // ResolveAllPending(). |
| 96 bool has_pending_requests() const { return !requests_.empty(); } | 96 bool has_pending_requests() const { return !requests_.empty(); } |
| 97 | 97 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 size_t num_resolve_; | 138 size_t num_resolve_; |
| 139 size_t num_resolve_from_cache_; | 139 size_t num_resolve_from_cache_; |
| 140 | 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); | 141 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 class MockHostResolver : public MockHostResolverBase { | 144 class MockHostResolver : public MockHostResolverBase { |
| 145 public: | 145 public: |
| 146 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} | 146 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} |
| 147 virtual ~MockHostResolver() {} | 147 ~MockHostResolver() override {} |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 // Same as MockHostResolver, except internally it uses a host-cache. | 150 // Same as MockHostResolver, except internally it uses a host-cache. |
| 151 // | 151 // |
| 152 // Note that tests are advised to use MockHostResolver instead, since it is | 152 // Note that tests are advised to use MockHostResolver instead, since it is |
| 153 // more predictable. (MockHostResolver also can be put into synchronous | 153 // more predictable. (MockHostResolver also can be put into synchronous |
| 154 // operation mode in case that is what you needed from the caching version). | 154 // operation mode in case that is what you needed from the caching version). |
| 155 class MockCachingHostResolver : public MockHostResolverBase { | 155 class MockCachingHostResolver : public MockHostResolverBase { |
| 156 public: | 156 public: |
| 157 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} | 157 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} |
| 158 virtual ~MockCachingHostResolver() {} | 158 ~MockCachingHostResolver() override {} |
| 159 }; | 159 }; |
| 160 | 160 |
| 161 // RuleBasedHostResolverProc applies a set of rules to map a host string to | 161 // RuleBasedHostResolverProc applies a set of rules to map a host string to |
| 162 // a replacement host string. It then uses the system host resolver to return | 162 // a replacement host string. It then uses the system host resolver to return |
| 163 // a socket address. Generally the replacement should be an IPv4 literal so | 163 // a socket address. Generally the replacement should be an IPv4 literal so |
| 164 // there is no network dependency. | 164 // there is no network dependency. |
| 165 class RuleBasedHostResolverProc : public HostResolverProc { | 165 class RuleBasedHostResolverProc : public HostResolverProc { |
| 166 public: | 166 public: |
| 167 explicit RuleBasedHostResolverProc(HostResolverProc* previous); | 167 explicit RuleBasedHostResolverProc(HostResolverProc* previous); |
| 168 | 168 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 195 // host resolver procedures. It can also be a pattern. | 195 // host resolver procedures. It can also be a pattern. |
| 196 void AllowDirectLookup(const std::string& host); | 196 void AllowDirectLookup(const std::string& host); |
| 197 | 197 |
| 198 // Simulate a lookup failure for |host| (it also can be a pattern). | 198 // Simulate a lookup failure for |host| (it also can be a pattern). |
| 199 void AddSimulatedFailure(const std::string& host); | 199 void AddSimulatedFailure(const std::string& host); |
| 200 | 200 |
| 201 // Deletes all the rules that have been added. | 201 // Deletes all the rules that have been added. |
| 202 void ClearRules(); | 202 void ClearRules(); |
| 203 | 203 |
| 204 // HostResolverProc methods: | 204 // HostResolverProc methods: |
| 205 virtual int Resolve(const std::string& host, | 205 int Resolve(const std::string& host, |
| 206 AddressFamily address_family, | 206 AddressFamily address_family, |
| 207 HostResolverFlags host_resolver_flags, | 207 HostResolverFlags host_resolver_flags, |
| 208 AddressList* addrlist, | 208 AddressList* addrlist, |
| 209 int* os_error) override; | 209 int* os_error) override; |
| 210 | 210 |
| 211 private: | 211 private: |
| 212 struct Rule; | 212 struct Rule; |
| 213 typedef std::list<Rule> RuleList; | 213 typedef std::list<Rule> RuleList; |
| 214 | 214 |
| 215 virtual ~RuleBasedHostResolverProc(); | 215 ~RuleBasedHostResolverProc() override; |
| 216 | 216 |
| 217 RuleList rules_; | 217 RuleList rules_; |
| 218 }; | 218 }; |
| 219 | 219 |
| 220 // Create rules that map all requests to localhost. | 220 // Create rules that map all requests to localhost. |
| 221 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc(); | 221 RuleBasedHostResolverProc* CreateCatchAllHostResolverProc(); |
| 222 | 222 |
| 223 // HangingHostResolver never completes its |Resolve| request. | 223 // HangingHostResolver never completes its |Resolve| request. |
| 224 class HangingHostResolver : public HostResolver { | 224 class HangingHostResolver : public HostResolver { |
| 225 public: | 225 public: |
| 226 virtual int Resolve(const RequestInfo& info, | 226 int Resolve(const RequestInfo& info, |
| 227 RequestPriority priority, | 227 RequestPriority priority, |
| 228 AddressList* addresses, | 228 AddressList* addresses, |
| 229 const CompletionCallback& callback, | 229 const CompletionCallback& callback, |
| 230 RequestHandle* out_req, | 230 RequestHandle* out_req, |
| 231 const BoundNetLog& net_log) override; | 231 const BoundNetLog& net_log) override; |
| 232 virtual int ResolveFromCache(const RequestInfo& info, | 232 int ResolveFromCache(const RequestInfo& info, |
| 233 AddressList* addresses, | 233 AddressList* addresses, |
| 234 const BoundNetLog& net_log) override; | 234 const BoundNetLog& net_log) override; |
| 235 virtual void CancelRequest(RequestHandle req) override {} | 235 void CancelRequest(RequestHandle req) override {} |
| 236 }; | 236 }; |
| 237 | 237 |
| 238 // This class sets the default HostResolverProc for a particular scope. The | 238 // This class sets the default HostResolverProc for a particular scope. The |
| 239 // chain of resolver procs starting at |proc| is placed in front of any existing | 239 // chain of resolver procs starting at |proc| is placed in front of any existing |
| 240 // default resolver proc(s). This means that if multiple | 240 // default resolver proc(s). This means that if multiple |
| 241 // ScopedDefaultHostResolverProcs are declared, then resolving will start with | 241 // ScopedDefaultHostResolverProcs are declared, then resolving will start with |
| 242 // the procs given to the last-allocated one, then fall back to the procs given | 242 // the procs given to the last-allocated one, then fall back to the procs given |
| 243 // to the previously-allocated one, and so forth. | 243 // to the previously-allocated one, and so forth. |
| 244 // | 244 // |
| 245 // NOTE: Only use this as a catch-all safety net. Individual tests should use | 245 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 246 // MockHostResolver. | 246 // MockHostResolver. |
| 247 class ScopedDefaultHostResolverProc { | 247 class ScopedDefaultHostResolverProc { |
| 248 public: | 248 public: |
| 249 ScopedDefaultHostResolverProc(); | 249 ScopedDefaultHostResolverProc(); |
| 250 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); | 250 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 251 | 251 |
| 252 ~ScopedDefaultHostResolverProc(); | 252 ~ScopedDefaultHostResolverProc(); |
| 253 | 253 |
| 254 void Init(HostResolverProc* proc); | 254 void Init(HostResolverProc* proc); |
| 255 | 255 |
| 256 private: | 256 private: |
| 257 scoped_refptr<HostResolverProc> current_proc_; | 257 scoped_refptr<HostResolverProc> current_proc_; |
| 258 scoped_refptr<HostResolverProc> previous_proc_; | 258 scoped_refptr<HostResolverProc> previous_proc_; |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 } // namespace net | 261 } // namespace net |
| 262 | 262 |
| 263 #endif // NET_DNS_MOCK_HOST_RESOLVER_H_ | 263 #endif // NET_DNS_MOCK_HOST_RESOLVER_H_ |
| OLD | NEW |