OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/proxy/dhcp_proxy_script_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 printf("Adapter '%s' has PAC URL '%s' configured in DHCP.\n", | 41 printf("Adapter '%s' has PAC URL '%s' configured in DHCP.\n", |
42 adapter_name.c_str(), | 42 adapter_name.c_str(), |
43 pac_url.c_str()); | 43 pac_url.c_str()); |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 // Helper for RealFetch* tests below. | 47 // Helper for RealFetch* tests below. |
48 class RealFetchTester { | 48 class RealFetchTester { |
49 public: | 49 public: |
50 RealFetchTester() | 50 RealFetchTester() |
51 : context_((new TestURLRequestContext())), | 51 : context_(new TestURLRequestContext), |
52 fetcher_(new DhcpProxyScriptFetcherWin(context_.get())), | 52 fetcher_(new DhcpProxyScriptFetcherWin(context_.get())), |
53 finished_(false), | 53 finished_(false), |
54 on_completion_is_error_(false) { | 54 on_completion_is_error_(false) { |
55 // Make sure the test ends. | 55 // Make sure the test ends. |
56 timeout_.Start(FROM_HERE, | 56 timeout_.Start(FROM_HERE, |
57 base::TimeDelta::FromSeconds(5), this, &RealFetchTester::OnTimeout); | 57 base::TimeDelta::FromSeconds(5), this, &RealFetchTester::OnTimeout); |
58 } | 58 } |
59 | 59 |
60 void RunTest() { | 60 void RunTest() { |
61 int result = fetcher_->Fetch( | 61 int result = fetcher_->Fetch( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // Attempts to give worker threads time to finish. This is currently | 107 // Attempts to give worker threads time to finish. This is currently |
108 // very simplistic as completion (via completion callback or cancellation) | 108 // very simplistic as completion (via completion callback or cancellation) |
109 // immediately "detaches" any worker threads, so the best we can do is give | 109 // immediately "detaches" any worker threads, so the best we can do is give |
110 // them a little time. If we start running into Valgrind leaks, we can | 110 // them a little time. If we start running into Valgrind leaks, we can |
111 // do something a bit more clever to track worker threads even when the | 111 // do something a bit more clever to track worker threads even when the |
112 // DhcpProxyScriptFetcherWin state machine has finished. | 112 // DhcpProxyScriptFetcherWin state machine has finished. |
113 void FinishTestAllowCleanup() { | 113 void FinishTestAllowCleanup() { |
114 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); | 114 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
115 } | 115 } |
116 | 116 |
117 scoped_refptr<URLRequestContext> context_; | 117 scoped_ptr<URLRequestContext> context_; |
118 scoped_ptr<DhcpProxyScriptFetcherWin> fetcher_; | 118 scoped_ptr<DhcpProxyScriptFetcherWin> fetcher_; |
119 bool finished_; | 119 bool finished_; |
120 string16 pac_text_; | 120 string16 pac_text_; |
121 base::OneShotTimer<RealFetchTester> timeout_; | 121 base::OneShotTimer<RealFetchTester> timeout_; |
122 base::OneShotTimer<RealFetchTester> cancel_timer_; | 122 base::OneShotTimer<RealFetchTester> cancel_timer_; |
123 bool on_completion_is_error_; | 123 bool on_completion_is_error_; |
124 }; | 124 }; |
125 | 125 |
126 TEST(DhcpProxyScriptFetcherWin, RealFetch) { | 126 TEST(DhcpProxyScriptFetcherWin, RealFetch) { |
127 // This tests a call to Fetch() with no stubbing out of dependencies. | 127 // This tests a call to Fetch() with no stubbing out of dependencies. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 } | 193 } |
194 }; | 194 }; |
195 | 195 |
196 TEST(DhcpProxyScriptFetcherWin, RealFetchWithDeferredCancel) { | 196 TEST(DhcpProxyScriptFetcherWin, RealFetchWithDeferredCancel) { |
197 // Does a Fetch() with a slightly delayed cancel. As before, just | 197 // Does a Fetch() with a slightly delayed cancel. As before, just |
198 // exercises the code without stubbing out dependencies, but | 198 // exercises the code without stubbing out dependencies, but |
199 // introduces a guaranteed 20 ms delay on the worker threads so that | 199 // introduces a guaranteed 20 ms delay on the worker threads so that |
200 // the cancel is called before they complete. | 200 // the cancel is called before they complete. |
201 RealFetchTester fetcher; | 201 RealFetchTester fetcher; |
202 fetcher.fetcher_.reset( | 202 fetcher.fetcher_.reset( |
203 new DelayingDhcpProxyScriptFetcherWin(fetcher.context_)); | 203 new DelayingDhcpProxyScriptFetcherWin(fetcher.context_.get())); |
204 fetcher.on_completion_is_error_ = true; | 204 fetcher.on_completion_is_error_ = true; |
205 fetcher.RunTestWithDeferredCancel(); | 205 fetcher.RunTestWithDeferredCancel(); |
206 fetcher.WaitUntilDone(); | 206 fetcher.WaitUntilDone(); |
207 } | 207 } |
208 | 208 |
209 // The remaining tests are to exercise our state machine in various | 209 // The remaining tests are to exercise our state machine in various |
210 // situations, with actual network access fully stubbed out. | 210 // situations, with actual network access fully stubbed out. |
211 | 211 |
212 class DummyDhcpProxyScriptAdapterFetcher | 212 class DummyDhcpProxyScriptAdapterFetcher |
213 : public DhcpProxyScriptAdapterFetcher { | 213 : public DhcpProxyScriptAdapterFetcher { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 365 |
366 int max_wait_ms_; | 366 int max_wait_ms_; |
367 int num_fetchers_created_; | 367 int num_fetchers_created_; |
368 base::WaitableEvent worker_finished_event_; | 368 base::WaitableEvent worker_finished_event_; |
369 }; | 369 }; |
370 | 370 |
371 class FetcherClient { | 371 class FetcherClient { |
372 public: | 372 public: |
373 FetcherClient() | 373 FetcherClient() |
374 : context_(new TestURLRequestContext), | 374 : context_(new TestURLRequestContext), |
375 fetcher_(context_), | 375 fetcher_(context_.get()), |
376 finished_(false), | 376 finished_(false), |
377 result_(ERR_UNEXPECTED) { | 377 result_(ERR_UNEXPECTED) { |
378 } | 378 } |
379 | 379 |
380 void RunTest() { | 380 void RunTest() { |
381 int result = fetcher_.Fetch( | 381 int result = fetcher_.Fetch( |
382 &pac_text_, | 382 &pac_text_, |
383 base::Bind(&FetcherClient::OnCompletion, base::Unretained(this))); | 383 base::Bind(&FetcherClient::OnCompletion, base::Unretained(this))); |
384 ASSERT_EQ(ERR_IO_PENDING, result); | 384 ASSERT_EQ(ERR_IO_PENDING, result); |
385 } | 385 } |
(...skipping 18 matching lines...) Expand all Loading... |
404 result_ = result; | 404 result_ = result; |
405 } | 405 } |
406 | 406 |
407 void ResetTestState() { | 407 void ResetTestState() { |
408 finished_ = false; | 408 finished_ = false; |
409 result_ = ERR_UNEXPECTED; | 409 result_ = ERR_UNEXPECTED; |
410 pac_text_ = L""; | 410 pac_text_ = L""; |
411 fetcher_.ResetTestState(); | 411 fetcher_.ResetTestState(); |
412 } | 412 } |
413 | 413 |
414 scoped_refptr<URLRequestContext> context_; | 414 scoped_ptr<URLRequestContext> context_; |
415 MockDhcpProxyScriptFetcherWin fetcher_; | 415 MockDhcpProxyScriptFetcherWin fetcher_; |
416 bool finished_; | 416 bool finished_; |
417 int result_; | 417 int result_; |
418 string16 pac_text_; | 418 string16 pac_text_; |
419 }; | 419 }; |
420 | 420 |
421 // We separate out each test's logic so that we can easily implement | 421 // We separate out each test's logic so that we can easily implement |
422 // the ReuseFetcher test at the bottom. | 422 // the ReuseFetcher test at the bottom. |
423 void TestNormalCaseURLConfiguredOneAdapter(FetcherClient* client) { | 423 void TestNormalCaseURLConfiguredOneAdapter(FetcherClient* client) { |
424 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 424 TestURLRequestContext context; |
425 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 425 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
426 new DummyDhcpProxyScriptAdapterFetcher(context)); | 426 new DummyDhcpProxyScriptAdapterFetcher(&context)); |
427 adapter_fetcher->Configure(true, OK, L"bingo", 1); | 427 adapter_fetcher->Configure(true, OK, L"bingo", 1); |
428 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); | 428 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); |
429 client->RunTest(); | 429 client->RunTest(); |
430 client->RunMessageLoopUntilComplete(); | 430 client->RunMessageLoopUntilComplete(); |
431 ASSERT_EQ(OK, client->result_); | 431 ASSERT_EQ(OK, client->result_); |
432 ASSERT_EQ(L"bingo", client->pac_text_); | 432 ASSERT_EQ(L"bingo", client->pac_text_); |
433 } | 433 } |
434 | 434 |
435 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredOneAdapter) { | 435 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredOneAdapter) { |
436 FetcherClient client; | 436 FetcherClient client; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 client->fetcher_.max_wait_ms_ - (client->fetcher_.max_wait_ms_ / 10)), | 566 client->fetcher_.max_wait_ms_ - (client->fetcher_.max_wait_ms_ / 10)), |
567 timer.Elapsed()); | 567 timer.Elapsed()); |
568 } | 568 } |
569 | 569 |
570 TEST(DhcpProxyScriptFetcherWin, ShortCircuitLessPreferredAdapters) { | 570 TEST(DhcpProxyScriptFetcherWin, ShortCircuitLessPreferredAdapters) { |
571 FetcherClient client; | 571 FetcherClient client; |
572 TestShortCircuitLessPreferredAdapters(&client); | 572 TestShortCircuitLessPreferredAdapters(&client); |
573 } | 573 } |
574 | 574 |
575 void TestImmediateCancel(FetcherClient* client) { | 575 void TestImmediateCancel(FetcherClient* client) { |
576 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 576 TestURLRequestContext context; |
577 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 577 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
578 new DummyDhcpProxyScriptAdapterFetcher(context)); | 578 new DummyDhcpProxyScriptAdapterFetcher(&context)); |
579 adapter_fetcher->Configure(true, OK, L"bingo", 1); | 579 adapter_fetcher->Configure(true, OK, L"bingo", 1); |
580 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); | 580 client->fetcher_.PushBackAdapter("a", adapter_fetcher.release()); |
581 client->RunTest(); | 581 client->RunTest(); |
582 client->fetcher_.Cancel(); | 582 client->fetcher_.Cancel(); |
583 client->RunMessageLoopUntilWorkerDone(); | 583 client->RunMessageLoopUntilWorkerDone(); |
584 ASSERT_EQ(0, client->fetcher_.num_fetchers_created_); | 584 ASSERT_EQ(0, client->fetcher_.num_fetchers_created_); |
585 } | 585 } |
586 | 586 |
587 // Regression test to check that when we cancel immediately, no | 587 // Regression test to check that when we cancel immediately, no |
588 // adapter fetchers get created. | 588 // adapter fetchers get created. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 } | 624 } |
625 | 625 |
626 // Re-do the first test to make sure the last test that was run did | 626 // Re-do the first test to make sure the last test that was run did |
627 // not leave things in a bad state. | 627 // not leave things in a bad state. |
628 (*test_functions.begin())(&client); | 628 (*test_functions.begin())(&client); |
629 } | 629 } |
630 | 630 |
631 } // namespace | 631 } // namespace |
632 | 632 |
633 } // namespace net | 633 } // namespace net |
OLD | NEW |