| 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/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 prefs::CreatePrefService(std::set<PrefValueStore::PrefStoreType>(), |
| 201 worker_pool_->pool()), |
| 202 mojo::MakeRequest(&service_ptr)); |
| 203 connector_ = base::MakeUnique<MockConnector>(std::move(service_ptr)); |
| 204 } else { |
| 205 feature_list_.InitAndDisableFeature(features::kPrefService); |
| 206 } |
| 102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; | 207 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()); | 208 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
| 106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; | 209 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; |
| 107 it != kConfiguration + arraysize(kConfiguration); | 210 it != kConfiguration + arraysize(kConfiguration); |
| 108 ++it) { | 211 ++it) { |
| 109 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { | 212 if (it->strategy == PrefHashFilter::PrefTrackingStrategy::ATOMIC) { |
| 110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 213 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
| 111 } else { | 214 } else { |
| 112 profile_pref_registry_->RegisterDictionaryPref(it->name); | 215 profile_pref_registry_->RegisterDictionaryPref(it->name); |
| 113 } | 216 } |
| 114 } | 217 } |
| 115 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); | 218 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); |
| 116 | 219 |
| 117 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be | 220 // 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 | 221 // declared as protected in order to be read from the proper store by the |
| 119 // SegregatedPrefStore. Only declare it after configured prefs have been | 222 // SegregatedPrefStore. Only declare it after configured prefs have been |
| 120 // registered above for this test as kPreferenceResetTime is already | 223 // registered above for this test as kPreferenceResetTime is already |
| 121 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. | 224 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
| 122 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { | 225 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = { |
| 123 configuration_.rbegin()->reporting_id + 1, | 226 configuration_.rbegin()->reporting_id + 1, |
| 124 user_prefs::kPreferenceResetTime, | 227 user_prefs::kPreferenceResetTime, |
| 125 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, | 228 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 126 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; | 229 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 127 configuration_.push_back(pref_reset_time_config); | 230 configuration_.push_back(pref_reset_time_config); |
| 128 | 231 |
| 129 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 232 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 130 ReloadConfiguration(); | 233 ReloadConfiguration(); |
| 131 } | 234 } |
| 132 | 235 |
| 133 void ReloadConfiguration() { | 236 void ReloadConfiguration() { |
| 237 RelaunchPrefService(); |
| 134 manager_.reset(new ProfilePrefStoreManager( | 238 manager_.reset(new ProfilePrefStoreManager( |
| 135 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, | 239 profile_dir_.GetPath(), configuration_, kReportingIdCount, seed_, |
| 136 "device_id", &local_state_)); | 240 "device_id", &local_state_)); |
| 137 } | 241 } |
| 138 | 242 |
| 139 void TearDown() override { DestroyPrefStore(); } | 243 void TearDown() override { |
| 244 DestroyPrefStore(); |
| 245 if (GetParam()) { |
| 246 connector_.reset(); |
| 247 pref_service_context_.reset(); |
| 248 } |
| 249 worker_pool_.reset(); |
| 250 } |
| 140 | 251 |
| 141 protected: | 252 protected: |
| 253 bool SupportsPreferenceTracking() { |
| 254 return ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking && |
| 255 !GetParam(); |
| 256 } |
| 257 |
| 258 void RelaunchPrefService() { |
| 259 if (!GetParam()) |
| 260 return; |
| 261 |
| 262 service_manager::mojom::ServicePtr service_ptr; |
| 263 pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
| 264 prefs::CreatePrefService(std::set<PrefValueStore::PrefStoreType>(), |
| 265 worker_pool_->pool()), |
| 266 mojo::MakeRequest(&service_ptr)); |
| 267 connector_ = base::MakeUnique<MockConnector>(std::move(service_ptr)); |
| 268 } |
| 269 |
| 142 // Verifies whether a reset was reported via the RecordReset() hook. Also | 270 // Verifies whether a reset was reported via the RecordReset() hook. Also |
| 143 // verifies that GetResetTime() was set (or not) accordingly. | 271 // verifies that GetResetTime() was set (or not) accordingly. |
| 144 void VerifyResetRecorded(bool reset_expected) { | 272 void VerifyResetRecorded(bool reset_expected) { |
| 145 EXPECT_EQ(reset_expected, reset_recorded_); | 273 EXPECT_EQ(reset_expected, reset_recorded_); |
| 146 | 274 |
| 147 PrefServiceFactory pref_service_factory; | 275 PrefServiceFactory pref_service_factory; |
| 148 pref_service_factory.set_user_prefs(pref_store_); | 276 pref_service_factory.set_user_prefs(pref_store_); |
| 149 | 277 |
| 150 std::unique_ptr<PrefService> pref_service( | 278 std::unique_ptr<PrefService> pref_service( |
| 151 pref_service_factory.Create(profile_pref_registry_.get())); | 279 pref_service_factory.Create(profile_pref_registry_.get())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 | 291 |
| 164 std::unique_ptr<PrefService> pref_service( | 292 std::unique_ptr<PrefService> pref_service( |
| 165 pref_service_factory.Create(profile_pref_registry_.get())); | 293 pref_service_factory.Create(profile_pref_registry_.get())); |
| 166 | 294 |
| 167 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); | 295 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); |
| 168 } | 296 } |
| 169 | 297 |
| 170 void InitializePrefs() { | 298 void InitializePrefs() { |
| 171 // According to the implementation of ProfilePrefStoreManager, this is | 299 // According to the implementation of ProfilePrefStoreManager, this is |
| 172 // actually a SegregatedPrefStore backed by two underlying pref stores. | 300 // actually a SegregatedPrefStore backed by two underlying pref stores. |
| 301 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 302 mock_validation_delegate_record_); |
| 173 scoped_refptr<PersistentPrefStore> pref_store = | 303 scoped_refptr<PersistentPrefStore> pref_store = |
| 174 manager_->CreateProfilePrefStore( | 304 manager_->CreateProfilePrefStore( |
| 175 main_message_loop_.task_runner(), | 305 main_message_loop_.task_runner(), |
| 176 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 306 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 177 base::Unretained(this)), | 307 base::Unretained(this)), |
| 178 mock_validation_delegate_.get()); | 308 mock_validation_delegate_.get(), connector_.get()); |
| 179 InitializePrefStore(pref_store.get()); | 309 InitializePrefStore(pref_store.get()); |
| 180 pref_store = NULL; | 310 pref_store = NULL; |
| 181 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 182 } | 312 } |
| 183 | 313 |
| 184 void DestroyPrefStore() { | 314 void DestroyPrefStore() { |
| 185 if (pref_store_.get()) { | 315 if (pref_store_.get()) { |
| 186 ClearResetRecorded(); | 316 ClearResetRecorded(); |
| 187 // Force everything to be written to disk, triggering the PrefHashFilter | 317 // Force everything to be written to disk, triggering the PrefHashFilter |
| 188 // while our RegistryVerifier is watching. | 318 // while our RegistryVerifier is watching. |
| 189 pref_store_->CommitPendingWrite(); | 319 pref_store_->CommitPendingWrite(); |
| 190 base::RunLoop().RunUntilIdle(); | 320 base::RunLoop().RunUntilIdle(); |
| 321 base::RunLoop run_loop; |
| 322 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 323 worker_pool_->pool().get()) |
| 324 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
| 325 run_loop.QuitClosure()); |
| 326 run_loop.Run(); |
| 191 | 327 |
| 192 pref_store_->RemoveObserver(®istry_verifier_); | 328 pref_store_->RemoveObserver(®istry_verifier_); |
| 193 pref_store_ = NULL; | 329 pref_store_ = NULL; |
| 194 // Nothing should have to happen on the background threads, but just in | 330 // Nothing should have to happen on the background threads, but just in |
| 195 // case... | 331 // case... |
| 196 base::RunLoop().RunUntilIdle(); | 332 base::RunLoop().RunUntilIdle(); |
| 197 } | 333 } |
| 334 RelaunchPrefService(); |
| 198 } | 335 } |
| 199 | 336 |
| 200 void InitializePrefStore(PersistentPrefStore* pref_store) { | 337 void InitializePrefStore(PersistentPrefStore* pref_store) { |
| 201 pref_store->AddObserver(®istry_verifier_); | 338 pref_store->AddObserver(®istry_verifier_); |
| 202 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | 339 PrefStoreReadObserver read_observer(pref_store); |
| 340 PersistentPrefStore::PrefReadError error = read_observer.Read(); |
| 203 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | 341 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
| 204 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), | 342 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), |
| 205 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 343 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 206 pref_store->SetValue(kProtectedAtomic, | 344 pref_store->SetValue(kProtectedAtomic, |
| 207 base::MakeUnique<base::Value>(kHelloWorld), | 345 base::MakeUnique<base::Value>(kHelloWorld), |
| 208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 346 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 209 pref_store->SetValue(kUnprotectedPref, | 347 pref_store->SetValue(kUnprotectedPref, |
| 210 base::MakeUnique<base::Value>(kFoobar), | 348 base::MakeUnique<base::Value>(kFoobar), |
| 211 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 349 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
| 212 pref_store->RemoveObserver(®istry_verifier_); | 350 pref_store->RemoveObserver(®istry_verifier_); |
| 213 pref_store->CommitPendingWrite(); | 351 pref_store->CommitPendingWrite(); |
| 214 base::RunLoop().RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
| 353 base::RunLoop run_loop; |
| 354 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 355 worker_pool_->pool().get()) |
| 356 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
| 357 run_loop.QuitClosure()); |
| 358 run_loop.Run(); |
| 215 } | 359 } |
| 216 | 360 |
| 217 void LoadExistingPrefs() { | 361 void LoadExistingPrefs() { |
| 218 DestroyPrefStore(); | 362 DestroyPrefStore(); |
| 363 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( |
| 364 mock_validation_delegate_record_); |
| 219 pref_store_ = manager_->CreateProfilePrefStore( | 365 pref_store_ = manager_->CreateProfilePrefStore( |
| 220 main_message_loop_.task_runner(), | 366 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
| 367 worker_pool_->pool().get()), |
| 221 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 368 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, |
| 222 base::Unretained(this)), | 369 base::Unretained(this)), |
| 223 NULL); | 370 mock_validation_delegate_.get(), connector_.get()); |
| 224 pref_store_->AddObserver(®istry_verifier_); | 371 pref_store_->AddObserver(®istry_verifier_); |
| 225 pref_store_->ReadPrefs(); | 372 PrefStoreReadObserver read_observer(pref_store_); |
| 373 read_observer.Read(); |
| 226 } | 374 } |
| 227 | 375 |
| 228 void ReplaceStringInPrefs(const std::string& find, | 376 void ReplaceStringInPrefs(const std::string& find, |
| 229 const std::string& replace) { | 377 const std::string& replace) { |
| 230 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, | 378 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, |
| 231 base::FileEnumerator::FILES); | 379 base::FileEnumerator::FILES); |
| 232 | 380 |
| 233 for (base::FilePath path = file_enum.Next(); !path.empty(); | 381 for (base::FilePath path = file_enum.Next(); !path.empty(); |
| 234 path = file_enum.Next()) { | 382 path = file_enum.Next()) { |
| 235 // Tamper with the file's contents | 383 // Tamper with the file's contents |
| (...skipping 13 matching lines...) Expand all Loading... |
| 249 ADD_FAILURE() << name << " is not a defined value."; | 397 ADD_FAILURE() << name << " is not a defined value."; |
| 250 } else if (!value->GetAsString(&as_string)) { | 398 } else if (!value->GetAsString(&as_string)) { |
| 251 ADD_FAILURE() << name << " could not be coerced to a string."; | 399 ADD_FAILURE() << name << " could not be coerced to a string."; |
| 252 } else { | 400 } else { |
| 253 EXPECT_EQ(expected, as_string); | 401 EXPECT_EQ(expected, as_string); |
| 254 } | 402 } |
| 255 } | 403 } |
| 256 | 404 |
| 257 void ExpectValidationObserved(const std::string& pref_path) { | 405 void ExpectValidationObserved(const std::string& pref_path) { |
| 258 // No validations are expected for platforms that do not support tracking. | 406 // No validations are expected for platforms that do not support tracking. |
| 259 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) | 407 if (!SupportsPreferenceTracking()) |
| 260 return; | 408 return; |
| 261 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) | 409 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) |
| 262 ADD_FAILURE() << "No validation observed for preference: " << pref_path; | 410 ADD_FAILURE() << "No validation observed for preference: " << pref_path; |
| 263 } | 411 } |
| 264 | 412 |
| 265 base::MessageLoop main_message_loop_; | 413 base::MessageLoop main_message_loop_; |
| 266 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; | 414 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; |
| 267 base::ScopedTempDir profile_dir_; | 415 base::ScopedTempDir profile_dir_; |
| 268 TestingPrefServiceSimple local_state_; | 416 TestingPrefServiceSimple local_state_; |
| 269 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; | 417 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; |
| 270 RegistryVerifier registry_verifier_; | 418 RegistryVerifier registry_verifier_; |
| 271 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; | 419 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; |
| 272 std::unique_ptr<MockValidationDelegate> mock_validation_delegate_; | 420 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> |
| 421 mock_validation_delegate_; |
| 273 std::unique_ptr<ProfilePrefStoreManager> manager_; | 422 std::unique_ptr<ProfilePrefStoreManager> manager_; |
| 274 scoped_refptr<PersistentPrefStore> pref_store_; | 423 scoped_refptr<PersistentPrefStore> pref_store_; |
| 275 | 424 |
| 276 std::string seed_; | 425 std::string seed_; |
| 277 | 426 |
| 278 private: | 427 private: |
| 279 void RecordReset() { | 428 void RecordReset() { |
| 280 // As-is |reset_recorded_| is only designed to remember a single reset, make | 429 // As-is |reset_recorded_| is only designed to remember a single reset, make |
| 281 // sure none was previously recorded (or that ClearResetRecorded() was | 430 // sure none was previously recorded (or that ClearResetRecorded() was |
| 282 // called). | 431 // called). |
| 283 EXPECT_FALSE(reset_recorded_); | 432 EXPECT_FALSE(reset_recorded_); |
| 284 reset_recorded_ = true; | 433 reset_recorded_ = true; |
| 285 } | 434 } |
| 286 | 435 |
| 436 base::test::ScopedFeatureList feature_list_; |
| 287 bool reset_recorded_; | 437 bool reset_recorded_; |
| 438 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; |
| 439 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
| 440 std::unique_ptr<service_manager::Connector> connector_; |
| 288 }; | 441 }; |
| 289 | 442 |
| 290 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { | 443 TEST_P(ProfilePrefStoreManagerTest, StoreValues) { |
| 291 InitializePrefs(); | 444 InitializePrefs(); |
| 292 | 445 |
| 293 LoadExistingPrefs(); | 446 LoadExistingPrefs(); |
| 294 | 447 |
| 295 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 448 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 296 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 449 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 297 VerifyResetRecorded(false); | 450 VerifyResetRecorded(false); |
| 298 ExpectValidationObserved(kTrackedAtomic); | 451 ExpectValidationObserved(kTrackedAtomic); |
| 299 ExpectValidationObserved(kProtectedAtomic); | 452 ExpectValidationObserved(kProtectedAtomic); |
| 300 } | 453 } |
| 301 | 454 |
| 302 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { | 455 TEST_P(ProfilePrefStoreManagerTest, ProtectValues) { |
| 303 InitializePrefs(); | 456 InitializePrefs(); |
| 304 | 457 |
| 305 ReplaceStringInPrefs(kFoobar, kBarfoo); | 458 ReplaceStringInPrefs(kFoobar, kBarfoo); |
| 306 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | 459 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); |
| 307 | 460 |
| 308 LoadExistingPrefs(); | 461 LoadExistingPrefs(); |
| 309 | 462 |
| 310 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | 463 // kTrackedAtomic is unprotected and thus will be loaded as it appears on |
| 311 // disk. | 464 // disk. |
| 312 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | 465 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); |
| 313 | 466 |
| 314 // If preference tracking is supported, the tampered value of kProtectedAtomic | 467 // If preference tracking is supported, the tampered value of kProtectedAtomic |
| 315 // will be discarded at load time, leaving this preference undefined. | 468 // will be discarded at load time, leaving this preference undefined. |
| 316 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 469 EXPECT_NE(SupportsPreferenceTracking(), |
| 317 pref_store_->GetValue(kProtectedAtomic, NULL)); | 470 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 318 VerifyResetRecorded( | 471 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 319 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 320 | 472 |
| 321 ExpectValidationObserved(kTrackedAtomic); | 473 ExpectValidationObserved(kTrackedAtomic); |
| 322 ExpectValidationObserved(kProtectedAtomic); | 474 ExpectValidationObserved(kProtectedAtomic); |
| 323 } | 475 } |
| 324 | 476 |
| 325 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 477 TEST_P(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
| 326 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); | 478 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); |
| 327 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); | 479 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); |
| 328 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); | 480 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); |
| 329 EXPECT_TRUE( | 481 EXPECT_TRUE( |
| 330 manager_->InitializePrefsFromMasterPrefs(std::move(master_prefs))); | 482 manager_->InitializePrefsFromMasterPrefs(std::move(master_prefs))); |
| 331 | 483 |
| 332 LoadExistingPrefs(); | 484 LoadExistingPrefs(); |
| 333 | 485 |
| 334 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 486 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
| 335 // necessary to authenticate these values. | 487 // necessary to authenticate these values. |
| 336 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 488 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 337 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 489 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 338 VerifyResetRecorded(false); | 490 VerifyResetRecorded(false); |
| 339 } | 491 } |
| 340 | 492 |
| 341 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { | 493 TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtected) { |
| 342 InitializePrefs(); | 494 InitializePrefs(); |
| 343 | 495 |
| 344 ExpectValidationObserved(kTrackedAtomic); | 496 ExpectValidationObserved(kTrackedAtomic); |
| 345 ExpectValidationObserved(kProtectedAtomic); | 497 ExpectValidationObserved(kProtectedAtomic); |
| 346 | 498 |
| 347 LoadExistingPrefs(); | 499 LoadExistingPrefs(); |
| 348 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 500 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 349 | 501 |
| 350 // Ensure everything is written out to disk. | 502 // Ensure everything is written out to disk. |
| 351 DestroyPrefStore(); | 503 DestroyPrefStore(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 372 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 524 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
| 373 VerifyResetRecorded(false); | 525 VerifyResetRecorded(false); |
| 374 | 526 |
| 375 // Ensure everything is written out to disk. | 527 // Ensure everything is written out to disk. |
| 376 DestroyPrefStore(); | 528 DestroyPrefStore(); |
| 377 | 529 |
| 378 // It's protected now, so (if the platform supports it) any tampering should | 530 // It's protected now, so (if the platform supports it) any tampering should |
| 379 // lead to a reset. | 531 // lead to a reset. |
| 380 ReplaceStringInPrefs(kBarfoo, kFoobar); | 532 ReplaceStringInPrefs(kBarfoo, kFoobar); |
| 381 LoadExistingPrefs(); | 533 LoadExistingPrefs(); |
| 382 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 534 EXPECT_NE(SupportsPreferenceTracking(), |
| 383 pref_store_->GetValue(kUnprotectedPref, NULL)); | 535 pref_store_->GetValue(kUnprotectedPref, NULL)); |
| 384 VerifyResetRecorded( | 536 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 385 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
| 386 } | 537 } |
| 387 | 538 |
| 388 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | 539 TEST_P(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
| 389 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | 540 std::vector<PrefHashFilter::TrackedPreferenceMetadata> |
| 390 original_configuration = configuration_; | 541 original_configuration = configuration_; |
| 391 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 542 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
| 392 configuration_.begin(); | 543 configuration_.begin(); |
| 393 it != configuration_.end(); | 544 it != configuration_.end(); |
| 394 ++it) { | 545 ++it) { |
| 395 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; | 546 it->enforcement_level = PrefHashFilter::EnforcementLevel::NO_ENFORCEMENT; |
| 396 } | 547 } |
| 397 ReloadConfiguration(); | 548 ReloadConfiguration(); |
| 398 | 549 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 418 | 569 |
| 419 // And try loading with the new configuration. | 570 // And try loading with the new configuration. |
| 420 LoadExistingPrefs(); | 571 LoadExistingPrefs(); |
| 421 | 572 |
| 422 // Since there was a valid super MAC we were able to extend the existing trust | 573 // Since there was a valid super MAC we were able to extend the existing trust |
| 423 // to the newly tracked & protected preference. | 574 // to the newly tracked & protected preference. |
| 424 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | 575 ExpectStringValueEquals(kUnprotectedPref, kFoobar); |
| 425 VerifyResetRecorded(false); | 576 VerifyResetRecorded(false); |
| 426 } | 577 } |
| 427 | 578 |
| 428 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | 579 TEST_P(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
| 429 InitializePrefs(); | 580 InitializePrefs(); |
| 430 | 581 |
| 431 ExpectValidationObserved(kTrackedAtomic); | 582 ExpectValidationObserved(kTrackedAtomic); |
| 432 ExpectValidationObserved(kProtectedAtomic); | 583 ExpectValidationObserved(kProtectedAtomic); |
| 433 | 584 |
| 434 // Now update the configuration to protect it. | 585 // Now update the configuration to protect it. |
| 435 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 586 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
| 436 kExtraReportingId, kUnprotectedPref, | 587 kExtraReportingId, kUnprotectedPref, |
| 437 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, | 588 PrefHashFilter::EnforcementLevel::ENFORCE_ON_LOAD, |
| 438 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; | 589 PrefHashFilter::PrefTrackingStrategy::ATOMIC}; |
| 439 configuration_.push_back(new_protected); | 590 configuration_.push_back(new_protected); |
| 440 seed_ = "new-seed-to-break-trust"; | 591 seed_ = "new-seed-to-break-trust"; |
| 441 ReloadConfiguration(); | 592 ReloadConfiguration(); |
| 442 | 593 |
| 443 // And try loading with the new configuration. | 594 // And try loading with the new configuration. |
| 444 LoadExistingPrefs(); | 595 LoadExistingPrefs(); |
| 445 | 596 |
| 446 // If preference tracking is supported, kUnprotectedPref will have been | 597 // If preference tracking is supported, kUnprotectedPref will have been |
| 447 // discarded because new values are not accepted without a valid super MAC. | 598 // discarded because new values are not accepted without a valid super MAC. |
| 448 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 599 EXPECT_NE(SupportsPreferenceTracking(), |
| 449 pref_store_->GetValue(kUnprotectedPref, NULL)); | 600 pref_store_->GetValue(kUnprotectedPref, NULL)); |
| 450 VerifyResetRecorded( | 601 VerifyResetRecorded(SupportsPreferenceTracking()); |
| 451 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 |