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 #include "chrome/browser/net/resolve_proxy_msg_helper.h" | 5 #include "chrome/browser/net/resolve_proxy_msg_helper.h" |
6 | 6 |
7 #include "base/waitable_event.h" | 7 #include "base/waitable_event.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/proxy/proxy_config_service.h" | 9 #include "net/proxy/proxy_config_service.h" |
10 #include "net/proxy/proxy_resolver.h" | 10 #include "net/proxy/proxy_resolver.h" |
| 11 #include "net/proxy/single_threaded_proxy_resolver.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 // This ProxyConfigService always returns "http://pac" as the PAC url to use. | 14 // This ProxyConfigService always returns "http://pac" as the PAC url to use. |
14 class MockProxyConfigService: public net::ProxyConfigService { | 15 class MockProxyConfigService: public net::ProxyConfigService { |
15 public: | 16 public: |
16 virtual int GetProxyConfig(net::ProxyConfig* results) { | 17 virtual int GetProxyConfig(net::ProxyConfig* results) { |
17 results->pac_url = GURL("http://pac"); | 18 results->pac_url = GURL("http://pac"); |
18 return net::OK; | 19 return net::OK; |
19 } | 20 } |
20 }; | 21 }; |
21 | 22 |
22 // This PAC resolver always returns the hostname of the query URL as the | 23 // This PAC resolver always returns the hostname of the query URL as the |
23 // proxy to use. The Block() method will make GetProxyForURL() hang until | 24 // proxy to use. The Block() method will make GetProxyForURL() hang until |
24 // Unblock() is called. | 25 // Unblock() is called. |
25 class MockProxyResolver : public net::ProxyResolver { | 26 class SyncMockProxyResolver : public net::ProxyResolver { |
26 public: | 27 public: |
27 MockProxyResolver() : ProxyResolver(true), event_(false, false), | 28 SyncMockProxyResolver() : ProxyResolver(false /*expects_pac_bytes*/), |
28 is_blocked_(false) { | 29 event_(false, false), |
| 30 is_blocked_(false) { |
29 } | 31 } |
30 | 32 |
31 virtual int GetProxyForURL(const GURL& query_url, | 33 virtual int GetProxyForURL(const GURL& query_url, |
32 const GURL& /*pac_url*/, | 34 net::ProxyInfo* results, |
33 net::ProxyInfo* results) { | 35 net::CompletionCallback* callback, |
| 36 RequestHandle* request) { |
34 if (is_blocked_) | 37 if (is_blocked_) |
35 event_.Wait(); | 38 event_.Wait(); |
36 results->UseNamedProxy(query_url.host()); | 39 results->UseNamedProxy(query_url.host()); |
37 return net::OK; | 40 return net::OK; |
38 } | 41 } |
39 | 42 |
| 43 virtual void CancelRequest(RequestHandle request) { |
| 44 NOTREACHED(); |
| 45 } |
| 46 |
| 47 virtual void SetPacScriptByUrlInternal(const GURL& pac_url) {} |
| 48 |
40 void Block() { | 49 void Block() { |
41 is_blocked_ = true; | 50 is_blocked_ = true; |
42 event_.Reset(); | 51 event_.Reset(); |
43 } | 52 } |
44 | 53 |
45 void Unblock() { | 54 void Unblock() { |
46 is_blocked_ = false; | 55 is_blocked_ = false; |
47 event_.Signal(); | 56 event_.Signal(); |
48 } | 57 } |
49 | 58 |
50 private: | 59 private: |
51 base::WaitableEvent event_; | 60 base::WaitableEvent event_; |
52 bool is_blocked_; | 61 bool is_blocked_; |
53 }; | 62 }; |
54 | 63 |
| 64 class MockProxyResolver : public net::SingleThreadedProxyResolver { |
| 65 public: |
| 66 MockProxyResolver() |
| 67 : net::SingleThreadedProxyResolver(new SyncMockProxyResolver) { |
| 68 x = reinterpret_cast<SyncMockProxyResolver*>(resolver()); |
| 69 } |
| 70 |
| 71 // TODO(eroman): cleanup. |
| 72 SyncMockProxyResolver* x; |
| 73 }; |
| 74 |
55 // This struct holds the values that were passed to | 75 // This struct holds the values that were passed to |
56 // Delegate::OnResolveProxyCompleted(). The caller should use WaitUntilDone() | 76 // Delegate::OnResolveProxyCompleted(). The caller should use WaitUntilDone() |
57 // to block until the result has been populated. | 77 // to block until the result has been populated. |
58 struct ResultFuture { | 78 struct ResultFuture { |
59 public: | 79 public: |
60 ResultFuture() | 80 ResultFuture() |
61 : reply_msg(NULL), | 81 : reply_msg(NULL), |
62 error_code(0), | 82 error_code(0), |
63 started_(false, false), | 83 started_(false, false), |
64 completed_(false, false) { | 84 completed_(false, false) { |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 284 |
265 GURL url1("http://www.google1.com/"); | 285 GURL url1("http://www.google1.com/"); |
266 GURL url2("http://www.google2.com/"); | 286 GURL url2("http://www.google2.com/"); |
267 GURL url3("http://www.google3.com/"); | 287 GURL url3("http://www.google3.com/"); |
268 | 288 |
269 scoped_ptr<IPC::Message> msg1(new IPC::Message()); | 289 scoped_ptr<IPC::Message> msg1(new IPC::Message()); |
270 scoped_ptr<IPC::Message> msg2(new IPC::Message()); | 290 scoped_ptr<IPC::Message> msg2(new IPC::Message()); |
271 scoped_ptr<IPC::Message> msg3(new IPC::Message()); | 291 scoped_ptr<IPC::Message> msg3(new IPC::Message()); |
272 | 292 |
273 // Make the proxy resolver hang on the next request. | 293 // Make the proxy resolver hang on the next request. |
274 runner.proxy_resolver()->Block(); | 294 runner.proxy_resolver()->x->Block(); |
275 | 295 |
276 // Start three requests. Since the proxy resolver is hung, the second two | 296 // Start three requests. Since the proxy resolver is hung, the second two |
277 // will be pending. | 297 // will be pending. |
278 | 298 |
279 scoped_ptr<ResultFuture> result1(runner.Start(url1, msg1.get())); | 299 scoped_ptr<ResultFuture> result1(runner.Start(url1, msg1.get())); |
280 scoped_ptr<ResultFuture> result2(runner.Start(url2, msg2.get())); | 300 scoped_ptr<ResultFuture> result2(runner.Start(url2, msg2.get())); |
281 scoped_ptr<ResultFuture> result3(runner.Start(url3, msg3.get())); | 301 scoped_ptr<ResultFuture> result3(runner.Start(url3, msg3.get())); |
282 | 302 |
283 // Wait for the final request to have been scheduled. Otherwise we may rush | 303 // Wait for the final request to have been scheduled. Otherwise we may rush |
284 // to calling Unblock() without actually having blocked anything. | 304 // to calling Unblock() without actually having blocked anything. |
285 result3->WaitUntilStarted(); | 305 result3->WaitUntilStarted(); |
286 | 306 |
287 // Unblock the proxy service so requests 1-3 can complete. | 307 // Unblock the proxy service so requests 1-3 can complete. |
288 runner.proxy_resolver()->Unblock(); | 308 runner.proxy_resolver()->x->Unblock(); |
289 | 309 |
290 // Wait for all the requests to finish (they run in FIFO order). | 310 // Wait for all the requests to finish (they run in FIFO order). |
291 result3->WaitUntilDone(); | 311 result3->WaitUntilDone(); |
292 | 312 |
293 // Check that each call invoked the callback with the right parameters. | 313 // Check that each call invoked the callback with the right parameters. |
294 | 314 |
295 EXPECT_EQ(msg1.get(), result1->reply_msg); | 315 EXPECT_EQ(msg1.get(), result1->reply_msg); |
296 EXPECT_EQ(net::OK, result1->error_code); | 316 EXPECT_EQ(net::OK, result1->error_code); |
297 EXPECT_EQ("PROXY www.google1.com:80", result1->proxy_list); | 317 EXPECT_EQ("PROXY www.google1.com:80", result1->proxy_list); |
298 | 318 |
(...skipping 14 matching lines...) Expand all Loading... |
313 GURL url2("http://www.google2.com/"); | 333 GURL url2("http://www.google2.com/"); |
314 GURL url3("http://www.google3.com/"); | 334 GURL url3("http://www.google3.com/"); |
315 | 335 |
316 // NOTE: these are not scoped ptr, since they will be deleted by the | 336 // NOTE: these are not scoped ptr, since they will be deleted by the |
317 // request's cancellation. | 337 // request's cancellation. |
318 IPC::Message* msg1 = new IPC::Message(); | 338 IPC::Message* msg1 = new IPC::Message(); |
319 IPC::Message* msg2 = new IPC::Message(); | 339 IPC::Message* msg2 = new IPC::Message(); |
320 IPC::Message* msg3 = new IPC::Message(); | 340 IPC::Message* msg3 = new IPC::Message(); |
321 | 341 |
322 // Make the next request block. | 342 // Make the next request block. |
323 runner.proxy_resolver()->Block(); | 343 runner.proxy_resolver()->x->Block(); |
324 | 344 |
325 // Start three requests; since the first one blocked, the other two should | 345 // Start three requests; since the first one blocked, the other two should |
326 // be pending. | 346 // be pending. |
327 | 347 |
328 scoped_ptr<ResultFuture> result1(runner.Start(url1, msg1)); | 348 scoped_ptr<ResultFuture> result1(runner.Start(url1, msg1)); |
329 scoped_ptr<ResultFuture> result2(runner.Start(url2, msg2)); | 349 scoped_ptr<ResultFuture> result2(runner.Start(url2, msg2)); |
330 scoped_ptr<ResultFuture> result3(runner.Start(url3, msg3)); | 350 scoped_ptr<ResultFuture> result3(runner.Start(url3, msg3)); |
331 | 351 |
332 result3->WaitUntilStarted(); | 352 result3->WaitUntilStarted(); |
333 | 353 |
334 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all | 354 // Delete the underlying ResolveProxyMsgHelper -- this should cancel all |
335 // the requests which are outstanding. | 355 // the requests which are outstanding. |
336 runner.DestroyAsyncRunner(); | 356 runner.DestroyAsyncRunner(); |
337 | 357 |
338 // Unblocking the proxy resolver means the three requests can complete -- | 358 // Unblocking the proxy resolver means the three requests can complete -- |
339 // however they should not try to notify the delegate since we have already | 359 // however they should not try to notify the delegate since we have already |
340 // deleted the helper. | 360 // deleted the helper. |
341 runner.proxy_resolver()->Unblock(); | 361 runner.proxy_resolver()->x->Unblock(); |
342 | 362 |
343 // Check that none of the requests were sent to the delegate. | 363 // Check that none of the requests were sent to the delegate. |
344 EXPECT_FALSE( | 364 EXPECT_FALSE( |
345 result1->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); | 365 result1->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); |
346 EXPECT_FALSE( | 366 EXPECT_FALSE( |
347 result2->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); | 367 result2->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); |
348 EXPECT_FALSE( | 368 EXPECT_FALSE( |
349 result3->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); | 369 result3->TimedWaitUntilDone(base::TimeDelta::FromMilliseconds(2))); |
350 | 370 |
351 // It should also be the case that msg1, msg2, msg3 were deleted by the | 371 // It should also be the case that msg1, msg2, msg3 were deleted by the |
352 // cancellation. (Else will show up as a leak in Purify). | 372 // cancellation. (Else will show up as a leak in Purify). |
353 } | 373 } |
OLD | NEW |