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