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
292
|
// Copyright 2014 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/prefs/profile_pref_store_manager.h"
#include <vector>
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/json_pref_store.h"
#include "base/prefs/persistent_pref_store.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_store.h"
#include "base/prefs/testing_pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_hash_filter.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class FirstEqualsPredicate {
public:
explicit FirstEqualsPredicate(const std::string& expected)
: expected_(expected) {}
bool operator()(const std::pair<std::string, base::Value*>& pair) {
return pair.first == expected_;
}
private:
const std::string expected_;
};
// Observes changes to the PrefStore and verifies that only registered prefs are
// written.
class RegistryVerifier : public PrefStore::Observer {
public:
explicit RegistryVerifier(PrefRegistry* pref_registry)
: pref_registry_(pref_registry) {}
// PrefStore::Observer implementation
virtual void OnPrefValueChanged(const std::string& key) OVERRIDE {
EXPECT_TRUE(pref_registry_->end() !=
std::find_if(pref_registry_->begin(),
pref_registry_->end(),
FirstEqualsPredicate(key)))
<< "Unregistered key " << key << " was changed.";
}
virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {}
private:
scoped_refptr<PrefRegistry> pref_registry_;
};
const char kTrackedAtomic[] = "tracked_atomic";
const char kProtectedAtomic[] = "protected_atomic";
const char kProtectedSplit[] = "protected_split";
const char kFoobar[] = "FOOBAR";
const char kBarfoo[] = "BARFOO";
const char kHelloWorld[] = "HELLOWORLD";
const char kGoodbyeWorld[] = "GOODBYEWORLD";
const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = {
{0, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC},
{1, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC},
{2, kProtectedSplit, PrefHashFilter::ENFORCE_ON_LOAD,
PrefHashFilter::TRACKING_STRATEGY_SPLIT}};
} // namespace
class ProfilePrefStoreManagerTest : public testing::Test {
public:
ProfilePrefStoreManagerTest()
: configuration_(kConfiguration,
kConfiguration + arraysize(kConfiguration)),
profile_pref_registry_(new user_prefs::PrefRegistrySyncable),
registry_verifier_(profile_pref_registry_) {}
virtual void SetUp() OVERRIDE {
ProfilePrefStoreManager::RegisterPrefs(local_state_.registry());
ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_);
for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration;
it != kConfiguration + arraysize(kConfiguration);
++it) {
if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) {
profile_pref_registry_->RegisterStringPref(
it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
} else {
profile_pref_registry_->RegisterDictionaryPref(
it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
}
ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(),
configuration_,
configuration_.size(),
"seed",
"device_id",
&local_state_));
}
virtual void TearDown() OVERRIDE {
if (pref_store_) {
// Force everything to be written to disk, triggering the PrefHashFilter
// while our RegistryVerifier is watching.
pref_store_->CommitPendingWrite();
base::RunLoop().RunUntilIdle();
pref_store_->RemoveObserver(®istry_verifier_);
pref_store_ = NULL;
// Nothing should have to happen on the background threads, but just in
// case...
base::RunLoop().RunUntilIdle();
}
}
protected:
void InitializePrefs() {
scoped_refptr<PersistentPrefStore> pref_store =
manager_->CreateProfilePrefStore(
main_message_loop_.message_loop_proxy());
pref_store->AddObserver(®istry_verifier_);
PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs();
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
pref_store->RemoveObserver(®istry_verifier_);
pref_store = NULL;
base::RunLoop().RunUntilIdle();
}
void LoadExistingPrefs() {
pref_store_ = manager_->CreateProfilePrefStore(
main_message_loop_.message_loop_proxy());
pref_store_->AddObserver(®istry_verifier_);
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
pref_store_->ReadPrefs());
}
void ReplaceStringInPrefs(const std::string& find,
const std::string& replace) {
// Tamper with the file's contents
base::FilePath pref_file_path =
ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
profile_dir_.path());
std::string pref_file_contents;
EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents));
ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace);
EXPECT_EQ(static_cast<int>(pref_file_contents.length()),
base::WriteFile(pref_file_path,
pref_file_contents.c_str(),
pref_file_contents.length()));
}
void ExpectStringValueEquals(const std::string& name,
const std::string& expected) {
const base::Value* value = NULL;
std::string as_string;
if (!pref_store_->GetValue(name, &value)) {
ADD_FAILURE() << name << " is not a defined value.";
} else if (!value->GetAsString(&as_string)) {
ADD_FAILURE() << name << " could not be coerced to a string.";
} else {
EXPECT_EQ(expected, as_string);
}
}
base::MessageLoop main_message_loop_;
std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_;
base::ScopedTempDir profile_dir_;
TestingPrefServiceSimple local_state_;
scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_;
RegistryVerifier registry_verifier_;
scoped_ptr<ProfilePrefStoreManager> manager_;
scoped_refptr<PersistentPrefStore> pref_store_;
};
TEST_F(ProfilePrefStoreManagerTest, StoreValues) {
InitializePrefs();
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
}
TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) {
base::FilePath pref_file_path =
ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
profile_dir_.path());
ASSERT_FALSE(base::PathExists(pref_file_path));
InitializePrefs();
ASSERT_TRUE(base::PathExists(pref_file_path));
}
TEST_F(ProfilePrefStoreManagerTest, ProtectValues) {
InitializePrefs();
ReplaceStringInPrefs(kFoobar, kBarfoo);
ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld);
LoadExistingPrefs();
// kTrackedAtomic is unprotected and thus will be loaded as it appears on
// disk.
ExpectStringValueEquals(kTrackedAtomic, kBarfoo);
// If preference tracking is supported, the tampered value of kProtectedAtomic
// will be discarded at load time, leaving this preference undefined.
EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
}
TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) {
InitializePrefs();
manager_->ResetPrefHashStore();
LoadExistingPrefs();
// kTrackedAtomic is loaded as it appears on disk.
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
// If preference tracking is supported, the tampered value of kProtectedAtomic
// will be discarded at load time, leaving this preference undefined.
EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
}
TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) {
InitializePrefs();
ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
LoadExistingPrefs();
// kTrackedAtomic is loaded as it appears on disk.
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
// If preference tracking is supported, kProtectedAtomic will be undefined
// because the value was discarded due to loss of the hash store contents.
EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
}
TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) {
InitializePrefs();
manager_->ResetPrefHashStore();
// This is a no-op if !kPlatformSupportsPreferenceTracking.
manager_->UpdateProfileHashStoreIfRequired(
main_message_loop_.message_loop_proxy());
base::RunLoop().RunUntilIdle();
// At the moment, UpdateProfileHashStoreIfRequired will accept existing
// values.
LoadExistingPrefs();
// These expectations hold whether or not tracking is supported.
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
}
TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) {
scoped_ptr<base::DictionaryValue> master_prefs(
new base::DictionaryValue);
master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar));
master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld));
ASSERT_TRUE(
manager_->InitializePrefsFromMasterPrefs(*master_prefs));
LoadExistingPrefs();
// Verify that InitializePrefsFromMasterPrefs correctly applied the MACs
// necessary to authenticate these values.
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
}
|