Chromium Code Reviews| 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 const int signal_strength; | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
nit: const not required
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Done.
| |
| 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 | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
nit: "0" -> 0
for consistency.
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Done.
| |
| 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 | |
| 686 class DeviceStatusCollectorNetworkInterfacesTest | 729 class DeviceStatusCollectorNetworkInterfacesTest |
| 687 : public DeviceStatusCollectorTest { | 730 : public DeviceStatusCollectorTest { |
| 688 protected: | 731 protected: |
| 689 virtual void SetUp() override { | 732 virtual void SetUp() override { |
| 690 chromeos::DBusThreadManager::Initialize(); | 733 chromeos::DBusThreadManager::Initialize(); |
| 691 chromeos::NetworkHandler::Initialize(); | 734 chromeos::NetworkHandler::Initialize(); |
| 692 chromeos::ShillDeviceClient::TestInterface* test_device_client = | 735 chromeos::ShillDeviceClient::TestInterface* test_device_client = |
| 693 chromeos::DBusThreadManager::Get()->GetShillDeviceClient()-> | 736 chromeos::DBusThreadManager::Get()->GetShillDeviceClient()-> |
| 694 GetTestInterface(); | 737 GetTestInterface(); |
| 695 test_device_client->ClearDevices(); | 738 test_device_client->ClearDevices(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 707 dev.device_path, shill::kMeidProperty, | 750 dev.device_path, shill::kMeidProperty, |
| 708 base::StringValue(dev.meid)); | 751 base::StringValue(dev.meid)); |
| 709 } | 752 } |
| 710 if (*dev.imei) { | 753 if (*dev.imei) { |
| 711 test_device_client->SetDeviceProperty( | 754 test_device_client->SetDeviceProperty( |
| 712 dev.device_path, shill::kImeiProperty, | 755 dev.device_path, shill::kImeiProperty, |
| 713 base::StringValue(dev.imei)); | 756 base::StringValue(dev.imei)); |
| 714 } | 757 } |
| 715 } | 758 } |
| 716 | 759 |
| 760 chromeos::ShillServiceClient::TestInterface* service_client = | |
| 761 chromeos::DBusThreadManager::Get()->GetShillServiceClient()-> | |
| 762 GetTestInterface(); | |
| 763 service_client->ClearServices(); | |
| 764 | |
| 765 // Now add services for every fake network. | |
| 766 for (size_t i = 0; i < arraysize(kFakeNetworks); ++i) { | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
should be possible to replace by
for (const FakeN
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Done.
| |
| 767 service_client->AddService( | |
| 768 kFakeNetworks[i].name, /* service_path */ | |
| 769 kFakeNetworks[i].name /* guid */, | |
| 770 kFakeNetworks[i].name /* name */, | |
| 771 kFakeNetworks[i].type /* type */, | |
| 772 kFakeNetworks[i].connection_status, | |
| 773 true /* visible */); | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
would you want to test a not visible network as we
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Added a test for this (we have a disconnected netw
| |
| 774 service_client->SetServiceProperty( | |
| 775 kFakeNetworks[i].name, shill::kSignalStrengthProperty, | |
| 776 base::FundamentalValue(kFakeNetworks[i].signal_strength)); | |
| 777 service_client->SetServiceProperty( | |
| 778 kFakeNetworks[i].name, shill::kDeviceProperty, | |
| 779 base::StringValue(kFakeNetworks[i].device_path)); | |
| 780 } | |
| 781 | |
| 717 // Flush out pending state updates. | 782 // Flush out pending state updates. |
| 718 base::RunLoop().RunUntilIdle(); | 783 base::RunLoop().RunUntilIdle(); |
| 784 | |
| 785 chromeos::NetworkStateHandler::NetworkStateList state_list; | |
| 786 chromeos::NetworkStateHandler* network_state_handler = | |
| 787 chromeos::NetworkHandler::Get()->network_state_handler(); | |
| 788 network_state_handler->GetNetworkListByType( | |
| 789 chromeos::NetworkTypePattern::Default(), | |
| 790 false, // configured_only | |
| 791 false, // visible_only, | |
| 792 0, // no limit to number of results | |
| 793 &state_list); | |
| 794 ASSERT_EQ(arraysize(kFakeNetworks), state_list.size()); | |
| 719 } | 795 } |
| 720 | 796 |
| 721 virtual void TearDown() override { | 797 virtual void TearDown() override { |
| 722 chromeos::NetworkHandler::Shutdown(); | 798 chromeos::NetworkHandler::Shutdown(); |
| 723 chromeos::DBusThreadManager::Shutdown(); | 799 chromeos::DBusThreadManager::Shutdown(); |
| 724 } | 800 } |
| 725 }; | 801 }; |
| 726 | 802 |
| 727 TEST_F(DeviceStatusCollectorNetworkInterfacesTest, NetworkInterfaces) { | 803 TEST_F(DeviceStatusCollectorNetworkInterfacesTest, NetworkInterfaces) { |
| 728 // Interfaces should be reported by default. | 804 // Interfaces should be reported by default. |
| 729 GetStatus(); | 805 GetStatus(); |
| 730 EXPECT_LT(0, status_.network_interface_size()); | 806 EXPECT_LT(0, status_.network_interface_size()); |
| 807 EXPECT_LT(0, status_.network_state_size()); | |
| 731 | 808 |
| 732 // No interfaces should be reported if the policy is off. | 809 // No interfaces should be reported if the policy is off. |
| 733 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); | 810 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); |
| 734 GetStatus(); | 811 GetStatus(); |
| 735 EXPECT_EQ(0, status_.network_interface_size()); | 812 EXPECT_EQ(0, status_.network_interface_size()); |
| 813 EXPECT_EQ(0, status_.network_state_size()); | |
| 736 | 814 |
| 737 // Switch the policy on and verify the interface list is present. | 815 // Switch the policy on and verify the interface list is present. |
| 738 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); | 816 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); |
| 739 GetStatus(); | 817 GetStatus(); |
| 740 | 818 |
| 741 int count = 0; | 819 int count = 0; |
| 742 for (size_t i = 0; i < arraysize(kFakeDevices); ++i) { | 820 for (size_t i = 0; i < arraysize(kFakeDevices); ++i) { |
| 743 const FakeDeviceData& dev = kFakeDevices[i]; | 821 const FakeDeviceData& dev = kFakeDevices[i]; |
| 744 if (dev.expected_type == -1) | 822 if (dev.expected_type == -1) |
| 745 continue; | 823 continue; |
| 746 | 824 |
| 747 // Find the corresponding entry in reporting data. | 825 // Find the corresponding entry in reporting data. |
| 748 bool found_match = false; | 826 bool found_match = false; |
| 749 google::protobuf::RepeatedPtrField<em::NetworkInterface>::const_iterator | 827 google::protobuf::RepeatedPtrField<em::NetworkInterface>::const_iterator |
| 750 iface; | 828 iface; |
| 751 for (iface = status_.network_interface().begin(); | 829 for (iface = status_.network_interface().begin(); |
| 752 iface != status_.network_interface().end(); | 830 iface != status_.network_interface().end(); |
| 753 ++iface) { | 831 ++iface) { |
| 754 // Check whether type, field presence and field values match. | 832 // Check whether type, field presence and field values match. |
| 755 if (dev.expected_type == iface->type() && | 833 if (dev.expected_type == iface->type() && |
| 756 iface->has_mac_address() == !!*dev.mac_address && | 834 iface->has_mac_address() == !!*dev.mac_address && |
| 757 iface->has_meid() == !!*dev.meid && | 835 iface->has_meid() == !!*dev.meid && |
| 758 iface->has_imei() == !!*dev.imei && | 836 iface->has_imei() == !!*dev.imei && |
| 759 iface->mac_address() == dev.mac_address && | 837 iface->mac_address() == dev.mac_address && |
| 760 iface->meid() == dev.meid && | 838 iface->meid() == dev.meid && |
| 761 iface->imei() == dev.imei) { | 839 iface->imei() == dev.imei && |
| 840 iface->device_path() == dev.device_path) { | |
| 762 found_match = true; | 841 found_match = true; |
| 763 break; | 842 break; |
| 764 } | 843 } |
| 765 } | 844 } |
| 766 | 845 |
| 767 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; | 846 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; |
| 768 count++; | 847 count++; |
| 769 } | 848 } |
| 770 | 849 |
| 771 EXPECT_EQ(count, status_.network_interface_size()); | 850 EXPECT_EQ(count, status_.network_interface_size()); |
| 851 | |
| 852 // Now make sure network state list is correct. | |
| 853 EXPECT_EQ(arraysize(kFakeNetworks), | |
| 854 static_cast<size_t>(status_.network_state_size())); | |
| 855 for (size_t i = 0; i < arraysize(kFakeNetworks); ++i) { | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
same here
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Done.
| |
| 856 const FakeNetworkState& state = kFakeNetworks[i]; | |
| 857 bool found_match = false; | |
| 858 google::protobuf::RepeatedPtrField<em::NetworkState>::const_iterator | |
| 859 proto_state; | |
| 860 for (proto_state = status_.network_state().begin(); | |
|
pneubeck (no reviews)
2014/12/09 03:00:10
for (const em::NetworkState& proto_state : status_
Andrew T Wilson (Slow)
2014/12/11 00:09:55
Done.
| |
| 861 proto_state != status_.network_state().end(); | |
| 862 ++proto_state) { | |
| 863 // Make sure every item has a matching entry in the proto. | |
| 864 if (proto_state->has_device_path() == (strlen(state.device_path) > 0) && | |
| 865 proto_state->signal_strength() == state.signal_strength && | |
| 866 proto_state->connection_state() == state.expected_state) { | |
| 867 found_match = true; | |
| 868 break; | |
| 869 } | |
| 870 } | |
| 871 EXPECT_TRUE(found_match) << "No matching state for fake network " << i | |
| 872 << " (" << state.name << ")"; | |
| 873 } | |
| 772 } | 874 } |
| 773 | 875 |
| 774 } // namespace policy | 876 } // namespace policy |
| OLD | NEW |