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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 set_initial_backoff_no_portal(base::TimeDelta()); | 156 set_initial_backoff_no_portal(base::TimeDelta()); |
157 set_initial_backoff_portal(base::TimeDelta()); | 157 set_initial_backoff_portal(base::TimeDelta()); |
158 | 158 |
159 // Disable jitter, so can check exact values. | 159 // Disable jitter, so can check exact values. |
160 set_jitter_factor(0.0); | 160 set_jitter_factor(0.0); |
161 | 161 |
162 // These values make checking exponential backoff easier. | 162 // These values make checking exponential backoff easier. |
163 set_multiply_factor(2.0); | 163 set_multiply_factor(2.0); |
164 set_maximum_backoff(base::TimeDelta::FromSeconds(1600)); | 164 set_maximum_backoff(base::TimeDelta::FromSeconds(1600)); |
165 | 165 |
166 // This means backoff starts after the first "failure", which is the second | 166 // This means backoff starts after the second "failure", which is the third |
167 // 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. Since |
168 set_num_errors_to_ignore(0); | 168 // the first request uses no delay, this means the delays will be in |
| 169 // the pattern 0, 0, 100, 200, 400, etc. There are two zeros because the |
| 170 // first check never has a delay, and the first check to have a new result |
| 171 // is followed by no delay. |
| 172 set_num_errors_to_ignore(1); |
169 | 173 |
170 EnableCaptivePortalDetectionPreference(true); | 174 EnableCaptivePortalDetectionPreference(true); |
171 } | 175 } |
172 | 176 |
173 // Sets the captive portal checking preference. | 177 // Sets the captive portal checking preference. |
174 void EnableCaptivePortalDetectionPreference(bool enabled) { | 178 void EnableCaptivePortalDetectionPreference(bool enabled) { |
175 profile()->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, | 179 profile()->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, |
176 enabled); | 180 enabled); |
177 } | 181 } |
178 | 182 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 EXPECT_FALSE(TimerRunning()); | 271 EXPECT_FALSE(TimerRunning()); |
268 EXPECT_EQ(1, observer.num_results_received()); | 272 EXPECT_EQ(1, observer.num_results_received()); |
269 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 273 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
270 } | 274 } |
271 | 275 |
272 // Tests exponential backoff. Prior to calling, the relevant recheck settings | 276 // Tests exponential backoff. Prior to calling, the relevant recheck settings |
273 // must be set to have a minimum time of 100 seconds, with 2 checks before | 277 // must be set to have a minimum time of 100 seconds, with 2 checks before |
274 // starting exponential backoff. | 278 // starting exponential backoff. |
275 void RunBackoffTest(Result expected_result, int net_error, int status_code) { | 279 void RunBackoffTest(Result expected_result, int net_error, int status_code) { |
276 RunTest(expected_result, net_error, status_code, 0, NULL); | 280 RunTest(expected_result, net_error, status_code, 0, NULL); |
| 281 RunTest(expected_result, net_error, status_code, 0, NULL); |
277 RunTest(expected_result, net_error, status_code, 100, NULL); | 282 RunTest(expected_result, net_error, status_code, 100, NULL); |
278 RunTest(expected_result, net_error, status_code, 200, NULL); | 283 RunTest(expected_result, net_error, status_code, 200, NULL); |
279 RunTest(expected_result, net_error, status_code, 400, NULL); | 284 RunTest(expected_result, net_error, status_code, 400, NULL); |
280 RunTest(expected_result, net_error, status_code, 800, NULL); | 285 RunTest(expected_result, net_error, status_code, 800, NULL); |
281 RunTest(expected_result, net_error, status_code, 1600, NULL); | 286 RunTest(expected_result, net_error, status_code, 1600, NULL); |
282 RunTest(expected_result, net_error, status_code, 1600, NULL); | 287 RunTest(expected_result, net_error, status_code, 1600, NULL); |
283 } | 288 } |
284 | 289 |
285 bool FetchingURL() { | 290 bool FetchingURL() { |
286 return service()->FetchingURL(); | 291 return service()->FetchingURL(); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 Initialize(true); | 400 Initialize(true); |
396 | 401 |
397 // This value should have no effect on this test, until the end. | 402 // This value should have no effect on this test, until the end. |
398 set_initial_backoff_portal(base::TimeDelta::FromSeconds(1)); | 403 set_initial_backoff_portal(base::TimeDelta::FromSeconds(1)); |
399 | 404 |
400 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 405 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
401 RunBackoffTest(RESULT_INTERNET_CONNECTED, net::OK, 204); | 406 RunBackoffTest(RESULT_INTERNET_CONNECTED, net::OK, 204); |
402 | 407 |
403 // Make sure that getting a new result resets the timer. | 408 // Make sure that getting a new result resets the timer. |
404 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1600, NULL); | 409 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1600, NULL); |
| 410 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); |
405 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1, NULL); | 411 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1, NULL); |
| 412 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 2, NULL); |
406 } | 413 } |
407 | 414 |
408 // Checks exponential backoff when there's an HTTP error. | 415 // Checks exponential backoff when there's an HTTP error. |
409 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckError) { | 416 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckError) { |
410 Initialize(true); | 417 Initialize(true); |
411 | 418 |
412 // This value should have no effect on this test. | 419 // This value should have no effect on this test. |
413 set_initial_backoff_portal(base::TimeDelta::FromDays(1)); | 420 set_initial_backoff_portal(base::TimeDelta::FromDays(1)); |
414 | 421 |
415 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 422 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
416 RunBackoffTest(RESULT_NO_RESPONSE, net::OK, 500); | 423 RunBackoffTest(RESULT_NO_RESPONSE, net::OK, 500); |
417 | 424 |
418 // Make sure that getting a new result resets the timer. | 425 // Make sure that getting a new result resets the timer. |
419 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); | 426 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); |
| 427 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); |
420 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 100, NULL); | 428 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 100, NULL); |
421 } | 429 } |
422 | 430 |
423 // Checks exponential backoff when there's a captive portal. | 431 // Checks exponential backoff when there's a captive portal. |
424 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckBehindPortal) { | 432 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckBehindPortal) { |
425 Initialize(true); | 433 Initialize(true); |
426 | 434 |
427 // This value should have no effect on this test, until the end. | 435 // This value should have no effect on this test, until the end. |
428 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(250)); | 436 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(250)); |
429 | 437 |
430 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); | 438 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); |
431 RunBackoffTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200); | 439 RunBackoffTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200); |
432 | 440 |
433 // Make sure that getting a new result resets the timer. | 441 // Make sure that getting a new result resets the timer. |
434 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); | 442 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); |
| 443 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); |
435 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 250, NULL); | 444 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 250, NULL); |
436 } | 445 } |
437 | 446 |
438 // Check that everything works as expected when captive portal checking is | 447 // Check that everything works as expected when captive portal checking is |
439 // disabled, including throttling. Then enables it again and runs another test. | 448 // disabled, including throttling. Then enables it again and runs another test. |
440 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) { | 449 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) { |
441 Initialize(true); | 450 Initialize(true); |
442 | 451 |
443 // This value should have no effect on this test. | 452 // This value should have no effect on this test. |
444 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); | 453 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 Initialize(false); | 559 Initialize(false); |
551 RunDisabledTest(0); | 560 RunDisabledTest(0); |
552 } | 561 } |
553 | 562 |
554 // Checks that jitter gives us values in the correct range. | 563 // Checks that jitter gives us values in the correct range. |
555 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) { | 564 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) { |
556 Initialize(true); | 565 Initialize(true); |
557 set_jitter_factor(0.3); | 566 set_jitter_factor(0.3); |
558 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 567 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
559 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); | 568 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); |
| 569 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); |
560 | 570 |
561 for (int i = 0; i < 50; ++i) { | 571 for (int i = 0; i < 50; ++i) { |
562 int interval_sec = GetTimeUntilNextRequest().InSeconds(); | 572 int interval_sec = GetTimeUntilNextRequest().InSeconds(); |
563 // Allow for roundoff, though shouldn't be necessary. | 573 // Allow for roundoff, though shouldn't be necessary. |
564 EXPECT_LE(69, interval_sec); | 574 EXPECT_LE(69, interval_sec); |
565 EXPECT_LE(interval_sec, 101); | 575 EXPECT_LE(interval_sec, 101); |
566 } | 576 } |
567 } | 577 } |
568 | 578 |
569 // Check a Retry-After header that contains a delay in seconds. | 579 // Check a Retry-After header that contains a delay in seconds. |
570 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSeconds) { | 580 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSeconds) { |
571 Initialize(true); | 581 Initialize(true); |
572 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 582 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
| 583 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; |
573 | 584 |
574 RunTest(RESULT_NO_RESPONSE, | 585 // Check that Retry-After headers work both on the first request to return a |
575 net::OK, | 586 // result and on subsequent requests. |
576 503, | 587 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); |
577 0, | 588 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 101, retry_after); |
578 "HTTP/1.1 503 OK\nRetry-After: 101\n\n"); | 589 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 101, NULL); |
579 | 590 |
580 // Run another captive portal check to make sure the time until the next check | 591 // Make sure that there's no effect on the next captive portal check after |
581 // is as expected. | 592 // login. |
582 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 101, NULL); | 593 EXPECT_EQ(base::TimeDelta::FromSeconds(0), GetTimeUntilNextRequest()); |
583 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); | |
584 } | 594 } |
585 | 595 |
586 // Check that the RecheckPolicy is still respected on 503 responses with | 596 // Check that the RecheckPolicy is still respected on 503 responses with |
587 // Retry-After headers. | 597 // Retry-After headers. |
588 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSecondsTooShort) { | 598 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSecondsTooShort) { |
589 Initialize(true); | 599 Initialize(true); |
590 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 600 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
| 601 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 99\n\n"; |
591 | 602 |
592 RunTest(RESULT_NO_RESPONSE, | 603 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); |
593 net::OK, | 604 // Normally would be no delay on the first check with a new result. |
594 503, | 605 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 99, retry_after); |
595 0, | |
596 "HTTP/1.1 503 OK\nRetry-After: 99\n\n"); | |
597 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); | 606 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); |
598 } | 607 } |
599 | 608 |
600 // Check a Retry-After header that contains a date. | 609 // Check a Retry-After header that contains a date. |
601 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterDate) { | 610 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterDate) { |
602 Initialize(true); | 611 Initialize(true); |
603 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(50)); | 612 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(50)); |
604 | 613 |
605 // base has a function to get a time in the right format from a string, but | 614 // base has a function to get a time in the right format from a string, but |
606 // not the other way around. | 615 // not the other way around. |
607 base::Time start_time; | 616 base::Time start_time; |
608 ASSERT_TRUE( | 617 ASSERT_TRUE( |
609 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time)); | 618 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time)); |
610 service()->set_time(start_time); | 619 service()->set_time(start_time); |
611 | 620 |
612 RunTest(RESULT_NO_RESPONSE, | 621 RunTest(RESULT_NO_RESPONSE, |
613 net::OK, | 622 net::OK, |
614 503, | 623 503, |
615 0, | 624 0, |
616 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); | 625 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); |
617 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); | 626 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); |
618 } | 627 } |
619 | 628 |
620 // Check invalid Retry-After headers are ignored. | 629 // Check invalid Retry-After headers are ignored. |
621 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterInvalid) { | 630 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterInvalid) { |
622 Initialize(true); | 631 Initialize(true); |
623 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); | 632 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); |
| 633 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"; |
624 | 634 |
625 RunTest(RESULT_NO_RESPONSE, | 635 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); |
626 net::OK, | 636 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); |
627 503, | |
628 0, | |
629 "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"); | |
630 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); | 637 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); |
631 } | 638 } |
632 | 639 |
633 } // namespace captive_portal | 640 } // namespace captive_portal |
OLD | NEW |