Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_provider_unittest.cc
blob824f61522f81f8db3eb7bd2e66136f3906c50234
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/settings/device_settings_provider.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/files/file_util.h"
12 #include "base/path_service.h"
13 #include "base/test/scoped_path_override.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/policy/device_local_account.h"
16 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
17 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
18 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/test/base/scoped_testing_local_state.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chromeos/settings/cros_settings_names.h"
24 #include "components/user_manager/fake_user_manager.h"
25 #include "components/user_manager/user.h"
26 #include "policy/proto/device_management_backend.pb.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 namespace em = enterprise_management;
32 namespace chromeos {
34 using ::testing::AtLeast;
35 using ::testing::AnyNumber;
36 using ::testing::Mock;
37 using ::testing::_;
39 namespace {
41 const char kDisabledMessage[] = "This device has been disabled.";
43 } // namespace
45 class DeviceSettingsProviderTest : public DeviceSettingsTestBase {
46 public:
47 MOCK_METHOD1(SettingChanged, void(const std::string&));
48 MOCK_METHOD0(GetTrustedCallback, void(void));
50 protected:
51 DeviceSettingsProviderTest()
52 : local_state_(TestingBrowserProcess::GetGlobal()),
53 user_data_dir_override_(chrome::DIR_USER_DATA) {}
55 virtual void SetUp() override {
56 DeviceSettingsTestBase::SetUp();
58 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
59 provider_.reset(
60 new DeviceSettingsProvider(
61 base::Bind(&DeviceSettingsProviderTest::SettingChanged,
62 base::Unretained(this)),
63 &device_settings_service_));
64 Mock::VerifyAndClearExpectations(this);
67 virtual void TearDown() override {
68 DeviceSettingsTestBase::TearDown();
71 // Helper routine to enable/disable all reporting settings in policy.
72 void SetReportingSettings(bool enable_reporting, int frequency) {
73 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
74 em::DeviceReportingProto* proto =
75 device_policy_.payload().mutable_device_reporting();
76 proto->set_report_version_info(enable_reporting);
77 proto->set_report_activity_times(enable_reporting);
78 proto->set_report_boot_mode(enable_reporting);
79 proto->set_report_location(enable_reporting);
80 proto->set_report_network_interfaces(enable_reporting);
81 proto->set_report_users(enable_reporting);
82 proto->set_report_hardware_status(enable_reporting);
83 proto->set_report_session_status(enable_reporting);
84 proto->set_device_status_frequency(frequency);
85 device_policy_.Build();
86 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
87 ReloadDeviceSettings();
88 Mock::VerifyAndClearExpectations(this);
91 // Helper routine to enable/disable all reporting settings in policy.
92 void SetHeartbeatSettings(bool enable_heartbeat, int frequency) {
93 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
94 em::DeviceHeartbeatSettingsProto* proto =
95 device_policy_.payload().mutable_device_heartbeat_settings();
96 proto->set_heartbeat_enabled(enable_heartbeat);
97 proto->set_heartbeat_frequency(frequency);
98 device_policy_.Build();
99 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
100 ReloadDeviceSettings();
101 Mock::VerifyAndClearExpectations(this);
104 // Helper routine to ensure all heartbeat policies have been correctly
105 // decoded.
106 void VerifyHeartbeatSettings(bool expected_enable_state,
107 int expected_frequency) {
109 const base::FundamentalValue expected_enabled_value(expected_enable_state);
110 EXPECT_TRUE(base::Value::Equals(provider_->Get(kHeartbeatEnabled),
111 &expected_enabled_value));
113 const base::FundamentalValue expected_frequency_value(expected_frequency);
114 EXPECT_TRUE(base::Value::Equals(provider_->Get(kHeartbeatFrequency),
115 &expected_frequency_value));
118 // Helper routine to ensure all reporting policies have been correctly
119 // decoded.
120 void VerifyReportingSettings(bool expected_enable_state,
121 int expected_frequency) {
122 const char* reporting_settings[] = {
123 kReportDeviceVersionInfo,
124 kReportDeviceActivityTimes,
125 kReportDeviceBootMode,
126 // Device location reporting is not currently supported.
127 // kReportDeviceLocation,
128 kReportDeviceNetworkInterfaces,
129 kReportDeviceUsers,
130 kReportDeviceHardwareStatus,
131 kReportDeviceSessionStatus
134 const base::FundamentalValue expected_enable_value(expected_enable_state);
135 for (const auto& setting : reporting_settings) {
136 EXPECT_TRUE(base::Value::Equals(provider_->Get(setting),
137 &expected_enable_value))
138 << "Value for " << setting << " does not match expected";
140 const base::FundamentalValue expected_frequency_value(expected_frequency);
141 EXPECT_TRUE(base::Value::Equals(provider_->Get(kReportUploadFrequency),
142 &expected_frequency_value));
145 ScopedTestingLocalState local_state_;
147 scoped_ptr<DeviceSettingsProvider> provider_;
149 base::ScopedPathOverride user_data_dir_override_;
151 private:
152 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
155 TEST_F(DeviceSettingsProviderTest, InitializationTest) {
156 // Have the service load a settings blob.
157 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
158 ReloadDeviceSettings();
159 Mock::VerifyAndClearExpectations(this);
161 // Verify that the policy blob has been correctly parsed and trusted.
162 // The trusted flag should be set before the call to PrepareTrustedValues.
163 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
164 provider_->PrepareTrustedValues(base::Closure()));
165 const base::Value* value = provider_->Get(kStatsReportingPref);
166 ASSERT_TRUE(value);
167 bool bool_value;
168 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
169 EXPECT_FALSE(bool_value);
172 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
173 // Have the service check the key.
174 owner_key_util_->Clear();
175 ReloadDeviceSettings();
177 // The trusted flag should be set before the call to PrepareTrustedValues.
178 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
179 provider_->PrepareTrustedValues(base::Closure()));
180 const base::Value* value = provider_->Get(kReleaseChannel);
181 ASSERT_TRUE(value);
182 std::string string_value;
183 EXPECT_TRUE(value->GetAsString(&string_value));
184 EXPECT_TRUE(string_value.empty());
186 // Sets should succeed though and be readable from the cache.
187 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
188 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
189 base::StringValue new_value("stable-channel");
190 provider_->Set(kReleaseChannel, new_value);
191 Mock::VerifyAndClearExpectations(this);
193 // This shouldn't trigger a write.
194 device_settings_test_helper_.set_policy_blob(std::string());
195 FlushDeviceSettings();
196 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
198 // Verify the change has been applied.
199 const base::Value* saved_value = provider_->Get(kReleaseChannel);
200 ASSERT_TRUE(saved_value);
201 EXPECT_TRUE(saved_value->GetAsString(&string_value));
202 ASSERT_EQ("stable-channel", string_value);
205 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
206 // If we are not the owner no sets should work.
207 base::FundamentalValue value(true);
208 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
209 provider_->Set(kStatsReportingPref, value);
210 Mock::VerifyAndClearExpectations(this);
212 // This shouldn't trigger a write.
213 device_settings_test_helper_.set_policy_blob(std::string());
214 FlushDeviceSettings();
215 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
217 // Verify the change has not been applied.
218 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
219 ASSERT_TRUE(saved_value);
220 bool bool_value;
221 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
222 EXPECT_FALSE(bool_value);
225 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
226 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
227 InitOwner(device_policy_.policy_data().username(), true);
228 FlushDeviceSettings();
230 base::FundamentalValue value(true);
231 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
232 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
233 provider_->Set(kStatsReportingPref, value);
234 Mock::VerifyAndClearExpectations(this);
236 // Process the store.
237 device_settings_test_helper_.set_policy_blob(std::string());
238 FlushDeviceSettings();
240 // Verify that the device policy has been adjusted.
241 ASSERT_TRUE(device_settings_service_.device_settings());
242 EXPECT_TRUE(device_settings_service_.device_settings()->
243 metrics_enabled().metrics_enabled());
245 // Verify the change has been applied.
246 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
247 ASSERT_TRUE(saved_value);
248 bool bool_value;
249 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
250 EXPECT_TRUE(bool_value);
253 TEST_F(DeviceSettingsProviderTest, SetPrefTwice) {
254 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
255 InitOwner(device_policy_.policy_data().username(), true);
256 FlushDeviceSettings();
258 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
260 base::StringValue value1("beta");
261 provider_->Set(kReleaseChannel, value1);
262 base::StringValue value2("dev");
263 provider_->Set(kReleaseChannel, value2);
265 // Let the changes propagate through the system.
266 device_settings_test_helper_.set_policy_blob(std::string());
267 FlushDeviceSettings();
269 // Verify the second change has been applied.
270 const base::Value* saved_value = provider_->Get(kReleaseChannel);
271 EXPECT_TRUE(value2.Equals(saved_value));
273 Mock::VerifyAndClearExpectations(this);
276 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) {
277 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
278 device_policy_.policy().set_policy_data_signature("bad signature");
279 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
280 ReloadDeviceSettings();
282 // Verify that the cached settings blob is not "trusted".
283 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
284 device_settings_service_.status());
285 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
286 provider_->PrepareTrustedValues(base::Closure()));
289 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) {
290 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
291 device_settings_test_helper_.set_policy_blob(std::string());
292 ReloadDeviceSettings();
294 // Verify that the cached settings blob is not "trusted".
295 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
296 device_settings_service_.status());
297 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
298 provider_->PrepareTrustedValues(base::Closure()));
301 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
302 device_settings_test_helper_.set_policy_blob(std::string());
304 EXPECT_CALL(*this, GetTrustedCallback());
305 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
306 provider_->PrepareTrustedValues(
307 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
308 base::Unretained(this))));
310 ReloadDeviceSettings();
311 Mock::VerifyAndClearExpectations(this);
313 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
314 provider_->PrepareTrustedValues(base::Closure()));
317 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
318 EXPECT_CALL(*this, GetTrustedCallback());
320 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
321 provider_->PrepareTrustedValues(
322 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
323 base::Unretained(this))));
325 ReloadDeviceSettings();
326 Mock::VerifyAndClearExpectations(this);
329 TEST_F(DeviceSettingsProviderTest, StatsReportingMigration) {
330 // Create the legacy consent file.
331 base::FilePath consent_file;
332 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &consent_file));
333 consent_file = consent_file.AppendASCII("Consent To Send Stats");
334 ASSERT_EQ(1, base::WriteFile(consent_file, "0", 1));
336 // This should trigger migration because the metrics policy isn't in the blob.
337 device_settings_test_helper_.set_policy_blob(std::string());
338 FlushDeviceSettings();
339 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
341 // Verify that migration has kicked in.
342 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
343 ASSERT_TRUE(saved_value);
344 bool bool_value;
345 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
346 EXPECT_FALSE(bool_value);
349 TEST_F(DeviceSettingsProviderTest, LegacyDeviceLocalAccounts) {
350 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
351 em::DeviceLocalAccountInfoProto* account =
352 device_policy_.payload().mutable_device_local_accounts()->add_account();
353 account->set_deprecated_public_session_id(
354 policy::PolicyBuilder::kFakeUsername);
355 device_policy_.Build();
356 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
357 ReloadDeviceSettings();
358 Mock::VerifyAndClearExpectations(this);
360 // On load, the deprecated spec should have been converted to the new format.
361 base::ListValue expected_accounts;
362 scoped_ptr<base::DictionaryValue> entry_dict(new base::DictionaryValue());
363 entry_dict->SetString(kAccountsPrefDeviceLocalAccountsKeyId,
364 policy::PolicyBuilder::kFakeUsername);
365 entry_dict->SetInteger(kAccountsPrefDeviceLocalAccountsKeyType,
366 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION);
367 expected_accounts.Append(entry_dict.release());
368 const base::Value* actual_accounts =
369 provider_->Get(kAccountsPrefDeviceLocalAccounts);
370 EXPECT_TRUE(base::Value::Equals(&expected_accounts, actual_accounts));
373 TEST_F(DeviceSettingsProviderTest, OwnerIsStillSetWhenDeviceIsConsumerManaged) {
374 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
375 InitOwner(device_policy_.policy_data().username(), true);
376 device_policy_.policy_data().set_management_mode(
377 em::PolicyData::CONSUMER_MANAGED);
378 device_policy_.policy_data().set_request_token("test request token");
379 device_policy_.Build();
380 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
381 FlushDeviceSettings();
383 // Expect that kDeviceOwner is not empty.
384 const base::Value* value = provider_->Get(kDeviceOwner);
385 ASSERT_TRUE(value);
386 std::string string_value;
387 EXPECT_TRUE(value->GetAsString(&string_value));
388 EXPECT_FALSE(string_value.empty());
391 TEST_F(DeviceSettingsProviderTest, DecodeDeviceState) {
392 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
393 device_policy_.policy_data().mutable_device_state()->set_device_mode(
394 em::DeviceState::DEVICE_MODE_DISABLED);
395 device_policy_.policy_data().mutable_device_state()->
396 mutable_disabled_state()->set_message(kDisabledMessage);
397 device_policy_.Build();
398 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
399 ReloadDeviceSettings();
400 Mock::VerifyAndClearExpectations(this);
402 // Verify that the device state has been decoded correctly.
403 const base::FundamentalValue expected_disabled_value(true);
404 EXPECT_TRUE(base::Value::Equals(provider_->Get(kDeviceDisabled),
405 &expected_disabled_value));
406 const base::StringValue expected_disabled_message_value(kDisabledMessage);
407 EXPECT_TRUE(base::Value::Equals(provider_->Get(kDeviceDisabledMessage),
408 &expected_disabled_message_value));
410 // Verify that a change to the device state triggers a notification.
411 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
412 device_policy_.policy_data().mutable_device_state()->clear_device_mode();
413 device_policy_.Build();
414 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
415 ReloadDeviceSettings();
416 Mock::VerifyAndClearExpectations(this);
418 // Verify that the updated state has been decoded correctly.
419 EXPECT_FALSE(provider_->Get(kDeviceDisabled));
422 TEST_F(DeviceSettingsProviderTest, DecodeReportingSettings) {
423 // Turn on all reporting and verify that the reporting settings have been
424 // decoded correctly.
425 const int status_frequency = 50000;
426 SetReportingSettings(true, status_frequency);
427 VerifyReportingSettings(true, status_frequency);
429 // Turn off all reporting and verify that the settings are decoded
430 // correctly.
431 SetReportingSettings(false, status_frequency);
432 VerifyReportingSettings(false, status_frequency);
435 TEST_F(DeviceSettingsProviderTest, DecodeHeartbeatSettings) {
436 // Turn on heartbeats and verify that the heartbeat settings have been
437 // decoded correctly.
438 const int heartbeat_frequency = 50000;
439 SetHeartbeatSettings(true, heartbeat_frequency);
440 VerifyHeartbeatSettings(true, heartbeat_frequency);
442 // Turn off all reporting and verify that the settings are decoded
443 // correctly.
444 SetHeartbeatSettings(false, heartbeat_frequency);
445 VerifyHeartbeatSettings(false, heartbeat_frequency);
448 } // namespace chromeos