device_settings_provider_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/chromeos/settings/device_settings_provider.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "base/callback.h"
11#include "base/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/cros/cros_library.h"
16#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
17#include "chrome/browser/chromeos/settings/cros_settings_names.h"
18#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
19#include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h"
20#include "chrome/common/chrome_paths.h"
21#include "chrome/test/base/scoped_testing_local_state.h"
22#include "chrome/test/base/testing_browser_process.h"
23#include "testing/gmock/include/gmock/gmock.h"
24#include "testing/gtest/include/gtest/gtest.h"
25
26namespace em = enterprise_management;
27
28namespace chromeos {
29
30using ::testing::AnyNumber;
31using ::testing::Mock;
32using ::testing::_;
33
34class DeviceSettingsProviderTest : public DeviceSettingsTestBase {
35 public:
36  MOCK_METHOD1(SettingChanged, void(const std::string&));
37  MOCK_METHOD0(GetTrustedCallback, void(void));
38
39 protected:
40  DeviceSettingsProviderTest()
41      : local_state_(TestingBrowserProcess::GetGlobal()),
42        user_data_dir_override_(chrome::DIR_USER_DATA) {}
43
44  virtual void SetUp() OVERRIDE {
45    DeviceSettingsTestBase::SetUp();
46
47    EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
48    provider_.reset(
49        new DeviceSettingsProvider(
50            base::Bind(&DeviceSettingsProviderTest::SettingChanged,
51                       base::Unretained(this)),
52            &device_settings_service_));
53    Mock::VerifyAndClearExpectations(this);
54  }
55
56  virtual void TearDown() OVERRIDE {
57    DeviceSettingsTestBase::TearDown();
58  }
59
60  ScopedStubCrosEnabler stub_cros_enabler_;
61
62  ScopedTestingLocalState local_state_;
63
64  scoped_ptr<DeviceSettingsProvider> provider_;
65
66  base::ScopedPathOverride user_data_dir_override_;
67
68 private:
69  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsProviderTest);
70};
71
72TEST_F(DeviceSettingsProviderTest, InitializationTest) {
73  // Have the service load a settings blob.
74  EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
75  ReloadDeviceSettings();
76  Mock::VerifyAndClearExpectations(this);
77
78  // Verify that the policy blob has been correctly parsed and trusted.
79  // The trusted flag should be set before the call to PrepareTrustedValues.
80  EXPECT_EQ(CrosSettingsProvider::TRUSTED,
81            provider_->PrepareTrustedValues(base::Closure()));
82  const base::Value* value = provider_->Get(kStatsReportingPref);
83  ASSERT_TRUE(value);
84  bool bool_value;
85  EXPECT_TRUE(value->GetAsBoolean(&bool_value));
86  EXPECT_FALSE(bool_value);
87}
88
89TEST_F(DeviceSettingsProviderTest, InitializationTestUnowned) {
90  // Have the service check the key.
91  owner_key_util_->Clear();
92  ReloadDeviceSettings();
93
94  // The trusted flag should be set before the call to PrepareTrustedValues.
95  EXPECT_EQ(CrosSettingsProvider::TRUSTED,
96            provider_->PrepareTrustedValues(base::Closure()));
97  const base::Value* value = provider_->Get(kReleaseChannel);
98  ASSERT_TRUE(value);
99  std::string string_value;
100  EXPECT_TRUE(value->GetAsString(&string_value));
101  EXPECT_TRUE(string_value.empty());
102
103  // Sets should succeed though and be readable from the cache.
104  EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
105  EXPECT_CALL(*this, SettingChanged(kReleaseChannel)).Times(1);
106  base::StringValue new_value("stable-channel");
107  provider_->Set(kReleaseChannel, new_value);
108  Mock::VerifyAndClearExpectations(this);
109
110  // This shouldn't trigger a write.
111  device_settings_test_helper_.set_policy_blob(std::string());
112  FlushDeviceSettings();
113  EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
114
115  // Verify the change has been applied.
116  const base::Value* saved_value = provider_->Get(kReleaseChannel);
117  ASSERT_TRUE(saved_value);
118  EXPECT_TRUE(saved_value->GetAsString(&string_value));
119  ASSERT_EQ("stable-channel", string_value);
120}
121
122TEST_F(DeviceSettingsProviderTest, SetPrefFailed) {
123  // If we are not the owner no sets should work.
124  base::FundamentalValue value(true);
125  EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
126  provider_->Set(kStatsReportingPref, value);
127  Mock::VerifyAndClearExpectations(this);
128
129  // This shouldn't trigger a write.
130  device_settings_test_helper_.set_policy_blob(std::string());
131  FlushDeviceSettings();
132  EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
133
134  // Verify the change has not been applied.
135  const base::Value* saved_value = provider_->Get(kStatsReportingPref);
136  ASSERT_TRUE(saved_value);
137  bool bool_value;
138  EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
139  EXPECT_FALSE(bool_value);
140}
141
142TEST_F(DeviceSettingsProviderTest, SetPrefSucceed) {
143  owner_key_util_->SetPrivateKey(device_policy_.signing_key());
144  device_settings_service_.SetUsername(device_policy_.policy_data().username());
145  FlushDeviceSettings();
146
147  base::FundamentalValue value(true);
148  EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
149  EXPECT_CALL(*this, SettingChanged(kStatsReportingPref)).Times(1);
150  provider_->Set(kStatsReportingPref, value);
151  Mock::VerifyAndClearExpectations(this);
152
153  // Process the store.
154  device_settings_test_helper_.set_policy_blob(std::string());
155  FlushDeviceSettings();
156
157  // Verify that the device policy has been adjusted.
158  ASSERT_TRUE(device_settings_service_.device_settings());
159  EXPECT_TRUE(device_settings_service_.device_settings()->
160                  metrics_enabled().metrics_enabled());
161
162  // Verify the change has been applied.
163  const base::Value* saved_value = provider_->Get(kStatsReportingPref);
164  ASSERT_TRUE(saved_value);
165  bool bool_value;
166  EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
167  EXPECT_TRUE(bool_value);
168}
169
170TEST_F(DeviceSettingsProviderTest, SetPrefTwice) {
171  owner_key_util_->SetPrivateKey(device_policy_.signing_key());
172  device_settings_service_.SetUsername(device_policy_.policy_data().username());
173  FlushDeviceSettings();
174
175  EXPECT_CALL(*this, SettingChanged(_)).Times(AnyNumber());
176
177  base::StringValue value1("beta");
178  provider_->Set(kReleaseChannel, value1);
179  base::StringValue value2("dev");
180  provider_->Set(kReleaseChannel, value2);
181
182  // Let the changes propagate through the system.
183  device_settings_test_helper_.set_policy_blob(std::string());
184  FlushDeviceSettings();
185
186  // Verify the second change has been applied.
187  const base::Value* saved_value = provider_->Get(kReleaseChannel);
188  EXPECT_TRUE(value2.Equals(saved_value));
189
190  Mock::VerifyAndClearExpectations(this);
191}
192
193TEST_F(DeviceSettingsProviderTest, PolicyRetrievalFailedBadSignature) {
194  owner_key_util_->SetPublicKeyFromPrivateKey(device_policy_.signing_key());
195  device_policy_.policy().set_policy_data_signature("bad signature");
196  device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
197  ReloadDeviceSettings();
198
199  // Verify that the cached settings blob is not "trusted".
200  EXPECT_EQ(DeviceSettingsService::STORE_VALIDATION_ERROR,
201            device_settings_service_.status());
202  EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
203            provider_->PrepareTrustedValues(base::Closure()));
204}
205
206TEST_F(DeviceSettingsProviderTest, PolicyRetrievalNoPolicy) {
207  owner_key_util_->SetPublicKeyFromPrivateKey(device_policy_.signing_key());
208  device_settings_test_helper_.set_policy_blob(std::string());
209  ReloadDeviceSettings();
210
211  // Verify that the cached settings blob is not "trusted".
212  EXPECT_EQ(DeviceSettingsService::STORE_NO_POLICY,
213            device_settings_service_.status());
214  EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
215            provider_->PrepareTrustedValues(base::Closure()));
216}
217
218TEST_F(DeviceSettingsProviderTest, PolicyFailedPermanentlyNotification) {
219  device_settings_test_helper_.set_policy_blob(std::string());
220
221  EXPECT_CALL(*this, GetTrustedCallback());
222  EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
223            provider_->PrepareTrustedValues(
224                base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
225                           base::Unretained(this))));
226
227  ReloadDeviceSettings();
228  Mock::VerifyAndClearExpectations(this);
229
230  EXPECT_EQ(CrosSettingsProvider::PERMANENTLY_UNTRUSTED,
231            provider_->PrepareTrustedValues(base::Closure()));
232}
233
234TEST_F(DeviceSettingsProviderTest, PolicyLoadNotification) {
235  EXPECT_CALL(*this, GetTrustedCallback());
236
237  EXPECT_EQ(CrosSettingsProvider::TEMPORARILY_UNTRUSTED,
238            provider_->PrepareTrustedValues(
239                base::Bind(&DeviceSettingsProviderTest::GetTrustedCallback,
240                           base::Unretained(this))));
241
242  ReloadDeviceSettings();
243  Mock::VerifyAndClearExpectations(this);
244}
245
246TEST_F(DeviceSettingsProviderTest, StatsReportingMigration) {
247  // Create the legacy consent file.
248  base::FilePath consent_file;
249  ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &consent_file));
250  consent_file = consent_file.AppendASCII("Consent To Send Stats");
251  ASSERT_EQ(1, file_util::WriteFile(consent_file, "0", 1));
252
253  // This should trigger migration because the metrics policy isn't in the blob.
254  device_settings_test_helper_.set_policy_blob(std::string());
255  FlushDeviceSettings();
256  EXPECT_EQ(std::string(), device_settings_test_helper_.policy_blob());
257
258  // Verify that migration has kicked in.
259  const base::Value* saved_value = provider_->Get(kStatsReportingPref);
260  ASSERT_TRUE(saved_value);
261  bool bool_value;
262  EXPECT_TRUE(saved_value->GetAsBoolean(&bool_value));
263  EXPECT_FALSE(bool_value);
264}
265
266} // namespace chromeos
267