Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_provider_unittest.cc
blob5451f0d1fae7f3ebfc930befc47fc300364164f1
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 // Helper routine to set LoginScreenDomainAutoComplete policy.
146 void SetDomainAutoComplete(const std::string& domain) {
147 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
148 em::LoginScreenDomainAutoCompleteProto* proto =
149 device_policy_.payload().mutable_login_screen_domain_auto_complete();
150 proto->set_login_screen_domain_auto_complete(domain);
151 device_policy_.Build();
152 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
153 ReloadDeviceSettings();
154 Mock::VerifyAndClearExpectations(this);
157 // Helper routine to check value of the LoginScreenDomainAutoComplete policy.
158 void VerifyDomainAutoComplete(
159 const base::StringValue* const ptr_to_expected_value) {
160 EXPECT_TRUE(base::Value::Equals(
161 provider_->Get(kAccountsPrefLoginScreenDomainAutoComplete),
162 ptr_to_expected_value));
165 ScopedTestingLocalState local_state_;
167 scoped_ptr<DeviceSettingsProvider> provider_;
169 base::ScopedPathOverride user_data_dir_override_;
171 private:
172 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
175 TEST_F(DeviceSettingsProviderTest, InitializationTest) {
176 // Have the service load a settings blob.
177 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
178 ReloadDeviceSettings();
179 Mock::VerifyAndClearExpectations(this);
181 // Verify that the policy blob has been correctly parsed and trusted.
182 // The trusted flag should be set before the call to PrepareTrustedValues.
183 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
184 provider_->PrepareTrustedValues(base::Closure()));
185 const base::Value* value = provider_->Get(kStatsReportingPref);
186 ASSERT_TRUE(value);
187 bool bool_value;
188 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
189 EXPECT_FALSE(bool_value);
192 TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
193 // Have the service check the key.
194 owner_key_util_->Clear();
195 ReloadDeviceSettings();
197 // The trusted flag should be set before the call to PrepareTrustedValues.
198 EXPECT_EQ(CrosSettingsProvider::TRUSTED,
199 provider_->PrepareTrustedValues(base::Closure()));
200 const base::Value* value = provider_->Get(kReleaseChannel);
201 ASSERT_TRUE(value);
202 std::string string_value;
203 EXPECT_TRUE(value->GetAsString(&string_value));
204 EXPECT_TRUE(string_value.empty());
206 // Sets should succeed though and be readable from the cache.
207 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
208 EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
209 base::StringValue new_value("stable-channel");
210 provider_->Set(kReleaseChannel, new_value);
211 Mock::VerifyAndClearExpectations(this);
213 // This shouldn't trigger a write.
214 device_settings_test_helper_.set_policy_blob(std::string());
215 FlushDeviceSettings();
216 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
218 // Verify the change has been applied.
219 const base::Value* saved_value = provider_->Get(kReleaseChannel);
220 ASSERT_TRUE(saved_value);
221 EXPECT_TRUE(saved_value->GetAsString(&string_value));
222 ASSERT_EQ("stable-channel", string_value);
225 TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
226 // If we are not the owner no sets should work.
227 base::FundamentalValue value(true);
228 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
229 provider_->Set(kStatsReportingPref, value);
230 Mock::VerifyAndClearExpectations(this);
232 // This shouldn't trigger a write.
233 device_settings_test_helper_.set_policy_blob(std::string());
234 FlushDeviceSettings();
235 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
237 // Verify the change has not been applied.
238 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
239 ASSERT_TRUE(saved_value);
240 bool bool_value;
241 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
242 EXPECT_FALSE(bool_value);
245 TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
246 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
247 InitOwner(device_policy_.policy_data().username(), true);
248 FlushDeviceSettings();
250 base::FundamentalValue value(true);
251 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
252 EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
253 provider_->Set(kStatsReportingPref, value);
254 Mock::VerifyAndClearExpectations(this);
256 // Process the store.
257 device_settings_test_helper_.set_policy_blob(std::string());
258 FlushDeviceSettings();
260 // Verify that the device policy has been adjusted.
261 ASSERT_TRUE(device_settings_service_.device_settings());
262 EXPECT_TRUE(device_settings_service_.device_settings()->
263 metrics_enabled().metrics_enabled());
265 // Verify the change has been applied.
266 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
267 ASSERT_TRUE(saved_value);
268 bool bool_value;
269 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
270 EXPECT_TRUE(bool_value);
273 TEST_F(DeviceSettingsProviderTest, SetPrefTwice) {
274 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
275 InitOwner(device_policy_.policy_data().username(), true);
276 FlushDeviceSettings();
278 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
280 base::StringValue value1("beta");
281 provider_->Set(kReleaseChannel, value1);
282 base::StringValue value2("dev");
283 provider_->Set(kReleaseChannel, value2);
285 // Let the changes propagate through the system.
286 device_settings_test_helper_.set_policy_blob(std::string());
287 FlushDeviceSettings();
289 // Verify the second change has been applied.
290 const base::Value* saved_value = provider_->Get(kReleaseChannel);
291 EXPECT_TRUE(value2.Equals(saved_value));
293 Mock::VerifyAndClearExpectations(this);
296 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) {
297 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
298 device_policy_.policy().set_policy_data_signature("bad signature");
299 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
300 ReloadDeviceSettings();
302 // Verify that the cached settings blob is not "trusted".
303 EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
304 device_settings_service_.status());
305 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
306 provider_->PrepareTrustedValues(base::Closure()));
309 TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) {
310 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
311 device_settings_test_helper_.set_policy_blob(std::string());
312 ReloadDeviceSettings();
314 // Verify that the cached settings blob is not "trusted".
315 EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
316 device_settings_service_.status());
317 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
318 provider_->PrepareTrustedValues(base::Closure()));
321 TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
322 device_settings_test_helper_.set_policy_blob(std::string());
324 EXPECT_CALL(*this, GetTrustedCallback());
325 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
326 provider_->PrepareTrustedValues(
327 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
328 base::Unretained(this))));
330 ReloadDeviceSettings();
331 Mock::VerifyAndClearExpectations(this);
333 EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
334 provider_->PrepareTrustedValues(base::Closure()));
337 TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
338 EXPECT_CALL(*this, GetTrustedCallback());
340 EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
341 provider_->PrepareTrustedValues(
342 base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
343 base::Unretained(this))));
345 ReloadDeviceSettings();
346 Mock::VerifyAndClearExpectations(this);
349 TEST_F(DeviceSettingsProviderTest, StatsReportingMigration) {
350 // Create the legacy consent file.
351 base::FilePath consent_file;
352 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &consent_file));
353 consent_file = consent_file.AppendASCII("Consent To Send Stats");
354 ASSERT_EQ(1, base::WriteFile(consent_file, "0", 1));
356 // This should trigger migration because the metrics policy isn't in the blob.
357 device_settings_test_helper_.set_policy_blob(std::string());
358 FlushDeviceSettings();
359 EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
361 // Verify that migration has kicked in.
362 const base::Value* saved_value = provider_->Get(kStatsReportingPref);
363 ASSERT_TRUE(saved_value);
364 bool bool_value;
365 EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
366 EXPECT_FALSE(bool_value);
369 TEST_F(DeviceSettingsProviderTest, LegacyDeviceLocalAccounts) {
370 EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
371 em::DeviceLocalAccountInfoProto* account =
372 device_policy_.payload().mutable_device_local_accounts()->add_account();
373 account->set_deprecated_public_session_id(
374 policy::PolicyBuilder::kFakeUsername);
375 device_policy_.Build();
376 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
377 ReloadDeviceSettings();
378 Mock::VerifyAndClearExpectations(this);
380 // On load, the deprecated spec should have been converted to the new format.
381 base::ListValue expected_accounts;
382 scoped_ptr<base::DictionaryValue> entry_dict(new base::DictionaryValue());
383 entry_dict->SetString(kAccountsPrefDeviceLocalAccountsKeyId,
384 policy::PolicyBuilder::kFakeUsername);
385 entry_dict->SetInteger(kAccountsPrefDeviceLocalAccountsKeyType,
386 policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION);
387 expected_accounts.Append(entry_dict.release());
388 const base::Value* actual_accounts =
389 provider_->Get(kAccountsPrefDeviceLocalAccounts);
390 EXPECT_TRUE(base::Value::Equals(&expected_accounts, actual_accounts));
393 TEST_F(DeviceSettingsProviderTest, OwnerIsStillSetWhenDeviceIsConsumerManaged) {
394 owner_key_util_->SetPrivateKey(device_policy_.GetSigningKey());
395 InitOwner(device_policy_.policy_data().username(), true);
396 device_policy_.policy_data().set_management_mode(
397 em::PolicyData::CONSUMER_MANAGED);
398 device_policy_.policy_data().set_request_token("test request token");
399 device_policy_.Build();
400 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
401 FlushDeviceSettings();
403 // Expect that kDeviceOwner is not empty.
404 const base::Value* value = provider_->Get(kDeviceOwner);
405 ASSERT_TRUE(value);
406 std::string string_value;
407 EXPECT_TRUE(value->GetAsString(&string_value));
408 EXPECT_FALSE(string_value.empty());
411 TEST_F(DeviceSettingsProviderTest, DecodeDeviceState) {
412 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
413 device_policy_.policy_data().mutable_device_state()->set_device_mode(
414 em::DeviceState::DEVICE_MODE_DISABLED);
415 device_policy_.policy_data().mutable_device_state()->
416 mutable_disabled_state()->set_message(kDisabledMessage);
417 device_policy_.Build();
418 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
419 ReloadDeviceSettings();
420 Mock::VerifyAndClearExpectations(this);
422 // Verify that the device state has been decoded correctly.
423 const base::FundamentalValue expected_disabled_value(true);
424 EXPECT_TRUE(base::Value::Equals(provider_->Get(kDeviceDisabled),
425 &expected_disabled_value));
426 const base::StringValue expected_disabled_message_value(kDisabledMessage);
427 EXPECT_TRUE(base::Value::Equals(provider_->Get(kDeviceDisabledMessage),
428 &expected_disabled_message_value));
430 // Verify that a change to the device state triggers a notification.
431 EXPECT_CALL(*this, SettingChanged(_)).Times(AtLeast(1));
432 device_policy_.policy_data().mutable_device_state()->clear_device_mode();
433 device_policy_.Build();
434 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
435 ReloadDeviceSettings();
436 Mock::VerifyAndClearExpectations(this);
438 // Verify that the updated state has been decoded correctly.
439 EXPECT_FALSE(provider_->Get(kDeviceDisabled));
442 TEST_F(DeviceSettingsProviderTest, DecodeReportingSettings) {
443 // Turn on all reporting and verify that the reporting settings have been
444 // decoded correctly.
445 const int status_frequency = 50000;
446 SetReportingSettings(true, status_frequency);
447 VerifyReportingSettings(true, status_frequency);
449 // Turn off all reporting and verify that the settings are decoded
450 // correctly.
451 SetReportingSettings(false, status_frequency);
452 VerifyReportingSettings(false, status_frequency);
455 TEST_F(DeviceSettingsProviderTest, DecodeHeartbeatSettings) {
456 // Turn on heartbeats and verify that the heartbeat settings have been
457 // decoded correctly.
458 const int heartbeat_frequency = 50000;
459 SetHeartbeatSettings(true, heartbeat_frequency);
460 VerifyHeartbeatSettings(true, heartbeat_frequency);
462 // Turn off all reporting and verify that the settings are decoded
463 // correctly.
464 SetHeartbeatSettings(false, heartbeat_frequency);
465 VerifyHeartbeatSettings(false, heartbeat_frequency);
468 TEST_F(DeviceSettingsProviderTest, DecodeDomainAutoComplete) {
469 // By default LoginScreenDomainAutoComplete policy should not be set.
470 VerifyDomainAutoComplete(nullptr);
472 // Empty string means that the policy is not set.
473 SetDomainAutoComplete("");
474 VerifyDomainAutoComplete(nullptr);
476 // Check some meaningful value. Policy should be set.
477 const std::string domain = "domain.test";
478 const base::StringValue domain_value(domain);
479 SetDomainAutoComplete(domain);
480 VerifyDomainAutoComplete(&domain_value);
483 } // namespace chromeos