| OLD | NEW |
| 1 // Copyright (c) 2010 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_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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 | 10 |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "net/base/host_resolver_impl.h" | 12 #include "net/base/host_resolver_impl.h" |
| 13 #include "net/base/host_resolver_proc.h" | 13 #include "net/base/host_resolver_proc.h" |
| 14 #include "net/base/net_api.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 class RuleBasedHostResolverProc; | 18 class RuleBasedHostResolverProc; |
| 18 | 19 |
| 19 // In most cases, it is important that unit tests avoid relying on making actual | 20 // In most cases, it is important that unit tests avoid relying on making actual |
| 20 // DNS queries since the resulting tests can be flaky, especially if the network | 21 // DNS queries since the resulting tests can be flaky, especially if the network |
| 21 // is unreliable for some reason. To simplify writing tests that avoid making | 22 // is unreliable for some reason. To simplify writing tests that avoid making |
| 22 // actual DNS queries, pass a MockHostResolver as the HostResolver dependency. | 23 // actual DNS queries, pass a MockHostResolver as the HostResolver dependency. |
| 23 // The socket addresses returned can be configured using the | 24 // The socket addresses returned can be configured using the |
| 24 // RuleBasedHostResolverProc: | 25 // RuleBasedHostResolverProc: |
| 25 // | 26 // |
| 26 // host_resolver->rules()->AddRule("foo.com", "1.2.3.4"); | 27 // host_resolver->rules()->AddRule("foo.com", "1.2.3.4"); |
| 27 // host_resolver->rules()->AddRule("bar.com", "2.3.4.5"); | 28 // host_resolver->rules()->AddRule("bar.com", "2.3.4.5"); |
| 28 // | 29 // |
| 29 // The above rules define a static mapping from hostnames to IP address | 30 // The above rules define a static mapping from hostnames to IP address |
| 30 // literals. The first parameter to AddRule specifies a host pattern to match | 31 // literals. The first parameter to AddRule specifies a host pattern to match |
| 31 // against, and the second parameter indicates what value should be used to | 32 // against, and the second parameter indicates what value should be used to |
| 32 // replace the given hostname. So, the following is also supported: | 33 // replace the given hostname. So, the following is also supported: |
| 33 // | 34 // |
| 34 // host_mapper->AddRule("*.com", "127.0.0.1"); | 35 // host_mapper->AddRule("*.com", "127.0.0.1"); |
| 35 // | 36 // |
| 36 // Replacement doesn't have to be string representing an IP address. It can | 37 // Replacement doesn't have to be string representing an IP address. It can |
| 37 // re-map one hostname to another as well. | 38 // re-map one hostname to another as well. |
| 38 | 39 |
| 39 // Base class shared by MockHostResolver and MockCachingHostResolver. | 40 // Base class shared by MockHostResolver and MockCachingHostResolver. |
| 40 class MockHostResolverBase : public HostResolver { | 41 class NET_TEST MockHostResolverBase : public HostResolver { |
| 41 public: | 42 public: |
| 42 virtual ~MockHostResolverBase(); | 43 virtual ~MockHostResolverBase(); |
| 43 | 44 |
| 44 RuleBasedHostResolverProc* rules() { return rules_; } | 45 RuleBasedHostResolverProc* rules() { return rules_; } |
| 45 | 46 |
| 46 // Controls whether resolutions complete synchronously or asynchronously. | 47 // Controls whether resolutions complete synchronously or asynchronously. |
| 47 void set_synchronous_mode(bool is_synchronous) { | 48 void set_synchronous_mode(bool is_synchronous) { |
| 48 synchronous_mode_ = is_synchronous; | 49 synchronous_mode_ = is_synchronous; |
| 49 } | 50 } |
| 50 | 51 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 class MockCachingHostResolver : public MockHostResolverBase { | 95 class MockCachingHostResolver : public MockHostResolverBase { |
| 95 public: | 96 public: |
| 96 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} | 97 MockCachingHostResolver() : MockHostResolverBase(true /*use_caching*/) {} |
| 97 ~MockCachingHostResolver() {} | 98 ~MockCachingHostResolver() {} |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 // RuleBasedHostResolverProc applies a set of rules to map a host string to | 101 // RuleBasedHostResolverProc applies a set of rules to map a host string to |
| 101 // a replacement host string. It then uses the system host resolver to return | 102 // a replacement host string. It then uses the system host resolver to return |
| 102 // a socket address. Generally the replacement should be an IPv4 literal so | 103 // a socket address. Generally the replacement should be an IPv4 literal so |
| 103 // there is no network dependency. | 104 // there is no network dependency. |
| 104 class RuleBasedHostResolverProc : public HostResolverProc { | 105 class NET_TEST RuleBasedHostResolverProc : public HostResolverProc { |
| 105 public: | 106 public: |
| 106 explicit RuleBasedHostResolverProc(HostResolverProc* previous); | 107 explicit RuleBasedHostResolverProc(HostResolverProc* previous); |
| 107 | 108 |
| 108 // Any hostname matching the given pattern will be replaced with the given | 109 // Any hostname matching the given pattern will be replaced with the given |
| 109 // replacement value. Usually, replacement should be an IP address literal. | 110 // replacement value. Usually, replacement should be an IP address literal. |
| 110 void AddRule(const std::string& host_pattern, | 111 void AddRule(const std::string& host_pattern, |
| 111 const std::string& replacement); | 112 const std::string& replacement); |
| 112 | 113 |
| 113 // Same as AddRule(), but further restricts to |address_family|. | 114 // Same as AddRule(), but further restricts to |address_family|. |
| 114 void AddRuleForAddressFamily(const std::string& host_pattern, | 115 void AddRuleForAddressFamily(const std::string& host_pattern, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 145 private: | 146 private: |
| 146 struct Rule; | 147 struct Rule; |
| 147 typedef std::list<Rule> RuleList; | 148 typedef std::list<Rule> RuleList; |
| 148 | 149 |
| 149 ~RuleBasedHostResolverProc(); | 150 ~RuleBasedHostResolverProc(); |
| 150 | 151 |
| 151 RuleList rules_; | 152 RuleList rules_; |
| 152 }; | 153 }; |
| 153 | 154 |
| 154 // Using WaitingHostResolverProc you can simulate very long lookups. | 155 // Using WaitingHostResolverProc you can simulate very long lookups. |
| 155 class WaitingHostResolverProc : public HostResolverProc { | 156 class NET_TEST WaitingHostResolverProc : public HostResolverProc { |
| 156 public: | 157 public: |
| 157 explicit WaitingHostResolverProc(HostResolverProc* previous); | 158 explicit WaitingHostResolverProc(HostResolverProc* previous); |
| 158 | 159 |
| 159 void Signal(); | 160 void Signal(); |
| 160 | 161 |
| 161 // HostResolverProc methods: | 162 // HostResolverProc methods: |
| 162 virtual int Resolve(const std::string& host, | 163 virtual int Resolve(const std::string& host, |
| 163 AddressFamily address_family, | 164 AddressFamily address_family, |
| 164 HostResolverFlags host_resolver_flags, | 165 HostResolverFlags host_resolver_flags, |
| 165 AddressList* addrlist, | 166 AddressList* addrlist, |
| 166 int* os_error); | 167 int* os_error); |
| 167 | 168 |
| 168 private: | 169 private: |
| 169 virtual ~WaitingHostResolverProc(); | 170 virtual ~WaitingHostResolverProc(); |
| 170 | 171 |
| 171 base::WaitableEvent event_; | 172 base::WaitableEvent event_; |
| 172 }; | 173 }; |
| 173 | 174 |
| 174 // This class sets the default HostResolverProc for a particular scope. The | 175 // This class sets the default HostResolverProc for a particular scope. The |
| 175 // chain of resolver procs starting at |proc| is placed in front of any existing | 176 // chain of resolver procs starting at |proc| is placed in front of any existing |
| 176 // default resolver proc(s). This means that if multiple | 177 // default resolver proc(s). This means that if multiple |
| 177 // ScopedDefaultHostResolverProcs are declared, then resolving will start with | 178 // ScopedDefaultHostResolverProcs are declared, then resolving will start with |
| 178 // the procs given to the last-allocated one, then fall back to the procs given | 179 // the procs given to the last-allocated one, then fall back to the procs given |
| 179 // to the previously-allocated one, and so forth. | 180 // to the previously-allocated one, and so forth. |
| 180 // | 181 // |
| 181 // NOTE: Only use this as a catch-all safety net. Individual tests should use | 182 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 182 // MockHostResolver. | 183 // MockHostResolver. |
| 183 class ScopedDefaultHostResolverProc { | 184 class NET_TEST ScopedDefaultHostResolverProc { |
| 184 public: | 185 public: |
| 185 ScopedDefaultHostResolverProc(); | 186 ScopedDefaultHostResolverProc(); |
| 186 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); | 187 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 187 | 188 |
| 188 ~ScopedDefaultHostResolverProc(); | 189 ~ScopedDefaultHostResolverProc(); |
| 189 | 190 |
| 190 void Init(HostResolverProc* proc); | 191 void Init(HostResolverProc* proc); |
| 191 | 192 |
| 192 private: | 193 private: |
| 193 scoped_refptr<HostResolverProc> current_proc_; | 194 scoped_refptr<HostResolverProc> current_proc_; |
| 194 scoped_refptr<HostResolverProc> previous_proc_; | 195 scoped_refptr<HostResolverProc> previous_proc_; |
| 195 }; | 196 }; |
| 196 | 197 |
| 197 } // namespace net | 198 } // namespace net |
| 198 | 199 |
| 199 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ | 200 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
| OLD | NEW |