OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/base/layered_network_delegate.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "net/base/auth.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/base/network_delegate_impl.h" | |
17 #include "net/base/request_priority.h" | |
18 #include "net/base/test_completion_callback.h" | |
19 #include "net/http/http_response_headers.h" | |
20 #include "net/proxy/proxy_config_service.h" | |
21 #include "net/proxy/proxy_info.h" | |
22 #include "net/proxy/proxy_service.h" | |
23 #include "net/url_request/url_request.h" | |
24 #include "net/url_request/url_request_test_util.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 #include "url/gurl.h" | |
27 | |
28 namespace net { | |
29 namespace { | |
30 | |
31 typedef std::map<const char*, int> CountersMap; | |
32 | |
33 class TestNetworkDelegateImpl : public NetworkDelegateImpl { | |
34 public: | |
35 TestNetworkDelegateImpl(CountersMap* layered_network_delegate_counters) | |
36 : layered_network_delegate_counters_(layered_network_delegate_counters) {} | |
37 | |
38 ~TestNetworkDelegateImpl() override {} | |
39 | |
40 // NetworkDelegateImpl implementation: | |
41 int OnBeforeURLRequest(URLRequest* request, | |
42 const CompletionCallback& callback, | |
43 GURL* new_url) override { | |
44 IncrementAndCompareCounter("on_before_url_request_count"); | |
45 return OK; | |
46 } | |
47 | |
48 void OnResolveProxy(const GURL& url, | |
49 int load_flags, | |
50 const ProxyService& proxy_service, | |
51 ProxyInfo* result) override { | |
52 IncrementAndCompareCounter("on_resolve_proxy_count"); | |
53 } | |
54 | |
55 void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) override { | |
56 IncrementAndCompareCounter("on_proxy_fallback_count"); | |
57 } | |
58 | |
59 int OnBeforeSendHeaders(URLRequest* request, | |
60 const CompletionCallback& callback, | |
61 HttpRequestHeaders* headers) override { | |
62 IncrementAndCompareCounter("on_before_send_headers_count"); | |
63 return OK; | |
64 } | |
65 | |
66 void OnBeforeSendProxyHeaders(URLRequest* request, | |
67 const ProxyInfo& proxy_info, | |
68 HttpRequestHeaders* headers) override { | |
69 IncrementAndCompareCounter("on_before_send_proxy_headers_count"); | |
70 } | |
71 | |
72 void OnSendHeaders(URLRequest* request, | |
73 const HttpRequestHeaders& headers) override { | |
74 IncrementAndCompareCounter("on_send_headers_count"); | |
75 } | |
76 | |
77 int OnHeadersReceived( | |
78 URLRequest* request, | |
79 const CompletionCallback& callback, | |
80 const HttpResponseHeaders* original_response_headers, | |
81 scoped_refptr<HttpResponseHeaders>* override_response_headers, | |
82 GURL* allowed_unsafe_redirect_url) override { | |
83 IncrementAndCompareCounter("on_headers_received_count"); | |
84 return OK; | |
85 } | |
86 | |
87 void OnBeforeRedirect(URLRequest* request, | |
88 const GURL& new_location) override { | |
89 IncrementAndCompareCounter("on_before_redirect_count"); | |
90 } | |
91 | |
92 void OnResponseStarted(URLRequest* request) override { | |
93 IncrementAndCompareCounter("on_response_started_count"); | |
94 } | |
95 | |
96 void OnRawBytesRead(const URLRequest& request, int bytes_read) override { | |
97 IncrementAndCompareCounter("on_raw_bytes_read_count"); | |
98 } | |
99 | |
100 void OnCompleted(URLRequest* request, bool started) override { | |
101 IncrementAndCompareCounter("on_completed_count"); | |
102 } | |
103 | |
104 void OnURLRequestDestroyed(URLRequest* request) override { | |
105 IncrementAndCompareCounter("on_url_request_destroyed_count"); | |
106 } | |
107 | |
108 void OnPACScriptError(int line_number, const base::string16& error) override { | |
109 IncrementAndCompareCounter("on_pac_script_error_count"); | |
110 } | |
111 | |
112 AuthRequiredResponse OnAuthRequired(URLRequest* request, | |
113 const AuthChallengeInfo& auth_info, | |
114 const AuthCallback& callback, | |
115 AuthCredentials* credentials) override { | |
116 IncrementAndCompareCounter("on_auth_required_count"); | |
117 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
118 } | |
119 | |
120 bool OnCanGetCookies(const URLRequest& request, | |
121 const CookieList& cookie_list) override { | |
122 IncrementAndCompareCounter("on_can_get_cookies_count"); | |
123 return false; | |
124 } | |
125 | |
126 bool OnCanSetCookie(const URLRequest& request, | |
127 const std::string& cookie_line, | |
128 CookieOptions* options) override { | |
129 IncrementAndCompareCounter("on_can_set_cookie_count"); | |
130 return false; | |
131 } | |
132 | |
133 bool OnCanAccessFile(const URLRequest& request, | |
134 const base::FilePath& path) const override { | |
135 IncrementAndCompareCounter("on_can_access_file_count"); | |
136 return false; | |
137 } | |
138 | |
139 bool OnCanThrottleRequest(const URLRequest& request) const override { | |
140 IncrementAndCompareCounter("on_can_throttle_request_count"); | |
141 return false; | |
142 } | |
143 | |
144 bool OnCanEnablePrivacyMode( | |
145 const GURL& url, | |
146 const GURL& first_party_for_cookies) const override { | |
147 IncrementAndCompareCounter("on_can_enable_privacy_mode_count"); | |
148 return false; | |
149 } | |
150 | |
151 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader( | |
152 const URLRequest& request, | |
153 const GURL& target_url, | |
154 const GURL& referrer_url) const override { | |
155 IncrementAndCompareCounter( | |
156 "on_cancel_url_request_with_policy_violating_referrer_header_count"); | |
157 return false; | |
158 } | |
159 | |
160 private: | |
161 void IncrementAndCompareCounter(const char* counter_name) const { | |
162 ++counters_[counter_name]; | |
163 EXPECT_EQ((*layered_network_delegate_counters_)[counter_name], | |
164 counters_[counter_name]); | |
165 } | |
166 | |
167 mutable CountersMap counters_; | |
168 mutable CountersMap* layered_network_delegate_counters_; | |
169 | |
170 DISALLOW_COPY_AND_ASSIGN(TestNetworkDelegateImpl); | |
171 }; | |
172 | |
173 class TestLayeredNetworkDelegate : public LayeredNetworkDelegate { | |
174 public: | |
175 TestLayeredNetworkDelegate(scoped_ptr<NetworkDelegate> network_delegate, | |
176 CountersMap* counters) | |
177 : LayeredNetworkDelegate(network_delegate.Pass()), | |
178 context_(true), | |
179 counters_(counters) { | |
180 context_.Init(); | |
181 } | |
182 | |
183 ~TestLayeredNetworkDelegate() override {} | |
184 | |
185 void CallAndVerify() { | |
186 scoped_refptr<AuthChallengeInfo> auth_challenge(new AuthChallengeInfo()); | |
187 scoped_ptr<URLRequest> request = | |
188 context_.CreateRequest(GURL(), IDLE, &delegate_, NULL); | |
189 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); | |
190 scoped_refptr<HttpResponseHeaders> response_headers( | |
191 new HttpResponseHeaders("")); | |
192 TestCompletionCallback completion_callback; | |
193 scoped_ptr<ProxyService> proxy_service(ProxyService::CreateDirect()); | |
194 scoped_ptr<ProxyInfo> proxy_info(new ProxyInfo()); | |
195 | |
196 EXPECT_EQ(OK, OnBeforeURLRequest(request.get(), | |
197 completion_callback.callback(), NULL)); | |
198 OnResolveProxy(GURL(), 0, *proxy_service, proxy_info.get()); | |
199 OnProxyFallback(ProxyServer(), 0); | |
200 EXPECT_EQ(OK, OnBeforeSendHeaders(NULL, completion_callback.callback(), | |
201 request_headers.get())); | |
202 OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get()); | |
203 OnSendHeaders(NULL, *request_headers); | |
204 EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(), | |
205 response_headers.get(), NULL, NULL)); | |
206 OnResponseStarted(request.get()); | |
207 OnRawBytesRead(*request, 0); | |
208 OnCompleted(request.get(), false); | |
209 OnURLRequestDestroyed(request.get()); | |
210 OnPACScriptError(0, base::string16()); | |
211 EXPECT_EQ( | |
212 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION, | |
213 OnAuthRequired(request.get(), *auth_challenge, AuthCallback(), NULL)); | |
214 EXPECT_FALSE(OnCanGetCookies(*request, CookieList())); | |
215 EXPECT_FALSE(OnCanSetCookie(*request, std::string(), NULL)); | |
216 EXPECT_FALSE(OnCanAccessFile(*request, base::FilePath())); | |
217 EXPECT_FALSE(OnCanThrottleRequest(*request)); | |
218 EXPECT_FALSE(OnCanEnablePrivacyMode(GURL(), GURL())); | |
219 EXPECT_FALSE(OnCancelURLRequestWithPolicyViolatingReferrerHeader( | |
220 *request, GURL(), GURL())); | |
221 } | |
222 | |
223 protected: | |
224 void OnBeforeURLRequestInternal(URLRequest* request, | |
225 const CompletionCallback& callback, | |
226 GURL* new_url) override { | |
227 ++(*counters_)["on_before_url_request_count"]; | |
228 EXPECT_EQ(1, (*counters_)["on_before_url_request_count"]); | |
229 } | |
230 | |
231 void OnResolveProxyInternal(const GURL& url, | |
232 int load_flags, | |
233 const ProxyService& proxy_service, | |
234 ProxyInfo* result) override { | |
235 ++(*counters_)["on_resolve_proxy_count"]; | |
236 EXPECT_EQ(1, (*counters_)["on_resolve_proxy_count"]); | |
237 } | |
238 | |
239 void OnProxyFallbackInternal(const ProxyServer& bad_proxy, | |
240 int net_error) override { | |
241 ++(*counters_)["on_proxy_fallback_count"]; | |
242 EXPECT_EQ(1, (*counters_)["on_proxy_fallback_count"]); | |
243 } | |
244 | |
245 void OnBeforeSendHeadersInternal(URLRequest* request, | |
246 const CompletionCallback& callback, | |
247 HttpRequestHeaders* headers) override { | |
248 ++(*counters_)["on_before_send_headers_count"]; | |
249 EXPECT_EQ(1, (*counters_)["on_before_send_headers_count"]); | |
250 } | |
251 | |
252 void OnBeforeSendProxyHeadersInternal(URLRequest* request, | |
253 const ProxyInfo& proxy_info, | |
254 HttpRequestHeaders* headers) override { | |
255 ++(*counters_)["on_before_send_proxy_headers_count"]; | |
256 EXPECT_EQ(1, (*counters_)["on_before_send_proxy_headers_count"]); | |
257 } | |
258 | |
259 void OnSendHeadersInternal(URLRequest* request, | |
260 const HttpRequestHeaders& headers) override { | |
261 ++(*counters_)["on_send_headers_count"]; | |
262 EXPECT_EQ(1, (*counters_)["on_send_headers_count"]); | |
263 } | |
264 | |
265 void OnHeadersReceivedInternal( | |
266 URLRequest* request, | |
267 const CompletionCallback& callback, | |
268 const HttpResponseHeaders* original_response_headers, | |
269 scoped_refptr<HttpResponseHeaders>* override_response_headers, | |
270 GURL* allowed_unsafe_redirect_url) override { | |
271 ++(*counters_)["on_headers_received_count"]; | |
272 EXPECT_EQ(1, (*counters_)["on_headers_received_count"]); | |
273 } | |
274 | |
275 void OnBeforeRedirectInternal(URLRequest* request, | |
276 const GURL& new_location) override { | |
277 ++(*counters_)["on_before_redirect_count"]; | |
278 EXPECT_EQ(1, (*counters_)["on_before_redirect_count"]); | |
279 } | |
280 | |
281 void OnResponseStartedInternal(URLRequest* request) override { | |
282 ++(*counters_)["on_response_started_count"]; | |
283 EXPECT_EQ(1, (*counters_)["on_response_started_count"]); | |
284 } | |
285 | |
286 void OnRawBytesReadInternal(const URLRequest& request, | |
287 int bytes_read) override { | |
288 ++(*counters_)["on_raw_bytes_read_count"]; | |
289 EXPECT_EQ(1, (*counters_)["on_raw_bytes_read_count"]); | |
290 } | |
291 | |
292 void OnCompletedInternal(URLRequest* request, bool started) override { | |
293 ++(*counters_)["on_completed_count"]; | |
294 EXPECT_EQ(1, (*counters_)["on_completed_count"]); | |
295 } | |
296 | |
297 void OnURLRequestDestroyedInternal(URLRequest* request) override { | |
298 ++(*counters_)["on_url_request_destroyed_count"]; | |
299 EXPECT_EQ(1, (*counters_)["on_url_request_destroyed_count"]); | |
300 } | |
301 | |
302 void OnPACScriptErrorInternal(int line_number, | |
303 const base::string16& error) override { | |
304 ++(*counters_)["on_pac_script_error_count"]; | |
305 EXPECT_EQ(1, (*counters_)["on_pac_script_error_count"]); | |
306 } | |
307 | |
308 void OnAuthRequiredInternal(URLRequest* request, | |
309 const AuthChallengeInfo& auth_info, | |
310 const AuthCallback& callback, | |
311 AuthCredentials* credentials) override { | |
312 ++(*counters_)["on_auth_required_count"]; | |
313 EXPECT_EQ(1, (*counters_)["on_auth_required_count"]); | |
314 } | |
315 | |
316 void OnCanGetCookiesInternal(const URLRequest& request, | |
317 const CookieList& cookie_list) override { | |
318 ++(*counters_)["on_can_get_cookies_count"]; | |
319 EXPECT_EQ(1, (*counters_)["on_can_get_cookies_count"]); | |
320 } | |
321 | |
322 void OnCanSetCookieInternal(const URLRequest& request, | |
323 const std::string& cookie_line, | |
324 CookieOptions* options) override { | |
325 ++(*counters_)["on_can_set_cookie_count"]; | |
326 EXPECT_EQ(1, (*counters_)["on_can_set_cookie_count"]); | |
327 } | |
328 | |
329 void OnCanAccessFileInternal(const URLRequest& request, | |
330 const base::FilePath& path) const override { | |
331 ++(*counters_)["on_can_access_file_count"]; | |
332 EXPECT_EQ(1, (*counters_)["on_can_access_file_count"]); | |
333 } | |
334 | |
335 void OnCanThrottleRequestInternal(const URLRequest& request) const override { | |
336 ++(*counters_)["on_can_throttle_request_count"]; | |
337 EXPECT_EQ(1, (*counters_)["on_can_throttle_request_count"]); | |
338 } | |
339 | |
340 void OnCanEnablePrivacyModeInternal( | |
341 const GURL& url, | |
342 const GURL& first_party_for_cookies) const override { | |
343 ++(*counters_)["on_can_enable_privacy_mode_count"]; | |
344 EXPECT_EQ(1, (*counters_)["on_can_enable_privacy_mode_count"]); | |
345 } | |
346 | |
347 void OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal( | |
348 const URLRequest& request, | |
349 const GURL& target_url, | |
350 const GURL& referrer_url) const override { | |
351 ++(*counters_) | |
352 ["on_cancel_url_request_with_policy_" | |
353 "violating_referrer_header_count"]; | |
354 EXPECT_EQ(1, (*counters_) | |
355 ["on_cancel_url_request_with_policy_" | |
356 "violating_referrer_header_count"]); | |
357 } | |
358 | |
359 private: | |
360 TestURLRequestContext context_; | |
361 TestDelegate delegate_; | |
362 mutable CountersMap* counters_; | |
363 | |
364 DISALLOW_COPY_AND_ASSIGN(TestLayeredNetworkDelegate); | |
365 }; | |
366 | |
367 } // namespace | |
368 | |
369 class LayeredNetworkDelegateTest : public testing::Test { | |
370 public: | |
371 LayeredNetworkDelegateTest() { | |
372 scoped_ptr<TestNetworkDelegateImpl> test_network_delegate( | |
373 new TestNetworkDelegateImpl(&layered_network_delegate_counters)); | |
374 test_network_delegate_ = test_network_delegate.get(); | |
375 layered_network_delegate_ = | |
376 scoped_ptr<TestLayeredNetworkDelegate>(new TestLayeredNetworkDelegate( | |
377 test_network_delegate.Pass(), &layered_network_delegate_counters)); | |
378 } | |
379 | |
380 CountersMap layered_network_delegate_counters; | |
381 TestNetworkDelegateImpl* test_network_delegate_; | |
382 scoped_ptr<TestLayeredNetworkDelegate> layered_network_delegate_; | |
383 }; | |
384 | |
385 TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) { | |
386 layered_network_delegate_->CallAndVerify(); | |
387 } | |
388 | |
389 } // namespace net | |
OLD | NEW |