| OLD | NEW |
| 1 // Copyright (c) 2006-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_HOST_RESOLVER_UNITTEST_H_ | 5 #ifndef NET_BASE_MOCK_HOST_RESOLVER_H_ |
| 6 #define NET_BASE_HOST_RESOLVER_UNITTEST_H_ | 6 #define NET_BASE_MOCK_HOST_RESOLVER_H_ |
| 7 | |
| 8 #ifdef UNIT_TEST | |
| 9 | 7 |
| 10 #include <list> | 8 #include <list> |
| 11 | 9 |
| 12 #include "base/string_util.h" | |
| 13 #include "base/platform_thread.h" | |
| 14 #include "base/ref_counted.h" | |
| 15 #include "base/waitable_event.h" | 10 #include "base/waitable_event.h" |
| 16 #include "net/base/host_resolver.h" | 11 #include "net/base/host_resolver_impl.h" |
| 17 #include "net/base/net_errors.h" | 12 #include "net/base/host_resolver_proc.h" |
| 18 | 13 |
| 19 namespace net { | 14 namespace net { |
| 20 | 15 |
| 16 class RuleBasedHostResolverProc; |
| 17 |
| 21 // In most cases, it is important that unit tests avoid making actual DNS | 18 // In most cases, it is important that unit tests avoid making actual DNS |
| 22 // queries since the resulting tests can be flaky, especially if the network is | 19 // queries since the resulting tests can be flaky, especially if the network is |
| 23 // unreliable for some reason. To simplify writing tests that avoid making | 20 // unreliable for some reason. To simplify writing tests that avoid making |
| 24 // actual DNS queries, the following helper class may be used: | 21 // actual DNS queries, pass a MockHostResolver as the HostResolver dependency. |
| 22 // The socket addresses returned can be configured using the |
| 23 // RuleBasedHostResolverProc: |
| 25 // | 24 // |
| 26 // scoped_refptr<RuleBasedHostMapper> host_mapper = new RuleBasedHostMapper(); | 25 // host_resolver->rules()->AddRule("foo.com", "1.2.3.4"); |
| 27 // host_mapper->AddRule("foo.com", "1.2.3.4"); | 26 // host_resolver->rules()->AddRule("bar.com", "2.3.4.5"); |
| 28 // host_mapper->AddRule("bar.com", "2.3.4.5"); | |
| 29 // | |
| 30 // Don't forget to actually set your mapper, probably with ScopedHostMapper! | |
| 31 // | 27 // |
| 32 // The above rules define a static mapping from hostnames to IP address | 28 // The above rules define a static mapping from hostnames to IP address |
| 33 // literals. The first parameter to AddRule specifies a host pattern to match | 29 // literals. The first parameter to AddRule specifies a host pattern to match |
| 34 // against, and the second parameter indicates what value should be used to | 30 // against, and the second parameter indicates what value should be used to |
| 35 // replace the given hostname. So, the following is also supported: | 31 // replace the given hostname. So, the following is also supported: |
| 36 // | 32 // |
| 37 // host_mapper->AddRule("*.com", "127.0.0.1"); | 33 // host_mapper->AddRule("*.com", "127.0.0.1"); |
| 38 // | 34 // |
| 39 // 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 |
| 40 // re-map one hostname to another as well. | 36 // re-map one hostname to another as well. |
| 41 class RuleBasedHostMapper : public HostMapper { | 37 class MockHostResolver : public HostResolver { |
| 42 public: | 38 public: |
| 39 // Creates a MockHostResolver that does NOT cache entries |
| 40 // (the HostResolverProc will be called for every lookup). If you need |
| 41 // caching behavior, call Reset() with non-zero cache size. |
| 42 MockHostResolver(); |
| 43 |
| 44 virtual ~MockHostResolver() {} |
| 45 |
| 46 // HostResolver methods: |
| 47 virtual int Resolve(const RequestInfo& info, AddressList* addresses, |
| 48 CompletionCallback* callback, RequestHandle* out_req); |
| 49 virtual void CancelRequest(RequestHandle req); |
| 50 virtual void AddObserver(Observer* observer); |
| 51 virtual void RemoveObserver(Observer* observer); |
| 52 // TODO(eroman): temp hack for http://crbug.com/15513 |
| 53 virtual void Shutdown(); |
| 54 |
| 55 RuleBasedHostResolverProc* rules() { return rules_; } |
| 56 |
| 57 // Resets the mock. |
| 58 void Reset(HostResolverProc* interceptor, |
| 59 int max_cache_entries, |
| 60 int max_cache_age_ms); |
| 61 |
| 62 private: |
| 63 scoped_refptr<HostResolverImpl> impl_; |
| 64 scoped_refptr<RuleBasedHostResolverProc> rules_; |
| 65 }; |
| 66 |
| 67 // RuleBasedHostResolverProc applies a set of rules to map a host string to |
| 68 // a replacement host string. It then uses the system host resolver to return |
| 69 // a socket address. Generally the replacement should be an IPv4 literal so |
| 70 // there is no network dependency. |
| 71 class RuleBasedHostResolverProc : public HostResolverProc { |
| 72 public: |
| 73 explicit RuleBasedHostResolverProc(HostResolverProc* previous); |
| 74 ~RuleBasedHostResolverProc(); |
| 75 |
| 43 // Any hostname matching the given pattern will be replaced with the given | 76 // Any hostname matching the given pattern will be replaced with the given |
| 44 // replacement value. Usually, replacement should be an IP address literal. | 77 // replacement value. Usually, replacement should be an IP address literal. |
| 45 void AddRule(const char* host_pattern, const char* replacement) { | 78 void AddRule(const std::string& host_pattern, |
| 46 rules_.push_back(Rule(host_pattern, replacement)); | 79 const std::string& replacement); |
| 47 } | |
| 48 | 80 |
| 49 void AddRuleWithLatency(const char* host_pattern, const char* replacement, | 81 void AddRuleWithLatency(const std::string& host_pattern, |
| 50 int latency) { | 82 const std::string& replacement, |
| 51 rules_.push_back(Rule(host_pattern, replacement, latency)); | 83 int latency_ms); |
| 52 } | |
| 53 | 84 |
| 54 // Make sure that |host| will not be re-mapped or even processed by underlying | 85 // Make sure that |host| will not be re-mapped or even processed by underlying |
| 55 // host mappers. It can also be a pattern. | 86 // host resolver procedures. It can also be a pattern. |
| 56 void AllowDirectLookup(const char* host) { | 87 void AllowDirectLookup(const std::string& host); |
| 57 rules_.push_back(Rule(host, "", true)); | |
| 58 } | |
| 59 | 88 |
| 60 // Simulate a lookup failure for |host| (it also can be a pattern). | 89 // Simulate a lookup failure for |host| (it also can be a pattern). |
| 61 void AddSimulatedFailure(const char* host) { | 90 void AddSimulatedFailure(const std::string& host); |
| 62 AddRule(host, ""); | |
| 63 } | |
| 64 | 91 |
| 65 virtual std::string Map(const std::string& host) { | 92 // HostResolverProc methods: |
| 66 RuleList::iterator r; | 93 virtual int Resolve(const std::string& host, AddressList* addrlist); |
| 67 for (r = rules_.begin(); r != rules_.end(); ++r) { | |
| 68 if (MatchPattern(host, r->host_pattern)) { | |
| 69 if (r->latency != 0) { | |
| 70 PlatformThread::Sleep(r->latency); | |
| 71 r->latency = 1; // Simulate cache warmup. | |
| 72 } | |
| 73 return r->direct ? host : r->replacement; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 return MapUsingPrevious(host); | |
| 78 } | |
| 79 | 94 |
| 80 private: | 95 private: |
| 81 struct Rule { | 96 struct Rule; |
| 82 std::string host_pattern; | |
| 83 std::string replacement; | |
| 84 int latency; // in milliseconds | |
| 85 bool direct; // if true, don't mangle hostname and ignore replacement | |
| 86 Rule(const char* h, const char* r) | |
| 87 : host_pattern(h), | |
| 88 replacement(r), | |
| 89 latency(0), | |
| 90 direct(false) {} | |
| 91 Rule(const char* h, const char* r, const int l) | |
| 92 : host_pattern(h), | |
| 93 replacement(r), | |
| 94 latency(l), | |
| 95 direct(false) {} | |
| 96 Rule(const char* h, const char* r, const bool d) | |
| 97 : host_pattern(h), | |
| 98 replacement(r), | |
| 99 latency(0), | |
| 100 direct(d) {} | |
| 101 }; | |
| 102 typedef std::list<Rule> RuleList; | 97 typedef std::list<Rule> RuleList; |
| 103 RuleList rules_; | 98 RuleList rules_; |
| 104 }; | 99 }; |
| 105 | 100 |
| 106 // Using WaitingHostMapper you can simulate very long lookups, for example | 101 // Using WaitingHostResolverProc you can simulate very long lookups. |
| 107 // to test code which cancels a request. Example usage: | 102 class WaitingHostResolverProc : public HostResolverProc { |
| 108 // | |
| 109 // scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper(); | |
| 110 // ScopedHostMapper scoped_mapper(mapper.get()); | |
| 111 // | |
| 112 // (start the lookup asynchronously) | |
| 113 // (cancel the lookup) | |
| 114 // | |
| 115 // mapper->Signal(); | |
| 116 class WaitingHostMapper : public HostMapper { | |
| 117 public: | 103 public: |
| 118 WaitingHostMapper() : event_(false, false) { | 104 explicit WaitingHostResolverProc(HostResolverProc* previous) |
| 119 } | 105 : HostResolverProc(previous), event_(false, false) {} |
| 120 | 106 |
| 121 void Signal() { | 107 void Signal() { |
| 122 event_.Signal(); | 108 event_.Signal(); |
| 123 } | 109 } |
| 124 | 110 |
| 125 private: | 111 // HostResolverProc methods: |
| 126 virtual std::string Map(const std::string& host) { | 112 virtual int Resolve(const std::string& host, AddressList* addrlist) { |
| 127 event_.Wait(); | 113 event_.Wait(); |
| 128 return MapUsingPrevious(host); | 114 return ResolveUsingPrevious(host, addrlist); |
| 129 } | 115 } |
| 130 | 116 |
| 131 base::WaitableEvent event_; | 117 base::WaitableEvent event_; |
| 132 }; | 118 }; |
| 133 | 119 |
| 134 // This class sets the HostMapper for a particular scope. If there are multiple | 120 // This class sets the HostResolverProc for a particular scope. If there are |
| 135 // ScopedHostMappers in existence, then the last one allocated will be used. | 121 // multiple ScopedDefaultHostResolverProc in existence, then the last one |
| 136 // However, if it does not provide a matching rule, then it should delegate | 122 // allocated will be used. However, if it does not provide a matching rule, |
| 137 // to the previously set HostMapper (see SetHostMapper). This is true for all | 123 // then it should delegate to the previously set HostResolverProc. |
| 138 // mappers defined in this file. If no HostMapper matches a given hostname, then | 124 // |
| 139 // the hostname will be unmodified. | 125 // NOTE: Only use this as a catch-all safety net. Individual tests should use |
| 140 class ScopedHostMapper { | 126 // MockHostResolver. |
| 127 class ScopedDefaultHostResolverProc { |
| 141 public: | 128 public: |
| 142 ScopedHostMapper(HostMapper* mapper) : current_host_mapper_(mapper) { | 129 ScopedDefaultHostResolverProc() {} |
| 143 previous_host_mapper_ = SetHostMapper(current_host_mapper_.get()); | 130 explicit ScopedDefaultHostResolverProc(HostResolverProc* proc); |
| 144 current_host_mapper_->set_previous_mapper(previous_host_mapper_.get()); | |
| 145 } | |
| 146 | 131 |
| 147 ScopedHostMapper() {} | 132 ~ScopedDefaultHostResolverProc(); |
| 148 | 133 |
| 149 ~ScopedHostMapper() { | 134 void Init(HostResolverProc* proc); |
| 150 HostMapper* old_mapper = SetHostMapper(previous_host_mapper_.get()); | |
| 151 // The lifetimes of multiple instances must be nested. | |
| 152 CHECK(old_mapper == current_host_mapper_.get()); | |
| 153 } | |
| 154 | |
| 155 void Init(HostMapper* mapper) { | |
| 156 current_host_mapper_ = mapper; | |
| 157 previous_host_mapper_ = SetHostMapper(current_host_mapper_.get()); | |
| 158 current_host_mapper_->set_previous_mapper(previous_host_mapper_.get()); | |
| 159 } | |
| 160 | 135 |
| 161 private: | 136 private: |
| 162 scoped_refptr<HostMapper> current_host_mapper_; | 137 scoped_refptr<HostResolverProc> current_proc_; |
| 163 scoped_refptr<HostMapper> previous_host_mapper_; | 138 scoped_refptr<HostResolverProc> previous_proc_; |
| 164 }; | 139 }; |
| 165 | 140 |
| 166 } // namespace net | 141 } // namespace net |
| 167 | 142 |
| 168 #endif // UNIT_TEST | 143 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
| 169 | |
| 170 #endif // NET_BASE_HOST_RESOLVER_UNITTEST_H_ | |
| OLD | NEW |