Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/prefs/profile_pref_store_manager_unittest.cc

Issue 218583003: Separate storage for protected preferences into Protected Preferences file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/prefs/profile_pref_store_manager.h" 5 #include "chrome/browser/prefs/profile_pref_store_manager.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h"
11 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
15 #include "base/prefs/json_pref_store.h" 16 #include "base/prefs/json_pref_store.h"
16 #include "base/prefs/persistent_pref_store.h" 17 #include "base/prefs/persistent_pref_store.h"
17 #include "base/prefs/pref_service.h" 18 #include "base/prefs/pref_service.h"
19 #include "base/prefs/pref_service_factory.h"
18 #include "base/prefs/pref_store.h" 20 #include "base/prefs/pref_store.h"
19 #include "base/prefs/testing_pref_service.h" 21 #include "base/prefs/testing_pref_service.h"
20 #include "base/run_loop.h" 22 #include "base/run_loop.h"
21 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
22 #include "base/values.h" 24 #include "base/values.h"
23 #include "chrome/browser/prefs/pref_hash_filter.h" 25 #include "chrome/browser/prefs/pref_hash_filter.h"
24 #include "components/user_prefs/pref_registry_syncable.h" 26 #include "components/user_prefs/pref_registry_syncable.h"
25 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
26 28
27 namespace { 29 namespace {
(...skipping 25 matching lines...) Expand all
53 FirstEqualsPredicate(key))) 55 FirstEqualsPredicate(key)))
54 << "Unregistered key " << key << " was changed."; 56 << "Unregistered key " << key << " was changed.";
55 } 57 }
56 58
57 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {} 59 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {}
58 60
59 private: 61 private:
60 scoped_refptr<PrefRegistry> pref_registry_; 62 scoped_refptr<PrefRegistry> pref_registry_;
61 }; 63 };
62 64
65 const char kUnprotectedAtomic[] = "unprotected_atomic";
gab 2014/04/03 15:38:49 nit: s/unprotected_atomic/unprotected_pref "atomi
63 const char kTrackedAtomic[] = "tracked_atomic"; 66 const char kTrackedAtomic[] = "tracked_atomic";
64 const char kProtectedAtomic[] = "protected_atomic"; 67 const char kProtectedAtomic[] = "protected_atomic";
65 const char kProtectedSplit[] = "protected_split";
66 68
67 const char kFoobar[] = "FOOBAR"; 69 const char kFoobar[] = "FOOBAR";
68 const char kBarfoo[] = "BARFOO"; 70 const char kBarfoo[] = "BARFOO";
69 const char kHelloWorld[] = "HELLOWORLD"; 71 const char kHelloWorld[] = "HELLOWORLD";
70 const char kGoodbyeWorld[] = "GOODBYEWORLD"; 72 const char kGoodbyeWorld[] = "GOODBYEWORLD";
71 73
72 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { 74 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = {
73 {0, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT, 75 {0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT,
74 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}, 76 PrefHashFilter::TRACKING_STRATEGY_ATOMIC},
75 {1, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, 77 {1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
76 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}, 78 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}};
77 {2, kProtectedSplit, PrefHashFilter::ENFORCE_ON_LOAD, 79
78 PrefHashFilter::TRACKING_STRATEGY_SPLIT}}; 80 const size_t kExtraReportingId = 2u;
81 const size_t kReportingIdCount = 3u;
79 82
80 } // namespace 83 } // namespace
81 84
82 class ProfilePrefStoreManagerTest : public testing::Test { 85 class ProfilePrefStoreManagerTest : public testing::Test {
83 public: 86 public:
84 ProfilePrefStoreManagerTest() 87 ProfilePrefStoreManagerTest()
85 : configuration_(kConfiguration, 88 : configuration_(kConfiguration,
86 kConfiguration + arraysize(kConfiguration)), 89 kConfiguration + arraysize(kConfiguration)),
87 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), 90 profile_pref_registry_(new user_prefs::PrefRegistrySyncable),
88 registry_verifier_(profile_pref_registry_) {} 91 registry_verifier_(profile_pref_registry_) {}
89 92
90 virtual void SetUp() OVERRIDE { 93 virtual void SetUp() OVERRIDE {
91 ProfilePrefStoreManager::RegisterPrefs(local_state_.registry()); 94 ProfilePrefStoreManager::RegisterPrefs(local_state_.registry());
92 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_); 95 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_);
93 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; 96 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration;
94 it != kConfiguration + arraysize(kConfiguration); 97 it != kConfiguration + arraysize(kConfiguration);
95 ++it) { 98 ++it) {
96 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { 99 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) {
97 profile_pref_registry_->RegisterStringPref( 100 profile_pref_registry_->RegisterStringPref(
98 it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 101 it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
gab 2014/04/03 15:38:49 nit: s/""/std::string()
99 } else { 102 } else {
100 profile_pref_registry_->RegisterDictionaryPref( 103 profile_pref_registry_->RegisterDictionaryPref(
101 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 104 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
102 } 105 }
103 } 106 }
107 profile_pref_registry_->RegisterStringPref(
108 kUnprotectedAtomic,
109 std::string(),
110 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111
104 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); 112 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
113 ReloadConfiguration();
114 }
105 115
116 void ReloadConfiguration() {
106 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(), 117 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(),
107 configuration_, 118 configuration_,
108 configuration_.size(), 119 kReportingIdCount,
109 "seed", 120 "seed",
110 "device_id", 121 "device_id",
111 &local_state_)); 122 &local_state_));
112 } 123 }
113 124
114 virtual void TearDown() OVERRIDE { 125 virtual void TearDown() OVERRIDE { DestroyPrefStore(); }
126
127 protected:
128 bool WasResetRecorded() {
129 base::PrefServiceFactory pref_service_factory;
130 pref_service_factory.set_user_prefs(pref_store_);
131
132 scoped_ptr<PrefService> pref_service(
133 pref_service_factory.Create(profile_pref_registry_));
134
135 return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null();
136 }
137
138 void InitializePrefs() {
139 // According to the implementation of ProfilePrefStoreManager, this is
140 // actually a SegregatedPrefStore backed by two underlying pref stores.
141 scoped_refptr<PersistentPrefStore> pref_store =
142 manager_->CreateProfilePrefStore(
143 main_message_loop_.message_loop_proxy());
144 InitializePrefStore(pref_store);
145 pref_store = NULL;
146 base::RunLoop().RunUntilIdle();
147 }
148
149 void DestroyPrefStore() {
115 if (pref_store_) { 150 if (pref_store_) {
116 // Force everything to be written to disk, triggering the PrefHashFilter 151 // Force everything to be written to disk, triggering the PrefHashFilter
117 // while our RegistryVerifier is watching. 152 // while our RegistryVerifier is watching.
118 pref_store_->CommitPendingWrite(); 153 pref_store_->CommitPendingWrite();
119 base::RunLoop().RunUntilIdle(); 154 base::RunLoop().RunUntilIdle();
120 155
121 pref_store_->RemoveObserver(&registry_verifier_); 156 pref_store_->RemoveObserver(&registry_verifier_);
122 pref_store_ = NULL; 157 pref_store_ = NULL;
123 // Nothing should have to happen on the background threads, but just in 158 // Nothing should have to happen on the background threads, but just in
124 // case... 159 // case...
125 base::RunLoop().RunUntilIdle(); 160 base::RunLoop().RunUntilIdle();
126 } 161 }
127 } 162 }
128 163
129 protected: 164 void InitializeDeprecatedCombinedProfilePrefStore() {
130 void InitializePrefs() {
131 scoped_refptr<PersistentPrefStore> pref_store = 165 scoped_refptr<PersistentPrefStore> pref_store =
132 manager_->CreateProfilePrefStore( 166 manager_->CreateDeprecatedCombinedProfilePrefStore(
133 main_message_loop_.message_loop_proxy()); 167 main_message_loop_.message_loop_proxy());
134 pref_store->AddObserver(&registry_verifier_); 168 InitializePrefStore(pref_store);
135 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs();
136 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
137 pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
138 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
139 pref_store->RemoveObserver(&registry_verifier_);
140 pref_store = NULL; 169 pref_store = NULL;
141 base::RunLoop().RunUntilIdle(); 170 base::RunLoop().RunUntilIdle();
142 } 171 }
143 172
173 void InitializePrefStore(PersistentPrefStore* pref_store) {
174 pref_store->AddObserver(&registry_verifier_);
175 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs();
176 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
177 pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
178 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
179 pref_store->SetValue(kUnprotectedAtomic, new base::StringValue(kFoobar));
180 pref_store->RemoveObserver(&registry_verifier_);
181 pref_store->CommitPendingWrite();
182 base::RunLoop().RunUntilIdle();
183 }
184
144 void LoadExistingPrefs() { 185 void LoadExistingPrefs() {
186 DestroyPrefStore();
145 pref_store_ = manager_->CreateProfilePrefStore( 187 pref_store_ = manager_->CreateProfilePrefStore(
146 main_message_loop_.message_loop_proxy()); 188 main_message_loop_.message_loop_proxy());
147 pref_store_->AddObserver(&registry_verifier_); 189 pref_store_->AddObserver(&registry_verifier_);
148 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, 190 pref_store_->ReadPrefs();
149 pref_store_->ReadPrefs());
150 } 191 }
151 192
152 void ReplaceStringInPrefs(const std::string& find, 193 void ReplaceStringInPrefs(const std::string& find,
153 const std::string& replace) { 194 const std::string& replace) {
154 // Tamper with the file's contents 195 base::FileEnumerator file_enum(
155 base::FilePath pref_file_path = 196 profile_dir_.path(), true, base::FileEnumerator::FILES);
156 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( 197
157 profile_dir_.path()); 198 for (base::FilePath path = file_enum.Next(); !path.empty();
158 std::string pref_file_contents; 199 path = file_enum.Next()) {
159 EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents)); 200 // Tamper with the file's contents
160 ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace); 201 std::string contents;
161 EXPECT_EQ(static_cast<int>(pref_file_contents.length()), 202 EXPECT_TRUE(base::ReadFileToString(path, &contents));
162 base::WriteFile(pref_file_path, 203 ReplaceSubstringsAfterOffset(&contents, 0u, find, replace);
163 pref_file_contents.c_str(), 204 EXPECT_EQ(static_cast<int>(contents.length()),
164 pref_file_contents.length())); 205 base::WriteFile(path, contents.c_str(), contents.length()));
206 }
165 } 207 }
166 208
167 void ExpectStringValueEquals(const std::string& name, 209 void ExpectStringValueEquals(const std::string& name,
168 const std::string& expected) { 210 const std::string& expected) {
169 const base::Value* value = NULL; 211 const base::Value* value = NULL;
170 std::string as_string; 212 std::string as_string;
171 if (!pref_store_->GetValue(name, &value)) { 213 if (!pref_store_->GetValue(name, &value)) {
172 ADD_FAILURE() << name << " is not a defined value."; 214 ADD_FAILURE() << name << " is not a defined value.";
173 } else if (!value->GetAsString(&as_string)) { 215 } else if (!value->GetAsString(&as_string)) {
174 ADD_FAILURE() << name << " could not be coerced to a string."; 216 ADD_FAILURE() << name << " could not be coerced to a string.";
(...skipping 12 matching lines...) Expand all
187 scoped_refptr<PersistentPrefStore> pref_store_; 229 scoped_refptr<PersistentPrefStore> pref_store_;
188 }; 230 };
189 231
190 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { 232 TEST_F(ProfilePrefStoreManagerTest, StoreValues) {
191 InitializePrefs(); 233 InitializePrefs();
192 234
193 LoadExistingPrefs(); 235 LoadExistingPrefs();
194 236
195 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 237 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
196 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 238 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
239 EXPECT_FALSE(WasResetRecorded());
197 } 240 }
198 241
199 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) { 242 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) {
200 base::FilePath pref_file_path = 243 base::FilePath pref_file_path =
201 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( 244 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
202 profile_dir_.path()); 245 profile_dir_.path());
203 246
204 ASSERT_FALSE(base::PathExists(pref_file_path)); 247 EXPECT_FALSE(base::PathExists(pref_file_path));
205 248
206 InitializePrefs(); 249 InitializePrefs();
207 250
208 ASSERT_TRUE(base::PathExists(pref_file_path)); 251 EXPECT_TRUE(base::PathExists(pref_file_path));
209 } 252 }
210 253
211 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { 254 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) {
212 InitializePrefs(); 255 InitializePrefs();
213 256
214 ReplaceStringInPrefs(kFoobar, kBarfoo); 257 ReplaceStringInPrefs(kFoobar, kBarfoo);
215 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); 258 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld);
216 259
217 LoadExistingPrefs(); 260 LoadExistingPrefs();
218 261
219 // kTrackedAtomic is unprotected and thus will be loaded as it appears on 262 // kTrackedAtomic is unprotected and thus will be loaded as it appears on
220 // disk. 263 // disk.
221 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); 264 ExpectStringValueEquals(kTrackedAtomic, kBarfoo);
222 265
223 // If preference tracking is supported, the tampered value of kProtectedAtomic 266 // If preference tracking is supported, the tampered value of kProtectedAtomic
224 // will be discarded at load time, leaving this preference undefined. 267 // will be discarded at load time, leaving this preference undefined.
225 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 268 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
226 pref_store_->GetValue(kProtectedAtomic, NULL)); 269 pref_store_->GetValue(kProtectedAtomic, NULL));
270 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
271 WasResetRecorded());
227 } 272 }
228 273
229 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) { 274 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) {
230 InitializePrefs(); 275 InitializePrefs();
231 276
232 manager_->ResetPrefHashStore(); 277 manager_->ResetPrefHashStore();
233 278
234 LoadExistingPrefs(); 279 LoadExistingPrefs();
235 280
236 // kTrackedAtomic is loaded as it appears on disk. 281 // kTrackedAtomic is loaded as it appears on disk.
237 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 282 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
238 // If preference tracking is supported, the tampered value of kProtectedAtomic 283 // If preference tracking is supported, the tampered value of kProtectedAtomic
239 // will be discarded at load time, leaving this preference undefined. 284 // will be discarded at load time, leaving this preference undefined.
240 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 285 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
241 pref_store_->GetValue(kProtectedAtomic, NULL)); 286 pref_store_->GetValue(kProtectedAtomic, NULL));
287 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
288 WasResetRecorded());
242 } 289 }
243 290
244 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) { 291 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) {
245 InitializePrefs(); 292 InitializePrefs();
246 293
247 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); 294 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
248 295
249 LoadExistingPrefs(); 296 LoadExistingPrefs();
250 297
251 // kTrackedAtomic is loaded as it appears on disk. 298 // kTrackedAtomic is loaded as it appears on disk.
252 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 299 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
253 // If preference tracking is supported, kProtectedAtomic will be undefined 300 // If preference tracking is supported, kProtectedAtomic will be undefined
254 // because the value was discarded due to loss of the hash store contents. 301 // because the value was discarded due to loss of the hash store contents.
255 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 302 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
256 pref_store_->GetValue(kProtectedAtomic, NULL)); 303 pref_store_->GetValue(kProtectedAtomic, NULL));
304 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
305 WasResetRecorded());
306 }
307
308 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) {
309 InitializeDeprecatedCombinedProfilePrefStore();
310
311 LoadExistingPrefs();
312
313 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
314 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
315 EXPECT_FALSE(WasResetRecorded());
257 } 316 }
258 317
259 TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) { 318 TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) {
260 InitializePrefs(); 319 scoped_refptr<JsonPrefStore> legacy_prefs(
261 320 new JsonPrefStore(ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
262 manager_->ResetPrefHashStore(); 321 profile_dir_.path()),
322 main_message_loop_.message_loop_proxy(),
323 scoped_ptr<PrefFilter>()));
324 legacy_prefs->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
325 legacy_prefs->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
326 legacy_prefs = NULL;
327 base::RunLoop().RunUntilIdle();
263 328
264 // This is a no-op if !kPlatformSupportsPreferenceTracking. 329 // This is a no-op if !kPlatformSupportsPreferenceTracking.
265 manager_->UpdateProfileHashStoreIfRequired( 330 manager_->UpdateProfileHashStoreIfRequired(
266 main_message_loop_.message_loop_proxy()); 331 main_message_loop_.message_loop_proxy());
267 base::RunLoop().RunUntilIdle(); 332 base::RunLoop().RunUntilIdle();
268 333
269 // At the moment, UpdateProfileHashStoreIfRequired will accept existing 334 // At the moment, UpdateProfileHashStoreIfRequired will accept existing
270 // values. 335 // values.
271 LoadExistingPrefs(); 336 LoadExistingPrefs();
272 337
273 // These expectations hold whether or not tracking is supported. 338 // These expectations hold whether or not tracking is supported.
274 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 339 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
275 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 340 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
341 EXPECT_FALSE(WasResetRecorded());
276 } 342 }
277 343
278 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { 344 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) {
279 scoped_ptr<base::DictionaryValue> master_prefs( 345 scoped_ptr<base::DictionaryValue> master_prefs(
280 new base::DictionaryValue); 346 new base::DictionaryValue);
281 master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar)); 347 master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar));
282 master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); 348 master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld));
283 ASSERT_TRUE( 349 EXPECT_TRUE(
284 manager_->InitializePrefsFromMasterPrefs(*master_prefs)); 350 manager_->InitializePrefsFromMasterPrefs(*master_prefs));
285 351
286 LoadExistingPrefs(); 352 LoadExistingPrefs();
287 353
288 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs 354 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs
289 // necessary to authenticate these values. 355 // necessary to authenticate these values.
290 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 356 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
291 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 357 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
358 EXPECT_FALSE(WasResetRecorded());
292 } 359 }
360
361 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) {
362 InitializePrefs();
363 LoadExistingPrefs();
364 ExpectStringValueEquals(kUnprotectedAtomic, kFoobar);
365
366 // Ensure everything is written out to disk.
367 DestroyPrefStore();
368
369 ReplaceStringInPrefs(kFoobar, kBarfoo);
370
371 // It's unprotected, so we can load the modified value.
372 LoadExistingPrefs();
373 ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo);
374
375 // Now update the configuration to protect it.
376 PrefHashFilter::TrackedPreferenceMetadata new_protected = {
377 kExtraReportingId, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
378 PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
379 configuration_.push_back(new_protected);
380 ReloadConfiguration();
381
382 // And try loading with the new configuration.
383 LoadExistingPrefs();
384
385 // Since there was a valid super MAC we were able to extend the existing trust
386 // to the newly proteted preference.
387 ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo);
388 EXPECT_FALSE(WasResetRecorded());
389
390 // Ensure everything is written out to disk.
391 DestroyPrefStore();
392
393 // It's protected now, so (if the platform supports it) any tampering should
394 // lead to a reset.
395 ReplaceStringInPrefs(kBarfoo, kFoobar);
396 LoadExistingPrefs();
397 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
398 pref_store_->GetValue(kUnprotectedAtomic, NULL));
399 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
400 WasResetRecorded());
401 }
402
403 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) {
404 InitializePrefs();
405
406 // Now update the configuration to protect it.
407 PrefHashFilter::TrackedPreferenceMetadata new_protected = {
408 kExtraReportingId, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
409 PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
410 configuration_.push_back(new_protected);
411 ReloadConfiguration();
412 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
413
414 // And try loading with the new configuration.
415 LoadExistingPrefs();
416
417 // If preference tracking is supported, kUnprotectedAtomic will have been
418 // discarded because new values are not accepted without a valid super MAC.
419 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
420 pref_store_->GetValue(kUnprotectedAtomic, NULL));
421 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
422 WasResetRecorded());
423 }
424
425 // This test does not directly verify that the values are moved from one pref
426 // store to the other. segregated_pref_store_unittest.cc _does_ verify that
427 // functionality.
428 //
429 // _This_ test verifies that preference values are correctly maintained when a
430 // preference's protection state changes from protected to unprotected.
431 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) {
432 InitializePrefs();
433 DestroyPrefStore();
434
435 // Unconfigure protection for kProtectedAtomic
436 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it =
437 configuration_.begin();
438 it != configuration_.end();
439 ++it) {
440 if (it->name == kProtectedAtomic) {
441 configuration_.erase(it);
442 break;
443 }
444 }
445 ReloadConfiguration();
446
447 // Reset the hash stores and then try loading the prefs.
448 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
449 LoadExistingPrefs();
450
451 // Verify that the value was not reset.
452 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
453 EXPECT_FALSE(WasResetRecorded());
454
455 // Accessing the value of the previously protected pref didn't trigger its
456 // move to the unprotected preferences file, though the loading of the pref
457 // store should still have caused the MAC store to be recalculated.
458 LoadExistingPrefs();
459 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
460
461 // Trigger the logic that migrates it back to the unprotected preferences
462 // file.
463 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld));
464 LoadExistingPrefs();
465 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
466 EXPECT_FALSE(WasResetRecorded());
467 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/profile_pref_store_manager.cc ('k') | chrome/browser/prefs/tracked/segregated_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698