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