| 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 "chromeos/network/network_state_handler.h" | 5 #include "chromeos/network/network_state_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> |
| 9 #include <map> | 10 #include <map> |
| 10 #include <memory> | 11 #include <memory> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <string> | 13 #include <string> |
| 13 | 14 |
| 14 #include "base/bind.h" | 15 #include "base/bind.h" |
| 15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 18 #include "base/test/scoped_task_environment.h" | 19 #include "base/test/scoped_task_environment.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
| 21 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 22 #include "chromeos/dbus/shill_device_client.h" | 23 #include "chromeos/dbus/shill_device_client.h" |
| 23 #include "chromeos/dbus/shill_ipconfig_client.h" | 24 #include "chromeos/dbus/shill_ipconfig_client.h" |
| 24 #include "chromeos/dbus/shill_manager_client.h" | 25 #include "chromeos/dbus/shill_manager_client.h" |
| 25 #include "chromeos/dbus/shill_profile_client.h" | 26 #include "chromeos/dbus/shill_profile_client.h" |
| 26 #include "chromeos/dbus/shill_service_client.h" | 27 #include "chromeos/dbus/shill_service_client.h" |
| 27 #include "chromeos/network/device_state.h" | 28 #include "chromeos/network/device_state.h" |
| 28 #include "chromeos/network/network_state.h" | 29 #include "chromeos/network/network_state.h" |
| 29 #include "chromeos/network/network_state_handler.h" | 30 #include "chromeos/network/network_state_handler.h" |
| 30 #include "chromeos/network/network_state_handler_observer.h" | 31 #include "chromeos/network/network_state_handler_observer.h" |
| 31 #include "chromeos/network/tether_constants.h" | 32 #include "chromeos/network/tether_constants.h" |
| 32 #include "dbus/object_path.h" | 33 #include "dbus/object_path.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "third_party/cros_system_api/dbus/service_constants.h" | 35 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 35 | 36 |
| 37 namespace chromeos { |
| 38 |
| 36 namespace { | 39 namespace { |
| 37 | 40 |
| 38 void ErrorCallbackFunction(const std::string& error_name, | 41 void ErrorCallbackFunction(const std::string& error_name, |
| 39 const std::string& error_message) { | 42 const std::string& error_message) { |
| 40 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; | 43 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
| 41 } | 44 } |
| 42 | 45 |
| 43 const char kShillManagerClientStubWifiDevice[] = "/device/stub_wifi_device1"; | 46 const char kShillManagerClientStubWifiDevice[] = "/device/stub_wifi_device1"; |
| 44 const char kShillManagerClientStubCellularDevice[] = | 47 const char kShillManagerClientStubCellularDevice[] = |
| 45 "/device/stub_cellular_device1"; | 48 "/device/stub_cellular_device1"; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 std::string default_network_; | 195 std::string default_network_; |
| 193 std::string default_network_connection_state_; | 196 std::string default_network_connection_state_; |
| 194 std::map<std::string, int> property_updates_; | 197 std::map<std::string, int> property_updates_; |
| 195 std::map<std::string, int> device_property_updates_; | 198 std::map<std::string, int> device_property_updates_; |
| 196 std::map<std::string, int> connection_state_changes_; | 199 std::map<std::string, int> connection_state_changes_; |
| 197 std::map<std::string, std::string> network_connection_state_; | 200 std::map<std::string, std::string> network_connection_state_; |
| 198 | 201 |
| 199 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 202 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 200 }; | 203 }; |
| 201 | 204 |
| 205 class TestTetherSortDelegate : public NetworkStateHandler::TetherSortDelegate { |
| 206 public: |
| 207 TestTetherSortDelegate() {} |
| 208 ~TestTetherSortDelegate() {} |
| 209 |
| 210 // NetworkStateHandler::TetherSortDelegate: |
| 211 void SortTetherNetworkList( |
| 212 NetworkStateHandler::ManagedStateList* tether_networks) const override { |
| 213 std::sort(tether_networks->begin(), tether_networks->end(), |
| 214 [](const std::unique_ptr<ManagedState>& first, |
| 215 const std::unique_ptr<ManagedState>& second) { |
| 216 const NetworkState* first_network = |
| 217 static_cast<const NetworkState*>(first.get()); |
| 218 const NetworkState* second_network = |
| 219 static_cast<const NetworkState*>(second.get()); |
| 220 |
| 221 // Sort by reverse-alphabetical order of GUIDs. |
| 222 return first_network->guid() >= second_network->guid(); |
| 223 }); |
| 224 } |
| 225 |
| 226 private: |
| 227 DISALLOW_COPY_AND_ASSIGN(TestTetherSortDelegate); |
| 228 }; |
| 229 |
| 202 } // namespace | 230 } // namespace |
| 203 | 231 |
| 204 namespace chromeos { | |
| 205 | |
| 206 class NetworkStateHandlerTest : public testing::Test { | 232 class NetworkStateHandlerTest : public testing::Test { |
| 207 public: | 233 public: |
| 208 NetworkStateHandlerTest() | 234 NetworkStateHandlerTest() |
| 209 : scoped_task_environment_( | 235 : scoped_task_environment_( |
| 210 base::test::ScopedTaskEnvironment::MainThreadType::UI), | 236 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 211 device_test_(nullptr), | 237 device_test_(nullptr), |
| 212 manager_test_(nullptr), | 238 manager_test_(nullptr), |
| 213 profile_test_(nullptr), | 239 profile_test_(nullptr), |
| 214 service_test_(nullptr) {} | 240 service_test_(nullptr) {} |
| 215 ~NetworkStateHandlerTest() override {} | 241 ~NetworkStateHandlerTest() override {} |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, | 482 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 457 kTetherSignalStrength2, kTetherHasConnectedToHost2); | 483 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 458 | 484 |
| 459 GetTetherNetworkList(0 /* no limit */, &tether_networks); | 485 GetTetherNetworkList(0 /* no limit */, &tether_networks); |
| 460 EXPECT_EQ(2u, tether_networks.size()); | 486 EXPECT_EQ(2u, tether_networks.size()); |
| 461 | 487 |
| 462 GetTetherNetworkList(1 /* no limit */, &tether_networks); | 488 GetTetherNetworkList(1 /* no limit */, &tether_networks); |
| 463 EXPECT_EQ(1u, tether_networks.size()); | 489 EXPECT_EQ(1u, tether_networks.size()); |
| 464 } | 490 } |
| 465 | 491 |
| 492 TEST_F(NetworkStateHandlerTest, SortTetherNetworkList) { |
| 493 network_state_handler_->SetTetherTechnologyState( |
| 494 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 495 |
| 496 TestTetherSortDelegate sort_delegate; |
| 497 network_state_handler_->set_tether_sort_delegate(&sort_delegate); |
| 498 |
| 499 network_state_handler_->AddTetherNetworkState( |
| 500 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 501 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 502 network_state_handler_->AddTetherNetworkState( |
| 503 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 504 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 505 |
| 506 // Note: GetVisibleNetworkListByType() sorts before outputting networks. |
| 507 NetworkStateHandler::NetworkStateList tether_networks; |
| 508 network_state_handler_->GetVisibleNetworkListByType( |
| 509 NetworkTypePattern::Tether(), &tether_networks); |
| 510 |
| 511 // The list should have been reversed due to reverse-alphabetical sorting. |
| 512 EXPECT_EQ(2u, tether_networks.size()); |
| 513 EXPECT_EQ(kTetherGuid2, tether_networks[0]->guid()); |
| 514 EXPECT_EQ(kTetherGuid1, tether_networks[1]->guid()); |
| 515 } |
| 516 |
| 517 TEST_F(NetworkStateHandlerTest, SortTetherNetworkList_NoSortingDelegate) { |
| 518 network_state_handler_->SetTetherTechnologyState( |
| 519 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 520 |
| 521 // Do not set a TetherSortDelegate. |
| 522 |
| 523 network_state_handler_->AddTetherNetworkState( |
| 524 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 525 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 526 network_state_handler_->AddTetherNetworkState( |
| 527 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 528 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 529 |
| 530 // Note: GetVisibleNetworkListByType() sorts before outputting networks. |
| 531 NetworkStateHandler::NetworkStateList tether_networks; |
| 532 network_state_handler_->GetVisibleNetworkListByType( |
| 533 NetworkTypePattern::Tether(), &tether_networks); |
| 534 |
| 535 // The list should be in the original order. |
| 536 EXPECT_EQ(2u, tether_networks.size()); |
| 537 EXPECT_EQ(kTetherGuid1, tether_networks[0]->guid()); |
| 538 EXPECT_EQ(kTetherGuid2, tether_networks[1]->guid()); |
| 539 } |
| 540 |
| 541 TEST_F(NetworkStateHandlerTest, |
| 542 GetNetworks_TetherIncluded_ActiveBeforeNonActive) { |
| 543 network_state_handler_->SetTetherTechnologyState( |
| 544 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 545 |
| 546 TestTetherSortDelegate sort_delegate; |
| 547 network_state_handler_->set_tether_sort_delegate(&sort_delegate); |
| 548 |
| 549 // To start the test, |eth1| and |wifi1| are connected, while |wifi2| and |
| 550 // |cellular| are not. |
| 551 const std::string eth1 = kShillManagerClientStubDefaultService; |
| 552 const std::string wifi1 = kShillManagerClientStubDefaultWifi; |
| 553 const std::string wifi2 = kShillManagerClientStubWifi2; |
| 554 const std::string cellular = kShillManagerClientStubCellular; |
| 555 |
| 556 // Disconnect |wifi1|, which will serve as the underlying connection |
| 557 // for the Tether network under test. |
| 558 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, |
| 559 base::Value(shill::kStateIdle)); |
| 560 |
| 561 // Connect |cellular| for this test. |
| 562 service_test_->SetServiceProperty(cellular, shill::kStateProperty, |
| 563 base::Value(shill::kStateOnline)); |
| 564 base::RunLoop().RunUntilIdle(); |
| 565 |
| 566 // Add two Tether networks. Neither is connected yet. |
| 567 network_state_handler_->AddTetherNetworkState( |
| 568 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 569 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 570 network_state_handler_->AddTetherNetworkState( |
| 571 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 572 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 573 |
| 574 // Connect to the first Tether network (and the underlying Wi-Fi hotspot |
| 575 // network, |wifi1|). |
| 576 network_state_handler_->SetTetherNetworkStateConnecting(kTetherGuid1); |
| 577 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( |
| 578 kTetherGuid1, "wifi1_guid"); |
| 579 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, |
| 580 base::Value(shill::kStateOnline)); |
| 581 base::RunLoop().RunUntilIdle(); |
| 582 network_state_handler_->SetTetherNetworkStateConnected(kTetherGuid1); |
| 583 |
| 584 // At this point, |eth1|, |cellular|, and |kTetherGuid1| are connected. |
| 585 // |wifi1| is also connected, but it is not considered visible since it is the |
| 586 // underlying network for the Tether connection. |
| 587 NetworkStateHandler::NetworkStateList list; |
| 588 |
| 589 // Get Tether networks. Even though the networks should be sorted according to |
| 590 // reverse-alphabetical order, |kTetherGuid1| should be listed first since it |
| 591 // is active. |
| 592 network_state_handler_->GetVisibleNetworkListByType( |
| 593 NetworkTypePattern::Tether(), &list); |
| 594 ASSERT_EQ(2u, list.size()); |
| 595 EXPECT_EQ(kTetherGuid1, list[0]->guid()); |
| 596 EXPECT_EQ(kTetherGuid2, list[1]->guid()); |
| 597 |
| 598 // Get Mobile networks. The connected Tether network should be first, followed |
| 599 // by the connected Cellular network, followed by the non-connected Tether |
| 600 // network. |
| 601 network_state_handler_->GetVisibleNetworkListByType( |
| 602 NetworkTypePattern::Mobile(), &list); |
| 603 ASSERT_EQ(3u, list.size()); |
| 604 EXPECT_EQ(kTetherGuid1, list[0]->guid()); |
| 605 EXPECT_EQ(cellular, list[1]->path()); |
| 606 EXPECT_EQ(kTetherGuid2, list[2]->guid()); |
| 607 |
| 608 // Get all networks. The connected Ethernet network should be first, followed |
| 609 // by the connected Tether network, followed by the connected Cellular |
| 610 // network, followed by the non-connected Tether network, followed by the |
| 611 // non-connected Wi-Fi network. |
| 612 network_state_handler_->GetVisibleNetworkListByType( |
| 613 NetworkTypePattern::Default(), &list); |
| 614 EXPECT_EQ(5u, list.size()); |
| 615 EXPECT_EQ(eth1, list[0]->path()); |
| 616 EXPECT_EQ(kTetherGuid1, list[1]->guid()); |
| 617 EXPECT_EQ(cellular, list[2]->path()); |
| 618 EXPECT_EQ(kTetherGuid2, list[3]->guid()); |
| 619 EXPECT_EQ(wifi2, list[4]->path()); |
| 620 } |
| 621 |
| 466 TEST_F(NetworkStateHandlerTest, NetworkListChanged) { | 622 TEST_F(NetworkStateHandlerTest, NetworkListChanged) { |
| 467 size_t stub_network_count = test_observer_->network_count(); | 623 size_t stub_network_count = test_observer_->network_count(); |
| 468 // Set up two additional visible networks. | 624 // Set up two additional visible networks. |
| 469 const std::string wifi3 = "/service/wifi3"; | 625 const std::string wifi3 = "/service/wifi3"; |
| 470 const std::string wifi4 = "/service/wifi4"; | 626 const std::string wifi4 = "/service/wifi4"; |
| 471 service_test_->SetServiceProperties( | 627 service_test_->SetServiceProperties( |
| 472 wifi3, "wifi3_guid", "wifi3", | 628 wifi3, "wifi3_guid", "wifi3", |
| 473 shill::kTypeWifi, shill::kStateIdle, true /* visible */); | 629 shill::kTypeWifi, shill::kStateIdle, true /* visible */); |
| 474 service_test_->SetServiceProperties( | 630 service_test_->SetServiceProperties( |
| 475 wifi4, "wifi4_guid", "wifi4", | 631 wifi4, "wifi4_guid", "wifi4", |
| (...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1489 shill::kIPConfigProperty, | 1645 shill::kIPConfigProperty, |
| 1490 base::Value(kIPConfigPath)); | 1646 base::Value(kIPConfigPath)); |
| 1491 UpdateManagerProperties(); | 1647 UpdateManagerProperties(); |
| 1492 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice( | 1648 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice( |
| 1493 kShillManagerClientStubWifiDevice)); | 1649 kShillManagerClientStubWifiDevice)); |
| 1494 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( | 1650 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( |
| 1495 kShillManagerClientStubDefaultWifi)); | 1651 kShillManagerClientStubDefaultWifi)); |
| 1496 } | 1652 } |
| 1497 | 1653 |
| 1498 } // namespace chromeos | 1654 } // namespace chromeos |
| OLD | NEW |