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_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 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 // Resolves all pending requests. It is only valid to invoke this if | 88 // Resolves all pending requests. It is only valid to invoke this if |
89 // set_ondemand_mode was set before. The requests are resolved asynchronously, | 89 // set_ondemand_mode was set before. The requests are resolved asynchronously, |
90 // after this call returns. | 90 // after this call returns. |
91 void ResolveAllPending(); | 91 void ResolveAllPending(); |
92 | 92 |
93 // Returns true if there are pending requests that can be resolved by invoking | 93 // Returns true if there are pending requests that can be resolved by invoking |
94 // ResolveAllPending(). | 94 // ResolveAllPending(). |
95 bool has_pending_requests() const { return !requests_.empty(); } | 95 bool has_pending_requests() const { return !requests_.empty(); } |
96 | 96 |
| 97 // The number of times that Resolve() has been called. |
| 98 size_t num_resolve() const { |
| 99 return num_resolve_; |
| 100 } |
| 101 |
| 102 // The number of times that ResolveFromCache() has been called. |
| 103 size_t num_resolve_from_cache() const { |
| 104 return num_resolve_from_cache_; |
| 105 } |
| 106 |
97 protected: | 107 protected: |
98 explicit MockHostResolverBase(bool use_caching); | 108 explicit MockHostResolverBase(bool use_caching); |
99 | 109 |
100 private: | 110 private: |
101 struct Request; | 111 struct Request; |
102 typedef std::map<size_t, Request*> RequestMap; | 112 typedef std::map<size_t, Request*> RequestMap; |
103 | 113 |
104 // Resolve as IP or from |cache_| return cached error or | 114 // Resolve as IP or from |cache_| return cached error or |
105 // DNS_CACHE_MISS if failed. | 115 // DNS_CACHE_MISS if failed. |
106 int ResolveFromIPLiteralOrCache(const RequestInfo& info, | 116 int ResolveFromIPLiteralOrCache(const RequestInfo& info, |
107 AddressList* addresses); | 117 AddressList* addresses); |
108 // Resolve via |proc_|. | 118 // Resolve via |proc_|. |
109 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); | 119 int ResolveProc(size_t id, const RequestInfo& info, AddressList* addresses); |
110 // Resolve request stored in |requests_|. Pass rv to callback. | 120 // Resolve request stored in |requests_|. Pass rv to callback. |
111 void ResolveNow(size_t id); | 121 void ResolveNow(size_t id); |
112 | 122 |
113 bool synchronous_mode_; | 123 bool synchronous_mode_; |
114 bool ondemand_mode_; | 124 bool ondemand_mode_; |
115 scoped_refptr<RuleBasedHostResolverProc> rules_; | 125 scoped_refptr<RuleBasedHostResolverProc> rules_; |
116 scoped_ptr<HostCache> cache_; | 126 scoped_ptr<HostCache> cache_; |
117 RequestMap requests_; | 127 RequestMap requests_; |
118 size_t next_request_id_; | 128 size_t next_request_id_; |
119 | 129 |
| 130 size_t num_resolve_; |
| 131 size_t num_resolve_from_cache_; |
| 132 |
120 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); | 133 DISALLOW_COPY_AND_ASSIGN(MockHostResolverBase); |
121 }; | 134 }; |
122 | 135 |
123 class MockHostResolver : public MockHostResolverBase { | 136 class MockHostResolver : public MockHostResolverBase { |
124 public: | 137 public: |
125 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} | 138 MockHostResolver() : MockHostResolverBase(false /*use_caching*/) {} |
126 virtual ~MockHostResolver() {} | 139 virtual ~MockHostResolver() {} |
127 }; | 140 }; |
128 | 141 |
129 // Same as MockHostResolver, except internally it uses a host-cache. | 142 // Same as MockHostResolver, except internally it uses a host-cache. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 const std::string& replacement, | 181 const std::string& replacement, |
169 int latency_ms); | 182 int latency_ms); |
170 | 183 |
171 // Make sure that |host| will not be re-mapped or even processed by underlying | 184 // Make sure that |host| will not be re-mapped or even processed by underlying |
172 // host resolver procedures. It can also be a pattern. | 185 // host resolver procedures. It can also be a pattern. |
173 void AllowDirectLookup(const std::string& host); | 186 void AllowDirectLookup(const std::string& host); |
174 | 187 |
175 // Simulate a lookup failure for |host| (it also can be a pattern). | 188 // Simulate a lookup failure for |host| (it also can be a pattern). |
176 void AddSimulatedFailure(const std::string& host); | 189 void AddSimulatedFailure(const std::string& host); |
177 | 190 |
| 191 // Deletes all the rules that have been added. |
| 192 void ClearRules(); |
| 193 |
178 // HostResolverProc methods: | 194 // HostResolverProc methods: |
179 virtual int Resolve(const std::string& host, | 195 virtual int Resolve(const std::string& host, |
180 AddressFamily address_family, | 196 AddressFamily address_family, |
181 HostResolverFlags host_resolver_flags, | 197 HostResolverFlags host_resolver_flags, |
182 AddressList* addrlist, | 198 AddressList* addrlist, |
183 int* os_error) OVERRIDE; | 199 int* os_error) OVERRIDE; |
184 | 200 |
185 private: | 201 private: |
186 struct Rule; | 202 struct Rule; |
187 typedef std::list<Rule> RuleList; | 203 typedef std::list<Rule> RuleList; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 void Init(HostResolverProc* proc); | 243 void Init(HostResolverProc* proc); |
228 | 244 |
229 private: | 245 private: |
230 scoped_refptr<HostResolverProc> current_proc_; | 246 scoped_refptr<HostResolverProc> current_proc_; |
231 scoped_refptr<HostResolverProc> previous_proc_; | 247 scoped_refptr<HostResolverProc> previous_proc_; |
232 }; | 248 }; |
233 | 249 |
234 } // namespace net | 250 } // namespace net |
235 | 251 |
236 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ | 252 #endif // NET_BASE_MOCK_HOST_RESOLVER_H_ |
OLD | NEW |