| 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 <utility> | 10 #include <utility> |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 const char kUnprotectedPref[] = "unprotected_pref"; | 71 const char kUnprotectedPref[] = "unprotected_pref"; |
| 72 const char kTrackedAtomic[] = "tracked_atomic"; | 72 const char kTrackedAtomic[] = "tracked_atomic"; |
| 73 const char kProtectedAtomic[] = "protected_atomic"; | 73 const char kProtectedAtomic[] = "protected_atomic"; |
| 74 | 74 |
| 75 const char kFoobar[] = "FOOBAR"; | 75 const char kFoobar[] = "FOOBAR"; |
| 76 const char kBarfoo[] = "BARFOO"; | 76 const char kBarfoo[] = "BARFOO"; |
| 77 const char kHelloWorld[] = "HELLOWORLD"; | 77 const char kHelloWorld[] = "HELLOWORLD"; |
| 78 const char kGoodbyeWorld[] = "GOODBYEWORLD"; | 78 const char kGoodbyeWorld[] = "GOODBYEWORLD"; |
| 79 | 79 |
| 80 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { | 80 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { |
| 81 {0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT, | 81 {0u, kTrackedAtomic, PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT, |
| 82 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}, | 82 PrefHashFilter::PrefTrackingStrategy::ATOMIC}, |
| 83 {1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, | 83 {1u, kProtectedAtomic, PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 84 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}}; | 84 PrefHashFilter::PrefTrackingStrategy::ATOMIC}}; |
| 85 | 85 |
| 86 const size_t kExtraReportingId = 2u; | 86 const size_t kExtraReportingId = 2u; |
| 87 const size_t kReportingIdCount = 3u; | 87 const size_t kReportingIdCount = 3u; |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 class ProfilePrefStoreManagerTest : public testing::Test { | 91 class ProfilePrefStoreManagerTest : public testing::Test { |
| 92 public: | 92 public: |
| 93 ProfilePrefStoreManagerTest() | 93 ProfilePrefStoreManagerTest() |
| 94 : configuration_(kConfiguration, | 94 : configuration_(kConfiguration, |
| 95 kConfiguration + arraysize(kConfiguration)), | 95 kConfiguration + arraysize(kConfiguration)), |
| 96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), | 96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), |
| 97 registry_verifier_(profile_pref_registry_.get()), | 97 registry_verifier_(profile_pref_registry_.get()), |
| 98 seed_("seed"), | 98 seed_("seed"), |
| 99 reset_recorded_(false) {} | 99 reset_recorded_(false) {} |
| 100 | 100 |
| 101 void SetUp() override { | 101 void SetUp() override { |
| 102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; | 102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; |
| 103 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( | 103 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 104 mock_validation_delegate_record_); | 104 mock_validation_delegate_record_); |
| 105 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); | 105 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
| 106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; | 106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
| 107 it != kConfiguration + arraysize(kConfiguration); | 107 it != kConfiguration + arraysize(kConfiguration); |
| 108 ++it) { | 108 ++it) { |
| 109 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { | 109 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { |
| 110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
| 111 } else { | 111 } else { |
| 112 profile_pref_registry_->RegisterDictionaryPref(it->name); | 112 profile_pref_registry_->RegisterDictionaryPref(it->name); |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); | 115 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); |
| 116 | 116 |
| 117 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be | 117 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be |
| 118 // declared as protected in order to be read from the proper store by the | 118 // declared as protected in order to be read from the proper store by the |
| 119 // SegregatedPrefStore. Only declare it after configured prefs have been | 119 // SegregatedPrefStore. Only declare it after configured prefs have been |
| 120 // registered above for this test as kPreferenceResetTime is already | 120 // registered above for this test as kPreferenceResetTime is already |
| 121 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. | 121 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
| 122 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = | 122 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { |
| 123 {configuration_.rbegin()->reporting_id + 1, | 123 configuration_.rbegin()->reporting_id + 1, |
| 124 user_prefs::kPreferenceResetTime, | 124 user_prefs::kPreferenceResetTime, |
| 125 PrefHashFilter::ENFORCE_ON_LOAD, | 125 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 126 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 126 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 127 configuration_.push_back(pref_reset_time_config); | 127 configuration_.push_back(pref_reset_time_config); |
| 128 | 128 |
| 129 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 129 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 130 ReloadConfiguration(); | 130 ReloadConfiguration(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void ReloadConfiguration() { | 133 void ReloadConfiguration() { |
| 134 manager_.reset(new ProfilePrefStoreManager( | 134 manager_.reset(new ProfilePrefStoreManager( |
| 135 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, | 135 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, |
| 136 "device_id", &local_state_)); | 136 "device_id", &local_state_)); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 DestroyPrefStore(); | 351 DestroyPrefStore(); |
| 352 | 352 |
| 353 ReplaceStringInPrefs(kFoobar, kBarfoo); | 353 ReplaceStringInPrefs(kFoobar, kBarfoo); |
| 354 | 354 |
| 355 // It's unprotected, so we can load the modified value. | 355 // It's unprotected, so we can load the modified value. |
| 356 LoadExistingPrefs(); | 356 LoadExistingPrefs(); |
| 357 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 357 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 358 | 358 |
| 359 // Now update the configuration to protect it. | 359 // Now update the configuration to protect it. |
| 360 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 360 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 361 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 361 kExtraReportingId, kUnprotectedPref, |
| 362 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 362 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 363 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 363 configuration_.push_back(new_protected); | 364 configuration_.push_back(new_protected); |
| 364 ReloadConfiguration(); | 365 ReloadConfiguration(); |
| 365 | 366 |
| 366 // And try loading with the new configuration. | 367 // And try loading with the new configuration. |
| 367 LoadExistingPrefs(); | 368 LoadExistingPrefs(); |
| 368 | 369 |
| 369 // Since there was a valid super MAC we were able to extend the existing trust | 370 // Since there was a valid super MAC we were able to extend the existing trust |
| 370 // to the newly protected preference. | 371 // to the newly protected preference. |
| 371 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 372 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 372 VerifyResetRecorded(false); | 373 VerifyResetRecorded(false); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 384 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | 385 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); |
| 385 } | 386 } |
| 386 | 387 |
| 387 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | 388 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
| 388 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 389 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 389 original_configuration = configuration_; | 390 original_configuration = configuration_; |
| 390 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 391 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 391 configuration_.begin(); | 392 configuration_.begin(); |
| 392 it != configuration_.end(); | 393 it != configuration_.end(); |
| 393 ++it) { | 394 ++it) { |
| 394 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; | 395 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 395 } | 396 } |
| 396 ReloadConfiguration(); | 397 ReloadConfiguration(); |
| 397 | 398 |
| 398 InitializePrefs(); | 399 InitializePrefs(); |
| 399 | 400 |
| 400 ExpectValidationObserved(kTrackedAtomic); | 401 ExpectValidationObserved(kTrackedAtomic); |
| 401 ExpectValidationObserved(kProtectedAtomic); | 402 ExpectValidationObserved(kProtectedAtomic); |
| 402 | 403 |
| 403 LoadExistingPrefs(); | 404 LoadExistingPrefs(); |
| 404 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 405 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 405 | 406 |
| 406 // Ensure everything is written out to disk. | 407 // Ensure everything is written out to disk. |
| 407 DestroyPrefStore(); | 408 DestroyPrefStore(); |
| 408 | 409 |
| 409 // Now introduce protection, including the never-before tracked "new_pref". | 410 // Now introduce protection, including the never-before tracked "new_pref". |
| 410 configuration_ = original_configuration; | 411 configuration_ = original_configuration; |
| 411 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 412 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 412 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 413 kExtraReportingId, kUnprotectedPref, |
| 413 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 414 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 415 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 414 configuration_.push_back(new_protected); | 416 configuration_.push_back(new_protected); |
| 415 ReloadConfiguration(); | 417 ReloadConfiguration(); |
| 416 | 418 |
| 417 // And try loading with the new configuration. | 419 // And try loading with the new configuration. |
| 418 LoadExistingPrefs(); | 420 LoadExistingPrefs(); |
| 419 | 421 |
| 420 // Since there was a valid super MAC we were able to extend the existing trust | 422 // Since there was a valid super MAC we were able to extend the existing trust |
| 421 // to the newly tracked & protected preference. | 423 // to the newly tracked & protected preference. |
| 422 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 424 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 423 VerifyResetRecorded(false); | 425 VerifyResetRecorded(false); |
| 424 } | 426 } |
| 425 | 427 |
| 426 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | 428 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
| 427 InitializePrefs(); | 429 InitializePrefs(); |
| 428 | 430 |
| 429 ExpectValidationObserved(kTrackedAtomic); | 431 ExpectValidationObserved(kTrackedAtomic); |
| 430 ExpectValidationObserved(kProtectedAtomic); | 432 ExpectValidationObserved(kProtectedAtomic); |
| 431 | 433 |
| 432 // Now update the configuration to protect it. | 434 // Now update the configuration to protect it. |
| 433 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 435 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 434 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 436 kExtraReportingId, kUnprotectedPref, |
| 435 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 437 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 438 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 436 configuration_.push_back(new_protected); | 439 configuration_.push_back(new_protected); |
| 437 seed_ = "new-seed-to-break-trust"; | 440 seed_ = "new-seed-to-break-trust"; |
| 438 ReloadConfiguration(); | 441 ReloadConfiguration(); |
| 439 | 442 |
| 440 // And try loading with the new configuration. | 443 // And try loading with the new configuration. |
| 441 LoadExistingPrefs(); | 444 LoadExistingPrefs(); |
| 442 | 445 |
| 443 // If preference tracking is supported, kUnprotectedPref will have been | 446 // If preference tracking is supported, kUnprotectedPref will have been |
| 444 // discarded because new values are not accepted without a valid super MAC. | 447 // discarded because new values are not accepted without a valid super MAC. |
| 445 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 448 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 457 ExpectValidationObserved(kProtectedAtomic); | 460 ExpectValidationObserved(kProtectedAtomic); |
| 458 | 461 |
| 459 DestroyPrefStore(); | 462 DestroyPrefStore(); |
| 460 | 463 |
| 461 // Unconfigure protection for kProtectedAtomic | 464 // Unconfigure protection for kProtectedAtomic |
| 462 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 465 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 463 configuration_.begin(); | 466 configuration_.begin(); |
| 464 it != configuration_.end(); | 467 it != configuration_.end(); |
| 465 ++it) { | 468 ++it) { |
| 466 if (it->name == kProtectedAtomic) { | 469 if (it->name == kProtectedAtomic) { |
| 467 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; | 470 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 468 break; | 471 break; |
| 469 } | 472 } |
| 470 } | 473 } |
| 471 | 474 |
| 472 seed_ = "new-seed-to-break-trust"; | 475 seed_ = "new-seed-to-break-trust"; |
| 473 ReloadConfiguration(); | 476 ReloadConfiguration(); |
| 474 LoadExistingPrefs(); | 477 LoadExistingPrefs(); |
| 475 | 478 |
| 476 // Verify that the value was not reset. | 479 // Verify that the value was not reset. |
| 477 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 480 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 478 VerifyResetRecorded(false); | 481 VerifyResetRecorded(false); |
| 479 | 482 |
| 480 // Accessing the value of the previously protected pref didn't trigger its | 483 // Accessing the value of the previously protected pref didn't trigger its |
| 481 // move to the unprotected preferences file, though the loading of the pref | 484 // move to the unprotected preferences file, though the loading of the pref |
| 482 // store should still have caused the MAC store to be recalculated. | 485 // store should still have caused the MAC store to be recalculated. |
| 483 LoadExistingPrefs(); | 486 LoadExistingPrefs(); |
| 484 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 487 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 485 | 488 |
| 486 // Trigger the logic that migrates it back to the unprotected preferences | 489 // Trigger the logic that migrates it back to the unprotected preferences |
| 487 // file. | 490 // file. |
| 488 pref_store_->SetValue(kProtectedAtomic, | 491 pref_store_->SetValue(kProtectedAtomic, |
| 489 base::WrapUnique(new base::Value(kGoodbyeWorld)), | 492 base::WrapUnique(new base::Value(kGoodbyeWorld)), |
| 490 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 493 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 491 LoadExistingPrefs(); | 494 LoadExistingPrefs(); |
| 492 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 495 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
| 493 VerifyResetRecorded(false); | 496 VerifyResetRecorded(false); |
| 494 } | 497 } |
| OLD | NEW |