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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/environment.h" | 11 #include "base/environment.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/prefs/testing_pref_service.h" | 16 #include "base/prefs/testing_pref_service.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
| 19 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" | 19 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" |
| 20 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 20 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 21 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 21 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 22 #include "chrome/browser/chromeos/policy/device_local_account.h" | |
| 22 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | 23 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" |
| 23 #include "chrome/browser/chromeos/settings/cros_settings.h" | 24 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 24 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 25 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 25 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 26 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/test/base/testing_browser_process.h" | 28 #include "chrome/test/base/testing_browser_process.h" |
| 28 #include "chromeos/dbus/cros_disks_client.h" | 29 #include "chromeos/dbus/cros_disks_client.h" |
| 29 #include "chromeos/dbus/dbus_thread_manager.h" | 30 #include "chromeos/dbus/dbus_thread_manager.h" |
| 30 #include "chromeos/dbus/shill_device_client.h" | 31 #include "chromeos/dbus/shill_device_client.h" |
| 31 #include "chromeos/dbus/shill_ipconfig_client.h" | 32 #include "chromeos/dbus/shill_ipconfig_client.h" |
| 32 #include "chromeos/dbus/shill_service_client.h" | 33 #include "chromeos/dbus/shill_service_client.h" |
| 33 #include "chromeos/disks/disk_mount_manager.h" | 34 #include "chromeos/disks/disk_mount_manager.h" |
| 34 #include "chromeos/disks/mock_disk_mount_manager.h" | 35 #include "chromeos/disks/mock_disk_mount_manager.h" |
| 35 #include "chromeos/network/network_handler.h" | 36 #include "chromeos/network/network_handler.h" |
| 36 #include "chromeos/network/network_state.h" | 37 #include "chromeos/network/network_state.h" |
| 37 #include "chromeos/network/network_state_handler.h" | 38 #include "chromeos/network/network_state_handler.h" |
| 38 #include "chromeos/settings/cros_settings_names.h" | 39 #include "chromeos/settings/cros_settings_names.h" |
| 39 #include "chromeos/settings/cros_settings_provider.h" | 40 #include "chromeos/settings/cros_settings_provider.h" |
| 40 #include "chromeos/system/fake_statistics_provider.h" | 41 #include "chromeos/system/fake_statistics_provider.h" |
| 41 #include "content/public/browser/browser_thread.h" | 42 #include "content/public/browser/browser_thread.h" |
| 42 #include "content/public/browser/geolocation_provider.h" | 43 #include "content/public/browser/geolocation_provider.h" |
| 43 #include "content/public/test/test_browser_thread.h" | 44 #include "content/public/test/test_browser_thread.h" |
| 44 #include "content/public/test/test_utils.h" | 45 #include "content/public/test/test_utils.h" |
| 45 #include "policy/proto/device_management_backend.pb.h" | 46 #include "policy/proto/device_management_backend.pb.h" |
| 46 #include "testing/gmock/include/gmock/gmock.h" | 47 #include "testing/gmock/include/gmock/gmock.h" |
| 47 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
| 48 #include "third_party/cros_system_api/dbus/service_constants.h" | 49 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 49 | 50 |
| 51 using ::testing::Return; | |
| 50 using ::testing::ReturnRef; | 52 using ::testing::ReturnRef; |
| 51 using base::Time; | 53 using base::Time; |
| 52 using base::TimeDelta; | 54 using base::TimeDelta; |
| 53 using chromeos::disks::DiskMountManager; | 55 using chromeos::disks::DiskMountManager; |
| 54 | 56 |
| 55 namespace em = enterprise_management; | 57 namespace em = enterprise_management; |
| 56 | 58 |
| 57 namespace { | 59 namespace { |
| 58 | 60 |
| 59 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; | 61 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; |
| 62 const char kKioskAccountId[] = "kiosk_user@localhost"; | |
| 63 const char kKioskAppId[] = "kiosk_app_id"; | |
| 60 | 64 |
| 61 scoped_ptr<content::Geoposition> mock_position_to_return_next; | 65 scoped_ptr<content::Geoposition> mock_position_to_return_next; |
| 62 | 66 |
| 63 void SetMockPositionToReturnNext(const content::Geoposition &position) { | 67 void SetMockPositionToReturnNext(const content::Geoposition &position) { |
| 64 mock_position_to_return_next.reset(new content::Geoposition(position)); | 68 mock_position_to_return_next.reset(new content::Geoposition(position)); |
| 65 } | 69 } |
| 66 | 70 |
| 67 void MockPositionUpdateRequester( | 71 void MockPositionUpdateRequester( |
| 68 const content::GeolocationProvider::LocationUpdateCallback& callback) { | 72 const content::GeolocationProvider::LocationUpdateCallback& callback) { |
| 69 if (!mock_position_to_return_next.get()) | 73 if (!mock_position_to_return_next.get()) |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 } | 134 } |
| 131 | 135 |
| 132 void set_kiosk_mode(bool is_kiosk) { | 136 void set_kiosk_mode(bool is_kiosk) { |
| 133 kiosk_mode_ = is_kiosk; | 137 kiosk_mode_ = is_kiosk; |
| 134 } | 138 } |
| 135 | 139 |
| 136 bool IsAutoLaunchedKioskSession() override { | 140 bool IsAutoLaunchedKioskSession() override { |
| 137 return kiosk_mode_; | 141 return kiosk_mode_; |
| 138 } | 142 } |
| 139 | 143 |
| 144 std::string GetAppVersion(std::string app_id) override { | |
| 145 // Just return the app_id as the version - this makes it easy for tests | |
| 146 // to confirm that the correct app's version was requested. | |
| 147 return app_id; | |
| 148 } | |
| 149 | |
| 140 void RefreshSampleResourceUsage() { | 150 void RefreshSampleResourceUsage() { |
| 141 // Refresh our samples. Sample more than kMaxHardwareSamples times to | 151 // Refresh our samples. Sample more than kMaxHardwareSamples times to |
| 142 // make sure that the code correctly caps the number of cached samples. | 152 // make sure that the code correctly caps the number of cached samples. |
| 143 for (int i = 0; i < static_cast<int>(kMaxResourceUsageSamples + 1); ++i) | 153 for (int i = 0; i < static_cast<int>(kMaxResourceUsageSamples + 1); ++i) |
| 144 SampleResourceUsage(); | 154 SampleResourceUsage(); |
| 145 } | 155 } |
| 146 | 156 |
| 147 protected: | 157 protected: |
| 148 void CheckIdleState() override { | 158 void CheckIdleState() override { |
| 149 // This should never be called in testing, as it results in a dbus call. | 159 // This should never be called in testing, as it results in a dbus call. |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 | 343 |
| 334 void CheckThatALocationErrorIsReported() { | 344 void CheckThatALocationErrorIsReported() { |
| 335 GetStatus(); | 345 GetStatus(); |
| 336 EXPECT_TRUE(status_.has_device_location()); | 346 EXPECT_TRUE(status_.has_device_location()); |
| 337 em::DeviceLocation location = status_.device_location(); | 347 em::DeviceLocation location = status_.device_location(); |
| 338 EXPECT_TRUE(location.has_error_code()); | 348 EXPECT_TRUE(location.has_error_code()); |
| 339 EXPECT_EQ(em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE, | 349 EXPECT_EQ(em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE, |
| 340 location.error_code()); | 350 location.error_code()); |
| 341 } | 351 } |
| 342 | 352 |
| 353 void MockRunningKioskApp() { | |
| 354 DeviceLocalAccount local_account( | |
|
bartfab (slow)
2015/02/11 17:28:43
Nit: const.
Andrew T Wilson (Slow)
2015/02/12 15:15:39
moot due to refactoring.
| |
| 355 policy::DeviceLocalAccount::TYPE_KIOSK_APP, | |
| 356 kKioskAccountId, | |
| 357 kKioskAppId, | |
| 358 std::string()); | |
| 359 std::vector<DeviceLocalAccount> accounts; | |
| 360 accounts.push_back(local_account); | |
| 361 SetDeviceLocalAccounts(cros_settings_, accounts); | |
| 362 user_manager_->CreateKioskAppUser(local_account.user_id); | |
| 363 EXPECT_CALL(*user_manager_, IsLoggedInAsKioskApp()).WillRepeatedly( | |
| 364 Return(true)); | |
| 365 } | |
| 366 | |
| 343 protected: | 367 protected: |
| 344 // Convenience method. | 368 // Convenience method. |
| 345 int64 ActivePeriodMilliseconds() { | 369 int64 ActivePeriodMilliseconds() { |
| 346 return policy::DeviceStatusCollector::kIdlePollIntervalSeconds * 1000; | 370 return policy::DeviceStatusCollector::kIdlePollIntervalSeconds * 1000; |
| 347 } | 371 } |
| 348 | 372 |
| 349 // Since this is a unit test running in browser_tests we must do additional | 373 // Since this is a unit test running in browser_tests we must do additional |
| 350 // unit test setup and make a TestingBrowserProcess. Must be first member. | 374 // unit test setup and make a TestingBrowserProcess. Must be first member. |
| 351 TestingBrowserProcessInitializer initializer_; | 375 TestingBrowserProcessInitializer initializer_; |
| 352 base::MessageLoopForUI message_loop_; | 376 base::MessageLoopForUI message_loop_; |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 834 status_.cpu_utilization_pct().size()); | 858 status_.cpu_utilization_pct().size()); |
| 835 for (const auto utilization : status_.cpu_utilization_pct()) | 859 for (const auto utilization : status_.cpu_utilization_pct()) |
| 836 EXPECT_EQ(idle_cpu_usage, utilization); | 860 EXPECT_EQ(idle_cpu_usage, utilization); |
| 837 | 861 |
| 838 // Turning off hardware reporting should not report CPU utilization. | 862 // Turning off hardware reporting should not report CPU utilization. |
| 839 cros_settings_->SetBoolean(chromeos::kReportDeviceHardwareStatus, false); | 863 cros_settings_->SetBoolean(chromeos::kReportDeviceHardwareStatus, false); |
| 840 GetStatus(); | 864 GetStatus(); |
| 841 EXPECT_EQ(0, status_.cpu_utilization_pct().size()); | 865 EXPECT_EQ(0, status_.cpu_utilization_pct().size()); |
| 842 } | 866 } |
| 843 | 867 |
| 868 TEST_F(DeviceStatusCollectorTest, NoSessionStatusIfNotKioskMode) { | |
| 869 // Should not report session status if we don't have an active kiosk app. | |
| 870 cros_settings_->SetBoolean(chromeos::kReportDeviceSessionStatus, true); | |
| 871 status_collector_->set_kiosk_mode(false); | |
| 872 em::SessionStatusReportRequest session_status; | |
| 873 EXPECT_FALSE(status_collector_->GetDeviceSessionStatus(&session_status)); | |
| 874 } | |
| 875 | |
| 876 TEST_F(DeviceStatusCollectorTest, NoSessionStatusIfSessionReportingDisabled) { | |
| 877 // Should not report session status if session status reporting is disabled. | |
| 878 cros_settings_->SetBoolean(chromeos::kReportDeviceSessionStatus, false); | |
| 879 status_collector_->set_kiosk_mode(true); | |
| 880 MockRunningKioskApp(); | |
| 881 | |
| 882 em::SessionStatusReportRequest session_status; | |
| 883 EXPECT_FALSE(status_collector_->GetDeviceSessionStatus(&session_status)); | |
| 884 } | |
| 885 | |
| 886 TEST_F(DeviceStatusCollectorTest, ReportSession) { | |
|
bartfab (slow)
2015/02/11 17:28:43
Nit: s/ReportSession/ReportSessionStatus/
Andrew T Wilson (Slow)
2015/02/12 15:15:39
Done.
| |
| 887 cros_settings_->SetBoolean(chromeos::kReportDeviceSessionStatus, true); | |
| 888 status_collector_->set_kiosk_mode(true); | |
| 889 | |
| 890 // Setup a DeviceLocalAccount for a kiosk app. | |
|
bartfab (slow)
2015/02/11 17:28:43
Nit: s/Setup DeviceLocalAccount for a kiosk app/Se
| |
| 891 MockRunningKioskApp(); | |
| 892 em::SessionStatusReportRequest session_status; | |
|
bartfab (slow)
2015/02/11 17:28:43
Nit: Add blank line above. The setup phase is done
Andrew T Wilson (Slow)
2015/02/12 15:15:39
Done.
| |
| 893 EXPECT_TRUE(status_collector_->GetDeviceSessionStatus(&session_status)); | |
| 894 ASSERT_EQ(1, session_status.installed_apps_size()); | |
| 895 EXPECT_EQ(kKioskAccountId, session_status.device_local_account_id()); | |
| 896 em::AppStatus app = session_status.installed_apps(0); | |
|
bartfab (slow)
2015/02/11 17:28:43
Nit: const.
Andrew T Wilson (Slow)
2015/02/12 15:15:39
Done.
| |
| 897 EXPECT_EQ(kKioskAppId, app.app_id()); | |
| 898 // Test code just sets the version to the app ID. | |
| 899 EXPECT_EQ(kKioskAppId, app.extension_version()); | |
| 900 EXPECT_FALSE(app.has_status()); | |
| 901 EXPECT_FALSE(app.has_error()); | |
| 902 } | |
| 903 | |
| 844 // Fake device state. | 904 // Fake device state. |
| 845 struct FakeDeviceData { | 905 struct FakeDeviceData { |
| 846 const char* device_path; | 906 const char* device_path; |
| 847 const char* type; | 907 const char* type; |
| 848 const char* object_path; | 908 const char* object_path; |
| 849 const char* mac_address; | 909 const char* mac_address; |
| 850 const char* meid; | 910 const char* meid; |
| 851 const char* imei; | 911 const char* imei; |
| 852 int expected_type; // proto enum type value, -1 for not present. | 912 int expected_type; // proto enum type value, -1 for not present. |
| 853 }; | 913 }; |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1117 found_match = true; | 1177 found_match = true; |
| 1118 break; | 1178 break; |
| 1119 } | 1179 } |
| 1120 } | 1180 } |
| 1121 EXPECT_TRUE(found_match) << "No matching state for fake network " | 1181 EXPECT_TRUE(found_match) << "No matching state for fake network " |
| 1122 << " (" << state.name << ")"; | 1182 << " (" << state.name << ")"; |
| 1123 } | 1183 } |
| 1124 } | 1184 } |
| 1125 | 1185 |
| 1126 } // namespace policy | 1186 } // namespace policy |
| OLD | NEW |