| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_helpers.h" |
| 13 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 14 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
| 15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/macros.h" | 18 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 20 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
| 21 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
| 24 #include "components/prefs/json_pref_store.h" | 25 #include "components/prefs/json_pref_store.h" |
| 25 #include "components/prefs/persistent_pref_store.h" | 26 #include "components/prefs/persistent_pref_store.h" |
| 26 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 27 #include "components/prefs/pref_service_factory.h" | 28 #include "components/prefs/pref_service_factory.h" |
| 28 #include "components/prefs/pref_store.h" | 29 #include "components/prefs/pref_store.h" |
| 29 #include "components/prefs/testing_pref_service.h" | 30 #include "components/prefs/testing_pref_service.h" |
| 30 #include "components/user_prefs/tracked/mock_validation_delegate.h" | 31 #include "components/user_prefs/tracked/mock_validation_delegate.h" |
| 31 #include "components/user_prefs/tracked/pref_hash_filter.h" | 32 #include "components/user_prefs/tracked/pref_hash_filter.h" |
| 32 #include "components/user_prefs/tracked/pref_names.h" | 33 #include "components/user_prefs/tracked/pref_names.h" |
| 34 #include "services/preferences/public/cpp/persistent_pref_store_client.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 | 38 |
| 37 class FirstEqualsPredicate { | 39 class FirstEqualsPredicate { |
| 38 public: | 40 public: |
| 39 explicit FirstEqualsPredicate(const std::string& expected) | 41 explicit FirstEqualsPredicate(const std::string& expected) |
| 40 : expected_(expected) {} | 42 : expected_(expected) {} |
| 41 bool operator()(const PrefValueMap::Map::value_type& pair) { | 43 bool operator()(const PrefValueMap::Map::value_type& pair) { |
| 42 return pair.first == expected_; | 44 return pair.first == expected_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 61 FirstEqualsPredicate(key))) | 63 FirstEqualsPredicate(key))) |
| 62 << "Unregistered key " << key << " was changed."; | 64 << "Unregistered key " << key << " was changed."; |
| 63 } | 65 } |
| 64 | 66 |
| 65 void OnInitializationCompleted(bool succeeded) override {} | 67 void OnInitializationCompleted(bool succeeded) override {} |
| 66 | 68 |
| 67 private: | 69 private: |
| 68 scoped_refptr<PrefRegistry> pref_registry_; | 70 scoped_refptr<PrefRegistry> pref_registry_; |
| 69 }; | 71 }; |
| 70 | 72 |
| 73 class PrefStoreReadObserver : public PrefStore::Observer { |
| 74 public: |
| 75 explicit PrefStoreReadObserver(scoped_refptr<PersistentPrefStore> pref_store) |
| 76 : pref_store_(std::move(pref_store)) { |
| 77 pref_store_->AddObserver(this); |
| 78 } |
| 79 |
| 80 ~PrefStoreReadObserver() override { pref_store_->RemoveObserver(this); } |
| 81 |
| 82 PersistentPrefStore::PrefReadError Read() { |
| 83 base::RunLoop run_loop; |
| 84 stop_waiting_ = run_loop.QuitClosure(); |
| 85 pref_store_->ReadPrefsAsync(nullptr); |
| 86 run_loop.Run(); |
| 87 return pref_store_->GetReadError(); |
| 88 } |
| 89 |
| 90 // PrefStore::Observer implementation |
| 91 void OnPrefValueChanged(const std::string& key) override {} |
| 92 |
| 93 void OnInitializationCompleted(bool succeeded) override { |
| 94 if (!stop_waiting_.is_null()) { |
| 95 base::ResetAndReturn(&stop_waiting_).Run(); |
| 96 } |
| 97 } |
| 98 |
| 99 private: |
| 100 scoped_refptr<PersistentPrefStore> pref_store_; |
| 101 base::Closure stop_waiting_; |
| 102 }; |
| 103 |
| 71 const char kUnprotectedPref[] = "unprotected_pref"; | 104 const char kUnprotectedPref[] = "unprotected_pref"; |
| 72 const char kTrackedAtomic[] = "tracked_atomic"; | 105 const char kTrackedAtomic[] = "tracked_atomic"; |
| 73 const char kProtectedAtomic[] = "protected_atomic"; | 106 const char kProtectedAtomic[] = "protected_atomic"; |
| 74 | 107 |
| 75 const char kFoobar[] = "FOOBAR"; | 108 const char kFoobar[] = "FOOBAR"; |
| 76 const char kBarfoo[] = "BARFOO"; | 109 const char kBarfoo[] = "BARFOO"; |
| 77 const char kHelloWorld[] = "HELLOWORLD"; | 110 const char kHelloWorld[] = "HELLOWORLD"; |
| 78 const char kGoodbyeWorld[] = "GOODBYEWORLD"; | 111 const char kGoodbyeWorld[] = "GOODBYEWORLD"; |
| 79 | 112 |
| 80 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { | 113 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { |
| 81 {0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT, | 114 {0u, kTrackedAtomic, PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT, |
| 82 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}, | 115 PrefHashFilter::PrefTrackingStrategy::ATOMIC}, |
| 83 {1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, | 116 {1u, kProtectedAtomic, PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 84 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}}; | 117 PrefHashFilter::PrefTrackingStrategy::ATOMIC}}; |
| 85 | 118 |
| 86 const size_t kExtraReportingId = 2u; | 119 const size_t kExtraReportingId = 2u; |
| 87 const size_t kReportingIdCount = 3u; | 120 const size_t kReportingIdCount = 3u; |
| 88 | 121 |
| 89 } // namespace | 122 } // namespace |
| 90 | 123 |
| 91 class ProfilePrefStoreManagerTest : public testing::Test { | 124 class ProfilePrefStoreManagerTest : public testing::Test { |
| 92 public: | 125 public: |
| 93 ProfilePrefStoreManagerTest() | 126 ProfilePrefStoreManagerTest() |
| 94 : configuration_(kConfiguration, | 127 : configuration_(kConfiguration, |
| 95 kConfiguration + arraysize(kConfiguration)), | 128 kConfiguration + arraysize(kConfiguration)), |
| 96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), | 129 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), |
| 97 registry_verifier_(profile_pref_registry_.get()), | 130 registry_verifier_(profile_pref_registry_.get()), |
| 98 seed_("seed"), | 131 seed_("seed"), |
| 99 reset_recorded_(false) {} | 132 reset_recorded_(false) {} |
| 100 | 133 |
| 101 void SetUp() override { | 134 void SetUp() override { |
| 102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; | 135 mock_validation_delegate_record_ = new MockValidationDelegateRecord; |
| 103 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( | |
| 104 mock_validation_delegate_record_); | |
| 105 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); | 136 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
| 106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; | 137 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
| 107 it != kConfiguration + arraysize(kConfiguration); | 138 it != kConfiguration + arraysize(kConfiguration); |
| 108 ++it) { | 139 ++it) { |
| 109 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { | 140 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { |
| 110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 141 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
| 111 } else { | 142 } else { |
| 112 profile_pref_registry_->RegisterDictionaryPref(it->name); | 143 profile_pref_registry_->RegisterDictionaryPref(it->name); |
| 113 } | 144 } |
| 114 } | 145 } |
| 115 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); | 146 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); |
| 116 | 147 |
| 117 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be | 148 // 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 | 149 // declared as protected in order to be read from the proper store by the |
| 119 // SegregatedPrefStore. Only declare it after configured prefs have been | 150 // SegregatedPrefStore. Only declare it after configured prefs have been |
| 120 // registered above for this test as kPreferenceResetTime is already | 151 // registered above for this test as kPreferenceResetTime is already |
| 121 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. | 152 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
| 122 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = | 153 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { |
| 123 {configuration_.rbegin()->reporting_id + 1, | 154 configuration_.rbegin()->reporting_id + 1, |
| 124 user_prefs::kPreferenceResetTime, | 155 user_prefs::kPreferenceResetTime, |
| 125 PrefHashFilter::ENFORCE_ON_LOAD, | 156 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 126 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 157 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 127 configuration_.push_back(pref_reset_time_config); | 158 configuration_.push_back(pref_reset_time_config); |
| 128 | 159 |
| 129 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 160 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 130 ReloadConfiguration(); | 161 ReloadConfiguration(); |
| 131 } | 162 } |
| 132 | 163 |
| 133 void ReloadConfiguration() { | 164 void ReloadConfiguration() { |
| 134 manager_.reset(new ProfilePrefStoreManager( | 165 manager_.reset(new ProfilePrefStoreManager( |
| 135 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, | 166 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, |
| 136 "device_id", &local_state_)); | 167 "device_id", &local_state_)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 163 | 194 |
| 164 std::unique_ptr<PrefService> pref_service( | 195 std::unique_ptr<PrefService> pref_service( |
| 165 pref_service_factory.Create(profile_pref_registry_.get())); | 196 pref_service_factory.Create(profile_pref_registry_.get())); |
| 166 | 197 |
| 167 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); | 198 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); |
| 168 } | 199 } |
| 169 | 200 |
| 170 void InitializePrefs() { | 201 void InitializePrefs() { |
| 171 // According to the implementation of ProfilePrefStoreManager, this is | 202 // According to the implementation of ProfilePrefStoreManager, this is |
| 172 // actually a SegregatedPrefStore backed by two underlying pref stores. | 203 // actually a SegregatedPrefStore backed by two underlying pref stores. |
| 204 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 205 mock_validation_delegate_record_); |
| 206 prefs::mojom::PersistentPrefStoreConnectorPtr connector; |
| 173 scoped_refptr<PersistentPrefStore> pref_store = | 207 scoped_refptr<PersistentPrefStore> pref_store = |
| 174 manager_->CreateProfilePrefStore( | 208 manager_->CreateProfilePrefStore( |
| 175 main_message_loop_.task_runner(), | 209 main_message_loop_.task_runner(), main_message_loop_.task_runner(), |
| 176 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 210 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 177 base::Unretained(this)), | 211 base::Unretained(this)), |
| 178 mock_validation_delegate_.get()); | 212 &mock_validation_delegate_, &connector); |
| 213 if (!pref_store) { |
| 214 ASSERT_TRUE(connector); |
| 215 pref_store = new prefs::PersistentPrefStoreMojo(std::move(connector)); |
| 216 } |
| 179 InitializePrefStore(pref_store.get()); | 217 InitializePrefStore(pref_store.get()); |
| 180 pref_store = NULL; | 218 pref_store = NULL; |
| 181 base::RunLoop().RunUntilIdle(); | 219 base::RunLoop().RunUntilIdle(); |
| 182 } | 220 } |
| 183 | 221 |
| 184 void DestroyPrefStore() { | 222 void DestroyPrefStore() { |
| 185 if (pref_store_.get()) { | 223 if (pref_store_.get()) { |
| 186 ClearResetRecorded(); | 224 ClearResetRecorded(); |
| 187 // Force everything to be written to disk, triggering the PrefHashFilter | 225 // Force everything to be written to disk, triggering the PrefHashFilter |
| 188 // while our RegistryVerifier is watching. | 226 // while our RegistryVerifier is watching. |
| 189 pref_store_->CommitPendingWrite(); | 227 pref_store_->CommitPendingWrite(); |
| 190 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
| 191 | 229 |
| 192 pref_store_->RemoveObserver(®istry_verifier_); | 230 pref_store_->RemoveObserver(®istry_verifier_); |
| 193 pref_store_ = NULL; | 231 pref_store_ = NULL; |
| 194 // Nothing should have to happen on the background threads, but just in | 232 // Nothing should have to happen on the background threads, but just in |
| 195 // case... | 233 // case... |
| 196 base::RunLoop().RunUntilIdle(); | 234 base::RunLoop().RunUntilIdle(); |
| 197 } | 235 } |
| 198 } | 236 } |
| 199 | 237 |
| 200 void InitializePrefStore(PersistentPrefStore* pref_store) { | 238 void InitializePrefStore(PersistentPrefStore* pref_store) { |
| 201 pref_store->AddObserver(®istry_verifier_); | 239 pref_store->AddObserver(®istry_verifier_); |
| 202 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | 240 PrefStoreReadObserver read_observer(pref_store); |
| 241 PersistentPrefStore::PrefReadError error = read_observer.Read(); |
| 203 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | 242 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
| 204 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), | 243 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), |
| 205 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 244 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 206 pref_store->SetValue(kProtectedAtomic, | 245 pref_store->SetValue(kProtectedAtomic, |
| 207 base::MakeUnique<base::Value>(kHelloWorld), | 246 base::MakeUnique<base::Value>(kHelloWorld), |
| 208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 247 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 209 pref_store->SetValue(kUnprotectedPref, | 248 pref_store->SetValue(kUnprotectedPref, |
| 210 base::MakeUnique<base::Value>(kFoobar), | 249 base::MakeUnique<base::Value>(kFoobar), |
| 211 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 250 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 212 pref_store->RemoveObserver(®istry_verifier_); | 251 pref_store->RemoveObserver(®istry_verifier_); |
| 213 pref_store->CommitPendingWrite(); | 252 pref_store->CommitPendingWrite(); |
| 214 base::RunLoop().RunUntilIdle(); | 253 base::RunLoop().RunUntilIdle(); |
| 215 } | 254 } |
| 216 | 255 |
| 217 void LoadExistingPrefs() { | 256 void LoadExistingPrefs() { |
| 218 DestroyPrefStore(); | 257 DestroyPrefStore(); |
| 258 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 259 validation_delegate; |
| 260 prefs::mojom::PersistentPrefStoreConnectorPtr connector; |
| 219 pref_store_ = manager_->CreateProfilePrefStore( | 261 pref_store_ = manager_->CreateProfilePrefStore( |
| 220 main_message_loop_.task_runner(), | 262 main_message_loop_.task_runner(), main_message_loop_.task_runner(), |
| 221 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 263 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 222 base::Unretained(this)), | 264 base::Unretained(this)), |
| 223 NULL); | 265 &validation_delegate, &connector); |
| 266 if (!pref_store_) { |
| 267 ASSERT_TRUE(connector); |
| 268 pref_store_ = new prefs::PersistentPrefStoreMojo(std::move(connector)); |
| 269 } |
| 224 pref_store_->AddObserver(®istry_verifier_); | 270 pref_store_->AddObserver(®istry_verifier_); |
| 225 pref_store_->ReadPrefs(); | 271 PrefStoreReadObserver read_observer(pref_store_); |
| 272 read_observer.Read(); |
| 226 } | 273 } |
| 227 | 274 |
| 228 void ReplaceStringInPrefs(const std::string& find, | 275 void ReplaceStringInPrefs(const std::string& find, |
| 229 const std::string& replace) { | 276 const std::string& replace) { |
| 230 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, | 277 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, |
| 231 base::FileEnumerator::FILES); | 278 base::FileEnumerator::FILES); |
| 232 | 279 |
| 233 for (base::FilePath path = file_enum.Next(); !path.empty(); | 280 for (base::FilePath path = file_enum.Next(); !path.empty(); |
| 234 path = file_enum.Next()) { | 281 path = file_enum.Next()) { |
| 235 // Tamper with the file's contents | 282 // Tamper with the file's contents |
| (...skipping 26 matching lines...) Expand all Loading... |
| 262 ADD_FAILURE() << "No validation observed for preference: " << pref_path; | 309 ADD_FAILURE() << "No validation observed for preference: " << pref_path; |
| 263 } | 310 } |
| 264 | 311 |
| 265 base::MessageLoop main_message_loop_; | 312 base::MessageLoop main_message_loop_; |
| 266 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; | 313 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; |
| 267 base::ScopedTempDir profile_dir_; | 314 base::ScopedTempDir profile_dir_; |
| 268 TestingPrefServiceSimple local_state_; | 315 TestingPrefServiceSimple local_state_; |
| 269 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; | 316 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; |
| 270 RegistryVerifier registry_verifier_; | 317 RegistryVerifier registry_verifier_; |
| 271 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; | 318 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; |
| 272 std::unique_ptr<MockValidationDelegate> mock_validation_delegate_; | 319 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 320 mock_validation_delegate_; |
| 273 std::unique_ptr<ProfilePrefStoreManager> manager_; | 321 std::unique_ptr<ProfilePrefStoreManager> manager_; |
| 274 scoped_refptr<PersistentPrefStore> pref_store_; | 322 scoped_refptr<PersistentPrefStore> pref_store_; |
| 275 | 323 |
| 276 std::string seed_; | 324 std::string seed_; |
| 277 | 325 |
| 278 private: | 326 private: |
| 279 void RecordReset() { | 327 void RecordReset() { |
| 280 // As-is |reset_recorded_| is only designed to remember a single reset, make | 328 // As-is |reset_recorded_| is only designed to remember a single reset, make |
| 281 // sure none was previously recorded (or that ClearResetRecorded() was | 329 // sure none was previously recorded (or that ClearResetRecorded() was |
| 282 // called). | 330 // called). |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 DestroyPrefStore(); | 399 DestroyPrefStore(); |
| 352 | 400 |
| 353 ReplaceStringInPrefs(kFoobar, kBarfoo); | 401 ReplaceStringInPrefs(kFoobar, kBarfoo); |
| 354 | 402 |
| 355 // It's unprotected, so we can load the modified value. | 403 // It's unprotected, so we can load the modified value. |
| 356 LoadExistingPrefs(); | 404 LoadExistingPrefs(); |
| 357 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 405 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 358 | 406 |
| 359 // Now update the configuration to protect it. | 407 // Now update the configuration to protect it. |
| 360 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 408 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 361 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 409 kExtraReportingId, kUnprotectedPref, |
| 362 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 410 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 411 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 363 configuration_.push_back(new_protected); | 412 configuration_.push_back(new_protected); |
| 364 ReloadConfiguration(); | 413 ReloadConfiguration(); |
| 365 | 414 |
| 366 // And try loading with the new configuration. | 415 // And try loading with the new configuration. |
| 367 LoadExistingPrefs(); | 416 LoadExistingPrefs(); |
| 368 | 417 |
| 369 // Since there was a valid super MAC we were able to extend the existing trust | 418 // Since there was a valid super MAC we were able to extend the existing trust |
| 370 // to the newly protected preference. | 419 // to the newly protected preference. |
| 371 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 420 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 372 VerifyResetRecorded(false); | 421 VerifyResetRecorded(false); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 384 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | 433 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); |
| 385 } | 434 } |
| 386 | 435 |
| 387 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | 436 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
| 388 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 437 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 389 original_configuration = configuration_; | 438 original_configuration = configuration_; |
| 390 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 439 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 391 configuration_.begin(); | 440 configuration_.begin(); |
| 392 it != configuration_.end(); | 441 it != configuration_.end(); |
| 393 ++it) { | 442 ++it) { |
| 394 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; | 443 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 395 } | 444 } |
| 396 ReloadConfiguration(); | 445 ReloadConfiguration(); |
| 397 | 446 |
| 398 InitializePrefs(); | 447 InitializePrefs(); |
| 399 | 448 |
| 400 ExpectValidationObserved(kTrackedAtomic); | 449 ExpectValidationObserved(kTrackedAtomic); |
| 401 ExpectValidationObserved(kProtectedAtomic); | 450 ExpectValidationObserved(kProtectedAtomic); |
| 402 | 451 |
| 403 LoadExistingPrefs(); | 452 LoadExistingPrefs(); |
| 404 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 453 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 405 | 454 |
| 406 // Ensure everything is written out to disk. | 455 // Ensure everything is written out to disk. |
| 407 DestroyPrefStore(); | 456 DestroyPrefStore(); |
| 408 | 457 |
| 409 // Now introduce protection, including the never-before tracked "new_pref". | 458 // Now introduce protection, including the never-before tracked "new_pref". |
| 410 configuration_ = original_configuration; | 459 configuration_ = original_configuration; |
| 411 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 460 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 412 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 461 kExtraReportingId, kUnprotectedPref, |
| 413 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 462 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 463 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 414 configuration_.push_back(new_protected); | 464 configuration_.push_back(new_protected); |
| 415 ReloadConfiguration(); | 465 ReloadConfiguration(); |
| 416 | 466 |
| 417 // And try loading with the new configuration. | 467 // And try loading with the new configuration. |
| 418 LoadExistingPrefs(); | 468 LoadExistingPrefs(); |
| 419 | 469 |
| 420 // Since there was a valid super MAC we were able to extend the existing trust | 470 // Since there was a valid super MAC we were able to extend the existing trust |
| 421 // to the newly tracked & protected preference. | 471 // to the newly tracked & protected preference. |
| 422 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 472 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 423 VerifyResetRecorded(false); | 473 VerifyResetRecorded(false); |
| 424 } | 474 } |
| 425 | 475 |
| 426 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | 476 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
| 427 InitializePrefs(); | 477 InitializePrefs(); |
| 428 | 478 |
| 429 ExpectValidationObserved(kTrackedAtomic); | 479 ExpectValidationObserved(kTrackedAtomic); |
| 430 ExpectValidationObserved(kProtectedAtomic); | 480 ExpectValidationObserved(kProtectedAtomic); |
| 431 | 481 |
| 432 // Now update the configuration to protect it. | 482 // Now update the configuration to protect it. |
| 433 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 483 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 434 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 484 kExtraReportingId, kUnprotectedPref, |
| 435 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 485 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 486 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 436 configuration_.push_back(new_protected); | 487 configuration_.push_back(new_protected); |
| 437 seed_ = "new-seed-to-break-trust"; | 488 seed_ = "new-seed-to-break-trust"; |
| 438 ReloadConfiguration(); | 489 ReloadConfiguration(); |
| 439 | 490 |
| 440 // And try loading with the new configuration. | 491 // And try loading with the new configuration. |
| 441 LoadExistingPrefs(); | 492 LoadExistingPrefs(); |
| 442 | 493 |
| 443 // If preference tracking is supported, kUnprotectedPref will have been | 494 // If preference tracking is supported, kUnprotectedPref will have been |
| 444 // discarded because new values are not accepted without a valid super MAC. | 495 // discarded because new values are not accepted without a valid super MAC. |
| 445 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 496 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 457 ExpectValidationObserved(kProtectedAtomic); | 508 ExpectValidationObserved(kProtectedAtomic); |
| 458 | 509 |
| 459 DestroyPrefStore(); | 510 DestroyPrefStore(); |
| 460 | 511 |
| 461 // Unconfigure protection for kProtectedAtomic | 512 // Unconfigure protection for kProtectedAtomic |
| 462 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 513 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 463 configuration_.begin(); | 514 configuration_.begin(); |
| 464 it != configuration_.end(); | 515 it != configuration_.end(); |
| 465 ++it) { | 516 ++it) { |
| 466 if (it->name == kProtectedAtomic) { | 517 if (it->name == kProtectedAtomic) { |
| 467 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; | 518 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 468 break; | 519 break; |
| 469 } | 520 } |
| 470 } | 521 } |
| 471 | 522 |
| 472 seed_ = "new-seed-to-break-trust"; | 523 seed_ = "new-seed-to-break-trust"; |
| 473 ReloadConfiguration(); | 524 ReloadConfiguration(); |
| 474 LoadExistingPrefs(); | 525 LoadExistingPrefs(); |
| 475 | 526 |
| 476 // Verify that the value was not reset. | 527 // Verify that the value was not reset. |
| 477 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 528 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 478 VerifyResetRecorded(false); | 529 VerifyResetRecorded(false); |
| 479 | 530 |
| 480 // Accessing the value of the previously protected pref didn't trigger its | 531 // 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 | 532 // move to the unprotected preferences file, though the loading of the pref |
| 482 // store should still have caused the MAC store to be recalculated. | 533 // store should still have caused the MAC store to be recalculated. |
| 483 LoadExistingPrefs(); | 534 LoadExistingPrefs(); |
| 484 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 535 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 485 | 536 |
| 486 // Trigger the logic that migrates it back to the unprotected preferences | 537 // Trigger the logic that migrates it back to the unprotected preferences |
| 487 // file. | 538 // file. |
| 488 pref_store_->SetValue(kProtectedAtomic, | 539 pref_store_->SetValue(kProtectedAtomic, |
| 489 base::WrapUnique(new base::Value(kGoodbyeWorld)), | 540 base::WrapUnique(new base::Value(kGoodbyeWorld)), |
| 490 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 541 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 491 LoadExistingPrefs(); | 542 LoadExistingPrefs(); |
| 492 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 543 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
| 493 VerifyResetRecorded(false); | 544 VerifyResetRecorded(false); |
| 494 } | 545 } |
| OLD | NEW |