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"
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
;
34 using ::testing::AtLeast
;
35 using ::testing::AnyNumber
;
36 using ::testing::Mock
;
41 const char kDisabledMessage
[] = "This device has been disabled.";
45 class DeviceSettingsProviderTest
: public DeviceSettingsTestBase
{
47 MOCK_METHOD1(SettingChanged
, void(const std::string
&));
48 MOCK_METHOD0(GetTrustedCallback
, void(void));
51 DeviceSettingsProviderTest()
52 : local_state_(TestingBrowserProcess::GetGlobal()),
53 user_data_dir_override_(chrome::DIR_USER_DATA
) {}
55 void SetUp() override
{
56 DeviceSettingsTestBase::SetUp();
58 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
60 new DeviceSettingsProvider(
61 base::Bind(&DeviceSettingsProviderTest::SettingChanged
,
62 base::Unretained(this)),
63 &device_settings_service_
));
64 Mock::VerifyAndClearExpectations(this);
67 void TearDown() override
{ DeviceSettingsTestBase::TearDown(); }
69 // Helper routine to enable/disable all reporting settings in policy.
70 void SetReportingSettings(bool enable_reporting
, int frequency
) {
71 EXPECT_CALL(*this, SettingChanged(_
)).Times(AtLeast(1));
72 em::DeviceReportingProto
* proto
=
73 device_policy_
.payload().mutable_device_reporting();
74 proto
->set_report_version_info(enable_reporting
);
75 proto
->set_report_activity_times(enable_reporting
);
76 proto
->set_report_boot_mode(enable_reporting
);
77 proto
->set_report_location(enable_reporting
);
78 proto
->set_report_network_interfaces(enable_reporting
);
79 proto
->set_report_users(enable_reporting
);
80 proto
->set_report_hardware_status(enable_reporting
);
81 proto
->set_report_session_status(enable_reporting
);
82 proto
->set_device_status_frequency(frequency
);
83 device_policy_
.Build();
84 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
85 ReloadDeviceSettings();
86 Mock::VerifyAndClearExpectations(this);
89 // Helper routine to enable/disable all reporting settings in policy.
90 void SetHeartbeatSettings(bool enable_heartbeat
, int frequency
) {
91 EXPECT_CALL(*this, SettingChanged(_
)).Times(AtLeast(1));
92 em::DeviceHeartbeatSettingsProto
* proto
=
93 device_policy_
.payload().mutable_device_heartbeat_settings();
94 proto
->set_heartbeat_enabled(enable_heartbeat
);
95 proto
->set_heartbeat_frequency(frequency
);
96 device_policy_
.Build();
97 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
98 ReloadDeviceSettings();
99 Mock::VerifyAndClearExpectations(this);
102 // Helper routine to ensure all heartbeat policies have been correctly
104 void VerifyHeartbeatSettings(bool expected_enable_state
,
105 int expected_frequency
) {
107 const base::FundamentalValue
expected_enabled_value(expected_enable_state
);
108 EXPECT_TRUE(base::Value::Equals(provider_
->Get(kHeartbeatEnabled
),
109 &expected_enabled_value
));
111 const base::FundamentalValue
expected_frequency_value(expected_frequency
);
112 EXPECT_TRUE(base::Value::Equals(provider_
->Get(kHeartbeatFrequency
),
113 &expected_frequency_value
));
116 // Helper routine to ensure all reporting policies have been correctly
118 void VerifyReportingSettings(bool expected_enable_state
,
119 int expected_frequency
) {
120 const char* reporting_settings
[] = {
121 kReportDeviceVersionInfo
,
122 kReportDeviceActivityTimes
,
123 kReportDeviceBootMode
,
124 // Device location reporting is not currently supported.
125 // kReportDeviceLocation,
126 kReportDeviceNetworkInterfaces
,
128 kReportDeviceHardwareStatus
,
129 kReportDeviceSessionStatus
132 const base::FundamentalValue
expected_enable_value(expected_enable_state
);
133 for (const auto& setting
: reporting_settings
) {
134 EXPECT_TRUE(base::Value::Equals(provider_
->Get(setting
),
135 &expected_enable_value
))
136 << "Value for " << setting
<< " does not match expected";
138 const base::FundamentalValue
expected_frequency_value(expected_frequency
);
139 EXPECT_TRUE(base::Value::Equals(provider_
->Get(kReportUploadFrequency
),
140 &expected_frequency_value
));
143 // Helper routine to set LoginScreenDomainAutoComplete policy.
144 void SetDomainAutoComplete(const std::string
& domain
) {
145 EXPECT_CALL(*this, SettingChanged(_
)).Times(AtLeast(1));
146 em::LoginScreenDomainAutoCompleteProto
* proto
=
147 device_policy_
.payload().mutable_login_screen_domain_auto_complete();
148 proto
->set_login_screen_domain_auto_complete(domain
);
149 device_policy_
.Build();
150 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
151 ReloadDeviceSettings();
152 Mock::VerifyAndClearExpectations(this);
155 // Helper routine to check value of the LoginScreenDomainAutoComplete policy.
156 void VerifyDomainAutoComplete(
157 const base::StringValue
* const ptr_to_expected_value
) {
158 EXPECT_TRUE(base::Value::Equals(
159 provider_
->Get(kAccountsPrefLoginScreenDomainAutoComplete
),
160 ptr_to_expected_value
));
163 ScopedTestingLocalState local_state_
;
165 scoped_ptr
<DeviceSettingsProvider
> provider_
;
167 base::ScopedPathOverride user_data_dir_override_
;
170 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest
);
173 TEST_F(DeviceSettingsProviderTest
, InitializationTest
) {
174 // Have the service load a settings blob.
175 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
176 ReloadDeviceSettings();
177 Mock::VerifyAndClearExpectations(this);
179 // Verify that the policy blob has been correctly parsed and trusted.
180 // The trusted flag should be set before the call to PrepareTrustedValues.
181 EXPECT_EQ(CrosSettingsProvider::TRUSTED
,
182 provider_
->PrepareTrustedValues(base::Closure()));
183 const base::Value
* value
= provider_
->Get(kStatsReportingPref
);
186 EXPECT_TRUE(value
->GetAsBoolean(&bool_value
));
187 EXPECT_FALSE(bool_value
);
190 TEST_F(DeviceSettingsProviderTest
, InitializationTestUnowned
) {
191 // Have the service check the key.
192 owner_key_util_
->Clear();
193 ReloadDeviceSettings();
195 // The trusted flag should be set before the call to PrepareTrustedValues.
196 EXPECT_EQ(CrosSettingsProvider::TRUSTED
,
197 provider_
->PrepareTrustedValues(base::Closure()));
198 const base::Value
* value
= provider_
->Get(kReleaseChannel
);
200 std::string string_value
;
201 EXPECT_TRUE(value
->GetAsString(&string_value
));
202 EXPECT_TRUE(string_value
.empty());
204 // Sets should succeed though and be readable from the cache.
205 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
206 EXPECT_CALL(*this, SettingChanged(kReleaseChannel
)).Times(1);
207 base::StringValue
new_value("stable-channel");
208 provider_
->Set(kReleaseChannel
, new_value
);
209 Mock::VerifyAndClearExpectations(this);
211 // This shouldn't trigger a write.
212 device_settings_test_helper_
.set_policy_blob(std::string());
213 FlushDeviceSettings();
214 EXPECT_EQ(std::string(), device_settings_test_helper_
.policy_blob());
216 // Verify the change has been applied.
217 const base::Value
* saved_value
= provider_
->Get(kReleaseChannel
);
218 ASSERT_TRUE(saved_value
);
219 EXPECT_TRUE(saved_value
->GetAsString(&string_value
));
220 ASSERT_EQ("stable-channel", string_value
);
223 TEST_F(DeviceSettingsProviderTest
, SetPrefFailed
) {
224 // If we are not the owner no sets should work.
225 base::FundamentalValue
value(true);
226 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref
)).Times(1);
227 provider_
->Set(kStatsReportingPref
, value
);
228 Mock::VerifyAndClearExpectations(this);
230 // This shouldn't trigger a write.
231 device_settings_test_helper_
.set_policy_blob(std::string());
232 FlushDeviceSettings();
233 EXPECT_EQ(std::string(), device_settings_test_helper_
.policy_blob());
235 // Verify the change has not been applied.
236 const base::Value
* saved_value
= provider_
->Get(kStatsReportingPref
);
237 ASSERT_TRUE(saved_value
);
239 EXPECT_TRUE(saved_value
->GetAsBoolean(&bool_value
));
240 EXPECT_FALSE(bool_value
);
243 TEST_F(DeviceSettingsProviderTest
, SetPrefSucceed
) {
244 owner_key_util_
->SetPrivateKey(device_policy_
.GetSigningKey());
245 InitOwner(device_policy_
.policy_data().username(), true);
246 FlushDeviceSettings();
248 base::FundamentalValue
value(true);
249 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
250 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref
)).Times(1);
251 provider_
->Set(kStatsReportingPref
, value
);
252 Mock::VerifyAndClearExpectations(this);
254 // Process the store.
255 device_settings_test_helper_
.set_policy_blob(std::string());
256 FlushDeviceSettings();
258 // Verify that the device policy has been adjusted.
259 ASSERT_TRUE(device_settings_service_
.device_settings());
260 EXPECT_TRUE(device_settings_service_
.device_settings()->
261 metrics_enabled().metrics_enabled());
263 // Verify the change has been applied.
264 const base::Value
* saved_value
= provider_
->Get(kStatsReportingPref
);
265 ASSERT_TRUE(saved_value
);
267 EXPECT_TRUE(saved_value
->GetAsBoolean(&bool_value
));
268 EXPECT_TRUE(bool_value
);
271 TEST_F(DeviceSettingsProviderTest
, SetPrefTwice
) {
272 owner_key_util_
->SetPrivateKey(device_policy_
.GetSigningKey());
273 InitOwner(device_policy_
.policy_data().username(), true);
274 FlushDeviceSettings();
276 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
278 base::StringValue
value1("beta");
279 provider_
->Set(kReleaseChannel
, value1
);
280 base::StringValue
value2("dev");
281 provider_
->Set(kReleaseChannel
, value2
);
283 // Let the changes propagate through the system.
284 device_settings_test_helper_
.set_policy_blob(std::string());
285 FlushDeviceSettings();
287 // Verify the second change has been applied.
288 const base::Value
* saved_value
= provider_
->Get(kReleaseChannel
);
289 EXPECT_TRUE(value2
.Equals(saved_value
));
291 Mock::VerifyAndClearExpectations(this);
294 TEST_F(DeviceSettingsProviderTest
, PolicyRetrievalFailedBadSignature
) {
295 owner_key_util_
->SetPublicKeyFromPrivateKey(*device_policy_
.GetSigningKey());
296 device_policy_
.policy().set_policy_data_signature("bad signature");
297 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
298 ReloadDeviceSettings();
300 // Verify that the cached settings blob is not "trusted".
301 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR
,
302 device_settings_service_
.status());
303 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED
,
304 provider_
->PrepareTrustedValues(base::Closure()));
307 TEST_F(DeviceSettingsProviderTest
, PolicyRetrievalNoPolicy
) {
308 owner_key_util_
->SetPublicKeyFromPrivateKey(*device_policy_
.GetSigningKey());
309 device_settings_test_helper_
.set_policy_blob(std::string());
310 ReloadDeviceSettings();
312 // Verify that the cached settings blob is not "trusted".
313 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY
,
314 device_settings_service_
.status());
315 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED
,
316 provider_
->PrepareTrustedValues(base::Closure()));
319 TEST_F(DeviceSettingsProviderTest
, PolicyFailedPermanentlyNotification
) {
320 device_settings_test_helper_
.set_policy_blob(std::string());
322 EXPECT_CALL(*this, GetTrustedCallback());
323 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED
,
324 provider_
->PrepareTrustedValues(
325 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback
,
326 base::Unretained(this))));
328 ReloadDeviceSettings();
329 Mock::VerifyAndClearExpectations(this);
331 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED
,
332 provider_
->PrepareTrustedValues(base::Closure()));
335 TEST_F(DeviceSettingsProviderTest
, PolicyLoadNotification
) {
336 EXPECT_CALL(*this, GetTrustedCallback());
338 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED
,
339 provider_
->PrepareTrustedValues(
340 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback
,
341 base::Unretained(this))));
343 ReloadDeviceSettings();
344 Mock::VerifyAndClearExpectations(this);
347 TEST_F(DeviceSettingsProviderTest
, StatsReportingMigration
) {
348 // Create the legacy consent file.
349 base::FilePath consent_file
;
350 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA
, &consent_file
));
351 consent_file
= consent_file
.AppendASCII("Consent To Send Stats");
352 ASSERT_EQ(1, base::WriteFile(consent_file
, "0", 1));
354 // This should trigger migration because the metrics policy isn't in the blob.
355 device_settings_test_helper_
.set_policy_blob(std::string());
356 FlushDeviceSettings();
357 EXPECT_EQ(std::string(), device_settings_test_helper_
.policy_blob());
359 // Verify that migration has kicked in.
360 const base::Value
* saved_value
= provider_
->Get(kStatsReportingPref
);
361 ASSERT_TRUE(saved_value
);
363 EXPECT_TRUE(saved_value
->GetAsBoolean(&bool_value
));
364 EXPECT_FALSE(bool_value
);
367 TEST_F(DeviceSettingsProviderTest
, LegacyDeviceLocalAccounts
) {
368 EXPECT_CALL(*this, SettingChanged(_
)).Times(AnyNumber());
369 em::DeviceLocalAccountInfoProto
* account
=
370 device_policy_
.payload().mutable_device_local_accounts()->add_account();
371 account
->set_deprecated_public_session_id(
372 policy::PolicyBuilder::kFakeUsername
);
373 device_policy_
.Build();
374 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
375 ReloadDeviceSettings();
376 Mock::VerifyAndClearExpectations(this);
378 // On load, the deprecated spec should have been converted to the new format.
379 base::ListValue expected_accounts
;
380 scoped_ptr
<base::DictionaryValue
> entry_dict(new base::DictionaryValue());
381 entry_dict
->SetString(kAccountsPrefDeviceLocalAccountsKeyId
,
382 policy::PolicyBuilder::kFakeUsername
);
383 entry_dict
->SetInteger(kAccountsPrefDeviceLocalAccountsKeyType
,
384 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
);
385 expected_accounts
.Append(entry_dict
.release());
386 const base::Value
* actual_accounts
=
387 provider_
->Get(kAccountsPrefDeviceLocalAccounts
);
388 EXPECT_TRUE(base::Value::Equals(&expected_accounts
, actual_accounts
));
391 TEST_F(DeviceSettingsProviderTest
, OwnerIsStillSetWhenDeviceIsConsumerManaged
) {
392 owner_key_util_
->SetPrivateKey(device_policy_
.GetSigningKey());
393 InitOwner(device_policy_
.policy_data().username(), true);
394 device_policy_
.policy_data().set_management_mode(
395 em::PolicyData::CONSUMER_MANAGED
);
396 device_policy_
.policy_data().set_request_token("test request token");
397 device_policy_
.Build();
398 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
399 FlushDeviceSettings();
401 // Expect that kDeviceOwner is not empty.
402 const base::Value
* value
= provider_
->Get(kDeviceOwner
);
404 std::string string_value
;
405 EXPECT_TRUE(value
->GetAsString(&string_value
));
406 EXPECT_FALSE(string_value
.empty());
409 TEST_F(DeviceSettingsProviderTest
, DecodeDeviceState
) {
410 EXPECT_CALL(*this, SettingChanged(_
)).Times(AtLeast(1));
411 device_policy_
.policy_data().mutable_device_state()->set_device_mode(
412 em::DeviceState::DEVICE_MODE_DISABLED
);
413 device_policy_
.policy_data().mutable_device_state()->
414 mutable_disabled_state()->set_message(kDisabledMessage
);
415 device_policy_
.Build();
416 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
417 ReloadDeviceSettings();
418 Mock::VerifyAndClearExpectations(this);
420 // Verify that the device state has been decoded correctly.
421 const base::FundamentalValue
expected_disabled_value(true);
422 EXPECT_TRUE(base::Value::Equals(provider_
->Get(kDeviceDisabled
),
423 &expected_disabled_value
));
424 const base::StringValue
expected_disabled_message_value(kDisabledMessage
);
425 EXPECT_TRUE(base::Value::Equals(provider_
->Get(kDeviceDisabledMessage
),
426 &expected_disabled_message_value
));
428 // Verify that a change to the device state triggers a notification.
429 EXPECT_CALL(*this, SettingChanged(_
)).Times(AtLeast(1));
430 device_policy_
.policy_data().mutable_device_state()->clear_device_mode();
431 device_policy_
.Build();
432 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
433 ReloadDeviceSettings();
434 Mock::VerifyAndClearExpectations(this);
436 // Verify that the updated state has been decoded correctly.
437 EXPECT_FALSE(provider_
->Get(kDeviceDisabled
));
440 TEST_F(DeviceSettingsProviderTest
, DecodeReportingSettings
) {
441 // Turn on all reporting and verify that the reporting settings have been
442 // decoded correctly.
443 const int status_frequency
= 50000;
444 SetReportingSettings(true, status_frequency
);
445 VerifyReportingSettings(true, status_frequency
);
447 // Turn off all reporting and verify that the settings are decoded
449 SetReportingSettings(false, status_frequency
);
450 VerifyReportingSettings(false, status_frequency
);
453 TEST_F(DeviceSettingsProviderTest
, DecodeHeartbeatSettings
) {
454 // Turn on heartbeats and verify that the heartbeat settings have been
455 // decoded correctly.
456 const int heartbeat_frequency
= 50000;
457 SetHeartbeatSettings(true, heartbeat_frequency
);
458 VerifyHeartbeatSettings(true, heartbeat_frequency
);
460 // Turn off all reporting and verify that the settings are decoded
462 SetHeartbeatSettings(false, heartbeat_frequency
);
463 VerifyHeartbeatSettings(false, heartbeat_frequency
);
466 TEST_F(DeviceSettingsProviderTest
, DecodeDomainAutoComplete
) {
467 // By default LoginScreenDomainAutoComplete policy should not be set.
468 VerifyDomainAutoComplete(nullptr);
470 // Empty string means that the policy is not set.
471 SetDomainAutoComplete("");
472 VerifyDomainAutoComplete(nullptr);
474 // Check some meaningful value. Policy should be set.
475 const std::string domain
= "domain.test";
476 const base::StringValue
domain_value(domain
);
477 SetDomainAutoComplete(domain
);
478 VerifyDomainAutoComplete(&domain_value
);
481 } // namespace chromeos