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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 void PushBackAdapter(const std::string& adapter_name, | 301 void PushBackAdapter(const std::string& adapter_name, |
302 DhcpProxyScriptAdapterFetcher* fetcher) { | 302 DhcpProxyScriptAdapterFetcher* fetcher) { |
303 adapter_query_->mock_adapter_names_.push_back(adapter_name); | 303 adapter_query_->mock_adapter_names_.push_back(adapter_name); |
304 adapter_fetchers_.push_back(fetcher); | 304 adapter_fetchers_.push_back(fetcher); |
305 } | 305 } |
306 | 306 |
307 void ConfigureAndPushBackAdapter(const std::string& adapter_name, | 307 void ConfigureAndPushBackAdapter(const std::string& adapter_name, |
308 bool did_finish, | 308 bool did_finish, |
309 int result, | 309 int result, |
310 string16 pac_script, | 310 string16 pac_script, |
311 int fetch_delay_ms) { | 311 base::TimeDelta fetch_delay) { |
312 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 312 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
313 new DummyDhcpProxyScriptAdapterFetcher(url_request_context())); | 313 new DummyDhcpProxyScriptAdapterFetcher(url_request_context())); |
314 adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms); | 314 adapter_fetcher->Configure( |
| 315 did_finish, result, pac_script, fetch_delay.InMilliseconds()); |
315 PushBackAdapter(adapter_name, adapter_fetcher.release()); | 316 PushBackAdapter(adapter_name, adapter_fetcher.release()); |
316 } | 317 } |
317 | 318 |
318 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { | 319 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { |
319 ++num_fetchers_created_; | 320 ++num_fetchers_created_; |
320 return adapter_fetchers_[next_adapter_fetcher_index_++]; | 321 return adapter_fetchers_[next_adapter_fetcher_index_++]; |
321 } | 322 } |
322 | 323 |
323 virtual AdapterQuery* ImplCreateAdapterQuery() OVERRIDE { | 324 virtual AdapterQuery* ImplCreateAdapterQuery() OVERRIDE { |
324 DCHECK(adapter_query_); | 325 DCHECK(adapter_query_); |
325 return adapter_query_.get(); | 326 return adapter_query_.get(); |
326 } | 327 } |
327 | 328 |
328 int ImplGetMaxWaitMs() OVERRIDE { | 329 base::TimeDelta ImplGetMaxWait() OVERRIDE { |
329 return max_wait_ms_; | 330 return max_wait_; |
330 } | 331 } |
331 | 332 |
332 void ImplOnGetCandidateAdapterNamesDone() OVERRIDE { | 333 void ImplOnGetCandidateAdapterNamesDone() OVERRIDE { |
333 worker_finished_event_.Signal(); | 334 worker_finished_event_.Signal(); |
334 } | 335 } |
335 | 336 |
336 void ResetTestState() { | 337 void ResetTestState() { |
337 // Delete any adapter fetcher objects we didn't hand out. | 338 // Delete any adapter fetcher objects we didn't hand out. |
338 std::vector<DhcpProxyScriptAdapterFetcher*>::const_iterator it | 339 std::vector<DhcpProxyScriptAdapterFetcher*>::const_iterator it |
339 = adapter_fetchers_.begin(); | 340 = adapter_fetchers_.begin(); |
340 for (; it != adapter_fetchers_.end(); ++it) { | 341 for (; it != adapter_fetchers_.end(); ++it) { |
341 if (num_fetchers_created_-- <= 0) { | 342 if (num_fetchers_created_-- <= 0) { |
342 delete (*it); | 343 delete (*it); |
343 } | 344 } |
344 } | 345 } |
345 | 346 |
346 next_adapter_fetcher_index_ = 0; | 347 next_adapter_fetcher_index_ = 0; |
347 num_fetchers_created_ = 0; | 348 num_fetchers_created_ = 0; |
348 adapter_fetchers_.clear(); | 349 adapter_fetchers_.clear(); |
349 adapter_query_ = new MockAdapterQuery(); | 350 adapter_query_ = new MockAdapterQuery(); |
350 max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); | 351 max_wait_ = TestTimeouts::tiny_timeout(); |
351 } | 352 } |
352 | 353 |
353 bool HasPendingFetchers() { | 354 bool HasPendingFetchers() { |
354 return num_pending_fetchers() > 0; | 355 return num_pending_fetchers() > 0; |
355 } | 356 } |
356 | 357 |
357 int next_adapter_fetcher_index_; | 358 int next_adapter_fetcher_index_; |
358 | 359 |
359 // Ownership gets transferred to the implementation class via | 360 // Ownership gets transferred to the implementation class via |
360 // ImplCreateAdapterFetcher, but any objects not handed out are | 361 // ImplCreateAdapterFetcher, but any objects not handed out are |
361 // deleted on destruction. | 362 // deleted on destruction. |
362 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; | 363 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; |
363 | 364 |
364 scoped_refptr<MockAdapterQuery> adapter_query_; | 365 scoped_refptr<MockAdapterQuery> adapter_query_; |
365 | 366 |
366 int max_wait_ms_; | 367 base::TimeDelta max_wait_; |
367 int num_fetchers_created_; | 368 int num_fetchers_created_; |
368 base::WaitableEvent worker_finished_event_; | 369 base::WaitableEvent worker_finished_event_; |
369 }; | 370 }; |
370 | 371 |
371 class FetcherClient { | 372 class FetcherClient { |
372 public: | 373 public: |
373 FetcherClient() | 374 FetcherClient() |
374 : context_(new TestURLRequestContext), | 375 : context_(new TestURLRequestContext), |
375 fetcher_(context_.get()), | 376 fetcher_(context_.get()), |
376 finished_(false), | 377 finished_(false), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 ASSERT_EQ(L"bingo", client->pac_text_); | 433 ASSERT_EQ(L"bingo", client->pac_text_); |
433 } | 434 } |
434 | 435 |
435 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredOneAdapter) { | 436 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredOneAdapter) { |
436 FetcherClient client; | 437 FetcherClient client; |
437 TestNormalCaseURLConfiguredOneAdapter(&client); | 438 TestNormalCaseURLConfiguredOneAdapter(&client); |
438 } | 439 } |
439 | 440 |
440 void TestNormalCaseURLConfiguredMultipleAdapters(FetcherClient* client) { | 441 void TestNormalCaseURLConfiguredMultipleAdapters(FetcherClient* client) { |
441 client->fetcher_.ConfigureAndPushBackAdapter( | 442 client->fetcher_.ConfigureAndPushBackAdapter( |
442 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 443 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 444 base::TimeDelta::FromMilliseconds(1)); |
443 client->fetcher_.ConfigureAndPushBackAdapter( | 445 client->fetcher_.ConfigureAndPushBackAdapter( |
444 "second", true, OK, L"bingo", 50); | 446 "second", true, OK, L"bingo", base::TimeDelta::FromMilliseconds(50)); |
445 client->fetcher_.ConfigureAndPushBackAdapter( | 447 client->fetcher_.ConfigureAndPushBackAdapter( |
446 "third", true, OK, L"rocko", 1); | 448 "third", true, OK, L"rocko", base::TimeDelta::FromMilliseconds(1)); |
447 client->RunTest(); | 449 client->RunTest(); |
448 client->RunMessageLoopUntilComplete(); | 450 client->RunMessageLoopUntilComplete(); |
449 ASSERT_EQ(OK, client->result_); | 451 ASSERT_EQ(OK, client->result_); |
450 ASSERT_EQ(L"bingo", client->pac_text_); | 452 ASSERT_EQ(L"bingo", client->pac_text_); |
451 } | 453 } |
452 | 454 |
453 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredMultipleAdapters) { | 455 TEST(DhcpProxyScriptFetcherWin, NormalCaseURLConfiguredMultipleAdapters) { |
454 FetcherClient client; | 456 FetcherClient client; |
455 TestNormalCaseURLConfiguredMultipleAdapters(&client); | 457 TestNormalCaseURLConfiguredMultipleAdapters(&client); |
456 } | 458 } |
457 | 459 |
458 void TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout( | 460 void TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout( |
459 FetcherClient* client) { | 461 FetcherClient* client) { |
460 client->fetcher_.ConfigureAndPushBackAdapter( | 462 client->fetcher_.ConfigureAndPushBackAdapter( |
461 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 463 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 464 base::TimeDelta::FromMilliseconds(1)); |
462 // This will time out. | 465 // This will time out. |
463 client->fetcher_.ConfigureAndPushBackAdapter( | 466 client->fetcher_.ConfigureAndPushBackAdapter( |
464 "second", false, ERR_IO_PENDING, L"bingo", | 467 "second", false, ERR_IO_PENDING, L"bingo", |
465 TestTimeouts::action_timeout_ms()); | 468 TestTimeouts::action_timeout()); |
466 client->fetcher_.ConfigureAndPushBackAdapter( | 469 client->fetcher_.ConfigureAndPushBackAdapter( |
467 "third", true, OK, L"rocko", 1); | 470 "third", true, OK, L"rocko", base::TimeDelta::FromMilliseconds(1)); |
468 client->RunTest(); | 471 client->RunTest(); |
469 client->RunMessageLoopUntilComplete(); | 472 client->RunMessageLoopUntilComplete(); |
470 ASSERT_EQ(OK, client->result_); | 473 ASSERT_EQ(OK, client->result_); |
471 ASSERT_EQ(L"rocko", client->pac_text_); | 474 ASSERT_EQ(L"rocko", client->pac_text_); |
472 } | 475 } |
473 | 476 |
474 TEST(DhcpProxyScriptFetcherWin, | 477 TEST(DhcpProxyScriptFetcherWin, |
475 NormalCaseURLConfiguredMultipleAdaptersWithTimeout) { | 478 NormalCaseURLConfiguredMultipleAdaptersWithTimeout) { |
476 FetcherClient client; | 479 FetcherClient client; |
477 TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout(&client); | 480 TestNormalCaseURLConfiguredMultipleAdaptersWithTimeout(&client); |
478 } | 481 } |
479 | 482 |
480 void TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout( | 483 void TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout( |
481 FetcherClient* client) { | 484 FetcherClient* client) { |
482 client->fetcher_.ConfigureAndPushBackAdapter( | 485 client->fetcher_.ConfigureAndPushBackAdapter( |
483 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 486 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 487 base::TimeDelta::FromMilliseconds(1)); |
484 // This will time out. | 488 // This will time out. |
485 client->fetcher_.ConfigureAndPushBackAdapter( | 489 client->fetcher_.ConfigureAndPushBackAdapter( |
486 "second", false, ERR_IO_PENDING, L"bingo", | 490 "second", false, ERR_IO_PENDING, L"bingo", |
487 TestTimeouts::action_timeout_ms()); | 491 TestTimeouts::action_timeout()); |
488 // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such | 492 // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such |
489 // should be chosen. | 493 // should be chosen. |
490 client->fetcher_.ConfigureAndPushBackAdapter( | 494 client->fetcher_.ConfigureAndPushBackAdapter( |
491 "third", true, ERR_PAC_STATUS_NOT_OK, L"", 1); | 495 "third", true, ERR_PAC_STATUS_NOT_OK, L"", |
| 496 base::TimeDelta::FromMilliseconds(1)); |
492 client->fetcher_.ConfigureAndPushBackAdapter( | 497 client->fetcher_.ConfigureAndPushBackAdapter( |
493 "fourth", true, ERR_NOT_IMPLEMENTED, L"", 1); | 498 "fourth", true, ERR_NOT_IMPLEMENTED, L"", |
| 499 base::TimeDelta::FromMilliseconds(1)); |
494 client->RunTest(); | 500 client->RunTest(); |
495 client->RunMessageLoopUntilComplete(); | 501 client->RunMessageLoopUntilComplete(); |
496 ASSERT_EQ(ERR_PAC_STATUS_NOT_OK, client->result_); | 502 ASSERT_EQ(ERR_PAC_STATUS_NOT_OK, client->result_); |
497 ASSERT_EQ(L"", client->pac_text_); | 503 ASSERT_EQ(L"", client->pac_text_); |
498 } | 504 } |
499 | 505 |
500 TEST(DhcpProxyScriptFetcherWin, | 506 TEST(DhcpProxyScriptFetcherWin, |
501 FailureCaseURLConfiguredMultipleAdaptersWithTimeout) { | 507 FailureCaseURLConfiguredMultipleAdaptersWithTimeout) { |
502 FetcherClient client; | 508 FetcherClient client; |
503 TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout(&client); | 509 TestFailureCaseURLConfiguredMultipleAdaptersWithTimeout(&client); |
504 } | 510 } |
505 | 511 |
506 void TestFailureCaseNoURLConfigured(FetcherClient* client) { | 512 void TestFailureCaseNoURLConfigured(FetcherClient* client) { |
507 client->fetcher_.ConfigureAndPushBackAdapter( | 513 client->fetcher_.ConfigureAndPushBackAdapter( |
508 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 514 "most_preferred", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 515 base::TimeDelta::FromMilliseconds(1)); |
509 // This will time out. | 516 // This will time out. |
510 client->fetcher_.ConfigureAndPushBackAdapter( | 517 client->fetcher_.ConfigureAndPushBackAdapter( |
511 "second", false, ERR_IO_PENDING, L"bingo", | 518 "second", false, ERR_IO_PENDING, L"bingo", |
512 TestTimeouts::action_timeout_ms()); | 519 TestTimeouts::action_timeout()); |
513 // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such | 520 // This is the first non-ERR_PAC_NOT_IN_DHCP error and as such |
514 // should be chosen. | 521 // should be chosen. |
515 client->fetcher_.ConfigureAndPushBackAdapter( | 522 client->fetcher_.ConfigureAndPushBackAdapter( |
516 "third", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 523 "third", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 524 base::TimeDelta::FromMilliseconds(1)); |
517 client->RunTest(); | 525 client->RunTest(); |
518 client->RunMessageLoopUntilComplete(); | 526 client->RunMessageLoopUntilComplete(); |
519 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); | 527 ASSERT_EQ(ERR_PAC_NOT_IN_DHCP, client->result_); |
520 ASSERT_EQ(L"", client->pac_text_); | 528 ASSERT_EQ(L"", client->pac_text_); |
521 } | 529 } |
522 | 530 |
523 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoURLConfigured) { | 531 TEST(DhcpProxyScriptFetcherWin, FailureCaseNoURLConfigured) { |
524 FetcherClient client; | 532 FetcherClient client; |
525 TestFailureCaseNoURLConfigured(&client); | 533 TestFailureCaseNoURLConfigured(&client); |
526 } | 534 } |
(...skipping 10 matching lines...) Expand all Loading... |
537 FetcherClient client; | 545 FetcherClient client; |
538 TestFailureCaseNoDhcpAdapters(&client); | 546 TestFailureCaseNoDhcpAdapters(&client); |
539 } | 547 } |
540 | 548 |
541 void TestShortCircuitLessPreferredAdapters(FetcherClient* client) { | 549 void TestShortCircuitLessPreferredAdapters(FetcherClient* client) { |
542 // Here we have a bunch of adapters; the first reports no PAC in DHCP, | 550 // Here we have a bunch of adapters; the first reports no PAC in DHCP, |
543 // the second responds quickly with a PAC file, the rest take a long | 551 // the second responds quickly with a PAC file, the rest take a long |
544 // time. Verify that we complete quickly and do not wait for the slow | 552 // time. Verify that we complete quickly and do not wait for the slow |
545 // adapters, i.e. we finish before timeout. | 553 // adapters, i.e. we finish before timeout. |
546 client->fetcher_.ConfigureAndPushBackAdapter( | 554 client->fetcher_.ConfigureAndPushBackAdapter( |
547 "1", true, ERR_PAC_NOT_IN_DHCP, L"", 1); | 555 "1", true, ERR_PAC_NOT_IN_DHCP, L"", |
| 556 base::TimeDelta::FromMilliseconds(1)); |
548 client->fetcher_.ConfigureAndPushBackAdapter( | 557 client->fetcher_.ConfigureAndPushBackAdapter( |
549 "2", true, OK, L"bingo", 1); | 558 "2", true, OK, L"bingo", |
| 559 base::TimeDelta::FromMilliseconds(1)); |
550 client->fetcher_.ConfigureAndPushBackAdapter( | 560 client->fetcher_.ConfigureAndPushBackAdapter( |
551 "3", true, OK, L"wrongo", TestTimeouts::action_max_timeout_ms()); | 561 "3", true, OK, L"wrongo", TestTimeouts::action_max_timeout()); |
552 | 562 |
553 // Increase the timeout to ensure the short circuit mechanism has | 563 // Increase the timeout to ensure the short circuit mechanism has |
554 // time to kick in before the timeout waiting for more adapters kicks in. | 564 // time to kick in before the timeout waiting for more adapters kicks in. |
555 client->fetcher_.max_wait_ms_ = TestTimeouts::action_timeout_ms(); | 565 client->fetcher_.max_wait_ = TestTimeouts::action_timeout(); |
556 | 566 |
557 PerfTimer timer; | 567 PerfTimer timer; |
558 client->RunTest(); | 568 client->RunTest(); |
559 client->RunMessageLoopUntilComplete(); | 569 client->RunMessageLoopUntilComplete(); |
560 ASSERT_TRUE(client->fetcher_.HasPendingFetchers()); | 570 ASSERT_TRUE(client->fetcher_.HasPendingFetchers()); |
561 // Assert that the time passed is definitely less than the wait timer | 571 // Assert that the time passed is definitely less than the wait timer |
562 // timeout, to get a second signal that it was the shortcut mechanism | 572 // timeout, to get a second signal that it was the shortcut mechanism |
563 // (in OnFetcherDone) that kicked in, and not the timeout waiting for | 573 // (in OnFetcherDone) that kicked in, and not the timeout waiting for |
564 // more adapters. | 574 // more adapters. |
565 ASSERT_GT(base::TimeDelta::FromMilliseconds( | 575 ASSERT_GT(client->fetcher_.max_wait_ - (client->fetcher_.max_wait_ / 10), |
566 client->fetcher_.max_wait_ms_ - (client->fetcher_.max_wait_ms_ / 10)), | 576 timer.Elapsed()); |
567 timer.Elapsed()); | |
568 } | 577 } |
569 | 578 |
570 TEST(DhcpProxyScriptFetcherWin, ShortCircuitLessPreferredAdapters) { | 579 TEST(DhcpProxyScriptFetcherWin, ShortCircuitLessPreferredAdapters) { |
571 FetcherClient client; | 580 FetcherClient client; |
572 TestShortCircuitLessPreferredAdapters(&client); | 581 TestShortCircuitLessPreferredAdapters(&client); |
573 } | 582 } |
574 | 583 |
575 void TestImmediateCancel(FetcherClient* client) { | 584 void TestImmediateCancel(FetcherClient* client) { |
576 TestURLRequestContext context; | 585 TestURLRequestContext context; |
577 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 586 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
624 } | 633 } |
625 | 634 |
626 // Re-do the first test to make sure the last test that was run did | 635 // Re-do the first test to make sure the last test that was run did |
627 // not leave things in a bad state. | 636 // not leave things in a bad state. |
628 (*test_functions.begin())(&client); | 637 (*test_functions.begin())(&client); |
629 } | 638 } |
630 | 639 |
631 } // namespace | 640 } // namespace |
632 | 641 |
633 } // namespace net | 642 } // namespace net |
OLD | NEW |