| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_BASE_MOCK_HOST_RESOLVER_H_ | 5 #ifndef NET_BASE_MOCK_HOST_RESOLVER_H_ |
| 6 #define NET_BASE_MOCK_HOST_RESOLVER_H_ | 6 #define NET_BASE_MOCK_HOST_RESOLVER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/waitable_event.h" | 10 #include "base/waitable_event.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // replace the given hostname. So, the following is also supported: | 31 // replace the given hostname. So, the following is also supported: |
| 32 // | 32 // |
| 33 // host_mapper->AddRule("*.com", "127.0.0.1"); | 33 // host_mapper->AddRule("*.com", "127.0.0.1"); |
| 34 // | 34 // |
| 35 // Replacement doesn't have to be string representing an IP address. It can | 35 // Replacement doesn't have to be string representing an IP address. It can |
| 36 // re-map one hostname to another as well. | 36 // re-map one hostname to another as well. |
| 37 | 37 |
| 38 // Base class shared by MockHostResolver and MockCachingHostResolver. | 38 // Base class shared by MockHostResolver and MockCachingHostResolver. |
| 39 class MockHostResolverBase : public HostResolver { | 39 class MockHostResolverBase : public HostResolver { |
| 40 public: | 40 public: |
| 41 virtual ~MockHostResolverBase() {} | |
| 42 | |
| 43 // HostResolver methods: | 41 // HostResolver methods: |
| 44 virtual int Resolve(const RequestInfo& info, | 42 virtual int Resolve(const RequestInfo& info, |
| 45 AddressList* addresses, | 43 AddressList* addresses, |
| 46 CompletionCallback* callback, | 44 CompletionCallback* callback, |
| 47 RequestHandle* out_req, | 45 RequestHandle* out_req, |
| 48 LoadLog* load_log); | 46 LoadLog* load_log); |
| 49 virtual void CancelRequest(RequestHandle req); | 47 virtual void CancelRequest(RequestHandle req); |
| 50 virtual void AddObserver(Observer* observer); | 48 virtual void AddObserver(Observer* observer); |
| 51 virtual void RemoveObserver(Observer* observer); | 49 virtual void RemoveObserver(Observer* observer); |
| 52 virtual HostCache* GetHostCache(); | 50 virtual HostCache* GetHostCache(); |
| 53 // TODO(eroman): temp hack for http://crbug.com/18373 | 51 // TODO(eroman): temp hack for http://crbug.com/18373 |
| 54 virtual void Shutdown(); | 52 virtual void Shutdown(); |
| 55 | 53 |
| 56 RuleBasedHostResolverProc* rules() { return rules_; } | 54 RuleBasedHostResolverProc* rules() { return rules_; } |
| 57 | 55 |
| 58 // Controls whether resolutions complete synchronously or asynchronously. | 56 // Controls whether resolutions complete synchronously or asynchronously. |
| 59 void set_synchronous_mode(bool is_synchronous) { | 57 void set_synchronous_mode(bool is_synchronous) { |
| 60 synchronous_mode_ = is_synchronous; | 58 synchronous_mode_ = is_synchronous; |
| 61 } | 59 } |
| 62 | 60 |
| 63 // Resets the mock. | 61 // Resets the mock. |
| 64 void Reset(HostResolverProc* interceptor); | 62 void Reset(HostResolverProc* interceptor); |
| 65 | 63 |
| 66 protected: | 64 protected: |
| 67 MockHostResolverBase(bool use_caching); | 65 MockHostResolverBase(bool use_caching); |
| 66 virtual ~MockHostResolverBase() {} |
| 68 | 67 |
| 69 private: | |
| 70 scoped_refptr<HostResolverImpl> impl_; | 68 scoped_refptr<HostResolverImpl> impl_; |
| 71 scoped_refptr<RuleBasedHostResolverProc> rules_; | 69 scoped_refptr<RuleBasedHostResolverProc> rules_; |
| 72 bool synchronous_mode_; | 70 bool synchronous_mode_; |
| 73 bool use_caching_; | 71 bool use_caching_; |
| 74 }; | 72 }; |
| 75 | 73 |
| 76 class MockHostResolver : public MockHostResolverBase { | 74 class MockHostResolver : public MockHostResolverBase { |
| 77 public: | 75 public: |
| 78 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} | 76 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} |
| 77 |
| 78 private: |
| 79 virtual ~MockHostResolver() {} |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 // Same as MockHostResolver, except internally it uses a host-cache. | 82 // Same as MockHostResolver, except internally it uses a host-cache. |
| 82 // | 83 // |
| 83 // Note that tests are advised to use MockHostResolver instead, since it is | 84 // Note that tests are advised to use MockHostResolver instead, since it is |
| 84 // more predictable. (MockHostResolver also can be put into synchronous | 85 // more predictable. (MockHostResolver also can be put into synchronous |
| 85 // operation mode in case that is what you needed from the caching version). | 86 // operation mode in case that is what you needed from the caching version). |
| 86 class MockCachingHostResolver : public MockHostResolverBase { | 87 class MockCachingHostResolver : public MockHostResolverBase { |
| 87 public: | 88 public: |
| 88 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} | 89 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} |
| 90 |
| 91 private: |
| 92 ~MockCachingHostResolver() {} |
| 89 }; | 93 }; |
| 90 | 94 |
| 91 // RuleBasedHostResolverProc applies a set of rules to map a host string to | 95 // RuleBasedHostResolverProc applies a set of rules to map a host string to |
| 92 // a replacement host string. It then uses the system host resolver to return | 96 // a replacement host string. It then uses the system host resolver to return |
| 93 // a socket address. Generally the replacement should be an IPv4 literal so | 97 // a socket address. Generally the replacement should be an IPv4 literal so |
| 94 // there is no network dependency. | 98 // there is no network dependency. |
| 95 class RuleBasedHostResolverProc : public HostResolverProc { | 99 class RuleBasedHostResolverProc : public HostResolverProc { |
| 96 public: | 100 public: |
| 97 explicit RuleBasedHostResolverProc(HostResolverProc* previous); | 101 explicit RuleBasedHostResolverProc(HostResolverProc* previous); |
| 98 ~RuleBasedHostResolverProc(); | |
| 99 | 102 |
| 100 // Any hostname matching the given pattern will be replaced with the given | 103 // Any hostname matching the given pattern will be replaced with the given |
| 101 // replacement value. Usually, replacement should be an IP address literal. | 104 // replacement value. Usually, replacement should be an IP address literal. |
| 102 void AddRule(const std::string& host_pattern, | 105 void AddRule(const std::string& host_pattern, |
| 103 const std::string& replacement); | 106 const std::string& replacement); |
| 104 | 107 |
| 105 // Same as AddRule(), but further restricts to |address_family|. | 108 // Same as AddRule(), but further restricts to |address_family|. |
| 106 void AddRuleForAddressFamily(const std::string& host_pattern, | 109 void AddRuleForAddressFamily(const std::string& host_pattern, |
| 107 AddressFamily address_family, | 110 AddressFamily address_family, |
| 108 const std::string& replacement); | 111 const std::string& replacement); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 124 | 127 |
| 125 // Simulate a lookup failure for |host| (it also can be a pattern). | 128 // Simulate a lookup failure for |host| (it also can be a pattern). |
| 126 void AddSimulatedFailure(const std::string& host); | 129 void AddSimulatedFailure(const std::string& host); |
| 127 | 130 |
| 128 // HostResolverProc methods: | 131 // HostResolverProc methods: |
| 129 virtual int Resolve(const std::string& host, | 132 virtual int Resolve(const std::string& host, |
| 130 AddressFamily address_family, | 133 AddressFamily address_family, |
| 131 AddressList* addrlist); | 134 AddressList* addrlist); |
| 132 | 135 |
| 133 private: | 136 private: |
| 137 ~RuleBasedHostResolverProc(); |
| 138 |
| 134 struct Rule; | 139 struct Rule; |
| 135 typedef std::list<Rule> RuleList; | 140 typedef std::list<Rule> RuleList; |
| 136 | 141 |
| 137 RuleList rules_; | 142 RuleList rules_; |
| 138 }; | 143 }; |
| 139 | 144 |
| 140 // Using WaitingHostResolverProc you can simulate very long lookups. | 145 // Using WaitingHostResolverProc you can simulate very long lookups. |
| 141 class WaitingHostResolverProc : public HostResolverProc { | 146 class WaitingHostResolverProc : public HostResolverProc { |
| 142 public: | 147 public: |
| 143 explicit WaitingHostResolverProc(HostResolverProc* previous) | 148 explicit WaitingHostResolverProc(HostResolverProc* previous) |
| 144 : HostResolverProc(previous), event_(false, false) {} | 149 : HostResolverProc(previous), event_(false, false) {} |
| 145 | 150 |
| 146 void Signal() { | 151 void Signal() { |
| 147 event_.Signal(); | 152 event_.Signal(); |
| 148 } | 153 } |
| 149 | 154 |
| 150 // HostResolverProc methods: | 155 // HostResolverProc methods: |
| 151 virtual int Resolve(const std::string& host, | 156 virtual int Resolve(const std::string& host, |
| 152 AddressFamily address_family, | 157 AddressFamily address_family, |
| 153 AddressList* addrlist) { | 158 AddressList* addrlist) { |
| 154 event_.Wait(); | 159 event_.Wait(); |
| 155 return ResolveUsingPrevious(host, address_family, addrlist); | 160 return ResolveUsingPrevious(host, address_family, addrlist); |
| 156 } | 161 } |
| 157 | 162 |
| 163 private: |
| 164 ~WaitingHostResolverProc() {} |
| 165 |
| 158 base::WaitableEvent event_; | 166 base::WaitableEvent event_; |
| 159 }; | 167 }; |
| 160 | 168 |
| 161 // This class sets the HostResolverProc for a particular scope. If there are | 169 // This class sets the HostResolverProc for a particular scope. If there are |
| 162 // multiple ScopedDefaultHostResolverProc in existence, then the last one | 170 // multiple ScopedDefaultHostResolverProc in existence, then the last one |
| 163 // allocated will be used. However, if it does not provide a matching rule, | 171 // allocated will be used. However, if it does not provide a matching rule, |
| 164 // then it should delegate to the previously set HostResolverProc. | 172 // then it should delegate to the previously set HostResolverProc. |
| 165 // | 173 // |
| 166 // NOTE: Only use this as a catch-all safety net. Individual tests should use | 174 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 167 // MockHostResolver. | 175 // MockHostResolver. |
| 168 class ScopedDefaultHostResolverProc { | 176 class ScopedDefaultHostResolverProc { |
| 169 public: | 177 public: |
| 170 ScopedDefaultHostResolverProc() {} | 178 ScopedDefaultHostResolverProc() {} |
| 171 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); | 179 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 172 | 180 |
| 173 ~ScopedDefaultHostResolverProc(); | 181 ~ScopedDefaultHostResolverProc(); |
| 174 | 182 |
| 175 void Init(HostResolverProc* proc); | 183 void Init(HostResolverProc* proc); |
| 176 | 184 |
| 177 private: | 185 private: |
| 178 scoped_refptr<HostResolverProc> current_proc_; | 186 scoped_refptr<HostResolverProc> current_proc_; |
| 179 scoped_refptr<HostResolverProc> previous_proc_; | 187 scoped_refptr<HostResolverProc> previous_proc_; |
| 180 }; | 188 }; |
| 181 | 189 |
| 182 } // namespace net | 190 } // namespace net |
| 183 | 191 |
| 184 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ | 192 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
| OLD | NEW |