| OLD | NEW |
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
| 23 #include "components/prefs/json_pref_store.h" | 23 #include "components/prefs/json_pref_store.h" |
| 24 #include "components/prefs/persistent_pref_store.h" | 24 #include "components/prefs/persistent_pref_store.h" |
| 25 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
| 26 #include "components/prefs/pref_service_factory.h" | 26 #include "components/prefs/pref_service_factory.h" |
| 27 #include "components/prefs/pref_store.h" | 27 #include "components/prefs/pref_store.h" |
| 28 #include "components/prefs/testing_pref_service.h" | 28 #include "components/prefs/testing_pref_service.h" |
| 29 #include "components/user_prefs/tracked/mock_validation_delegate.h" | 29 #include "components/user_prefs/tracked/mock_validation_delegate.h" |
| 30 #include "components/user_prefs/tracked/pref_hash_filter.h" | 30 #include "components/user_prefs/tracked/pref_hash_filter.h" |
| 31 #include "components/user_prefs/tracked/pref_names.h" | 31 #include "components/user_prefs/tracked/pref_names.h" |
| 32 #include "components/user_prefs/tracked/pref_service_hash_store_contents.h" | |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 33 |
| 35 namespace { | 34 namespace { |
| 36 | 35 |
| 37 class FirstEqualsPredicate { | 36 class FirstEqualsPredicate { |
| 38 public: | 37 public: |
| 39 explicit FirstEqualsPredicate(const std::string& expected) | 38 explicit FirstEqualsPredicate(const std::string& expected) |
| 40 : expected_(expected) {} | 39 : expected_(expected) {} |
| 41 bool operator()(const std::pair<std::string, base::Value*>& pair) { | 40 bool operator()(const std::pair<std::string, base::Value*>& pair) { |
| 42 return pair.first == expected_; | 41 return pair.first == expected_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 public: | 91 public: |
| 93 ProfilePrefStoreManagerTest() | 92 ProfilePrefStoreManagerTest() |
| 94 : configuration_(kConfiguration, | 93 : configuration_(kConfiguration, |
| 95 kConfiguration + arraysize(kConfiguration)), | 94 kConfiguration + arraysize(kConfiguration)), |
| 96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), | 95 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), |
| 97 registry_verifier_(profile_pref_registry_.get()), | 96 registry_verifier_(profile_pref_registry_.get()), |
| 98 seed_("seed"), | 97 seed_("seed"), |
| 99 reset_recorded_(false) {} | 98 reset_recorded_(false) {} |
| 100 | 99 |
| 101 void SetUp() override { | 100 void SetUp() override { |
| 102 ProfilePrefStoreManager::RegisterPrefs(local_state_.registry()); | |
| 103 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); | 101 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
| 104 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; | 102 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
| 105 it != kConfiguration + arraysize(kConfiguration); | 103 it != kConfiguration + arraysize(kConfiguration); |
| 106 ++it) { | 104 ++it) { |
| 107 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { | 105 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { |
| 108 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 106 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
| 109 } else { | 107 } else { |
| 110 profile_pref_registry_->RegisterDictionaryPref(it->name); | 108 profile_pref_registry_->RegisterDictionaryPref(it->name); |
| 111 } | 109 } |
| 112 } | 110 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 192 | 190 |
| 193 pref_store_->RemoveObserver(®istry_verifier_); | 191 pref_store_->RemoveObserver(®istry_verifier_); |
| 194 pref_store_ = NULL; | 192 pref_store_ = NULL; |
| 195 // Nothing should have to happen on the background threads, but just in | 193 // Nothing should have to happen on the background threads, but just in |
| 196 // case... | 194 // case... |
| 197 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 198 } | 196 } |
| 199 } | 197 } |
| 200 | 198 |
| 201 void InitializeDeprecatedCombinedProfilePrefStore() { | |
| 202 scoped_refptr<PersistentPrefStore> pref_store = | |
| 203 manager_->CreateDeprecatedCombinedProfilePrefStore( | |
| 204 main_message_loop_.task_runner()); | |
| 205 InitializePrefStore(pref_store.get()); | |
| 206 pref_store = NULL; | |
| 207 base::RunLoop().RunUntilIdle(); | |
| 208 } | |
| 209 | |
| 210 void InitializePrefStore(PersistentPrefStore* pref_store) { | 199 void InitializePrefStore(PersistentPrefStore* pref_store) { |
| 211 pref_store->AddObserver(®istry_verifier_); | 200 pref_store->AddObserver(®istry_verifier_); |
| 212 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | 201 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); |
| 213 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | 202 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
| 214 pref_store->SetValue(kTrackedAtomic, | 203 pref_store->SetValue(kTrackedAtomic, |
| 215 base::WrapUnique(new base::StringValue(kFoobar)), | 204 base::WrapUnique(new base::StringValue(kFoobar)), |
| 216 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 205 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 217 pref_store->SetValue(kProtectedAtomic, | 206 pref_store->SetValue(kProtectedAtomic, |
| 218 base::WrapUnique(new base::StringValue(kHelloWorld)), | 207 base::WrapUnique(new base::StringValue(kHelloWorld)), |
| 219 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 // will be discarded at load time, leaving this preference undefined. | 314 // will be discarded at load time, leaving this preference undefined. |
| 326 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 315 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| 327 pref_store_->GetValue(kProtectedAtomic, NULL)); | 316 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 328 VerifyResetRecorded( | 317 VerifyResetRecorded( |
| 329 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | 318 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); |
| 330 | 319 |
| 331 ExpectValidationObserved(kTrackedAtomic); | 320 ExpectValidationObserved(kTrackedAtomic); |
| 332 ExpectValidationObserved(kProtectedAtomic); | 321 ExpectValidationObserved(kProtectedAtomic); |
| 333 } | 322 } |
| 334 | 323 |
| 335 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) { | |
| 336 InitializeDeprecatedCombinedProfilePrefStore(); | |
| 337 | |
| 338 // The deprecated model stores hashes in local state (on supported | |
| 339 // platforms).. | |
| 340 ASSERT_EQ( | |
| 341 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 342 local_state_.GetUserPrefValue( | |
| 343 PrefServiceHashStoreContents::kProfilePreferenceHashes) != NULL); | |
| 344 | |
| 345 LoadExistingPrefs(); | |
| 346 | |
| 347 // After a first migration, the hashes were copied to the two user preference | |
| 348 // files but were not cleaned. | |
| 349 ASSERT_EQ( | |
| 350 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 351 local_state_.GetUserPrefValue( | |
| 352 PrefServiceHashStoreContents::kProfilePreferenceHashes) != NULL); | |
| 353 | |
| 354 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | |
| 355 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
| 356 VerifyResetRecorded(false); | |
| 357 | |
| 358 LoadExistingPrefs(); | |
| 359 | |
| 360 // In a subsequent launch, the local state hash store should be reset. | |
| 361 ASSERT_FALSE(local_state_.GetUserPrefValue( | |
| 362 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
| 363 | |
| 364 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | |
| 365 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
| 366 VerifyResetRecorded(false); | |
| 367 } | |
| 368 | |
| 369 TEST_F(ProfilePrefStoreManagerTest, MigrateWithTampering) { | |
| 370 InitializeDeprecatedCombinedProfilePrefStore(); | |
| 371 | |
| 372 ReplaceStringInPrefs(kFoobar, kBarfoo); | |
| 373 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | |
| 374 | |
| 375 // The deprecated model stores hashes in local state (on supported | |
| 376 // platforms).. | |
| 377 ASSERT_EQ( | |
| 378 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 379 local_state_.GetUserPrefValue( | |
| 380 PrefServiceHashStoreContents::kProfilePreferenceHashes) != NULL); | |
| 381 | |
| 382 LoadExistingPrefs(); | |
| 383 | |
| 384 // After a first migration, the hashes were copied to the two user preference | |
| 385 // files but were not cleaned. | |
| 386 ASSERT_EQ( | |
| 387 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 388 local_state_.GetUserPrefValue( | |
| 389 PrefServiceHashStoreContents::kProfilePreferenceHashes) != NULL); | |
| 390 | |
| 391 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | |
| 392 // disk. | |
| 393 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | |
| 394 | |
| 395 // If preference tracking is supported, the tampered value of kProtectedAtomic | |
| 396 // will be discarded at load time, leaving this preference undefined. | |
| 397 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 398 pref_store_->GetValue(kProtectedAtomic, NULL)); | |
| 399 VerifyResetRecorded( | |
| 400 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 401 | |
| 402 LoadExistingPrefs(); | |
| 403 | |
| 404 // In a subsequent launch, the local state hash store would be reset. | |
| 405 ASSERT_FALSE(local_state_.GetUserPrefValue( | |
| 406 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
| 407 | |
| 408 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | |
| 409 VerifyResetRecorded(false); | |
| 410 } | |
| 411 | |
| 412 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 324 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
| 413 base::DictionaryValue master_prefs; | 325 base::DictionaryValue master_prefs; |
| 414 master_prefs.Set(kTrackedAtomic, new base::StringValue(kFoobar)); | 326 master_prefs.Set(kTrackedAtomic, new base::StringValue(kFoobar)); |
| 415 master_prefs.Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); | 327 master_prefs.Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); |
| 416 EXPECT_TRUE(manager_->InitializePrefsFromMasterPrefs(master_prefs)); | 328 EXPECT_TRUE(manager_->InitializePrefsFromMasterPrefs(master_prefs)); |
| 417 | 329 |
| 418 LoadExistingPrefs(); | 330 LoadExistingPrefs(); |
| 419 | 331 |
| 420 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 332 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
| 421 // necessary to authenticate these values. | 333 // necessary to authenticate these values. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 | 483 |
| 572 // Trigger the logic that migrates it back to the unprotected preferences | 484 // Trigger the logic that migrates it back to the unprotected preferences |
| 573 // file. | 485 // file. |
| 574 pref_store_->SetValue(kProtectedAtomic, | 486 pref_store_->SetValue(kProtectedAtomic, |
| 575 base::WrapUnique(new base::StringValue(kGoodbyeWorld)), | 487 base::WrapUnique(new base::StringValue(kGoodbyeWorld)), |
| 576 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 488 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 577 LoadExistingPrefs(); | 489 LoadExistingPrefs(); |
| 578 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 490 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
| 579 VerifyResetRecorded(false); | 491 VerifyResetRecorded(false); |
| 580 } | 492 } |
| OLD | NEW |