| 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 "chrome/browser/captive_portal/captive_portal_service.h" | 5 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 content::NotificationRegistrar registrar_; | 126 content::NotificationRegistrar registrar_; |
| 127 | 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver); | 128 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver); |
| 129 }; | 129 }; |
| 130 | 130 |
| 131 } // namespace | 131 } // namespace |
| 132 | 132 |
| 133 class CaptivePortalServiceTest : public testing::Test { | 133 class CaptivePortalServiceTest : public testing::Test { |
| 134 public: | 134 public: |
| 135 CaptivePortalServiceTest() {} | 135 CaptivePortalServiceTest() |
| 136 : was_service_disabled_for_tests_( |
| 137 CaptivePortalService::is_disabled_for_tests()) { |
| 138 } |
| 136 | 139 |
| 137 virtual ~CaptivePortalServiceTest() {} | 140 virtual ~CaptivePortalServiceTest() { |
| 141 CaptivePortalService::set_is_disabled_for_tests( |
| 142 was_service_disabled_for_tests_); |
| 143 } |
| 138 | 144 |
| 139 void Initialize(bool enable_on_command_line) { | 145 // |enable_service| is whether or not the captive portal service itself |
| 140 if (enable_on_command_line) { | 146 // should be disabled. This is different from enabling the captive portal |
| 141 CommandLine::ForCurrentProcess()->AppendSwitch( | 147 // detection preference. |
| 142 switches::kCaptivePortalDetection); | 148 void Initialize(bool enable_service) { |
| 143 } | 149 CaptivePortalService::set_is_disabled_for_tests(!enable_service); |
| 144 | 150 |
| 145 profile_.reset(new TestingProfile()); | 151 profile_.reset(new TestingProfile()); |
| 146 service_.reset(new TestCaptivePortalService(profile_.get())); | 152 service_.reset(new TestCaptivePortalService(profile_.get())); |
| 147 scoped_ptr<TestCaptivePortalService> service_; | 153 scoped_ptr<TestCaptivePortalService> service_; |
| 148 | 154 |
| 149 // Use no delays for most tests. | 155 // Use no delays for most tests. |
| 150 set_initial_backoff_no_portal(base::TimeDelta()); | 156 set_initial_backoff_no_portal(base::TimeDelta()); |
| 151 set_initial_backoff_portal(base::TimeDelta()); | 157 set_initial_backoff_portal(base::TimeDelta()); |
| 152 | 158 |
| 153 // Disable jitter, so can check exact values. | 159 // Disable jitter, so can check exact values. |
| 154 set_jitter_factor(0.0); | 160 set_jitter_factor(0.0); |
| 155 | 161 |
| 156 // These values make checking exponential backoff easier. | 162 // These values make checking exponential backoff easier. |
| 157 set_multiply_factor(2.0); | 163 set_multiply_factor(2.0); |
| 158 set_maximum_backoff(base::TimeDelta::FromSeconds(1600)); | 164 set_maximum_backoff(base::TimeDelta::FromSeconds(1600)); |
| 159 | 165 |
| 160 // This means backoff starts after the first "failure", which is the second | 166 // This means backoff starts after the first "failure", which is the second |
| 161 // captive portal test in a row that ends up with the same result. | 167 // captive portal test in a row that ends up with the same result. |
| 162 set_num_errors_to_ignore(0); | 168 set_num_errors_to_ignore(0); |
| 163 | 169 |
| 164 EnableCaptivePortalDetection(true); | 170 EnableCaptivePortalDetectionPreference(true); |
| 165 } | 171 } |
| 166 | 172 |
| 167 // Sets the captive portal checking preference. | 173 // Sets the captive portal checking preference. |
| 168 void EnableCaptivePortalDetection(bool enabled) { | 174 void EnableCaptivePortalDetectionPreference(bool enabled) { |
| 169 profile()->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, | 175 profile()->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, |
| 170 enabled); | 176 enabled); |
| 171 } | 177 } |
| 172 | 178 |
| 173 // Calls the corresponding CaptivePortalService function. | 179 // Calls the corresponding CaptivePortalService function. |
| 174 void OnURLFetchComplete(net::URLFetcher* fetcher) { | 180 void OnURLFetchComplete(net::URLFetcher* fetcher) { |
| 175 service()->OnURLFetchComplete(fetcher); | 181 service()->OnURLFetchComplete(fetcher); |
| 176 } | 182 } |
| 177 | 183 |
| 178 // Triggers a captive portal check, then simulates the URL request | 184 // Triggers a captive portal check, then simulates the URL request |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 322 |
| 317 void set_jitter_factor(double jitter_factor) { | 323 void set_jitter_factor(double jitter_factor) { |
| 318 service()->recheck_policy().backoff_policy.jitter_factor = jitter_factor; | 324 service()->recheck_policy().backoff_policy.jitter_factor = jitter_factor; |
| 319 } | 325 } |
| 320 | 326 |
| 321 TestingProfile* profile() { return profile_.get(); } | 327 TestingProfile* profile() { return profile_.get(); } |
| 322 | 328 |
| 323 TestCaptivePortalService* service() { return service_.get(); } | 329 TestCaptivePortalService* service() { return service_.get(); } |
| 324 | 330 |
| 325 private: | 331 private: |
| 332 // Stores the initial value of CaptivePortalService::is_disabled_for_tests |
| 333 // so it can be restored after the test. |
| 334 const bool was_service_disabled_for_tests_; |
| 335 |
| 326 MessageLoop message_loop_; | 336 MessageLoop message_loop_; |
| 327 | 337 |
| 328 // Note that the construction order of these matters. | 338 // Note that the construction order of these matters. |
| 329 scoped_ptr<TestingProfile> profile_; | 339 scoped_ptr<TestingProfile> profile_; |
| 330 scoped_ptr<TestCaptivePortalService> service_; | 340 scoped_ptr<TestCaptivePortalService> service_; |
| 331 }; | 341 }; |
| 332 | 342 |
| 333 // Test when connected to the Internet and get the expected 204 response. | 343 // Test when connected to the Internet and get the expected 204 response. |
| 334 TEST_F(CaptivePortalServiceTest, CaptivePortalInternetConnected) { | 344 TEST_F(CaptivePortalServiceTest, CaptivePortalInternetConnected) { |
| 335 Initialize(true); | 345 Initialize(true); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 // Check that everything works as expected when captive portal checking is | 438 // Check that everything works as expected when captive portal checking is |
| 429 // disabled, including throttling. Then enables it again and runs another test. | 439 // disabled, including throttling. Then enables it again and runs another test. |
| 430 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) { | 440 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) { |
| 431 Initialize(true); | 441 Initialize(true); |
| 432 | 442 |
| 433 // This value should have no effect on this test. | 443 // This value should have no effect on this test. |
| 434 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); | 444 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); |
| 435 | 445 |
| 436 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); | 446 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); |
| 437 | 447 |
| 438 EnableCaptivePortalDetection(false); | 448 EnableCaptivePortalDetectionPreference(false); |
| 439 | 449 |
| 440 RunDisabledTest(0); | 450 RunDisabledTest(0); |
| 441 for (int i = 0; i < 6; ++i) | 451 for (int i = 0; i < 6; ++i) |
| 442 RunDisabledTest(100); | 452 RunDisabledTest(100); |
| 443 | 453 |
| 444 EnableCaptivePortalDetection(true); | 454 EnableCaptivePortalDetectionPreference(true); |
| 445 | 455 |
| 446 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); | 456 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); |
| 447 } | 457 } |
| 448 | 458 |
| 449 // Check that disabling the captive portal service while a check is running | 459 // Check that disabling the captive portal service while a check is running |
| 450 // works. | 460 // works. |
| 451 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { | 461 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { |
| 452 Initialize(true); | 462 Initialize(true); |
| 453 CaptivePortalObserver observer(profile(), service()); | 463 CaptivePortalObserver observer(profile(), service()); |
| 454 | 464 |
| 455 // Needed to create the URLFetcher, even if it never returns any results. | 465 // Needed to create the URLFetcher, even if it never returns any results. |
| 456 net::TestURLFetcherFactory factory; | 466 net::TestURLFetcherFactory factory; |
| 457 service()->DetectCaptivePortal(); | 467 service()->DetectCaptivePortal(); |
| 458 | 468 |
| 459 MessageLoop::current()->RunAllPending(); | 469 MessageLoop::current()->RunAllPending(); |
| 460 EXPECT_TRUE(FetchingURL()); | 470 EXPECT_TRUE(FetchingURL()); |
| 461 EXPECT_FALSE(TimerRunning()); | 471 EXPECT_FALSE(TimerRunning()); |
| 462 | 472 |
| 463 EnableCaptivePortalDetection(false); | 473 EnableCaptivePortalDetectionPreference(false); |
| 464 EXPECT_FALSE(FetchingURL()); | 474 EXPECT_FALSE(FetchingURL()); |
| 465 EXPECT_TRUE(TimerRunning()); | 475 EXPECT_TRUE(TimerRunning()); |
| 466 EXPECT_EQ(0, observer.num_results_received()); | 476 EXPECT_EQ(0, observer.num_results_received()); |
| 467 | 477 |
| 468 MessageLoop::current()->RunAllPending(); | 478 MessageLoop::current()->RunAllPending(); |
| 469 | 479 |
| 470 EXPECT_FALSE(FetchingURL()); | 480 EXPECT_FALSE(FetchingURL()); |
| 471 EXPECT_FALSE(TimerRunning()); | 481 EXPECT_FALSE(TimerRunning()); |
| 472 EXPECT_EQ(1, observer.num_results_received()); | 482 EXPECT_EQ(1, observer.num_results_received()); |
| 473 | 483 |
| 474 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 484 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
| 475 } | 485 } |
| 476 | 486 |
| 477 // Check that disabling the captive portal service while a check is pending | 487 // Check that disabling the captive portal service while a check is pending |
| 478 // works. | 488 // works. |
| 479 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { | 489 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { |
| 480 Initialize(true); | 490 Initialize(true); |
| 481 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); | 491 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); |
| 482 | 492 |
| 483 // Needed to create the URLFetcher, even if it never returns any results. | 493 // Needed to create the URLFetcher, even if it never returns any results. |
| 484 net::TestURLFetcherFactory factory; | 494 net::TestURLFetcherFactory factory; |
| 485 | 495 |
| 486 CaptivePortalObserver observer(profile(), service()); | 496 CaptivePortalObserver observer(profile(), service()); |
| 487 service()->DetectCaptivePortal(); | 497 service()->DetectCaptivePortal(); |
| 488 EXPECT_FALSE(FetchingURL()); | 498 EXPECT_FALSE(FetchingURL()); |
| 489 EXPECT_TRUE(TimerRunning()); | 499 EXPECT_TRUE(TimerRunning()); |
| 490 | 500 |
| 491 EnableCaptivePortalDetection(false); | 501 EnableCaptivePortalDetectionPreference(false); |
| 492 EXPECT_FALSE(FetchingURL()); | 502 EXPECT_FALSE(FetchingURL()); |
| 493 EXPECT_TRUE(TimerRunning()); | 503 EXPECT_TRUE(TimerRunning()); |
| 494 EXPECT_EQ(0, observer.num_results_received()); | 504 EXPECT_EQ(0, observer.num_results_received()); |
| 495 | 505 |
| 496 MessageLoop::current()->RunAllPending(); | 506 MessageLoop::current()->RunAllPending(); |
| 497 | 507 |
| 498 EXPECT_FALSE(FetchingURL()); | 508 EXPECT_FALSE(FetchingURL()); |
| 499 EXPECT_FALSE(TimerRunning()); | 509 EXPECT_FALSE(TimerRunning()); |
| 500 EXPECT_EQ(1, observer.num_results_received()); | 510 EXPECT_EQ(1, observer.num_results_received()); |
| 501 | 511 |
| 502 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 512 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
| 503 } | 513 } |
| 504 | 514 |
| 505 // Check that disabling the captive portal service while a check is pending | 515 // Check that disabling the captive portal service while a check is pending |
| 506 // works. | 516 // works. |
| 507 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { | 517 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { |
| 508 Initialize(true); | 518 Initialize(true); |
| 509 | 519 |
| 510 EnableCaptivePortalDetection(false); | 520 EnableCaptivePortalDetectionPreference(false); |
| 511 RunDisabledTest(0); | 521 RunDisabledTest(0); |
| 512 | 522 |
| 513 CaptivePortalObserver observer(profile(), service()); | 523 CaptivePortalObserver observer(profile(), service()); |
| 514 service()->DetectCaptivePortal(); | 524 service()->DetectCaptivePortal(); |
| 515 EXPECT_FALSE(FetchingURL()); | 525 EXPECT_FALSE(FetchingURL()); |
| 516 EXPECT_TRUE(TimerRunning()); | 526 EXPECT_TRUE(TimerRunning()); |
| 517 | 527 |
| 518 net::TestURLFetcherFactory factory; | 528 net::TestURLFetcherFactory factory; |
| 519 | 529 |
| 520 EnableCaptivePortalDetection(true); | 530 EnableCaptivePortalDetectionPreference(true); |
| 521 EXPECT_FALSE(FetchingURL()); | 531 EXPECT_FALSE(FetchingURL()); |
| 522 EXPECT_TRUE(TimerRunning()); | 532 EXPECT_TRUE(TimerRunning()); |
| 523 | 533 |
| 524 MessageLoop::current()->RunAllPending(); | 534 MessageLoop::current()->RunAllPending(); |
| 525 ASSERT_TRUE(FetchingURL()); | 535 ASSERT_TRUE(FetchingURL()); |
| 526 EXPECT_FALSE(TimerRunning()); | 536 EXPECT_FALSE(TimerRunning()); |
| 527 | 537 |
| 528 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 538 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 529 fetcher->set_response_code(200); | 539 fetcher->set_response_code(200); |
| 530 OnURLFetchComplete(fetcher); | 540 OnURLFetchComplete(fetcher); |
| 531 EXPECT_FALSE(FetchingURL()); | 541 EXPECT_FALSE(FetchingURL()); |
| 532 EXPECT_FALSE(TimerRunning()); | 542 EXPECT_FALSE(TimerRunning()); |
| 533 | 543 |
| 534 EXPECT_EQ(1, observer.num_results_received()); | 544 EXPECT_EQ(1, observer.num_results_received()); |
| 535 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); | 545 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); |
| 536 } | 546 } |
| 537 | 547 |
| 538 // Checks that disabling with a command line flag works as expected. | 548 // Checks that disabling for browser tests works as expected. |
| 539 TEST_F(CaptivePortalServiceTest, CaptivePortalDisabledAtCommandLine) { | 549 TEST_F(CaptivePortalServiceTest, CaptivePortalDisableForTests) { |
| 540 Initialize(false); | 550 Initialize(false); |
| 541 RunDisabledTest(0); | 551 RunDisabledTest(0); |
| 542 } | 552 } |
| 543 | 553 |
| 544 // Checks that jitter gives us values in the correct range. | 554 // Checks that jitter gives us values in the correct range. |
| 545 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) { | 555 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) { |
| 546 Initialize(true); | 556 Initialize(true); |
| 547 set_jitter_factor(0.3); | 557 set_jitter_factor(0.3); |
| 548 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 558 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
| 549 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); | 559 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 | 624 |
| 615 RunTest(RESULT_NO_RESPONSE, | 625 RunTest(RESULT_NO_RESPONSE, |
| 616 net::OK, | 626 net::OK, |
| 617 503, | 627 503, |
| 618 0, | 628 0, |
| 619 "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"); | 629 "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"); |
| 620 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); | 630 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); |
| 621 } | 631 } |
| 622 | 632 |
| 623 } // namespace captive_portal | 633 } // namespace captive_portal |
| OLD | NEW |