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/chromeos/policy/device_status_collector.h" | 5 #include "chrome/browser/chromeos/policy/device_status_collector.h" |
6 | 6 |
7 #include "base/environment.h" | 7 #include "base/environment.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
15 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" | 15 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" |
16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
18 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | 18 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" |
19 #include "chrome/browser/chromeos/settings/cros_settings.h" | 19 #include "chrome/browser/chromeos/settings/cros_settings.h" |
20 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 20 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/base/testing_browser_process.h" | 23 #include "chrome/test/base/testing_browser_process.h" |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "chromeos/dbus/shill_device_client.h" | 25 #include "chromeos/dbus/shill_device_client.h" |
| 26 #include "chromeos/dbus/shill_service_client.h" |
26 #include "chromeos/network/network_handler.h" | 27 #include "chromeos/network/network_handler.h" |
| 28 #include "chromeos/network/network_state.h" |
| 29 #include "chromeos/network/network_state_handler.h" |
27 #include "chromeos/settings/cros_settings_names.h" | 30 #include "chromeos/settings/cros_settings_names.h" |
28 #include "chromeos/settings/cros_settings_provider.h" | 31 #include "chromeos/settings/cros_settings_provider.h" |
29 #include "chromeos/system/fake_statistics_provider.h" | 32 #include "chromeos/system/fake_statistics_provider.h" |
30 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/geolocation_provider.h" | 34 #include "content/public/browser/geolocation_provider.h" |
32 #include "content/public/test/test_browser_thread.h" | 35 #include "content/public/test/test_browser_thread.h" |
33 #include "content/public/test/test_utils.h" | 36 #include "content/public/test/test_utils.h" |
34 #include "policy/proto/device_management_backend.pb.h" | 37 #include "policy/proto/device_management_backend.pb.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
36 #include "third_party/cros_system_api/dbus/service_constants.h" | 39 #include "third_party/cros_system_api/dbus/service_constants.h" |
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 "aabbccddeeff", "", "", | 679 "aabbccddeeff", "", "", |
677 em::NetworkInterface::TYPE_WIFI }, | 680 em::NetworkInterface::TYPE_WIFI }, |
678 { "/device/bluetooth", shill::kTypeBluetooth, "bluetooth", | 681 { "/device/bluetooth", shill::kTypeBluetooth, "bluetooth", |
679 "", "", "", | 682 "", "", "", |
680 em::NetworkInterface::TYPE_BLUETOOTH }, | 683 em::NetworkInterface::TYPE_BLUETOOTH }, |
681 { "/device/vpn", shill::kTypeVPN, "vpn", | 684 { "/device/vpn", shill::kTypeVPN, "vpn", |
682 "", "", "", | 685 "", "", "", |
683 -1 }, | 686 -1 }, |
684 }; | 687 }; |
685 | 688 |
| 689 // Fake network state. |
| 690 struct FakeNetworkState { |
| 691 const char* name; |
| 692 const char* device_path; |
| 693 const char* type; |
| 694 int signal_strength; |
| 695 const char* connection_status; |
| 696 int expected_state; |
| 697 }; |
| 698 |
| 699 // List of fake networks - primarily used to make sure that signal strength |
| 700 // and connection state are properly populated in status reports. Note that |
| 701 // by convention shill will not report a signal strength of 0 for a visible |
| 702 // network, so we use 1 below. |
| 703 static const FakeNetworkState kFakeNetworks[] = { |
| 704 { "offline", "/device/wifi", shill::kTypeWifi, 35, |
| 705 shill::kStateOffline, em::NetworkState::OFFLINE }, |
| 706 { "ethernet", "/device/ethernet", shill::kTypeEthernet, 0, |
| 707 shill::kStateOnline, em::NetworkState::ONLINE }, |
| 708 { "wifi", "/device/wifi", shill::kTypeWifi, 23, shill::kStatePortal, |
| 709 em::NetworkState::PORTAL }, |
| 710 { "idle", "/device/cellular1", shill::kTypeCellular, 0, shill::kStateIdle, |
| 711 em::NetworkState::IDLE }, |
| 712 { "carrier", "/device/cellular1", shill::kTypeCellular, 0, |
| 713 shill::kStateCarrier, em::NetworkState::CARRIER }, |
| 714 { "association", "/device/cellular1", shill::kTypeCellular, 0, |
| 715 shill::kStateAssociation, em::NetworkState::ASSOCIATION }, |
| 716 { "config", "/device/cellular1", shill::kTypeCellular, 0, |
| 717 shill::kStateConfiguration, em::NetworkState::CONFIGURATION }, |
| 718 { "ready", "/device/cellular1", shill::kTypeCellular, 0, shill::kStateReady, |
| 719 em::NetworkState::READY }, |
| 720 { "disconnect", "/device/wifi", shill::kTypeWifi, 1, |
| 721 shill::kStateDisconnect, em::NetworkState::DISCONNECT }, |
| 722 { "failure", "/device/wifi", shill::kTypeWifi, 1, shill::kStateFailure, |
| 723 em::NetworkState::FAILURE }, |
| 724 { "activation-failure", "/device/cellular1", shill::kTypeCellular, 0, |
| 725 shill::kStateActivationFailure, em::NetworkState::ACTIVATION_FAILURE }, |
| 726 { "unknown", "", shill::kTypeWifi, 1, "unknown", em::NetworkState::UNKNOWN }, |
| 727 }; |
| 728 |
| 729 static const FakeNetworkState kUnconfiguredNetwork = { |
| 730 "unconfigured", "/device/unconfigured", shill::kTypeWifi, 35, |
| 731 shill::kStateOffline, em::NetworkState::OFFLINE |
| 732 }; |
| 733 |
686 class DeviceStatusCollectorNetworkInterfacesTest | 734 class DeviceStatusCollectorNetworkInterfacesTest |
687 : public DeviceStatusCollectorTest { | 735 : public DeviceStatusCollectorTest { |
688 protected: | 736 protected: |
689 virtual void SetUp() override { | 737 virtual void SetUp() override { |
690 chromeos::DBusThreadManager::Initialize(); | 738 chromeos::DBusThreadManager::Initialize(); |
691 chromeos::NetworkHandler::Initialize(); | 739 chromeos::NetworkHandler::Initialize(); |
692 chromeos::ShillDeviceClient::TestInterface* test_device_client = | 740 chromeos::ShillDeviceClient::TestInterface* test_device_client = |
693 chromeos::DBusThreadManager::Get()->GetShillDeviceClient()-> | 741 chromeos::DBusThreadManager::Get()->GetShillDeviceClient()-> |
694 GetTestInterface(); | 742 GetTestInterface(); |
695 test_device_client->ClearDevices(); | 743 test_device_client->ClearDevices(); |
(...skipping 11 matching lines...) Expand all Loading... |
707 dev.device_path, shill::kMeidProperty, | 755 dev.device_path, shill::kMeidProperty, |
708 base::StringValue(dev.meid)); | 756 base::StringValue(dev.meid)); |
709 } | 757 } |
710 if (*dev.imei) { | 758 if (*dev.imei) { |
711 test_device_client->SetDeviceProperty( | 759 test_device_client->SetDeviceProperty( |
712 dev.device_path, shill::kImeiProperty, | 760 dev.device_path, shill::kImeiProperty, |
713 base::StringValue(dev.imei)); | 761 base::StringValue(dev.imei)); |
714 } | 762 } |
715 } | 763 } |
716 | 764 |
| 765 chromeos::ShillServiceClient::TestInterface* service_client = |
| 766 chromeos::DBusThreadManager::Get()->GetShillServiceClient()-> |
| 767 GetTestInterface(); |
| 768 service_client->ClearServices(); |
| 769 |
| 770 // Now add services for every fake network. |
| 771 for (const FakeNetworkState& fake_network : kFakeNetworks) { |
| 772 // Shill forces non-visible networks to report a disconnected state. |
| 773 bool is_visible = |
| 774 fake_network.connection_status != shill::kStateDisconnect; |
| 775 service_client->AddService( |
| 776 fake_network.name, /* service_path */ |
| 777 fake_network.name /* guid */, |
| 778 fake_network.name /* name */, |
| 779 fake_network.type /* type */, |
| 780 fake_network.connection_status, |
| 781 is_visible); |
| 782 service_client->SetServiceProperty( |
| 783 fake_network.name, shill::kSignalStrengthProperty, |
| 784 base::FundamentalValue(fake_network.signal_strength)); |
| 785 service_client->SetServiceProperty( |
| 786 fake_network.name, shill::kDeviceProperty, |
| 787 base::StringValue(fake_network.device_path)); |
| 788 // Set the profile so this shows up as a configured network. |
| 789 service_client->SetServiceProperty( |
| 790 fake_network.name, shill::kProfileProperty, |
| 791 base::StringValue(fake_network.name)); |
| 792 } |
| 793 |
| 794 // Now add an unconfigured network - it should not show up in the |
| 795 // reported list of networks because it doesn't have a profile specified. |
| 796 service_client->AddService( |
| 797 kUnconfiguredNetwork.name, /* service_path */ |
| 798 kUnconfiguredNetwork.name /* guid */, |
| 799 kUnconfiguredNetwork.name /* name */, |
| 800 kUnconfiguredNetwork.type /* type */, |
| 801 kUnconfiguredNetwork.connection_status, |
| 802 true /* visible */); |
| 803 service_client->SetServiceProperty( |
| 804 kUnconfiguredNetwork.name, shill::kSignalStrengthProperty, |
| 805 base::FundamentalValue(kUnconfiguredNetwork.signal_strength)); |
| 806 service_client->SetServiceProperty( |
| 807 kUnconfiguredNetwork.name, shill::kDeviceProperty, |
| 808 base::StringValue(kUnconfiguredNetwork.device_path)); |
| 809 |
717 // Flush out pending state updates. | 810 // Flush out pending state updates. |
718 base::RunLoop().RunUntilIdle(); | 811 base::RunLoop().RunUntilIdle(); |
| 812 |
| 813 chromeos::NetworkStateHandler::NetworkStateList state_list; |
| 814 chromeos::NetworkStateHandler* network_state_handler = |
| 815 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 816 network_state_handler->GetNetworkListByType( |
| 817 chromeos::NetworkTypePattern::Default(), |
| 818 true, // configured_only |
| 819 false, // visible_only, |
| 820 0, // no limit to number of results |
| 821 &state_list); |
| 822 ASSERT_EQ(arraysize(kFakeNetworks), state_list.size()); |
719 } | 823 } |
720 | 824 |
721 virtual void TearDown() override { | 825 virtual void TearDown() override { |
722 chromeos::NetworkHandler::Shutdown(); | 826 chromeos::NetworkHandler::Shutdown(); |
723 chromeos::DBusThreadManager::Shutdown(); | 827 chromeos::DBusThreadManager::Shutdown(); |
724 } | 828 } |
725 }; | 829 }; |
726 | 830 |
727 TEST_F(DeviceStatusCollectorNetworkInterfacesTest, NetworkInterfaces) { | 831 TEST_F(DeviceStatusCollectorNetworkInterfacesTest, NetworkInterfaces) { |
728 // Interfaces should be reported by default. | 832 // Interfaces should be reported by default. |
729 GetStatus(); | 833 GetStatus(); |
730 EXPECT_LT(0, status_.network_interface_size()); | 834 EXPECT_LT(0, status_.network_interface_size()); |
| 835 EXPECT_LT(0, status_.network_state_size()); |
731 | 836 |
732 // No interfaces should be reported if the policy is off. | 837 // No interfaces should be reported if the policy is off. |
733 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); | 838 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); |
734 GetStatus(); | 839 GetStatus(); |
735 EXPECT_EQ(0, status_.network_interface_size()); | 840 EXPECT_EQ(0, status_.network_interface_size()); |
| 841 EXPECT_EQ(0, status_.network_state_size()); |
736 | 842 |
737 // Switch the policy on and verify the interface list is present. | 843 // Switch the policy on and verify the interface list is present. |
738 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); | 844 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); |
739 GetStatus(); | 845 GetStatus(); |
740 | 846 |
741 int count = 0; | 847 int count = 0; |
742 for (size_t i = 0; i < arraysize(kFakeDevices); ++i) { | 848 for (size_t i = 0; i < arraysize(kFakeDevices); ++i) { |
743 const FakeDeviceData& dev = kFakeDevices[i]; | 849 const FakeDeviceData& dev = kFakeDevices[i]; |
744 if (dev.expected_type == -1) | 850 if (dev.expected_type == -1) |
745 continue; | 851 continue; |
746 | 852 |
747 // Find the corresponding entry in reporting data. | 853 // Find the corresponding entry in reporting data. |
748 bool found_match = false; | 854 bool found_match = false; |
749 google::protobuf::RepeatedPtrField<em::NetworkInterface>::const_iterator | 855 google::protobuf::RepeatedPtrField<em::NetworkInterface>::const_iterator |
750 iface; | 856 iface; |
751 for (iface = status_.network_interface().begin(); | 857 for (iface = status_.network_interface().begin(); |
752 iface != status_.network_interface().end(); | 858 iface != status_.network_interface().end(); |
753 ++iface) { | 859 ++iface) { |
754 // Check whether type, field presence and field values match. | 860 // Check whether type, field presence and field values match. |
755 if (dev.expected_type == iface->type() && | 861 if (dev.expected_type == iface->type() && |
756 iface->has_mac_address() == !!*dev.mac_address && | 862 iface->has_mac_address() == !!*dev.mac_address && |
757 iface->has_meid() == !!*dev.meid && | 863 iface->has_meid() == !!*dev.meid && |
758 iface->has_imei() == !!*dev.imei && | 864 iface->has_imei() == !!*dev.imei && |
759 iface->mac_address() == dev.mac_address && | 865 iface->mac_address() == dev.mac_address && |
760 iface->meid() == dev.meid && | 866 iface->meid() == dev.meid && |
761 iface->imei() == dev.imei) { | 867 iface->imei() == dev.imei && |
| 868 iface->device_path() == dev.device_path) { |
762 found_match = true; | 869 found_match = true; |
763 break; | 870 break; |
764 } | 871 } |
765 } | 872 } |
766 | 873 |
767 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; | 874 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; |
768 count++; | 875 count++; |
769 } | 876 } |
770 | 877 |
771 EXPECT_EQ(count, status_.network_interface_size()); | 878 EXPECT_EQ(count, status_.network_interface_size()); |
| 879 |
| 880 // Now make sure network state list is correct. |
| 881 EXPECT_EQ(arraysize(kFakeNetworks), |
| 882 static_cast<size_t>(status_.network_state_size())); |
| 883 for (const FakeNetworkState& state : kFakeNetworks) { |
| 884 bool found_match = false; |
| 885 for (const em::NetworkState& proto_state : status_.network_state()) { |
| 886 // Make sure every item has a matching entry in the proto. |
| 887 if (proto_state.has_device_path() == (strlen(state.device_path) > 0) && |
| 888 proto_state.signal_strength() == state.signal_strength && |
| 889 proto_state.connection_state() == state.expected_state) { |
| 890 found_match = true; |
| 891 break; |
| 892 } |
| 893 } |
| 894 EXPECT_TRUE(found_match) << "No matching state for fake network " |
| 895 << " (" << state.name << ")"; |
| 896 } |
772 } | 897 } |
773 | 898 |
774 } // namespace policy | 899 } // namespace policy |
OLD | NEW |