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

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

Issue 266553002: Add TrackedPreferenceValidationDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: undo git cl format Created 6 years, 6 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/file_enumerator.h"
12 #include "base/files/scoped_temp_dir.h" 12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/prefs/json_pref_store.h" 16 #include "base/prefs/json_pref_store.h"
17 #include "base/prefs/persistent_pref_store.h" 17 #include "base/prefs/persistent_pref_store.h"
18 #include "base/prefs/pref_service.h" 18 #include "base/prefs/pref_service.h"
19 #include "base/prefs/pref_service_factory.h" 19 #include "base/prefs/pref_service_factory.h"
20 #include "base/prefs/pref_store.h" 20 #include "base/prefs/pref_store.h"
21 #include "base/prefs/testing_pref_service.h" 21 #include "base/prefs/testing_pref_service.h"
22 #include "base/run_loop.h" 22 #include "base/run_loop.h"
23 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
24 #include "base/values.h" 24 #include "base/values.h"
25 #include "chrome/browser/prefs/mock_validation_delegate.h"
25 #include "chrome/browser/prefs/pref_hash_filter.h" 26 #include "chrome/browser/prefs/pref_hash_filter.h"
26 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
27 #include "components/pref_registry/pref_registry_syncable.h" 28 #include "components/pref_registry/pref_registry_syncable.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 30
30 namespace { 31 namespace {
31 32
32 class FirstEqualsPredicate { 33 class FirstEqualsPredicate {
33 public: 34 public:
34 explicit FirstEqualsPredicate(const std::string& expected) 35 explicit FirstEqualsPredicate(const std::string& expected)
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 pref_service_factory.Create(profile_pref_registry_)); 148 pref_service_factory.Create(profile_pref_registry_));
148 149
149 return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null(); 150 return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null();
150 } 151 }
151 152
152 void InitializePrefs() { 153 void InitializePrefs() {
153 // According to the implementation of ProfilePrefStoreManager, this is 154 // According to the implementation of ProfilePrefStoreManager, this is
154 // actually a SegregatedPrefStore backed by two underlying pref stores. 155 // actually a SegregatedPrefStore backed by two underlying pref stores.
155 scoped_refptr<PersistentPrefStore> pref_store = 156 scoped_refptr<PersistentPrefStore> pref_store =
156 manager_->CreateProfilePrefStore( 157 manager_->CreateProfilePrefStore(
157 main_message_loop_.message_loop_proxy()); 158 main_message_loop_.message_loop_proxy(),
159 &mock_validation_delegate_);
158 InitializePrefStore(pref_store); 160 InitializePrefStore(pref_store);
159 pref_store = NULL; 161 pref_store = NULL;
160 base::RunLoop().RunUntilIdle(); 162 base::RunLoop().RunUntilIdle();
161 } 163 }
162 164
163 void DestroyPrefStore() { 165 void DestroyPrefStore() {
164 if (pref_store_) { 166 if (pref_store_) {
165 // Force everything to be written to disk, triggering the PrefHashFilter 167 // Force everything to be written to disk, triggering the PrefHashFilter
166 // while our RegistryVerifier is watching. 168 // while our RegistryVerifier is watching.
167 pref_store_->CommitPendingWrite(); 169 pref_store_->CommitPendingWrite();
(...skipping 24 matching lines...) Expand all
192 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld)); 194 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
193 pref_store->SetValue(kUnprotectedPref, new base::StringValue(kFoobar)); 195 pref_store->SetValue(kUnprotectedPref, new base::StringValue(kFoobar));
194 pref_store->RemoveObserver(&registry_verifier_); 196 pref_store->RemoveObserver(&registry_verifier_);
195 pref_store->CommitPendingWrite(); 197 pref_store->CommitPendingWrite();
196 base::RunLoop().RunUntilIdle(); 198 base::RunLoop().RunUntilIdle();
197 } 199 }
198 200
199 void LoadExistingPrefs() { 201 void LoadExistingPrefs() {
200 DestroyPrefStore(); 202 DestroyPrefStore();
201 pref_store_ = manager_->CreateProfilePrefStore( 203 pref_store_ = manager_->CreateProfilePrefStore(
202 main_message_loop_.message_loop_proxy()); 204 main_message_loop_.message_loop_proxy(), NULL);
203 pref_store_->AddObserver(&registry_verifier_); 205 pref_store_->AddObserver(&registry_verifier_);
204 pref_store_->ReadPrefs(); 206 pref_store_->ReadPrefs();
205 } 207 }
206 208
207 void ReplaceStringInPrefs(const std::string& find, 209 void ReplaceStringInPrefs(const std::string& find,
208 const std::string& replace) { 210 const std::string& replace) {
209 base::FileEnumerator file_enum( 211 base::FileEnumerator file_enum(
210 profile_dir_.path(), true, base::FileEnumerator::FILES); 212 profile_dir_.path(), true, base::FileEnumerator::FILES);
211 213
212 for (base::FilePath path = file_enum.Next(); !path.empty(); 214 for (base::FilePath path = file_enum.Next(); !path.empty();
(...skipping 13 matching lines...) Expand all
226 std::string as_string; 228 std::string as_string;
227 if (!pref_store_->GetValue(name, &value)) { 229 if (!pref_store_->GetValue(name, &value)) {
228 ADD_FAILURE() << name << " is not a defined value."; 230 ADD_FAILURE() << name << " is not a defined value.";
229 } else if (!value->GetAsString(&as_string)) { 231 } else if (!value->GetAsString(&as_string)) {
230 ADD_FAILURE() << name << " could not be coerced to a string."; 232 ADD_FAILURE() << name << " could not be coerced to a string.";
231 } else { 233 } else {
232 EXPECT_EQ(expected, as_string); 234 EXPECT_EQ(expected, as_string);
233 } 235 }
234 } 236 }
235 237
238 void ExpectValidationObserved(const std::string& pref_path) {
239 // No validations are expected for platforms that do not support tracking.
240 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking)
gab 2014/06/10 16:22:08 You could test this too here, change this method's
241 return;
242 if (!mock_validation_delegate_.GetEventForPath(pref_path))
243 ADD_FAILURE() << "No validation observed for preference: " << pref_path;
244 }
245
236 base::MessageLoop main_message_loop_; 246 base::MessageLoop main_message_loop_;
237 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; 247 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_;
238 base::ScopedTempDir profile_dir_; 248 base::ScopedTempDir profile_dir_;
239 TestingPrefServiceSimple local_state_; 249 TestingPrefServiceSimple local_state_;
240 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; 250 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_;
241 RegistryVerifier registry_verifier_; 251 RegistryVerifier registry_verifier_;
252 MockValidationDelegate mock_validation_delegate_;
242 scoped_ptr<ProfilePrefStoreManager> manager_; 253 scoped_ptr<ProfilePrefStoreManager> manager_;
243 scoped_refptr<PersistentPrefStore> pref_store_; 254 scoped_refptr<PersistentPrefStore> pref_store_;
244 }; 255 };
245 256
246 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { 257 TEST_F(ProfilePrefStoreManagerTest, StoreValues) {
247 InitializePrefs(); 258 InitializePrefs();
248 259
249 LoadExistingPrefs(); 260 LoadExistingPrefs();
250 261
251 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 262 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
252 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 263 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
253 EXPECT_FALSE(WasResetRecorded()); 264 EXPECT_FALSE(WasResetRecorded());
265 ExpectValidationObserved(kTrackedAtomic);
266 ExpectValidationObserved(kProtectedAtomic);
254 } 267 }
255 268
256 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) { 269 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) {
257 base::FilePath pref_file_path = 270 base::FilePath pref_file_path =
258 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( 271 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
259 profile_dir_.path()); 272 profile_dir_.path());
260 273
261 EXPECT_FALSE(base::PathExists(pref_file_path)); 274 EXPECT_FALSE(base::PathExists(pref_file_path));
262 275
263 InitializePrefs(); 276 InitializePrefs();
(...skipping 12 matching lines...) Expand all
276 // kTrackedAtomic is unprotected and thus will be loaded as it appears on 289 // kTrackedAtomic is unprotected and thus will be loaded as it appears on
277 // disk. 290 // disk.
278 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); 291 ExpectStringValueEquals(kTrackedAtomic, kBarfoo);
279 292
280 // If preference tracking is supported, the tampered value of kProtectedAtomic 293 // If preference tracking is supported, the tampered value of kProtectedAtomic
281 // will be discarded at load time, leaving this preference undefined. 294 // will be discarded at load time, leaving this preference undefined.
282 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 295 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
283 pref_store_->GetValue(kProtectedAtomic, NULL)); 296 pref_store_->GetValue(kProtectedAtomic, NULL));
284 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 297 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
285 WasResetRecorded()); 298 WasResetRecorded());
299
300 ExpectValidationObserved(kTrackedAtomic);
301 ExpectValidationObserved(kProtectedAtomic);
286 } 302 }
287 303
288 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) { 304 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) {
289 InitializePrefs(); 305 InitializePrefs();
290 306
291 manager_->ResetPrefHashStore(); 307 manager_->ResetPrefHashStore();
292 308
293 LoadExistingPrefs(); 309 LoadExistingPrefs();
294 310
295 // kTrackedAtomic is loaded as it appears on disk. 311 // kTrackedAtomic is loaded as it appears on disk.
296 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 312 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
297 // If preference tracking is supported, kProtectedAtomic will be undefined 313 // If preference tracking is supported, kProtectedAtomic will be undefined
298 // because the value was discarded due to loss of the hash store contents. 314 // because the value was discarded due to loss of the hash store contents.
299 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 315 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
300 pref_store_->GetValue(kProtectedAtomic, NULL)); 316 pref_store_->GetValue(kProtectedAtomic, NULL));
301 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 317 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
302 WasResetRecorded()); 318 WasResetRecorded());
319
320 ExpectValidationObserved(kTrackedAtomic);
321 ExpectValidationObserved(kProtectedAtomic);
303 } 322 }
304 323
305 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) { 324 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) {
306 InitializePrefs(); 325 InitializePrefs();
307 326
308 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); 327 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
309 328
310 LoadExistingPrefs(); 329 LoadExistingPrefs();
311 330
312 // kTrackedAtomic is loaded as it appears on disk. 331 // kTrackedAtomic is loaded as it appears on disk.
313 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 332 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
314 // If preference tracking is supported, kProtectedAtomic will be undefined 333 // If preference tracking is supported, kProtectedAtomic will be undefined
315 // because the value was discarded due to loss of the hash store contents. 334 // because the value was discarded due to loss of the hash store contents.
316 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 335 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
317 pref_store_->GetValue(kProtectedAtomic, NULL)); 336 pref_store_->GetValue(kProtectedAtomic, NULL));
318 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 337 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
319 WasResetRecorded()); 338 WasResetRecorded());
339
340 ExpectValidationObserved(kTrackedAtomic);
341 ExpectValidationObserved(kProtectedAtomic);
320 } 342 }
321 343
322 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) { 344 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) {
323 InitializeDeprecatedCombinedProfilePrefStore(); 345 InitializeDeprecatedCombinedProfilePrefStore();
324 346
325 LoadExistingPrefs(); 347 LoadExistingPrefs();
326 348
327 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 349 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
328 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 350 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
329 EXPECT_FALSE(WasResetRecorded()); 351 EXPECT_FALSE(WasResetRecorded());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 387
366 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs 388 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs
367 // necessary to authenticate these values. 389 // necessary to authenticate these values.
368 ExpectStringValueEquals(kTrackedAtomic, kFoobar); 390 ExpectStringValueEquals(kTrackedAtomic, kFoobar);
369 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 391 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
370 EXPECT_FALSE(WasResetRecorded()); 392 EXPECT_FALSE(WasResetRecorded());
371 } 393 }
372 394
373 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { 395 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) {
374 InitializePrefs(); 396 InitializePrefs();
397
398 ExpectValidationObserved(kTrackedAtomic);
399 ExpectValidationObserved(kProtectedAtomic);
400
375 LoadExistingPrefs(); 401 LoadExistingPrefs();
376 ExpectStringValueEquals(kUnprotectedPref, kFoobar); 402 ExpectStringValueEquals(kUnprotectedPref, kFoobar);
377 403
378 // Ensure everything is written out to disk. 404 // Ensure everything is written out to disk.
379 DestroyPrefStore(); 405 DestroyPrefStore();
380 406
381 ReplaceStringInPrefs(kFoobar, kBarfoo); 407 ReplaceStringInPrefs(kFoobar, kBarfoo);
382 408
383 // It's unprotected, so we can load the modified value. 409 // It's unprotected, so we can load the modified value.
384 LoadExistingPrefs(); 410 LoadExistingPrefs();
(...skipping 23 matching lines...) Expand all
408 LoadExistingPrefs(); 434 LoadExistingPrefs();
409 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 435 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
410 pref_store_->GetValue(kUnprotectedPref, NULL)); 436 pref_store_->GetValue(kUnprotectedPref, NULL));
411 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 437 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
412 WasResetRecorded()); 438 WasResetRecorded());
413 } 439 }
414 440
415 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { 441 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) {
416 InitializePrefs(); 442 InitializePrefs();
417 443
444 ExpectValidationObserved(kTrackedAtomic);
445 ExpectValidationObserved(kProtectedAtomic);
446
418 // Now update the configuration to protect it. 447 // Now update the configuration to protect it.
419 PrefHashFilter::TrackedPreferenceMetadata new_protected = { 448 PrefHashFilter::TrackedPreferenceMetadata new_protected = {
420 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, 449 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD,
421 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; 450 PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
422 configuration_.push_back(new_protected); 451 configuration_.push_back(new_protected);
423 ReloadConfiguration(); 452 ReloadConfiguration();
424 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); 453 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
425 454
426 // And try loading with the new configuration. 455 // And try loading with the new configuration.
427 LoadExistingPrefs(); 456 LoadExistingPrefs();
428 457
429 // If preference tracking is supported, kUnprotectedPref will have been 458 // If preference tracking is supported, kUnprotectedPref will have been
430 // discarded because new values are not accepted without a valid super MAC. 459 // discarded because new values are not accepted without a valid super MAC.
431 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 460 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
432 pref_store_->GetValue(kUnprotectedPref, NULL)); 461 pref_store_->GetValue(kUnprotectedPref, NULL));
433 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, 462 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
434 WasResetRecorded()); 463 WasResetRecorded());
435 } 464 }
436 465
437 // This test verifies that preference values are correctly maintained when a 466 // This test verifies that preference values are correctly maintained when a
438 // preference's protection state changes from protected to unprotected. 467 // preference's protection state changes from protected to unprotected.
439 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { 468 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) {
440 InitializePrefs(); 469 InitializePrefs();
470
471 ExpectValidationObserved(kTrackedAtomic);
472 ExpectValidationObserved(kProtectedAtomic);
gab 2014/06/10 16:22:08 You do this verification often after InitializePre
473
441 DestroyPrefStore(); 474 DestroyPrefStore();
442 475
443 // Unconfigure protection for kProtectedAtomic 476 // Unconfigure protection for kProtectedAtomic
444 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = 477 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it =
445 configuration_.begin(); 478 configuration_.begin();
446 it != configuration_.end(); 479 it != configuration_.end();
447 ++it) { 480 ++it) {
448 if (it->name == kProtectedAtomic) { 481 if (it->name == kProtectedAtomic) {
449 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; 482 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT;
450 break; 483 break;
(...skipping 15 matching lines...) Expand all
466 LoadExistingPrefs(); 499 LoadExistingPrefs();
467 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); 500 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
468 501
469 // Trigger the logic that migrates it back to the unprotected preferences 502 // Trigger the logic that migrates it back to the unprotected preferences
470 // file. 503 // file.
471 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); 504 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld));
472 LoadExistingPrefs(); 505 LoadExistingPrefs();
473 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); 506 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
474 EXPECT_FALSE(WasResetRecorded()); 507 EXPECT_FALSE(WasResetRecorded());
475 } 508 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698