| 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_adapter_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "base/timer/elapsed_timer.h" | 10 #include "base/timer/elapsed_timer.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 fetcher_delay_ms_(1), | 44 fetcher_delay_ms_(1), |
| 45 fetcher_result_(OK), | 45 fetcher_result_(OK), |
| 46 pac_script_("bingo") { | 46 pac_script_("bingo") { |
| 47 } | 47 } |
| 48 | 48 |
| 49 void Cancel() { | 49 void Cancel() { |
| 50 DhcpProxyScriptAdapterFetcher::Cancel(); | 50 DhcpProxyScriptAdapterFetcher::Cancel(); |
| 51 fetcher_ = NULL; | 51 fetcher_ = NULL; |
| 52 } | 52 } |
| 53 | 53 |
| 54 virtual ProxyScriptFetcher* ImplCreateScriptFetcher() OVERRIDE { | 54 virtual ProxyScriptFetcher* ImplCreateScriptFetcher() override { |
| 55 // We don't maintain ownership of the fetcher, it is transferred to | 55 // We don't maintain ownership of the fetcher, it is transferred to |
| 56 // the caller. | 56 // the caller. |
| 57 fetcher_ = new MockProxyScriptFetcher(); | 57 fetcher_ = new MockProxyScriptFetcher(); |
| 58 if (fetcher_delay_ms_ != -1) { | 58 if (fetcher_delay_ms_ != -1) { |
| 59 fetcher_timer_.Start(FROM_HERE, | 59 fetcher_timer_.Start(FROM_HERE, |
| 60 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_), | 60 base::TimeDelta::FromMilliseconds(fetcher_delay_ms_), |
| 61 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer); | 61 this, &MockDhcpProxyScriptAdapterFetcher::OnFetcherTimer); |
| 62 } | 62 } |
| 63 return fetcher_; | 63 return fetcher_; |
| 64 } | 64 } |
| 65 | 65 |
| 66 class DelayingDhcpQuery : public DhcpQuery { | 66 class DelayingDhcpQuery : public DhcpQuery { |
| 67 public: | 67 public: |
| 68 explicit DelayingDhcpQuery() | 68 explicit DelayingDhcpQuery() |
| 69 : DhcpQuery(), | 69 : DhcpQuery(), |
| 70 test_finished_event_(true, false) { | 70 test_finished_event_(true, false) { |
| 71 } | 71 } |
| 72 | 72 |
| 73 std::string ImplGetPacURLFromDhcp( | 73 std::string ImplGetPacURLFromDhcp( |
| 74 const std::string& adapter_name) OVERRIDE { | 74 const std::string& adapter_name) override { |
| 75 base::ElapsedTimer timer; | 75 base::ElapsedTimer timer; |
| 76 test_finished_event_.TimedWait(dhcp_delay_); | 76 test_finished_event_.TimedWait(dhcp_delay_); |
| 77 return configured_url_; | 77 return configured_url_; |
| 78 } | 78 } |
| 79 | 79 |
| 80 base::WaitableEvent test_finished_event_; | 80 base::WaitableEvent test_finished_event_; |
| 81 base::TimeDelta dhcp_delay_; | 81 base::TimeDelta dhcp_delay_; |
| 82 std::string configured_url_; | 82 std::string configured_url_; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 virtual DhcpQuery* ImplCreateDhcpQuery() OVERRIDE { | 85 virtual DhcpQuery* ImplCreateDhcpQuery() override { |
| 86 dhcp_query_ = new DelayingDhcpQuery(); | 86 dhcp_query_ = new DelayingDhcpQuery(); |
| 87 dhcp_query_->dhcp_delay_ = dhcp_delay_; | 87 dhcp_query_->dhcp_delay_ = dhcp_delay_; |
| 88 dhcp_query_->configured_url_ = configured_url_; | 88 dhcp_query_->configured_url_ = configured_url_; |
| 89 return dhcp_query_; | 89 return dhcp_query_; |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Use a shorter timeout so tests can finish more quickly. | 92 // Use a shorter timeout so tests can finish more quickly. |
| 93 virtual base::TimeDelta ImplGetTimeout() const OVERRIDE { | 93 virtual base::TimeDelta ImplGetTimeout() const override { |
| 94 return timeout_; | 94 return timeout_; |
| 95 } | 95 } |
| 96 | 96 |
| 97 void OnFetcherTimer() { | 97 void OnFetcherTimer() { |
| 98 // Note that there is an assumption by this mock implementation that | 98 // Note that there is an assumption by this mock implementation that |
| 99 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher | 99 // DhcpProxyScriptAdapterFetcher::Fetch will call ImplCreateScriptFetcher |
| 100 // and call Fetch on the fetcher before the message loop is re-entered. | 100 // and call Fetch on the fetcher before the message loop is re-entered. |
| 101 // This holds true today, but if you hit this DCHECK the problem can | 101 // This holds true today, but if you hit this DCHECK the problem can |
| 102 // possibly be resolved by having a separate subclass of | 102 // possibly be resolved by having a separate subclass of |
| 103 // MockProxyScriptFetcher that adds the delay internally (instead of | 103 // MockProxyScriptFetcher that adds the delay internally (instead of |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 : public MockDhcpProxyScriptAdapterFetcher { | 265 : public MockDhcpProxyScriptAdapterFetcher { |
| 266 public: | 266 public: |
| 267 explicit MockDhcpRealFetchProxyScriptAdapterFetcher( | 267 explicit MockDhcpRealFetchProxyScriptAdapterFetcher( |
| 268 URLRequestContext* context, | 268 URLRequestContext* context, |
| 269 scoped_refptr<base::TaskRunner> task_runner) | 269 scoped_refptr<base::TaskRunner> task_runner) |
| 270 : MockDhcpProxyScriptAdapterFetcher(context, task_runner), | 270 : MockDhcpProxyScriptAdapterFetcher(context, task_runner), |
| 271 url_request_context_(context) { | 271 url_request_context_(context) { |
| 272 } | 272 } |
| 273 | 273 |
| 274 // Returns a real proxy script fetcher. | 274 // Returns a real proxy script fetcher. |
| 275 ProxyScriptFetcher* ImplCreateScriptFetcher() OVERRIDE { | 275 ProxyScriptFetcher* ImplCreateScriptFetcher() override { |
| 276 ProxyScriptFetcher* fetcher = | 276 ProxyScriptFetcher* fetcher = |
| 277 new ProxyScriptFetcherImpl(url_request_context_); | 277 new ProxyScriptFetcherImpl(url_request_context_); |
| 278 return fetcher; | 278 return fetcher; |
| 279 } | 279 } |
| 280 | 280 |
| 281 URLRequestContext* url_request_context_; | 281 URLRequestContext* url_request_context_; |
| 282 }; | 282 }; |
| 283 | 283 |
| 284 TEST(DhcpProxyScriptAdapterFetcher, MockDhcpRealFetch) { | 284 TEST(DhcpProxyScriptAdapterFetcher, MockDhcpRealFetch) { |
| 285 SpawnedTestServer test_server( | 285 SpawnedTestServer test_server( |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 EXPECT_EQ(BASE_URL, | 329 EXPECT_EQ(BASE_URL, |
| 330 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString( | 330 DhcpProxyScriptAdapterFetcher::SanitizeDhcpApiString( |
| 331 BASE_URL "\0foo\0blat", kBaseUrlLen + 9)); | 331 BASE_URL "\0foo\0blat", kBaseUrlLen + 9)); |
| 332 } | 332 } |
| 333 | 333 |
| 334 #undef BASE_URL | 334 #undef BASE_URL |
| 335 | 335 |
| 336 } // namespace | 336 } // namespace |
| 337 | 337 |
| 338 } // namespace net | 338 } // namespace net |
| OLD | NEW |