| 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 #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/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/perftimer.h" | 10 #include "base/perftimer.h" |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 bool did_finish_; | 256 bool did_finish_; |
| 257 int result_; | 257 int result_; |
| 258 string16 pac_script_; | 258 string16 pac_script_; |
| 259 int fetch_delay_ms_; | 259 int fetch_delay_ms_; |
| 260 CompletionCallback* client_callback_; | 260 CompletionCallback* client_callback_; |
| 261 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; | 261 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; |
| 262 }; | 262 }; |
| 263 | 263 |
| 264 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { | 264 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
| 265 public: | 265 public: |
| 266 class MockWorkerThread : public WorkerThread { |
| 267 public: |
| 268 virtual ~MockWorkerThread() { |
| 269 } |
| 270 |
| 271 void Init(const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { |
| 272 WorkerThread::Init(owner); |
| 273 } |
| 274 |
| 275 virtual bool ImplGetCandidateAdapterNames( |
| 276 std::set<std::string>* adapter_names) OVERRIDE { |
| 277 adapter_names->insert( |
| 278 mock_adapter_names_.begin(), mock_adapter_names_.end()); |
| 279 return true; |
| 280 } |
| 281 |
| 282 std::vector<std::string> mock_adapter_names_; |
| 283 }; |
| 284 |
| 266 MockDhcpProxyScriptFetcherWin() | 285 MockDhcpProxyScriptFetcherWin() |
| 267 : DhcpProxyScriptFetcherWin(new TestURLRequestContext()) { | 286 : DhcpProxyScriptFetcherWin(new TestURLRequestContext()) { |
| 268 ResetTestState(); | 287 ResetTestState(); |
| 269 } | 288 } |
| 270 | 289 |
| 271 // Adds a fetcher object to the queue of fetchers used by | 290 // Adds a fetcher object to the queue of fetchers used by |
| 272 // |ImplCreateAdapterFetcher()|, and its name to the list of adapters | 291 // |ImplCreateAdapterFetcher()|, and its name to the list of adapters |
| 273 // returned by ImplGetCandidateAdapterNames. | 292 // returned by ImplGetCandidateAdapterNames. |
| 274 void PushBackAdapter(const std::string& adapter_name, | 293 void PushBackAdapter(const std::string& adapter_name, |
| 275 DhcpProxyScriptAdapterFetcher* fetcher) { | 294 DhcpProxyScriptAdapterFetcher* fetcher) { |
| 276 adapter_names_.push_back(adapter_name); | 295 worker_thread_->mock_adapter_names_.push_back(adapter_name); |
| 277 adapter_fetchers_.push_back(fetcher); | 296 adapter_fetchers_.push_back(fetcher); |
| 278 } | 297 } |
| 279 | 298 |
| 280 void ConfigureAndPushBackAdapter(const std::string& adapter_name, | 299 void ConfigureAndPushBackAdapter(const std::string& adapter_name, |
| 281 bool did_finish, | 300 bool did_finish, |
| 282 int result, | 301 int result, |
| 283 string16 pac_script, | 302 string16 pac_script, |
| 284 int fetch_delay_ms) { | 303 int fetch_delay_ms) { |
| 285 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 304 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
| 286 new DummyDhcpProxyScriptAdapterFetcher()); | 305 new DummyDhcpProxyScriptAdapterFetcher()); |
| 287 adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms); | 306 adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms); |
| 288 PushBackAdapter(adapter_name, adapter_fetcher.release()); | 307 PushBackAdapter(adapter_name, adapter_fetcher.release()); |
| 289 } | 308 } |
| 290 | 309 |
| 291 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { | 310 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { |
| 292 return adapter_fetchers_[next_adapter_fetcher_index_++]; | 311 return adapter_fetchers_[next_adapter_fetcher_index_++]; |
| 293 } | 312 } |
| 294 | 313 |
| 295 bool ImplGetCandidateAdapterNames( | 314 virtual WorkerThread* ImplCreateWorkerThread( |
| 296 std::set<std::string>* adapter_names) OVERRIDE { | 315 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) OVERRIDE { |
| 297 adapter_names->insert(adapter_names_.begin(), adapter_names_.end()); | 316 DCHECK(worker_thread_); |
| 298 return true; | 317 worker_thread_->Init(owner); |
| 318 return worker_thread_.get(); |
| 299 } | 319 } |
| 300 | 320 |
| 301 int ImplGetMaxWaitMs() OVERRIDE { | 321 int ImplGetMaxWaitMs() OVERRIDE { |
| 302 return max_wait_ms_; | 322 return max_wait_ms_; |
| 303 } | 323 } |
| 304 | 324 |
| 305 void ResetTestState() { | 325 void ResetTestState() { |
| 306 next_adapter_fetcher_index_ = 0; | 326 next_adapter_fetcher_index_ = 0; |
| 307 adapter_fetchers_.clear(); | 327 adapter_fetchers_.clear(); |
| 308 // String pointers contained herein will have been freed during test. | 328 worker_thread_ = new MockWorkerThread(); |
| 309 adapter_names_.clear(); | |
| 310 max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); | 329 max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); |
| 311 } | 330 } |
| 312 | 331 |
| 313 bool HasPendingFetchers() { | 332 bool HasPendingFetchers() { |
| 314 return num_pending_fetchers() > 0; | 333 return num_pending_fetchers() > 0; |
| 315 } | 334 } |
| 316 | 335 |
| 317 int next_adapter_fetcher_index_; | 336 int next_adapter_fetcher_index_; |
| 318 | 337 |
| 319 // Ownership is not here; it gets transferred to the implementation | 338 // Ownership is not here; it gets transferred to the implementation |
| 320 // class via ImplCreateAdapterFetcher. | 339 // class via ImplCreateAdapterFetcher. |
| 321 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; | 340 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; |
| 322 | 341 |
| 323 std::vector<std::string> adapter_names_; | 342 scoped_refptr<MockWorkerThread> worker_thread_; |
| 324 | 343 |
| 325 int max_wait_ms_; | 344 int max_wait_ms_; |
| 326 }; | 345 }; |
| 327 | 346 |
| 328 class FetcherClient { | 347 class FetcherClient { |
| 329 public: | 348 public: |
| 330 FetcherClient() | 349 FetcherClient() |
| 331 : finished_(false), | 350 : finished_(false), |
| 332 result_(ERR_UNEXPECTED), | 351 result_(ERR_UNEXPECTED), |
| 333 ALLOW_THIS_IN_INITIALIZER_LIST( | 352 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 334 completion_callback_(this, &FetcherClient::OnCompletion)) { | 353 completion_callback_(this, &FetcherClient::OnCompletion)) { |
| 335 } | 354 } |
| 336 | 355 |
| 337 void RunTest() { | 356 void RunTest() { |
| 338 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); | 357 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); |
| 339 ASSERT_EQ(ERR_IO_PENDING, result); | 358 ASSERT_EQ(ERR_IO_PENDING, result); |
| 340 } | 359 } |
| 341 | 360 |
| 342 void RunImmediateReturnTest() { | |
| 343 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); | |
| 344 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, result); | |
| 345 } | |
| 346 | |
| 347 void RunMessageLoopUntilComplete() { | 361 void RunMessageLoopUntilComplete() { |
| 348 while (!finished_) { | 362 while (!finished_) { |
| 349 MessageLoop::current()->RunAllPending(); | 363 MessageLoop::current()->RunAllPending(); |
| 350 } | 364 } |
| 351 MessageLoop::current()->RunAllPending(); | 365 MessageLoop::current()->RunAllPending(); |
| 352 } | 366 } |
| 353 | 367 |
| 354 void OnCompletion(int result) { | 368 void OnCompletion(int result) { |
| 355 finished_ = true; | 369 finished_ = true; |
| 356 result_ = result; | 370 result_ = result; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); | 484 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); |
| 471 ASSERT_EQ(L"", client->pac_text_); | 485 ASSERT_EQ(L"", client->pac_text_); |
| 472 } | 486 } |
| 473 | 487 |
| 474 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoURLConfigured) { | 488 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoURLConfigured) { |
| 475 FetcherClient client; | 489 FetcherClient client; |
| 476 TestFailureCaseNoURLConfigured(&client); | 490 TestFailureCaseNoURLConfigured(&client); |
| 477 } | 491 } |
| 478 | 492 |
| 479 void TestFailureCaseNoDhcpAdapters(FetcherClient* client) { | 493 void TestFailureCaseNoDhcpAdapters(FetcherClient* client) { |
| 480 client->RunImmediateReturnTest(); | 494 client->RunTest(); |
| 481 // In case there are any pending messages that get us in a bad state | 495 client->RunMessageLoopUntilComplete(); |
| 482 // (there shouldn't be). | 496 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); |
| 483 MessageLoop::current()->RunAllPending(); | 497 ASSERT_EQ(L"", client->pac_text_); |
| 484 } | 498 } |
| 485 | 499 |
| 486 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoDhcpAdapters) { | 500 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoDhcpAdapters) { |
| 487 FetcherClient client; | 501 FetcherClient client; |
| 488 TestFailureCaseNoDhcpAdapters(&client); | 502 TestFailureCaseNoDhcpAdapters(&client); |
| 489 } | 503 } |
| 490 | 504 |
| 491 void TestShortCircuitLessPreferredAdapters(FetcherClient* client) { | 505 void TestShortCircuitLessPreferredAdapters(FetcherClient* client) { |
| 492 // Here we have a bunch of adapters; the first reports no PAC in DHCP, | 506 // Here we have a bunch of adapters; the first reports no PAC in DHCP, |
| 493 // the second responds quickly with a PAC file, the rest take a long | 507 // the second responds quickly with a PAC file, the rest take a long |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 } | 568 } |
| 555 | 569 |
| 556 // Re-do the first test to make sure the last test that was run did | 570 // Re-do the first test to make sure the last test that was run did |
| 557 // not leave things in a bad state. | 571 // not leave things in a bad state. |
| 558 (*test_functions.begin())(&client); | 572 (*test_functions.begin())(&client); |
| 559 } | 573 } |
| 560 | 574 |
| 561 } // namespace | 575 } // namespace |
| 562 | 576 |
| 563 } // namespace net | 577 } // namespace net |
| OLD | NEW |