network_portal_detector_impl_unittest.cc revision a3f7b4e666c476898878fa745f637129375cd889
1// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/compiler_specific.h" 6#include "base/logging.h" 7#include "base/memory/scoped_ptr.h" 8#include "base/run_loop.h" 9#include "chrome/browser/captive_portal/captive_portal_detector.h" 10#include "chrome/browser/captive_portal/testing_utils.h" 11#include "chrome/browser/chromeos/net/network_portal_detector_impl.h" 12#include "chrome/test/base/testing_profile.h" 13#include "chromeos/dbus/dbus_thread_manager.h" 14#include "chromeos/dbus/shill_device_client.h" 15#include "chromeos/dbus/shill_service_client.h" 16#include "chromeos/network/network_state.h" 17#include "chromeos/network/network_state_handler.h" 18#include "content/public/test/test_browser_thread_bundle.h" 19#include "dbus/object_path.h" 20#include "net/base/net_errors.h" 21#include "testing/gtest/include/gtest/gtest.h" 22#include "third_party/cros_system_api/dbus/service_constants.h" 23 24namespace chromeos { 25 26namespace { 27 28void ErrorCallbackFunction(const std::string& error_name, 29 const std::string& error_message) { 30 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 31} 32 33} // namespace 34 35// Service paths for stub network devices. 36const char* kStubEthernet = "stub_ethernet"; 37const char* kStubWireless1 = "stub_wifi1"; 38const char* kStubWireless2 = "stub_wifi2"; 39const char* kStubCellular = "stub_cellular"; 40 41class NetworkPortalDetectorImplTest 42 : public testing::Test, 43 public captive_portal::CaptivePortalDetectorTestBase { 44 protected: 45 virtual void SetUp() { 46 DBusThreadManager::InitializeWithStub(); 47 SetupNetworkHandler(); 48 49 profile_.reset(new TestingProfile()); 50 network_portal_detector_.reset( 51 new NetworkPortalDetectorImpl(profile_->GetRequestContext())); 52 network_portal_detector_->Init(); 53 network_portal_detector_->Enable(false); 54 55 set_detector(network_portal_detector_->captive_portal_detector_.get()); 56 57 // Prevents flakiness due to message loop delays. 58 set_time_ticks(base::TimeTicks::Now()); 59 } 60 61 virtual void TearDown() { 62 network_portal_detector_->Shutdown(); 63 profile_.reset(); 64 NetworkHandler::Shutdown(); 65 DBusThreadManager::Shutdown(); 66 } 67 68 void CheckPortalState(NetworkPortalDetector::CaptivePortalStatus status, 69 int response_code, 70 const std::string& network_service_path) { 71 const NetworkState* network = 72 NetworkHandler::Get()->network_state_handler()->GetNetworkState( 73 network_service_path); 74 NetworkPortalDetector::CaptivePortalState state = 75 network_portal_detector()->GetCaptivePortalState(network); 76 ASSERT_EQ(status, state.status); 77 ASSERT_EQ(response_code, state.response_code); 78 } 79 80 void CheckRequestTimeoutAndCompleteAttempt( 81 int expected_attempt_count, 82 int expected_request_timeout_sec, 83 int net_error, 84 int status_code) { 85 ASSERT_TRUE(is_state_checking_for_portal()); 86 ASSERT_EQ(expected_attempt_count, attempt_count()); 87 ASSERT_EQ(expected_request_timeout_sec, get_request_timeout_sec()); 88 CompleteURLFetch(net_error, status_code, NULL); 89 } 90 91 Profile* profile() { return profile_.get(); } 92 93 NetworkPortalDetectorImpl* network_portal_detector() { 94 return network_portal_detector_.get(); 95 } 96 97 NetworkPortalDetectorImpl::State state() { 98 return network_portal_detector()->state(); 99 } 100 101 bool start_detection_if_idle() { 102 return network_portal_detector()->StartDetectionIfIdle(); 103 } 104 105 void enable_lazy_detection() { 106 network_portal_detector()->EnableLazyDetection(); 107 } 108 109 void disable_lazy_detection() { 110 network_portal_detector()->DisableLazyDetection(); 111 } 112 113 void cancel_portal_detection() { 114 network_portal_detector()->CancelPortalDetection(); 115 } 116 117 bool detection_timeout_is_cancelled() { 118 return 119 network_portal_detector()->DetectionTimeoutIsCancelledForTesting(); 120 } 121 122 int get_request_timeout_sec() { 123 return network_portal_detector()->GetRequestTimeoutSec(); 124 } 125 126 bool is_state_idle() { 127 return (NetworkPortalDetectorImpl::STATE_IDLE == state()); 128 } 129 130 bool is_state_portal_detection_pending() { 131 return (NetworkPortalDetectorImpl::STATE_PORTAL_CHECK_PENDING == state()); 132 } 133 134 bool is_state_checking_for_portal() { 135 return (NetworkPortalDetectorImpl::STATE_CHECKING_FOR_PORTAL == state()); 136 } 137 138 void set_request_timeout(const base::TimeDelta& timeout) { 139 network_portal_detector()->set_request_timeout_for_testing(timeout); 140 } 141 142 const base::TimeDelta& next_attempt_delay() { 143 return network_portal_detector()->next_attempt_delay_for_testing(); 144 } 145 146 int attempt_count() { 147 return network_portal_detector()->attempt_count_for_testing(); 148 } 149 150 void set_attempt_count(int ac) { 151 return network_portal_detector()->set_attempt_count_for_testing(ac); 152 } 153 154 void set_min_time_between_attempts(const base::TimeDelta& delta) { 155 network_portal_detector()->set_min_time_between_attempts_for_testing(delta); 156 } 157 158 void set_lazy_check_interval(const base::TimeDelta& delta) { 159 network_portal_detector()->set_lazy_check_interval_for_testing(delta); 160 } 161 162 void set_time_ticks(const base::TimeTicks& time_ticks) { 163 network_portal_detector()->set_time_ticks_for_testing(time_ticks); 164 } 165 166 void SetBehindPortal(const std::string& service_path) { 167 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 168 dbus::ObjectPath(service_path), 169 flimflam::kStateProperty, base::StringValue(flimflam::kStatePortal), 170 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 171 base::RunLoop().RunUntilIdle(); 172 } 173 174 void SetNetworkDeviceEnabled(const std::string& type, bool enabled) { 175 NetworkHandler::Get()->network_state_handler()->SetTechnologyEnabled( 176 type, enabled, network_handler::ErrorCallback()); 177 base::RunLoop().RunUntilIdle(); 178 } 179 180 void SetConnected(const std::string& service_path) { 181 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 182 dbus::ObjectPath(service_path), 183 flimflam::kStateProperty, base::StringValue(flimflam::kStateOnline), 184 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 185 base::RunLoop().RunUntilIdle(); 186 } 187 188 private: 189 void SetupDefaultShillState() { 190 base::RunLoop().RunUntilIdle(); 191 ShillServiceClient::TestInterface* service_test = 192 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 193 service_test->ClearServices(); 194 const bool add_to_visible = true; 195 const bool add_to_watchlist = true; 196 service_test->AddService(kStubEthernet, 197 kStubEthernet, 198 flimflam::kTypeEthernet, flimflam::kStateIdle, 199 add_to_visible, add_to_watchlist); 200 service_test->AddService(kStubWireless1, 201 kStubWireless1, 202 flimflam::kTypeWifi, flimflam::kStateIdle, 203 add_to_visible, add_to_watchlist); 204 service_test->AddService(kStubWireless2, 205 kStubWireless2, 206 flimflam::kTypeWifi, flimflam::kStateIdle, 207 add_to_visible, add_to_watchlist); 208 service_test->AddService(kStubCellular, 209 kStubCellular, 210 flimflam::kTypeCellular, flimflam::kStateIdle, 211 add_to_visible, add_to_watchlist); 212 } 213 214 void SetupNetworkHandler() { 215 SetupDefaultShillState(); 216 NetworkHandler::Initialize(); 217 } 218 219 content::TestBrowserThreadBundle thread_bundle_; 220 scoped_ptr<TestingProfile> profile_; 221 scoped_ptr<NetworkPortalDetectorImpl> network_portal_detector_; 222}; 223 224TEST_F(NetworkPortalDetectorImplTest, NoPortal) { 225 ASSERT_TRUE(is_state_idle()); 226 227 SetConnected(kStubWireless1); 228 229 ASSERT_TRUE(is_state_checking_for_portal()); 230 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 231 kStubWireless1); 232 233 CompleteURLFetch(net::OK, 204, NULL); 234 235 ASSERT_TRUE(is_state_idle()); 236 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 237 kStubWireless1); 238} 239 240TEST_F(NetworkPortalDetectorImplTest, Portal) { 241 ASSERT_TRUE(is_state_idle()); 242 243 // Check HTTP 200 response code. 244 SetConnected(kStubWireless1); 245 ASSERT_TRUE(is_state_checking_for_portal()); 246 247 CompleteURLFetch(net::OK, 200, NULL); 248 249 ASSERT_TRUE(is_state_idle()); 250 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 251 kStubWireless1); 252 253 // Check HTTP 301 response code. 254 SetConnected(kStubWireless2); 255 ASSERT_TRUE(is_state_checking_for_portal()); 256 257 CompleteURLFetch(net::OK, 301, NULL); 258 259 ASSERT_TRUE(is_state_idle()); 260 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 301, 261 kStubWireless2); 262 263 // Check HTTP 302 response code. 264 SetConnected(kStubEthernet); 265 ASSERT_TRUE(is_state_checking_for_portal()); 266 267 CompleteURLFetch(net::OK, 302, NULL); 268 269 ASSERT_TRUE(is_state_idle()); 270 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 302, 271 kStubEthernet); 272} 273 274TEST_F(NetworkPortalDetectorImplTest, TwoNetworks) { 275 ASSERT_TRUE(is_state_idle()); 276 277 SetConnected(kStubWireless1); 278 ASSERT_TRUE(is_state_checking_for_portal()); 279 280 // wifi is in portal state. 281 CompleteURLFetch(net::OK, 200, NULL); 282 ASSERT_TRUE(is_state_idle()); 283 284 SetConnected(kStubEthernet); 285 ASSERT_TRUE(is_state_checking_for_portal()); 286 287 // ethernet is in online state. 288 CompleteURLFetch(net::OK, 204, NULL); 289 ASSERT_TRUE(is_state_idle()); 290 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 291 kStubEthernet); 292 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 293 kStubWireless1); 294} 295 296TEST_F(NetworkPortalDetectorImplTest, NetworkChanged) { 297 ASSERT_TRUE(is_state_idle()); 298 299 SetConnected(kStubWireless1); 300 301 // WiFi is in portal state. 302 fetcher()->set_response_code(200); 303 ASSERT_TRUE(is_state_checking_for_portal()); 304 305 // Active network is changed during portal detection for wifi. 306 SetConnected(kStubEthernet); 307 308 // Portal detection for wifi is cancelled, portal detection for 309 // ethernet is initiated. 310 ASSERT_TRUE(is_state_checking_for_portal()); 311 312 // ethernet is in online state. 313 CompleteURLFetch(net::OK, 204, NULL); 314 ASSERT_TRUE(is_state_idle()); 315 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 316 kStubEthernet); 317 318 // As active network was changed during portal detection for wifi 319 // network, it's state must be unknown. 320 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 321 kStubWireless1); 322} 323 324TEST_F(NetworkPortalDetectorImplTest, NetworkStateNotChanged) { 325 ASSERT_TRUE(is_state_idle()); 326 327 SetConnected(kStubWireless1); 328 ASSERT_TRUE(is_state_checking_for_portal()); 329 330 CompleteURLFetch(net::OK, 204, NULL); 331 332 ASSERT_TRUE(is_state_idle()); 333 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 334 kStubWireless1); 335 336 SetConnected(kStubWireless1); 337 ASSERT_TRUE(is_state_idle()); 338} 339 340TEST_F(NetworkPortalDetectorImplTest, NetworkStateChanged) { 341 // Test for Portal -> Online -> Portal network state transitions. 342 ASSERT_TRUE(is_state_idle()); 343 344 SetBehindPortal(kStubWireless1); 345 ASSERT_TRUE(is_state_checking_for_portal()); 346 347 CompleteURLFetch(net::OK, 200, NULL); 348 349 ASSERT_TRUE(is_state_idle()); 350 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 351 kStubWireless1); 352 353 SetConnected(kStubWireless1); 354 ASSERT_TRUE(is_state_checking_for_portal()); 355 356 CompleteURLFetch(net::OK, 204, NULL); 357 358 ASSERT_TRUE(is_state_idle()); 359 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 360 kStubWireless1); 361 362 SetBehindPortal(kStubWireless1); 363 ASSERT_TRUE(is_state_checking_for_portal()); 364 365 CompleteURLFetch(net::OK, 200, NULL); 366 367 ASSERT_TRUE(is_state_idle()); 368 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 369 kStubWireless1); 370} 371 372TEST_F(NetworkPortalDetectorImplTest, PortalDetectionTimeout) { 373 ASSERT_TRUE(is_state_idle()); 374 375 // For instantaneous timeout. 376 set_request_timeout(base::TimeDelta::FromSeconds(0)); 377 378 ASSERT_TRUE(is_state_idle()); 379 ASSERT_EQ(0, attempt_count()); 380 381 SetConnected(kStubWireless1); 382 base::RunLoop().RunUntilIdle(); 383 384 // First portal detection timeouts, next portal detection is 385 // scheduled. 386 ASSERT_TRUE(is_state_portal_detection_pending()); 387 ASSERT_EQ(1, attempt_count()); 388 ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay()); 389} 390 391TEST_F(NetworkPortalDetectorImplTest, PortalDetectionRetryAfter) { 392 ASSERT_TRUE(is_state_idle()); 393 394 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; 395 396 ASSERT_TRUE(is_state_idle()); 397 ASSERT_EQ(0, attempt_count()); 398 399 SetConnected(kStubWireless1); 400 CompleteURLFetch(net::OK, 503, retry_after); 401 402 // First portal detection completed, next portal detection is 403 // scheduled after 101 seconds. 404 ASSERT_TRUE(is_state_portal_detection_pending()); 405 ASSERT_EQ(1, attempt_count()); 406 ASSERT_EQ(base::TimeDelta::FromSeconds(101), next_attempt_delay()); 407} 408 409TEST_F(NetworkPortalDetectorImplTest, PortalDetectorRetryAfterIsSmall) { 410 ASSERT_TRUE(is_state_idle()); 411 412 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 1\n\n"; 413 414 ASSERT_TRUE(is_state_idle()); 415 ASSERT_EQ(0, attempt_count()); 416 417 SetConnected(kStubWireless1); 418 CompleteURLFetch(net::OK, 503, retry_after); 419 420 // First portal detection completed, next portal detection is 421 // scheduled after 3 seconds (due to minimum time between detection 422 // attemps). 423 ASSERT_TRUE(is_state_portal_detection_pending()); 424 ASSERT_EQ(1, attempt_count()); 425 ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay()); 426} 427 428TEST_F(NetworkPortalDetectorImplTest, FirstAttemptFailed) { 429 ASSERT_TRUE(is_state_idle()); 430 431 set_min_time_between_attempts(base::TimeDelta()); 432 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n"; 433 434 ASSERT_TRUE(is_state_idle()); 435 ASSERT_EQ(0, attempt_count()); 436 437 SetConnected(kStubWireless1); 438 439 CompleteURLFetch(net::OK, 503, retry_after); 440 ASSERT_TRUE(is_state_portal_detection_pending()); 441 ASSERT_EQ(1, attempt_count()); 442 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); 443 444 // To run CaptivePortalDetector::DetectCaptivePortal(). 445 base::RunLoop().RunUntilIdle(); 446 447 CompleteURLFetch(net::OK, 204, NULL); 448 ASSERT_TRUE(is_state_idle()); 449 ASSERT_EQ(2, attempt_count()); 450 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 451 kStubWireless1); 452} 453 454TEST_F(NetworkPortalDetectorImplTest, AllAttemptsFailed) { 455 ASSERT_TRUE(is_state_idle()); 456 457 set_min_time_between_attempts(base::TimeDelta()); 458 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n"; 459 460 ASSERT_TRUE(is_state_idle()); 461 ASSERT_EQ(0, attempt_count()); 462 463 SetConnected(kStubWireless1); 464 465 CompleteURLFetch(net::OK, 503, retry_after); 466 ASSERT_TRUE(is_state_portal_detection_pending()); 467 ASSERT_EQ(1, attempt_count()); 468 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); 469 470 // To run CaptivePortalDetector::DetectCaptivePortal(). 471 base::RunLoop().RunUntilIdle(); 472 473 CompleteURLFetch(net::OK, 503, retry_after); 474 ASSERT_TRUE(is_state_portal_detection_pending()); 475 ASSERT_EQ(2, attempt_count()); 476 ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); 477 478 // To run CaptivePortalDetector::DetectCaptivePortal(). 479 base::RunLoop().RunUntilIdle(); 480 481 CompleteURLFetch(net::OK, 503, retry_after); 482 ASSERT_TRUE(is_state_idle()); 483 ASSERT_EQ(3, attempt_count()); 484 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE, 503, 485 kStubWireless1); 486} 487 488TEST_F(NetworkPortalDetectorImplTest, ProxyAuthRequired) { 489 ASSERT_TRUE(is_state_idle()); 490 set_min_time_between_attempts(base::TimeDelta()); 491 492 SetConnected(kStubWireless1); 493 CompleteURLFetch(net::OK, 407, NULL); 494 ASSERT_EQ(1, attempt_count()); 495 ASSERT_TRUE(is_state_portal_detection_pending()); 496 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 497 kStubWireless1); 498 499 // To run CaptivePortalDetector::DetectCaptivePortal(). 500 base::RunLoop().RunUntilIdle(); 501 502 CompleteURLFetch(net::OK, 407, NULL); 503 ASSERT_EQ(2, attempt_count()); 504 ASSERT_TRUE(is_state_portal_detection_pending()); 505 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 506 kStubWireless1); 507 508 // To run CaptivePortalDetector::DetectCaptivePortal(). 509 base::RunLoop().RunUntilIdle(); 510 511 CompleteURLFetch(net::OK, 407, NULL); 512 ASSERT_EQ(3, attempt_count()); 513 ASSERT_TRUE(is_state_idle()); 514 CheckPortalState( 515 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED, 407, 516 kStubWireless1); 517} 518 519TEST_F(NetworkPortalDetectorImplTest, NoResponseButBehindPortal) { 520 ASSERT_TRUE(is_state_idle()); 521 set_min_time_between_attempts(base::TimeDelta()); 522 523 SetBehindPortal(kStubWireless1); 524 ASSERT_TRUE(is_state_checking_for_portal()); 525 526 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, 527 net::URLFetcher::RESPONSE_CODE_INVALID, 528 NULL); 529 ASSERT_EQ(1, attempt_count()); 530 ASSERT_TRUE(is_state_portal_detection_pending()); 531 532 // To run CaptivePortalDetector::DetectCaptivePortal(). 533 base::RunLoop().RunUntilIdle(); 534 535 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, 536 net::URLFetcher::RESPONSE_CODE_INVALID, 537 NULL); 538 ASSERT_EQ(2, attempt_count()); 539 ASSERT_TRUE(is_state_portal_detection_pending()); 540 541 // To run CaptivePortalDetector::DetectCaptivePortal(). 542 base::RunLoop().RunUntilIdle(); 543 544 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, 545 net::URLFetcher::RESPONSE_CODE_INVALID, 546 NULL); 547 ASSERT_EQ(3, attempt_count()); 548 ASSERT_TRUE(is_state_idle()); 549 550 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 551 net::URLFetcher::RESPONSE_CODE_INVALID, 552 kStubWireless1); 553} 554 555TEST_F(NetworkPortalDetectorImplTest, DisableLazyDetectionWhilePendingRequest) { 556 ASSERT_TRUE(is_state_idle()); 557 set_attempt_count(3); 558 enable_lazy_detection(); 559 ASSERT_TRUE(is_state_portal_detection_pending()); 560 disable_lazy_detection(); 561 562 // To run CaptivePortalDetector::DetectCaptivePortal(). 563 base::MessageLoop::current()->RunUntilIdle(); 564} 565 566TEST_F(NetworkPortalDetectorImplTest, LazyDetectionForOnlineNetwork) { 567 ASSERT_TRUE(is_state_idle()); 568 set_min_time_between_attempts(base::TimeDelta()); 569 set_lazy_check_interval(base::TimeDelta()); 570 571 SetConnected(kStubWireless1); 572 enable_lazy_detection(); 573 CompleteURLFetch(net::OK, 204, NULL); 574 575 ASSERT_EQ(1, attempt_count()); 576 ASSERT_TRUE(is_state_portal_detection_pending()); 577 CheckPortalState( 578 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 579 kStubWireless1); 580 581 // To run CaptivePortalDetector::DetectCaptivePortal(). 582 base::RunLoop().RunUntilIdle(); 583 584 CompleteURLFetch(net::OK, 204, NULL); 585 586 ASSERT_EQ(2, attempt_count()); 587 ASSERT_TRUE(is_state_portal_detection_pending()); 588 CheckPortalState( 589 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 590 kStubWireless1); 591 592 // To run CaptivePortalDetector::DetectCaptivePortal(). 593 base::RunLoop().RunUntilIdle(); 594 595 disable_lazy_detection(); 596 597 // One more detection result, because DizableLazyDetection() doesn't 598 // cancel last detection request. 599 CompleteURLFetch(net::OK, 204, NULL); 600 ASSERT_EQ(3, attempt_count()); 601 ASSERT_TRUE(is_state_idle()); 602 CheckPortalState( 603 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 604 kStubWireless1); 605} 606 607TEST_F(NetworkPortalDetectorImplTest, LazyDetectionForPortalNetwork) { 608 ASSERT_TRUE(is_state_idle()); 609 set_min_time_between_attempts(base::TimeDelta()); 610 set_lazy_check_interval(base::TimeDelta()); 611 612 SetConnected(kStubWireless1); 613 enable_lazy_detection(); 614 615 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, 616 net::URLFetcher::RESPONSE_CODE_INVALID, 617 NULL); 618 ASSERT_EQ(1, attempt_count()); 619 ASSERT_TRUE(is_state_portal_detection_pending()); 620 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 621 kStubWireless1); 622 623 // To run CaptivePortalDetector::DetectCaptivePortal(). 624 base::RunLoop().RunUntilIdle(); 625 626 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, 627 net::URLFetcher::RESPONSE_CODE_INVALID, 628 NULL); 629 ASSERT_EQ(2, attempt_count()); 630 ASSERT_TRUE(is_state_portal_detection_pending()); 631 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 632 kStubWireless1); 633 634 // To run CaptivePortalDetector::DetectCaptivePortal(). 635 base::RunLoop().RunUntilIdle(); 636 637 CompleteURLFetch(net::OK, 200, NULL); 638 ASSERT_EQ(3, attempt_count()); 639 ASSERT_TRUE(is_state_portal_detection_pending()); 640 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 641 kStubWireless1); 642 643 // To run CaptivePortalDetector::DetectCaptivePortal(). 644 base::RunLoop().RunUntilIdle(); 645 646 disable_lazy_detection(); 647 648 // One more detection result, because DizableLazyDetection() doesn't 649 // cancel last detection request. 650 CompleteURLFetch(net::OK, 200, NULL); 651 ASSERT_EQ(3, attempt_count()); 652 ASSERT_TRUE(is_state_idle()); 653 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 654 kStubWireless1); 655} 656 657TEST_F(NetworkPortalDetectorImplTest, DetectionTimeoutIsCancelled) { 658 ASSERT_TRUE(is_state_idle()); 659 set_min_time_between_attempts(base::TimeDelta()); 660 661 SetConnected(kStubWireless1); 662 ASSERT_TRUE(is_state_checking_for_portal()); 663 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 664 kStubWireless1); 665 666 cancel_portal_detection(); 667 668 ASSERT_TRUE(is_state_idle()); 669 ASSERT_TRUE(detection_timeout_is_cancelled()); 670 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, 671 kStubWireless1); 672} 673 674TEST_F(NetworkPortalDetectorImplTest, TestDetectionRestart) { 675 ASSERT_TRUE(is_state_idle()); 676 set_min_time_between_attempts(base::TimeDelta()); 677 678 // First portal detection attempts determines ONLINE state. 679 SetConnected(kStubWireless1); 680 ASSERT_TRUE(is_state_checking_for_portal()); 681 ASSERT_FALSE(start_detection_if_idle()); 682 683 CompleteURLFetch(net::OK, 204, NULL); 684 685 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, 686 kStubWireless1); 687 ASSERT_TRUE(is_state_idle()); 688 689 // First portal detection attempts determines PORTAL state. 690 ASSERT_TRUE(start_detection_if_idle()); 691 ASSERT_TRUE(is_state_portal_detection_pending()); 692 ASSERT_FALSE(start_detection_if_idle()); 693 694 base::RunLoop().RunUntilIdle(); 695 ASSERT_TRUE(is_state_checking_for_portal()); 696 CompleteURLFetch(net::OK, 200, NULL); 697 698 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, 699 kStubWireless1); 700 ASSERT_TRUE(is_state_idle()); 701} 702 703TEST_F(NetworkPortalDetectorImplTest, RequestTimeouts) { 704 ASSERT_TRUE(is_state_idle()); 705 set_min_time_between_attempts(base::TimeDelta()); 706 set_lazy_check_interval(base::TimeDelta()); 707 708 SetNetworkDeviceEnabled(flimflam::kTypeWifi, false); 709 SetConnected(kStubCellular); 710 711 // First portal detection attempt for cellular1 uses 5sec timeout. 712 CheckRequestTimeoutAndCompleteAttempt(1, 5, net::ERR_CONNECTION_CLOSED, 713 net::URLFetcher::RESPONSE_CODE_INVALID); 714 715 // Second portal detection attempt for cellular1 uses 10sec timeout. 716 ASSERT_TRUE(is_state_portal_detection_pending()); 717 base::RunLoop().RunUntilIdle(); 718 CheckRequestTimeoutAndCompleteAttempt(2, 10, net::ERR_CONNECTION_CLOSED, 719 net::URLFetcher::RESPONSE_CODE_INVALID); 720 721 // Third portal detection attempt for cellular1 uses 15sec timeout. 722 ASSERT_TRUE(is_state_portal_detection_pending()); 723 base::RunLoop().RunUntilIdle(); 724 CheckRequestTimeoutAndCompleteAttempt(3, 15, net::ERR_CONNECTION_CLOSED, 725 net::URLFetcher::RESPONSE_CODE_INVALID); 726 727 ASSERT_TRUE(is_state_idle()); 728 729 // Check that in lazy detection for cellular1 15sec timeout is used. 730 enable_lazy_detection(); 731 ASSERT_TRUE(is_state_portal_detection_pending()); 732 base::RunLoop().RunUntilIdle(); 733 disable_lazy_detection(); 734 CheckRequestTimeoutAndCompleteAttempt(3, 15, net::ERR_CONNECTION_CLOSED, 735 net::URLFetcher::RESPONSE_CODE_INVALID); 736 ASSERT_TRUE(is_state_idle()); 737 738 SetNetworkDeviceEnabled(flimflam::kTypeWifi, true); 739 SetConnected(kStubWireless1); 740 741 // First portal detection attempt for wifi1 uses 5sec timeout. 742 CheckRequestTimeoutAndCompleteAttempt(1, 5, net::ERR_CONNECTION_CLOSED, 743 net::URLFetcher::RESPONSE_CODE_INVALID); 744 745 // Second portal detection attempt for wifi1 also uses 5sec timeout. 746 ASSERT_TRUE(is_state_portal_detection_pending()); 747 base::RunLoop().RunUntilIdle(); 748 CheckRequestTimeoutAndCompleteAttempt(2, 10, net::OK, 204); 749 ASSERT_TRUE(is_state_idle()); 750 751 // Check that in lazy detection for wifi1 5sec timeout is used. 752 enable_lazy_detection(); 753 ASSERT_TRUE(is_state_portal_detection_pending()); 754 base::RunLoop().RunUntilIdle(); 755 disable_lazy_detection(); 756 CheckRequestTimeoutAndCompleteAttempt(3, 15, net::OK, 204); 757 ASSERT_TRUE(is_state_idle()); 758} 759 760TEST_F(NetworkPortalDetectorImplTest, StartDetectionIfIdle) { 761 ASSERT_TRUE(is_state_idle()); 762 set_min_time_between_attempts(base::TimeDelta()); 763 SetConnected(kStubWireless1); 764 765 // First portal detection attempt for wifi1 uses 5sec timeout. 766 CheckRequestTimeoutAndCompleteAttempt(1, 5, net::ERR_CONNECTION_CLOSED, 767 net::URLFetcher::RESPONSE_CODE_INVALID); 768 ASSERT_TRUE(is_state_portal_detection_pending()); 769 base::RunLoop().RunUntilIdle(); 770 771 // Second portal detection attempt for wifi1 uses 10sec timeout. 772 CheckRequestTimeoutAndCompleteAttempt(2, 10, net::ERR_CONNECTION_CLOSED, 773 net::URLFetcher::RESPONSE_CODE_INVALID); 774 ASSERT_TRUE(is_state_portal_detection_pending()); 775 base::RunLoop().RunUntilIdle(); 776 777 // Second portal detection attempt for wifi1 uses 15sec timeout. 778 CheckRequestTimeoutAndCompleteAttempt(3, 15, net::ERR_CONNECTION_CLOSED, 779 net::URLFetcher::RESPONSE_CODE_INVALID); 780 ASSERT_TRUE(is_state_idle()); 781 start_detection_if_idle(); 782 783 ASSERT_TRUE(is_state_portal_detection_pending()); 784 785 // First portal detection attempt for wifi1 uses 5sec timeout. 786 base::RunLoop().RunUntilIdle(); 787 CheckRequestTimeoutAndCompleteAttempt(1, 5, net::OK, 204); 788 ASSERT_TRUE(is_state_idle()); 789} 790 791} // namespace chromeos 792