| 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" |
| 23 #include "base/test/scoped_feature_list.h" |
| 24 #include "base/test/sequenced_worker_pool_owner.h" |
| 25 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/values.h" | 26 #include "base/values.h" |
| 27 #include "chrome/common/chrome_features.h" |
| 23 #include "components/pref_registry/pref_registry_syncable.h" | 28 #include "components/pref_registry/pref_registry_syncable.h" |
| 24 #include "components/prefs/json_pref_store.h" | 29 #include "components/prefs/json_pref_store.h" |
| 25 #include "components/prefs/persistent_pref_store.h" | 30 #include "components/prefs/persistent_pref_store.h" |
| 26 #include "components/prefs/pref_service.h" | 31 #include "components/prefs/pref_service.h" |
| 27 #include "components/prefs/pref_service_factory.h" | 32 #include "components/prefs/pref_service_factory.h" |
| 28 #include "components/prefs/pref_store.h" | 33 #include "components/prefs/pref_store.h" |
| 29 #include "components/prefs/testing_pref_service.h" | 34 #include "components/prefs/testing_pref_service.h" |
| 30 #include "components/user_prefs/tracked/mock_validation_delegate.h" | 35 #include "components/user_prefs/tracked/mock_validation_delegate.h" |
| 31 #include "components/user_prefs/tracked/pref_hash_filter.h" | 36 #include "components/user_prefs/tracked/pref_hash_filter.h" |
| 32 #include "components/user_prefs/tracked/pref_names.h" | 37 #include "components/user_prefs/tracked/pref_names.h" |
| 38 #include "content/public/common/service_names.mojom.h" |
| 39 #include "services/preferences/public/cpp/pref_service_main.h" |
| 40 #include "services/preferences/public/interfaces/preferences.mojom.h" |
| 41 #include "services/service_manager/public/cpp/connector.h" |
| 42 #include "services/service_manager/public/cpp/service_context.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 44 |
| 35 namespace { | 45 namespace { |
| 36 | 46 |
| 37 class FirstEqualsPredicate { | 47 class FirstEqualsPredicate { |
| 38 public: | 48 public: |
| 39 explicit FirstEqualsPredicate(const std::string& expected) | 49 explicit FirstEqualsPredicate(const std::string& expected) |
| 40 : expected_(expected) {} | 50 : expected_(expected) {} |
| 41 bool operator()(const PrefValueMap::Map::value_type& pair) { | 51 bool operator()(const PrefValueMap::Map::value_type& pair) { |
| 42 return pair.first == expected_; | 52 return pair.first == expected_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 61 FirstEqualsPredicate(key))) | 71 FirstEqualsPredicate(key))) |
| 62 << "Unregistered key " << key << " was changed."; | 72 << "Unregistered key " << key << " was changed."; |
| 63 } | 73 } |
| 64 | 74 |
| 65 void OnInitializationCompleted(bool succeeded) override {} | 75 void OnInitializationCompleted(bool succeeded) override {} |
| 66 | 76 |
| 67 private: | 77 private: |
| 68 scoped_refptr<PrefRegistry> pref_registry_; | 78 scoped_refptr<PrefRegistry> pref_registry_; |
| 69 }; | 79 }; |
| 70 | 80 |
| 81 class PrefStoreReadObserver : public PrefStore::Observer { |
| 82 public: |
| 83 explicit PrefStoreReadObserver(scoped_refptr<PersistentPrefStore> pref_store) |
| 84 : pref_store_(std::move(pref_store)) { |
| 85 pref_store_->AddObserver(this); |
| 86 } |
| 87 |
| 88 ~PrefStoreReadObserver() override { pref_store_->RemoveObserver(this); } |
| 89 |
| 90 PersistentPrefStore::PrefReadError Read() { |
| 91 base::RunLoop run_loop; |
| 92 stop_waiting_ = run_loop.QuitClosure(); |
| 93 pref_store_->ReadPrefsAsync(nullptr); |
| 94 run_loop.Run(); |
| 95 return pref_store_->GetReadError(); |
| 96 } |
| 97 |
| 98 // PrefStore::Observer implementation |
| 99 void OnPrefValueChanged(const std::string& key) override {} |
| 100 |
| 101 void OnInitializationCompleted(bool succeeded) override { |
| 102 if (!stop_waiting_.is_null()) { |
| 103 base::ResetAndReturn(&stop_waiting_).Run(); |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 scoped_refptr<PersistentPrefStore> pref_store_; |
| 109 base::Closure stop_waiting_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(PrefStoreReadObserver); |
| 112 }; |
| 113 |
| 71 const char kUnprotectedPref[] = "unprotected_pref"; | 114 const char kUnprotectedPref[] = "unprotected_pref"; |
| 72 const char kTrackedAtomic[] = "tracked_atomic"; | 115 const char kTrackedAtomic[] = "tracked_atomic"; |
| 73 const char kProtectedAtomic[] = "protected_atomic"; | 116 const char kProtectedAtomic[] = "protected_atomic"; |
| 74 | 117 |
| 75 const char kFoobar[] = "FOOBAR"; | 118 const char kFoobar[] = "FOOBAR"; |
| 76 const char kBarfoo[] = "BARFOO"; | 119 const char kBarfoo[] = "BARFOO"; |
| 77 const char kHelloWorld[] = "HELLOWORLD"; | 120 const char kHelloWorld[] = "HELLOWORLD"; |
| 78 const char kGoodbyeWorld[] = "GOODBYEWORLD"; | 121 const char kGoodbyeWorld[] = "GOODBYEWORLD"; |
| 79 | 122 |
| 80 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { | 123 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { |
| 81 {0u, kTrackedAtomic, PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT, | 124 {0u, kTrackedAtomic, PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT, |
| 82 PrefHashFilter::PrefTrackingStrategy::ATOMIC}, | 125 PrefHashFilter::PrefTrackingStrategy::ATOMIC}, |
| 83 {1u, kProtectedAtomic, PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, | 126 {1u, kProtectedAtomic, PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 84 PrefHashFilter::PrefTrackingStrategy::ATOMIC}}; | 127 PrefHashFilter::PrefTrackingStrategy::ATOMIC}}; |
| 85 | 128 |
| 86 const size_t kExtraReportingId = 2u; | 129 const size_t kExtraReportingId = 2u; |
| 87 const size_t kReportingIdCount = 3u; | 130 const size_t kReportingIdCount = 3u; |
| 88 | 131 |
| 89 } // namespace | 132 } // namespace |
| 90 | 133 |
| 91 class ProfilePrefStoreManagerTest : public testing::Test { | 134 class ProfilePrefStoreManagerTest : public testing::TestWithParam<bool> { |
| 92 public: | 135 public: |
| 93 ProfilePrefStoreManagerTest() | 136 ProfilePrefStoreManagerTest() |
| 94 : configuration_(kConfiguration, | 137 : configuration_(kConfiguration, |
| 95 kConfiguration + arraysize(kConfiguration)), | 138 kConfiguration + arraysize(kConfiguration)), |
| 96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), | 139 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), |
| 97 registry_verifier_(profile_pref_registry_.get()), | 140 registry_verifier_(profile_pref_registry_.get()), |
| 98 seed_("seed"), | 141 seed_("seed"), |
| 99 reset_recorded_(false) {} | 142 reset_recorded_(false) {} |
| 100 | 143 |
| 101 void SetUp() override { | 144 void SetUp() override { |
| 145 worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( |
| 146 2, "ProfilePrefStoreManagerTest"); |
| 147 if (GetParam()) { |
| 148 feature_list_.InitAndEnableFeature(features::kPrefService); |
| 149 service_manager::mojom::ServicePtr service_ptr; |
| 150 pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
| 151 prefs::CreatePrefService(std::set<PrefValueStore::PrefStoreType>(), |
| 152 worker_pool_->pool()), |
| 153 mojo::MakeRequest(&service_ptr)); |
| 154 connector_ = service_manager::Connector::Create(&connector_request_); |
| 155 service_manager::Connector::TestApi test_api(connector_.get()); |
| 156 test_api.OverrideBinderForTesting( |
| 157 prefs::mojom::kPrefStoreServiceName, |
| 158 prefs::mojom::PrefStoreConnector::Name_, |
| 159 base::Bind(&ProfilePrefStoreManagerTest::BindInterface, |
| 160 base::Unretained(this), |
| 161 prefs::mojom::PrefStoreConnector::Name_)); |
| 162 test_api.OverrideBinderForTesting( |
| 163 prefs::mojom::kPrefStoreServiceName, |
| 164 prefs::mojom::PrefServiceControl::Name_, |
| 165 base::Bind(&ProfilePrefStoreManagerTest::BindInterface, |
| 166 base::Unretained(this), |
| 167 prefs::mojom::PrefServiceControl::Name_)); |
| 168 } else { |
| 169 feature_list_.InitAndDisableFeature(features::kPrefService); |
| 170 } |
| 102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; | 171 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()); | 172 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
| 106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; | 173 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
| 107 it != kConfiguration + arraysize(kConfiguration); | 174 it != kConfiguration + arraysize(kConfiguration); |
| 108 ++it) { | 175 ++it) { |
| 109 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { | 176 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { |
| 110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 177 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
| 111 } else { | 178 } else { |
| 112 profile_pref_registry_->RegisterDictionaryPref(it->name); | 179 profile_pref_registry_->RegisterDictionaryPref(it->name); |
| 113 } | 180 } |
| 114 } | 181 } |
| 115 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); | 182 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); |
| 116 | 183 |
| 117 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be | 184 // 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 | 185 // declared as protected in order to be read from the proper store by the |
| 119 // SegregatedPrefStore. Only declare it after configured prefs have been | 186 // SegregatedPrefStore. Only declare it after configured prefs have been |
| 120 // registered above for this test as kPreferenceResetTime is already | 187 // registered above for this test as kPreferenceResetTime is already |
| 121 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. | 188 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
| 122 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { | 189 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { |
| 123 configuration_.rbegin()->reporting_id + 1, | 190 configuration_.rbegin()->reporting_id + 1, |
| 124 user_prefs::kPreferenceResetTime, | 191 user_prefs::kPreferenceResetTime, |
| 125 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, | 192 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 126 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; | 193 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 127 configuration_.push_back(pref_reset_time_config); | 194 configuration_.push_back(pref_reset_time_config); |
| 128 | 195 |
| 129 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 196 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 130 ReloadConfiguration(); | 197 ReloadConfiguration(); |
| 131 } | 198 } |
| 132 | 199 |
| 133 void ReloadConfiguration() { | 200 void ReloadConfiguration() { |
| 201 RelaunchPrefService(); |
| 134 manager_.reset(new ProfilePrefStoreManager( | 202 manager_.reset(new ProfilePrefStoreManager( |
| 135 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, | 203 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, |
| 136 "device_id", &local_state_)); | 204 "device_id", &local_state_)); |
| 137 } | 205 } |
| 138 | 206 |
| 139 void TearDown() override { DestroyPrefStore(); } | 207 void TearDown() override { |
| 208 DestroyPrefStore(); |
| 209 if (GetParam()) { |
| 210 connector_.reset(); |
| 211 pref_service_context_.reset(); |
| 212 } |
| 213 worker_pool_.reset(); |
| 214 } |
| 140 | 215 |
| 141 protected: | 216 protected: |
| 217 bool SupportsPreferenceTracking() { |
| 218 return ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking && |
| 219 !GetParam(); |
| 220 } |
| 221 |
| 222 void RelaunchPrefService() { |
| 223 if (!GetParam()) |
| 224 return; |
| 225 |
| 226 service_manager::mojom::ServicePtr service_ptr; |
| 227 pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
| 228 prefs::CreatePrefService(std::set<PrefValueStore::PrefStoreType>(), |
| 229 worker_pool_->pool()), |
| 230 mojo::MakeRequest(&service_ptr)); |
| 231 } |
| 232 |
| 142 // Verifies whether a reset was reported via the RecordReset() hook. Also | 233 // Verifies whether a reset was reported via the RecordReset() hook. Also |
| 143 // verifies that GetResetTime() was set (or not) accordingly. | 234 // verifies that GetResetTime() was set (or not) accordingly. |
| 144 void VerifyResetRecorded(bool reset_expected) { | 235 void VerifyResetRecorded(bool reset_expected) { |
| 145 EXPECT_EQ(reset_expected, reset_recorded_); | 236 EXPECT_EQ(reset_expected, reset_recorded_); |
| 146 | 237 |
| 147 PrefServiceFactory pref_service_factory; | 238 PrefServiceFactory pref_service_factory; |
| 148 pref_service_factory.set_user_prefs(pref_store_); | 239 pref_service_factory.set_user_prefs(pref_store_); |
| 149 | 240 |
| 150 std::unique_ptr<PrefService> pref_service( | 241 std::unique_ptr<PrefService> pref_service( |
| 151 pref_service_factory.Create(profile_pref_registry_.get())); | 242 pref_service_factory.Create(profile_pref_registry_.get())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 | 254 |
| 164 std::unique_ptr<PrefService> pref_service( | 255 std::unique_ptr<PrefService> pref_service( |
| 165 pref_service_factory.Create(profile_pref_registry_.get())); | 256 pref_service_factory.Create(profile_pref_registry_.get())); |
| 166 | 257 |
| 167 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); | 258 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); |
| 168 } | 259 } |
| 169 | 260 |
| 170 void InitializePrefs() { | 261 void InitializePrefs() { |
| 171 // According to the implementation of ProfilePrefStoreManager, this is | 262 // According to the implementation of ProfilePrefStoreManager, this is |
| 172 // actually a SegregatedPrefStore backed by two underlying pref stores. | 263 // actually a SegregatedPrefStore backed by two underlying pref stores. |
| 264 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 265 mock_validation_delegate_record_); |
| 173 scoped_refptr<PersistentPrefStore> pref_store = | 266 scoped_refptr<PersistentPrefStore> pref_store = |
| 174 manager_->CreateProfilePrefStore( | 267 manager_->CreateProfilePrefStore( |
| 175 main_message_loop_.task_runner(), | 268 main_message_loop_.task_runner(), |
| 176 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 269 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 177 base::Unretained(this)), | 270 base::Unretained(this)), |
| 178 mock_validation_delegate_.get()); | 271 mock_validation_delegate_.get(), connector_.get(), |
| 272 profile_pref_registry_); |
| 179 InitializePrefStore(pref_store.get()); | 273 InitializePrefStore(pref_store.get()); |
| 180 pref_store = NULL; | 274 pref_store = NULL; |
| 181 base::RunLoop().RunUntilIdle(); | 275 base::RunLoop().RunUntilIdle(); |
| 182 } | 276 } |
| 183 | 277 |
| 184 void DestroyPrefStore() { | 278 void DestroyPrefStore() { |
| 185 if (pref_store_.get()) { | 279 if (pref_store_.get()) { |
| 186 ClearResetRecorded(); | 280 ClearResetRecorded(); |
| 187 // Force everything to be written to disk, triggering the PrefHashFilter | 281 // Force everything to be written to disk, triggering the PrefHashFilter |
| 188 // while our RegistryVerifier is watching. | 282 // while our RegistryVerifier is watching. |
| 189 pref_store_->CommitPendingWrite(); | 283 pref_store_->CommitPendingWrite(); |
| 190 base::RunLoop().RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
| 285 base::RunLoop run_loop; |
| 286 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 287 worker_pool_->pool().get()) |
| 288 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
| 289 run_loop.QuitClosure()); |
| 290 run_loop.Run(); |
| 191 | 291 |
| 192 pref_store_->RemoveObserver(®istry_verifier_); | 292 pref_store_->RemoveObserver(®istry_verifier_); |
| 193 pref_store_ = NULL; | 293 pref_store_ = NULL; |
| 194 // Nothing should have to happen on the background threads, but just in | 294 // Nothing should have to happen on the background threads, but just in |
| 195 // case... | 295 // case... |
| 196 base::RunLoop().RunUntilIdle(); | 296 base::RunLoop().RunUntilIdle(); |
| 197 } | 297 } |
| 298 RelaunchPrefService(); |
| 198 } | 299 } |
| 199 | 300 |
| 200 void InitializePrefStore(PersistentPrefStore* pref_store) { | 301 void InitializePrefStore(PersistentPrefStore* pref_store) { |
| 201 pref_store->AddObserver(®istry_verifier_); | 302 pref_store->AddObserver(®istry_verifier_); |
| 202 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | 303 PrefStoreReadObserver read_observer(pref_store); |
| 304 PersistentPrefStore::PrefReadError error = read_observer.Read(); |
| 203 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | 305 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
| 204 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), | 306 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), |
| 205 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 307 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 206 pref_store->SetValue(kProtectedAtomic, | 308 pref_store->SetValue(kProtectedAtomic, |
| 207 base::MakeUnique<base::Value>(kHelloWorld), | 309 base::MakeUnique<base::Value>(kHelloWorld), |
| 208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 310 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 209 pref_store->SetValue(kUnprotectedPref, | 311 pref_store->SetValue(kUnprotectedPref, |
| 210 base::MakeUnique<base::Value>(kFoobar), | 312 base::MakeUnique<base::Value>(kFoobar), |
| 211 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 313 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 212 pref_store->RemoveObserver(®istry_verifier_); | 314 pref_store->RemoveObserver(®istry_verifier_); |
| 213 pref_store->CommitPendingWrite(); | 315 pref_store->CommitPendingWrite(); |
| 214 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 317 base::RunLoop run_loop; |
| 318 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 319 worker_pool_->pool().get()) |
| 320 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
| 321 run_loop.QuitClosure()); |
| 322 run_loop.Run(); |
| 215 } | 323 } |
| 216 | 324 |
| 217 void LoadExistingPrefs() { | 325 void LoadExistingPrefs() { |
| 218 DestroyPrefStore(); | 326 DestroyPrefStore(); |
| 327 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 328 mock_validation_delegate_record_); |
| 219 pref_store_ = manager_->CreateProfilePrefStore( | 329 pref_store_ = manager_->CreateProfilePrefStore( |
| 220 main_message_loop_.task_runner(), | 330 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 331 worker_pool_->pool().get()), |
| 221 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 332 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 222 base::Unretained(this)), | 333 base::Unretained(this)), |
| 223 NULL); | 334 mock_validation_delegate_.get(), connector_.get(), |
| 335 profile_pref_registry_); |
| 224 pref_store_->AddObserver(®istry_verifier_); | 336 pref_store_->AddObserver(®istry_verifier_); |
| 225 pref_store_->ReadPrefs(); | 337 PrefStoreReadObserver read_observer(pref_store_); |
| 338 read_observer.Read(); |
| 226 } | 339 } |
| 227 | 340 |
| 228 void ReplaceStringInPrefs(const std::string& find, | 341 void ReplaceStringInPrefs(const std::string& find, |
| 229 const std::string& replace) { | 342 const std::string& replace) { |
| 230 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, | 343 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, |
| 231 base::FileEnumerator::FILES); | 344 base::FileEnumerator::FILES); |
| 232 | 345 |
| 233 for (base::FilePath path = file_enum.Next(); !path.empty(); | 346 for (base::FilePath path = file_enum.Next(); !path.empty(); |
| 234 path = file_enum.Next()) { | 347 path = file_enum.Next()) { |
| 235 // Tamper with the file's contents | 348 // Tamper with the file's contents |
| (...skipping 13 matching lines...) Expand all Loading... |
| 249 ADD_FAILURE() << name << " is not a defined value."; | 362 ADD_FAILURE() << name << " is not a defined value."; |
| 250 } else if (!value->GetAsString(&as_string)) { | 363 } else if (!value->GetAsString(&as_string)) { |
| 251 ADD_FAILURE() << name << " could not be coerced to a string."; | 364 ADD_FAILURE() << name << " could not be coerced to a string."; |
| 252 } else { | 365 } else { |
| 253 EXPECT_EQ(expected, as_string); | 366 EXPECT_EQ(expected, as_string); |
| 254 } | 367 } |
| 255 } | 368 } |
| 256 | 369 |
| 257 void ExpectValidationObserved(const std::string& pref_path) { | 370 void ExpectValidationObserved(const std::string& pref_path) { |
| 258 // No validations are expected for platforms that do not support tracking. | 371 // No validations are expected for platforms that do not support tracking. |
| 259 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) | 372 if (!SupportsPreferenceTracking()) |
| 260 return; | 373 return; |
| 261 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) | 374 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) |
| 262 ADD_FAILURE() << "No validation observed for preference: " << pref_path; | 375 ADD_FAILURE() << "No validation observed for preference: " << pref_path; |
| 263 } | 376 } |
| 264 | 377 |
| 265 base::MessageLoop main_message_loop_; | 378 base::MessageLoop main_message_loop_; |
| 266 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; | 379 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; |
| 267 base::ScopedTempDir profile_dir_; | 380 base::ScopedTempDir profile_dir_; |
| 268 TestingPrefServiceSimple local_state_; | 381 TestingPrefServiceSimple local_state_; |
| 269 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; | 382 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; |
| 270 RegistryVerifier registry_verifier_; | 383 RegistryVerifier registry_verifier_; |
| 271 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; | 384 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; |
| 272 std::unique_ptr<MockValidationDelegate> mock_validation_delegate_; | 385 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 386 mock_validation_delegate_; |
| 273 std::unique_ptr<ProfilePrefStoreManager> manager_; | 387 std::unique_ptr<ProfilePrefStoreManager> manager_; |
| 274 scoped_refptr<PersistentPrefStore> pref_store_; | 388 scoped_refptr<PersistentPrefStore> pref_store_; |
| 275 | 389 |
| 276 std::string seed_; | 390 std::string seed_; |
| 277 | 391 |
| 278 private: | 392 private: |
| 279 void RecordReset() { | 393 void RecordReset() { |
| 280 // As-is |reset_recorded_| is only designed to remember a single reset, make | 394 // As-is |reset_recorded_| is only designed to remember a single reset, make |
| 281 // sure none was previously recorded (or that ClearResetRecorded() was | 395 // sure none was previously recorded (or that ClearResetRecorded() was |
| 282 // called). | 396 // called). |
| 283 EXPECT_FALSE(reset_recorded_); | 397 EXPECT_FALSE(reset_recorded_); |
| 284 reset_recorded_ = true; | 398 reset_recorded_ = true; |
| 285 } | 399 } |
| 286 | 400 |
| 401 void BindInterface(const std::string& interface_name, |
| 402 mojo::ScopedMessagePipeHandle handle) { |
| 403 service_manager::ServiceInfo source( |
| 404 service_manager::Identity(content::mojom::kBrowserServiceName, |
| 405 service_manager::mojom::kRootUserID), |
| 406 service_manager::InterfaceProviderSpecMap()); |
| 407 static_cast<service_manager::mojom::Service*>(pref_service_context_.get()) |
| 408 ->OnBindInterface(source, interface_name, std::move(handle), |
| 409 base::Bind(&base::DoNothing)); |
| 410 } |
| 411 |
| 412 base::test::ScopedFeatureList feature_list_; |
| 287 bool reset_recorded_; | 413 bool reset_recorded_; |
| 414 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; |
| 415 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
| 416 std::unique_ptr<service_manager::Connector> connector_; |
| 417 service_manager::mojom::ConnectorRequest connector_request_; |
| 288 }; | 418 }; |
| 289 | 419 |
| 290 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { | 420 TEST_P(ProfilePrefStoreManagerTest, StoreValues) { |
| 291 InitializePrefs(); | 421 InitializePrefs(); |
| 292 | 422 |
| 293 LoadExistingPrefs(); | 423 LoadExistingPrefs(); |
| 294 | 424 |
| 295 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 425 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 296 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 426 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 297 VerifyResetRecorded(false); | 427 VerifyResetRecorded(false); |
| 298 ExpectValidationObserved(kTrackedAtomic); | 428 ExpectValidationObserved(kTrackedAtomic); |
| 299 ExpectValidationObserved(kProtectedAtomic); | 429 ExpectValidationObserved(kProtectedAtomic); |
| 300 } | 430 } |
| 301 | 431 |
| 302 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { | 432 TEST_P(ProfilePrefStoreManagerTest, ProtectValues) { |
| 303 InitializePrefs(); | 433 InitializePrefs(); |
| 304 | 434 |
| 305 ReplaceStringInPrefs(kFoobar, kBarfoo); | 435 ReplaceStringInPrefs(kFoobar, kBarfoo); |
| 306 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | 436 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); |
| 307 | 437 |
| 308 LoadExistingPrefs(); | 438 LoadExistingPrefs(); |
| 309 | 439 |
| 310 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | 440 // kTrackedAtomic is unprotected and thus will be loaded as it appears on |
| 311 // disk. | 441 // disk. |
| 312 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | 442 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); |
| 313 | 443 |
| 314 // If preference tracking is supported, the tampered value of kProtectedAtomic | 444 // If preference tracking is supported, the tampered value of kProtectedAtomic |
| 315 // will be discarded at load time, leaving this preference undefined. | 445 // will be discarded at load time, leaving this preference undefined. |
| 316 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 446 EXPECT_NE(SupportsPreferenceTracking(), |
| 317 pref_store_->GetValue(kProtectedAtomic, NULL)); | 447 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 318 VerifyResetRecorded( | 448 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 319 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 320 | 449 |
| 321 ExpectValidationObserved(kTrackedAtomic); | 450 ExpectValidationObserved(kTrackedAtomic); |
| 322 ExpectValidationObserved(kProtectedAtomic); | 451 ExpectValidationObserved(kProtectedAtomic); |
| 323 } | 452 } |
| 324 | 453 |
| 325 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 454 TEST_P(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
| 326 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); | 455 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); |
| 327 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); | 456 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); |
| 328 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); | 457 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); |
| 329 EXPECT_TRUE( | 458 EXPECT_TRUE( |
| 330 manager_->InitializePrefsFromMasterPrefs(std::move(master_prefs))); | 459 manager_->InitializePrefsFromMasterPrefs(std::move(master_prefs))); |
| 331 | 460 |
| 332 LoadExistingPrefs(); | 461 LoadExistingPrefs(); |
| 333 | 462 |
| 334 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 463 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
| 335 // necessary to authenticate these values. | 464 // necessary to authenticate these values. |
| 336 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 465 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 337 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 466 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 338 VerifyResetRecorded(false); | 467 VerifyResetRecorded(false); |
| 339 } | 468 } |
| 340 | 469 |
| 341 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { | 470 TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtected) { |
| 342 InitializePrefs(); | 471 InitializePrefs(); |
| 343 | 472 |
| 344 ExpectValidationObserved(kTrackedAtomic); | 473 ExpectValidationObserved(kTrackedAtomic); |
| 345 ExpectValidationObserved(kProtectedAtomic); | 474 ExpectValidationObserved(kProtectedAtomic); |
| 346 | 475 |
| 347 LoadExistingPrefs(); | 476 LoadExistingPrefs(); |
| 348 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 477 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 349 | 478 |
| 350 // Ensure everything is written out to disk. | 479 // Ensure everything is written out to disk. |
| 351 DestroyPrefStore(); | 480 DestroyPrefStore(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 372 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 501 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 373 VerifyResetRecorded(false); | 502 VerifyResetRecorded(false); |
| 374 | 503 |
| 375 // Ensure everything is written out to disk. | 504 // Ensure everything is written out to disk. |
| 376 DestroyPrefStore(); | 505 DestroyPrefStore(); |
| 377 | 506 |
| 378 // It's protected now, so (if the platform supports it) any tampering should | 507 // It's protected now, so (if the platform supports it) any tampering should |
| 379 // lead to a reset. | 508 // lead to a reset. |
| 380 ReplaceStringInPrefs(kBarfoo, kFoobar); | 509 ReplaceStringInPrefs(kBarfoo, kFoobar); |
| 381 LoadExistingPrefs(); | 510 LoadExistingPrefs(); |
| 382 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 511 EXPECT_NE(SupportsPreferenceTracking(), |
| 383 pref_store_->GetValue(kUnprotectedPref, NULL)); | 512 pref_store_->GetValue(kUnprotectedPref, NULL)); |
| 384 VerifyResetRecorded( | 513 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 385 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 386 } | 514 } |
| 387 | 515 |
| 388 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | 516 TEST_P(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
| 389 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 517 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 390 original_configuration = configuration_; | 518 original_configuration = configuration_; |
| 391 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 519 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 392 configuration_.begin(); | 520 configuration_.begin(); |
| 393 it != configuration_.end(); | 521 it != configuration_.end(); |
| 394 ++it) { | 522 ++it) { |
| 395 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; | 523 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 396 } | 524 } |
| 397 ReloadConfiguration(); | 525 ReloadConfiguration(); |
| 398 | 526 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 418 | 546 |
| 419 // And try loading with the new configuration. | 547 // And try loading with the new configuration. |
| 420 LoadExistingPrefs(); | 548 LoadExistingPrefs(); |
| 421 | 549 |
| 422 // Since there was a valid super MAC we were able to extend the existing trust | 550 // Since there was a valid super MAC we were able to extend the existing trust |
| 423 // to the newly tracked & protected preference. | 551 // to the newly tracked & protected preference. |
| 424 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 552 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 425 VerifyResetRecorded(false); | 553 VerifyResetRecorded(false); |
| 426 } | 554 } |
| 427 | 555 |
| 428 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | 556 TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
| 429 InitializePrefs(); | 557 InitializePrefs(); |
| 430 | 558 |
| 431 ExpectValidationObserved(kTrackedAtomic); | 559 ExpectValidationObserved(kTrackedAtomic); |
| 432 ExpectValidationObserved(kProtectedAtomic); | 560 ExpectValidationObserved(kProtectedAtomic); |
| 433 | 561 |
| 434 // Now update the configuration to protect it. | 562 // Now update the configuration to protect it. |
| 435 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 563 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 436 kExtraReportingId, kUnprotectedPref, | 564 kExtraReportingId, kUnprotectedPref, |
| 437 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, | 565 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 438 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; | 566 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 439 configuration_.push_back(new_protected); | 567 configuration_.push_back(new_protected); |
| 440 seed_ = "new-seed-to-break-trust"; | 568 seed_ = "new-seed-to-break-trust"; |
| 441 ReloadConfiguration(); | 569 ReloadConfiguration(); |
| 442 | 570 |
| 443 // And try loading with the new configuration. | 571 // And try loading with the new configuration. |
| 444 LoadExistingPrefs(); | 572 LoadExistingPrefs(); |
| 445 | 573 |
| 446 // If preference tracking is supported, kUnprotectedPref will have been | 574 // If preference tracking is supported, kUnprotectedPref will have been |
| 447 // discarded because new values are not accepted without a valid super MAC. | 575 // discarded because new values are not accepted without a valid super MAC. |
| 448 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 576 EXPECT_NE(SupportsPreferenceTracking(), |
| 449 pref_store_->GetValue(kUnprotectedPref, NULL)); | 577 pref_store_->GetValue(kUnprotectedPref, NULL)); |
| 450 VerifyResetRecorded( | 578 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 451 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 452 } | 579 } |
| 453 | 580 |
| 454 // This test verifies that preference values are correctly maintained when a | 581 // This test verifies that preference values are correctly maintained when a |
| 455 // preference's protection state changes from protected to unprotected. | 582 // preference's protection state changes from protected to unprotected. |
| 456 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { | 583 TEST_P(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
| 457 InitializePrefs(); | 584 InitializePrefs(); |
| 458 | 585 |
| 459 ExpectValidationObserved(kTrackedAtomic); | 586 ExpectValidationObserved(kTrackedAtomic); |
| 460 ExpectValidationObserved(kProtectedAtomic); | 587 ExpectValidationObserved(kProtectedAtomic); |
| 461 | 588 |
| 462 DestroyPrefStore(); | 589 DestroyPrefStore(); |
| 463 | 590 |
| 464 // Unconfigure protection for kProtectedAtomic | 591 // Unconfigure protection for kProtectedAtomic |
| 465 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 592 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 466 configuration_.begin(); | 593 configuration_.begin(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 488 | 615 |
| 489 // Trigger the logic that migrates it back to the unprotected preferences | 616 // Trigger the logic that migrates it back to the unprotected preferences |
| 490 // file. | 617 // file. |
| 491 pref_store_->SetValue(kProtectedAtomic, | 618 pref_store_->SetValue(kProtectedAtomic, |
| 492 base::WrapUnique(new base::Value(kGoodbyeWorld)), | 619 base::WrapUnique(new base::Value(kGoodbyeWorld)), |
| 493 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 620 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 494 LoadExistingPrefs(); | 621 LoadExistingPrefs(); |
| 495 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 622 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
| 496 VerifyResetRecorded(false); | 623 VerifyResetRecorded(false); |
| 497 } | 624 } |
| 625 |
| 626 INSTANTIATE_TEST_CASE_P(ProfilePrefStoreManagerTest, |
| 627 ProfilePrefStoreManagerTest, |
| 628 testing::Bool()); |
| OLD | NEW |