| 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 TestTetherNetworkListSorter |
| 206 : public NetworkStateHandler::TetherNetworkListSorter { |
| 207 public: |
| 208 TestTetherNetworkListSorter() {} |
| 209 ~TestTetherNetworkListSorter() {} |
| 210 |
| 211 // NetworkStateHandler::TetherNetworkListSorter: |
| 212 void SortTetherNetworkList( |
| 213 NetworkStateHandler::ManagedStateList* tether_networks) const override { |
| 214 std::sort(tether_networks->begin(), tether_networks->end(), |
| 215 [](const std::unique_ptr<ManagedState>& first, |
| 216 const std::unique_ptr<ManagedState>& second) { |
| 217 const NetworkState* first_network = |
| 218 static_cast<const NetworkState*>(first.get()); |
| 219 const NetworkState* second_network = |
| 220 static_cast<const NetworkState*>(second.get()); |
| 221 |
| 222 // Sort by reverse-alphabetical order of GUIDs. |
| 223 return first_network->guid() >= second_network->guid(); |
| 224 }); |
| 225 } |
| 226 |
| 227 private: |
| 228 DISALLOW_COPY_AND_ASSIGN(TestTetherNetworkListSorter); |
| 229 }; |
| 230 |
| 202 } // namespace | 231 } // namespace |
| 203 | 232 |
| 204 namespace chromeos { | |
| 205 | |
| 206 class NetworkStateHandlerTest : public testing::Test { | 233 class NetworkStateHandlerTest : public testing::Test { |
| 207 public: | 234 public: |
| 208 NetworkStateHandlerTest() | 235 NetworkStateHandlerTest() |
| 209 : scoped_task_environment_( | 236 : scoped_task_environment_( |
| 210 base::test::ScopedTaskEnvironment::MainThreadType::UI), | 237 base::test::ScopedTaskEnvironment::MainThreadType::UI), |
| 211 device_test_(nullptr), | 238 device_test_(nullptr), |
| 212 manager_test_(nullptr), | 239 manager_test_(nullptr), |
| 213 profile_test_(nullptr), | 240 profile_test_(nullptr), |
| 214 service_test_(nullptr) {} | 241 service_test_(nullptr) {} |
| 215 ~NetworkStateHandlerTest() override {} | 242 ~NetworkStateHandlerTest() override {} |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, | 483 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 457 kTetherSignalStrength2, kTetherHasConnectedToHost2); | 484 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 458 | 485 |
| 459 GetTetherNetworkList(0 /* no limit */, &tether_networks); | 486 GetTetherNetworkList(0 /* no limit */, &tether_networks); |
| 460 EXPECT_EQ(2u, tether_networks.size()); | 487 EXPECT_EQ(2u, tether_networks.size()); |
| 461 | 488 |
| 462 GetTetherNetworkList(1 /* no limit */, &tether_networks); | 489 GetTetherNetworkList(1 /* no limit */, &tether_networks); |
| 463 EXPECT_EQ(1u, tether_networks.size()); | 490 EXPECT_EQ(1u, tether_networks.size()); |
| 464 } | 491 } |
| 465 | 492 |
| 493 TEST_F(NetworkStateHandlerTest, SortTetherNetworkList) { |
| 494 network_state_handler_->SetTetherTechnologyState( |
| 495 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 496 |
| 497 TestTetherNetworkListSorter sorter; |
| 498 network_state_handler_->SetTetherNetworkListSorter(&sorter); |
| 499 |
| 500 network_state_handler_->AddTetherNetworkState( |
| 501 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 502 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 503 network_state_handler_->AddTetherNetworkState( |
| 504 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 505 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 506 |
| 507 // Note: GetVisibleNetworkListByType() sorts before outputting networks. |
| 508 NetworkStateHandler::NetworkStateList tether_networks; |
| 509 network_state_handler_->GetVisibleNetworkListByType( |
| 510 NetworkTypePattern::Tether(), &tether_networks); |
| 511 |
| 512 // The list should have been reversed due to reverse-alphabetical sorting. |
| 513 EXPECT_EQ(2u, tether_networks.size()); |
| 514 EXPECT_EQ(kTetherGuid2, tether_networks[0]->guid()); |
| 515 EXPECT_EQ(kTetherGuid1, tether_networks[1]->guid()); |
| 516 } |
| 517 |
| 518 TEST_F(NetworkStateHandlerTest, SortTetherNetworkList_NoSortingDelegate) { |
| 519 network_state_handler_->SetTetherTechnologyState( |
| 520 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 521 |
| 522 // Do not set a TetherNetworkListSorter. |
| 523 |
| 524 network_state_handler_->AddTetherNetworkState( |
| 525 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 526 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 527 network_state_handler_->AddTetherNetworkState( |
| 528 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 529 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 530 |
| 531 // Note: GetVisibleNetworkListByType() sorts before outputting networks. |
| 532 NetworkStateHandler::NetworkStateList tether_networks; |
| 533 network_state_handler_->GetVisibleNetworkListByType( |
| 534 NetworkTypePattern::Tether(), &tether_networks); |
| 535 |
| 536 // The list should be in the original order. |
| 537 EXPECT_EQ(2u, tether_networks.size()); |
| 538 EXPECT_EQ(kTetherGuid1, tether_networks[0]->guid()); |
| 539 EXPECT_EQ(kTetherGuid2, tether_networks[1]->guid()); |
| 540 } |
| 541 |
| 542 TEST_F(NetworkStateHandlerTest, |
| 543 GetNetworks_TetherIncluded_ActiveBeforeNonActive) { |
| 544 network_state_handler_->SetTetherTechnologyState( |
| 545 NetworkStateHandler::TECHNOLOGY_ENABLED); |
| 546 |
| 547 TestTetherNetworkListSorter sorter; |
| 548 network_state_handler_->SetTetherNetworkListSorter(&sorter); |
| 549 |
| 550 // To start the test, |eth1| and |wifi1| are connected, while |wifi2| and |
| 551 // |cellular| are not. |
| 552 const std::string eth1 = kShillManagerClientStubDefaultService; |
| 553 const std::string wifi1 = kShillManagerClientStubDefaultWifi; |
| 554 const std::string wifi2 = kShillManagerClientStubWifi2; |
| 555 const std::string cellular = kShillManagerClientStubCellular; |
| 556 |
| 557 // Disconnect |wifi1|, which will serve as the underlying connection |
| 558 // for the Tether network under test. |
| 559 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, |
| 560 base::Value(shill::kStateIdle)); |
| 561 |
| 562 // Connect |cellular| for this test. |
| 563 service_test_->SetServiceProperty(cellular, shill::kStateProperty, |
| 564 base::Value(shill::kStateOnline)); |
| 565 base::RunLoop().RunUntilIdle(); |
| 566 |
| 567 // Add two Tether networks. Neither is connected yet. |
| 568 network_state_handler_->AddTetherNetworkState( |
| 569 kTetherGuid1, kTetherName1, kTetherCarrier1, kTetherBatteryPercentage1, |
| 570 kTetherSignalStrength1, kTetherHasConnectedToHost1); |
| 571 network_state_handler_->AddTetherNetworkState( |
| 572 kTetherGuid2, kTetherName2, kTetherCarrier2, kTetherBatteryPercentage2, |
| 573 kTetherSignalStrength2, kTetherHasConnectedToHost2); |
| 574 |
| 575 // Connect to the first Tether network (and the underlying Wi-Fi hotspot |
| 576 // network, |wifi1|). |
| 577 network_state_handler_->SetTetherNetworkStateConnecting(kTetherGuid1); |
| 578 network_state_handler_->AssociateTetherNetworkStateWithWifiNetwork( |
| 579 kTetherGuid1, "wifi1_guid"); |
| 580 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, |
| 581 base::Value(shill::kStateOnline)); |
| 582 base::RunLoop().RunUntilIdle(); |
| 583 network_state_handler_->SetTetherNetworkStateConnected(kTetherGuid1); |
| 584 |
| 585 // At this point, |eth1|, |cellular|, and |kTetherGuid1| are connected. |
| 586 // |wifi1| is also connected, but it is not considered visible since it is the |
| 587 // underlying network for the Tether connection. |
| 588 NetworkStateHandler::NetworkStateList list; |
| 589 |
| 590 // Get Tether networks. Even though the networks should be sorted according to |
| 591 // reverse-alphabetical order, |kTetherGuid1| should be listed first since it |
| 592 // is active. |
| 593 network_state_handler_->GetVisibleNetworkListByType( |
| 594 NetworkTypePattern::Tether(), &list); |
| 595 ASSERT_EQ(2u, list.size()); |
| 596 EXPECT_EQ(kTetherGuid1, list[0]->guid()); |
| 597 EXPECT_EQ(kTetherGuid2, list[1]->guid()); |
| 598 |
| 599 // Get Mobile networks. The connected Tether network should be first, followed |
| 600 // by the connected Cellular network, followed by the non-connected Tether |
| 601 // network. |
| 602 network_state_handler_->GetVisibleNetworkListByType( |
| 603 NetworkTypePattern::Mobile(), &list); |
| 604 ASSERT_EQ(3u, list.size()); |
| 605 EXPECT_EQ(kTetherGuid1, list[0]->guid()); |
| 606 EXPECT_EQ(cellular, list[1]->path()); |
| 607 EXPECT_EQ(kTetherGuid2, list[2]->guid()); |
| 608 |
| 609 // Get all networks. The connected Ethernet network should be first, followed |
| 610 // by the connected Tether network, followed by the connected Cellular |
| 611 // network, followed by the non-connected Tether network, followed by the |
| 612 // non-connected Wi-Fi network. |
| 613 network_state_handler_->GetVisibleNetworkListByType( |
| 614 NetworkTypePattern::Default(), &list); |
| 615 EXPECT_EQ(5u, list.size()); |
| 616 EXPECT_EQ(eth1, list[0]->path()); |
| 617 EXPECT_EQ(kTetherGuid1, list[1]->guid()); |
| 618 EXPECT_EQ(cellular, list[2]->path()); |
| 619 EXPECT_EQ(kTetherGuid2, list[3]->guid()); |
| 620 EXPECT_EQ(wifi2, list[4]->path()); |
| 621 } |
| 622 |
| 466 TEST_F(NetworkStateHandlerTest, NetworkListChanged) { | 623 TEST_F(NetworkStateHandlerTest, NetworkListChanged) { |
| 467 size_t stub_network_count = test_observer_->network_count(); | 624 size_t stub_network_count = test_observer_->network_count(); |
| 468 // Set up two additional visible networks. | 625 // Set up two additional visible networks. |
| 469 const std::string wifi3 = "/service/wifi3"; | 626 const std::string wifi3 = "/service/wifi3"; |
| 470 const std::string wifi4 = "/service/wifi4"; | 627 const std::string wifi4 = "/service/wifi4"; |
| 471 service_test_->SetServiceProperties( | 628 service_test_->SetServiceProperties( |
| 472 wifi3, "wifi3_guid", "wifi3", | 629 wifi3, "wifi3_guid", "wifi3", |
| 473 shill::kTypeWifi, shill::kStateIdle, true /* visible */); | 630 shill::kTypeWifi, shill::kStateIdle, true /* visible */); |
| 474 service_test_->SetServiceProperties( | 631 service_test_->SetServiceProperties( |
| 475 wifi4, "wifi4_guid", "wifi4", | 632 wifi4, "wifi4_guid", "wifi4", |
| (...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1489 shill::kIPConfigProperty, | 1646 shill::kIPConfigProperty, |
| 1490 base::Value(kIPConfigPath)); | 1647 base::Value(kIPConfigPath)); |
| 1491 UpdateManagerProperties(); | 1648 UpdateManagerProperties(); |
| 1492 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice( | 1649 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice( |
| 1493 kShillManagerClientStubWifiDevice)); | 1650 kShillManagerClientStubWifiDevice)); |
| 1494 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( | 1651 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( |
| 1495 kShillManagerClientStubDefaultWifi)); | 1652 kShillManagerClientStubDefaultWifi)); |
| 1496 } | 1653 } |
| 1497 | 1654 |
| 1498 } // namespace chromeos | 1655 } // namespace chromeos |
| OLD | NEW |