OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/prefs/profile_pref_store_manager.h" | 5 #include "chrome/browser/prefs/profile_pref_store_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 15 matching lines...) Expand all Loading... | |
26 #include "base/values.h" | 26 #include "base/values.h" |
27 #include "chrome/common/chrome_features.h" | 27 #include "chrome/common/chrome_features.h" |
28 #include "components/pref_registry/pref_registry_syncable.h" | 28 #include "components/pref_registry/pref_registry_syncable.h" |
29 #include "components/prefs/json_pref_store.h" | 29 #include "components/prefs/json_pref_store.h" |
30 #include "components/prefs/persistent_pref_store.h" | 30 #include "components/prefs/persistent_pref_store.h" |
31 #include "components/prefs/pref_service.h" | 31 #include "components/prefs/pref_service.h" |
32 #include "components/prefs/pref_service_factory.h" | 32 #include "components/prefs/pref_service_factory.h" |
33 #include "components/prefs/pref_store.h" | 33 #include "components/prefs/pref_store.h" |
34 #include "components/prefs/testing_pref_service.h" | 34 #include "components/prefs/testing_pref_service.h" |
35 #include "content/public/common/service_names.mojom.h" | 35 #include "content/public/common/service_names.mojom.h" |
36 #include "mojo/public/cpp/bindings/binding_set.h" | |
36 #include "services/preferences/public/cpp/pref_service_main.h" | 37 #include "services/preferences/public/cpp/pref_service_main.h" |
37 #include "services/preferences/public/cpp/tracked/configuration.h" | 38 #include "services/preferences/public/cpp/tracked/configuration.h" |
38 #include "services/preferences/public/cpp/tracked/mock_validation_delegate.h" | 39 #include "services/preferences/public/cpp/tracked/mock_validation_delegate.h" |
39 #include "services/preferences/public/cpp/tracked/pref_names.h" | 40 #include "services/preferences/public/cpp/tracked/pref_names.h" |
40 #include "services/preferences/public/interfaces/preferences.mojom.h" | 41 #include "services/preferences/public/interfaces/preferences.mojom.h" |
41 #include "services/service_manager/public/cpp/connector.h" | 42 #include "services/service_manager/public/cpp/connector.h" |
42 #include "services/service_manager/public/cpp/service_context.h" | 43 #include "services/service_manager/public/cpp/service_context.h" |
43 #include "testing/gtest/include/gtest/gtest.h" | 44 #include "testing/gtest/include/gtest/gtest.h" |
44 | 45 |
45 namespace { | 46 namespace { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 {0u, kTrackedAtomic, EnforcementLevel::NO_ENFORCEMENT, | 131 {0u, kTrackedAtomic, EnforcementLevel::NO_ENFORCEMENT, |
131 PrefTrackingStrategy::ATOMIC}, | 132 PrefTrackingStrategy::ATOMIC}, |
132 {1u, kProtectedAtomic, EnforcementLevel::ENFORCE_ON_LOAD, | 133 {1u, kProtectedAtomic, EnforcementLevel::ENFORCE_ON_LOAD, |
133 PrefTrackingStrategy::ATOMIC}}; | 134 PrefTrackingStrategy::ATOMIC}}; |
134 | 135 |
135 const size_t kExtraReportingId = 2u; | 136 const size_t kExtraReportingId = 2u; |
136 const size_t kReportingIdCount = 3u; | 137 const size_t kReportingIdCount = 3u; |
137 | 138 |
138 } // namespace | 139 } // namespace |
139 | 140 |
140 class ProfilePrefStoreManagerTest : public testing::TestWithParam<bool> { | 141 class ProfilePrefStoreManagerTest : public testing::TestWithParam<bool>, |
142 public prefs::mojom::ResetOnLoadObserver { | |
141 public: | 143 public: |
142 ProfilePrefStoreManagerTest() | 144 ProfilePrefStoreManagerTest() |
143 : configuration_(prefs::ConstructTrackedConfiguration(kConfiguration)), | 145 : configuration_(prefs::ConstructTrackedConfiguration(kConfiguration)), |
144 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), | 146 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), |
145 registry_verifier_(profile_pref_registry_.get()), | 147 registry_verifier_(profile_pref_registry_.get()), |
146 seed_("seed"), | 148 seed_("seed"), |
147 reset_recorded_(false) {} | 149 reset_recorded_(false) {} |
148 | 150 |
149 void SetUp() override { | 151 void SetUp() override { |
150 worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | 152 worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( |
(...skipping 16 matching lines...) Expand all Loading... | |
167 prefs::mojom::PrefStoreConnector::Name_)); | 169 prefs::mojom::PrefStoreConnector::Name_)); |
168 test_api.OverrideBinderForTesting( | 170 test_api.OverrideBinderForTesting( |
169 prefs::mojom::kServiceName, prefs::mojom::PrefServiceControl::Name_, | 171 prefs::mojom::kServiceName, prefs::mojom::PrefServiceControl::Name_, |
170 base::Bind(&ProfilePrefStoreManagerTest::BindInterface, | 172 base::Bind(&ProfilePrefStoreManagerTest::BindInterface, |
171 base::Unretained(this), | 173 base::Unretained(this), |
172 prefs::mojom::PrefServiceControl::Name_)); | 174 prefs::mojom::PrefServiceControl::Name_)); |
173 } else { | 175 } else { |
174 feature_list_.InitAndDisableFeature(features::kPrefService); | 176 feature_list_.InitAndDisableFeature(features::kPrefService); |
175 } | 177 } |
176 mock_validation_delegate_record_ = new MockValidationDelegateRecord; | 178 mock_validation_delegate_record_ = new MockValidationDelegateRecord; |
179 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( | |
180 mock_validation_delegate_record_); | |
181 | |
177 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); | 182 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); |
178 for (const prefs::TrackedPreferenceMetadata* it = kConfiguration; | 183 for (const prefs::TrackedPreferenceMetadata* it = kConfiguration; |
179 it != kConfiguration + arraysize(kConfiguration); ++it) { | 184 it != kConfiguration + arraysize(kConfiguration); ++it) { |
180 if (it->strategy == PrefTrackingStrategy::ATOMIC) { | 185 if (it->strategy == PrefTrackingStrategy::ATOMIC) { |
181 profile_pref_registry_->RegisterStringPref(it->name, std::string()); | 186 profile_pref_registry_->RegisterStringPref(it->name, std::string()); |
182 } else { | 187 } else { |
183 profile_pref_registry_->RegisterDictionaryPref(it->name); | 188 profile_pref_registry_->RegisterDictionaryPref(it->name); |
184 } | 189 } |
185 } | 190 } |
186 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); | 191 profile_pref_registry_->RegisterStringPref(kUnprotectedPref, std::string()); |
187 | 192 |
188 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be | 193 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be |
189 // declared as protected in order to be read from the proper store by the | 194 // declared as protected in order to be read from the proper store by the |
190 // SegregatedPrefStore. Only declare it after configured prefs have been | 195 // SegregatedPrefStore. Only declare it after configured prefs have been |
191 // registered above for this test as kPreferenceResetTime is already | 196 // registered above for this test as kPreferenceResetTime is already |
192 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. | 197 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
193 prefs::TrackedPreferenceMetadata pref_reset_time_config = { | 198 prefs::TrackedPreferenceMetadata pref_reset_time_config = { |
194 (*configuration_.rbegin())->reporting_id + 1, | 199 (*configuration_.rbegin())->reporting_id + 1, |
195 user_prefs::kPreferenceResetTime, EnforcementLevel::ENFORCE_ON_LOAD, | 200 user_prefs::kPreferenceResetTime, EnforcementLevel::ENFORCE_ON_LOAD, |
196 PrefTrackingStrategy::ATOMIC}; | 201 PrefTrackingStrategy::ATOMIC}; |
197 configuration_.push_back( | 202 configuration_.push_back( |
198 prefs::ConstructTrackedMetadata(pref_reset_time_config)); | 203 prefs::ConstructTrackedMetadata(pref_reset_time_config)); |
199 | 204 |
200 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 205 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
201 ReloadConfiguration(); | 206 ReloadConfiguration(); |
202 } | 207 } |
203 | 208 |
204 void ReloadConfiguration() { | 209 void ReloadConfiguration() { |
205 RelaunchPrefService(); | 210 RelaunchPrefService(); |
206 manager_.reset(new ProfilePrefStoreManager( | 211 manager_.reset(new ProfilePrefStoreManager(profile_dir_.GetPath(), seed_, |
207 profile_dir_.GetPath(), | 212 "device_id")); |
208 prefs::CloneTrackedConfiguration(configuration_), kReportingIdCount, | |
209 seed_, "device_id", &local_state_)); | |
210 } | 213 } |
211 | 214 |
212 void TearDown() override { | 215 void TearDown() override { |
213 DestroyPrefStore(); | 216 DestroyPrefStore(); |
214 if (GetParam()) { | 217 if (GetParam()) { |
215 connector_.reset(); | 218 connector_.reset(); |
216 pref_service_context_.reset(); | 219 pref_service_context_.reset(); |
217 } | 220 } |
218 worker_pool_.reset(); | 221 worker_pool_.reset(); |
219 } | 222 } |
220 | 223 |
221 protected: | 224 protected: |
222 bool SupportsPreferenceTracking() { | |
223 return ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking && | |
224 !GetParam(); | |
225 } | |
226 | |
227 void RelaunchPrefService() { | 225 void RelaunchPrefService() { |
228 if (!GetParam()) | 226 if (!GetParam()) |
229 return; | 227 return; |
230 | 228 |
231 service_manager::mojom::ServicePtr service_ptr; | 229 service_manager::mojom::ServicePtr service_ptr; |
232 pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( | 230 pref_service_context_ = base::MakeUnique<service_manager::ServiceContext>( |
233 prefs::CreatePrefService( | 231 prefs::CreatePrefService( |
234 std::set<PrefValueStore::PrefStoreType>( | 232 std::set<PrefValueStore::PrefStoreType>( |
235 {PrefValueStore::USER_STORE, PrefValueStore::DEFAULT_STORE}), | 233 {PrefValueStore::USER_STORE, PrefValueStore::DEFAULT_STORE}), |
236 worker_pool_->pool()), | 234 worker_pool_->pool()), |
237 mojo::MakeRequest(&service_ptr)); | 235 mojo::MakeRequest(&service_ptr)); |
238 } | 236 } |
239 | 237 |
240 // Verifies whether a reset was reported via the RecordReset() hook. Also | 238 // Verifies whether a reset was reported via the OnResetOnLoad() hook. Also |
241 // verifies that GetResetTime() was set (or not) accordingly. | 239 // verifies that GetResetTime() was set (or not) accordingly. |
242 void VerifyResetRecorded(bool reset_expected) { | 240 void VerifyResetRecorded(bool reset_expected) { |
241 base::RunLoop().RunUntilIdle(); | |
243 EXPECT_EQ(reset_expected, reset_recorded_); | 242 EXPECT_EQ(reset_expected, reset_recorded_); |
244 | 243 |
245 PrefServiceFactory pref_service_factory; | 244 PrefServiceFactory pref_service_factory; |
246 pref_service_factory.set_user_prefs(pref_store_); | 245 pref_service_factory.set_user_prefs(pref_store_); |
247 | 246 |
248 std::unique_ptr<PrefService> pref_service( | 247 std::unique_ptr<PrefService> pref_service( |
249 pref_service_factory.Create(profile_pref_registry_.get())); | 248 pref_service_factory.Create(profile_pref_registry_.get())); |
250 | 249 |
251 EXPECT_EQ( | 250 EXPECT_EQ( |
252 reset_expected, | 251 reset_expected, |
253 !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null()); | 252 !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null()); |
254 } | 253 } |
255 | 254 |
256 void ClearResetRecorded() { | 255 void ClearResetRecorded() { |
257 reset_recorded_ = false; | 256 reset_recorded_ = false; |
258 | 257 |
259 PrefServiceFactory pref_service_factory; | 258 PrefServiceFactory pref_service_factory; |
260 pref_service_factory.set_user_prefs(pref_store_); | 259 pref_service_factory.set_user_prefs(pref_store_); |
261 | 260 |
262 std::unique_ptr<PrefService> pref_service( | 261 std::unique_ptr<PrefService> pref_service( |
263 pref_service_factory.Create(profile_pref_registry_.get())); | 262 pref_service_factory.Create(profile_pref_registry_.get())); |
264 | 263 |
265 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); | 264 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); |
266 } | 265 } |
267 | 266 |
268 void InitializePrefs() { | 267 void InitializePrefs() { |
269 // According to the implementation of ProfilePrefStoreManager, this is | 268 // According to the implementation of ProfilePrefStoreManager, this is |
270 // actually a SegregatedPrefStore backed by two underlying pref stores. | 269 // actually a SegregatedPrefStore backed by two underlying pref stores. |
271 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( | |
272 mock_validation_delegate_record_); | |
273 scoped_refptr<PersistentPrefStore> pref_store = | 270 scoped_refptr<PersistentPrefStore> pref_store = |
274 manager_->CreateProfilePrefStore( | 271 manager_->CreateProfilePrefStore( |
275 main_message_loop_.task_runner(), | 272 prefs::CloneTrackedConfiguration(configuration_), kReportingIdCount, |
276 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 273 worker_pool_->pool().get(), |
277 base::Unretained(this)), | 274 reset_on_load_observer_bindings_.CreateInterfacePtrAndBind(this), |
278 mock_validation_delegate_.get(), connector_.get(), | 275 mock_validation_delegate_bindings_.CreateInterfacePtrAndBind( |
279 profile_pref_registry_); | 276 mock_validation_delegate_.get()), |
277 connector_.get(), profile_pref_registry_); | |
280 InitializePrefStore(pref_store.get()); | 278 InitializePrefStore(pref_store.get()); |
281 pref_store = NULL; | 279 pref_store = nullptr; |
282 base::RunLoop().RunUntilIdle(); | 280 pref_service_context_.reset(); |
281 worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | |
282 2, "ProfilePrefStoreManagerTest"); | |
283 RelaunchPrefService(); | |
gab
2017/04/06 16:00:07
Add a comment here and below about why relaunching
| |
283 } | 284 } |
284 | 285 |
285 void DestroyPrefStore() { | 286 void DestroyPrefStore() { |
286 if (pref_store_.get()) { | 287 if (pref_store_.get()) { |
287 ClearResetRecorded(); | 288 ClearResetRecorded(); |
288 // Force everything to be written to disk, triggering the PrefHashFilter | 289 // Force everything to be written to disk, triggering the PrefHashFilter |
289 // while our RegistryVerifier is watching. | 290 // while our RegistryVerifier is watching. |
290 pref_store_->CommitPendingWrite(); | 291 pref_store_->CommitPendingWrite(); |
291 base::RunLoop().RunUntilIdle(); | 292 base::RunLoop().RunUntilIdle(); |
292 base::RunLoop run_loop; | 293 base::RunLoop run_loop; |
293 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), | 294 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
294 worker_pool_->pool().get()) | 295 worker_pool_->pool().get()) |
295 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), | 296 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
296 run_loop.QuitClosure()); | 297 run_loop.QuitClosure()); |
297 run_loop.Run(); | 298 run_loop.Run(); |
298 | 299 |
299 pref_store_->RemoveObserver(®istry_verifier_); | 300 pref_store_->RemoveObserver(®istry_verifier_); |
300 pref_store_ = NULL; | 301 pref_store_ = NULL; |
301 // Nothing should have to happen on the background threads, but just in | 302 // Nothing should have to happen on the background threads, but just in |
302 // case... | 303 // case... |
303 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
304 } | 305 } |
306 pref_service_context_.reset(); | |
307 worker_pool_ = base::MakeUnique<base::SequencedWorkerPoolOwner>( | |
308 2, "ProfilePrefStoreManagerTest"); | |
305 RelaunchPrefService(); | 309 RelaunchPrefService(); |
306 } | 310 } |
307 | 311 |
308 void InitializePrefStore(PersistentPrefStore* pref_store) { | 312 void InitializePrefStore(PersistentPrefStore* pref_store) { |
309 pref_store->AddObserver(®istry_verifier_); | 313 pref_store->AddObserver(®istry_verifier_); |
310 PrefStoreReadObserver read_observer(pref_store); | 314 PrefStoreReadObserver read_observer(pref_store); |
311 PersistentPrefStore::PrefReadError error = read_observer.Read(); | 315 PersistentPrefStore::PrefReadError error = read_observer.Read(); |
312 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | 316 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
313 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), | 317 pref_store->SetValue(kTrackedAtomic, base::MakeUnique<base::Value>(kFoobar), |
314 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 318 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
315 pref_store->SetValue(kProtectedAtomic, | 319 pref_store->SetValue(kProtectedAtomic, |
316 base::MakeUnique<base::Value>(kHelloWorld), | 320 base::MakeUnique<base::Value>(kHelloWorld), |
317 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 321 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
318 pref_store->SetValue(kUnprotectedPref, | 322 pref_store->SetValue(kUnprotectedPref, |
319 base::MakeUnique<base::Value>(kFoobar), | 323 base::MakeUnique<base::Value>(kFoobar), |
320 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 324 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
321 pref_store->RemoveObserver(®istry_verifier_); | 325 pref_store->RemoveObserver(®istry_verifier_); |
322 pref_store->CommitPendingWrite(); | 326 pref_store->CommitPendingWrite(); |
323 base::RunLoop().RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
324 base::RunLoop run_loop; | 328 base::RunLoop run_loop; |
325 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), | 329 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), |
326 worker_pool_->pool().get()) | 330 worker_pool_->pool().get()) |
327 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), | 331 ->PostTaskAndReply(FROM_HERE, base::Bind(&base::DoNothing), |
328 run_loop.QuitClosure()); | 332 run_loop.QuitClosure()); |
329 run_loop.Run(); | 333 run_loop.Run(); |
330 } | 334 } |
331 | 335 |
332 void LoadExistingPrefs() { | 336 void LoadExistingPrefs() { |
333 DestroyPrefStore(); | 337 DestroyPrefStore(); |
334 // Constructing the PrefStore consumes the tracking configuration in | |
335 // |manager_|, so recreate it before constructing another PrefStore. | |
336 ReloadConfiguration(); | |
337 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>( | |
338 mock_validation_delegate_record_); | |
339 pref_store_ = manager_->CreateProfilePrefStore( | 338 pref_store_ = manager_->CreateProfilePrefStore( |
340 JsonPrefStore::GetTaskRunnerForFile(profile_dir_.GetPath(), | 339 prefs::CloneTrackedConfiguration(configuration_), kReportingIdCount, |
341 worker_pool_->pool().get()), | 340 worker_pool_->pool().get(), |
342 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, | 341 reset_on_load_observer_bindings_.CreateInterfacePtrAndBind(this), |
343 base::Unretained(this)), | 342 mock_validation_delegate_bindings_.CreateInterfacePtrAndBind( |
344 mock_validation_delegate_.get(), connector_.get(), | 343 mock_validation_delegate_.get()), |
345 profile_pref_registry_); | 344 connector_.get(), profile_pref_registry_); |
346 pref_store_->AddObserver(®istry_verifier_); | 345 pref_store_->AddObserver(®istry_verifier_); |
347 PrefStoreReadObserver read_observer(pref_store_); | 346 PrefStoreReadObserver read_observer(pref_store_); |
348 read_observer.Read(); | 347 read_observer.Read(); |
349 } | 348 } |
350 | 349 |
351 void ReplaceStringInPrefs(const std::string& find, | 350 void ReplaceStringInPrefs(const std::string& find, |
352 const std::string& replace) { | 351 const std::string& replace) { |
353 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, | 352 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, |
354 base::FileEnumerator::FILES); | 353 base::FileEnumerator::FILES); |
355 | 354 |
(...skipping 16 matching lines...) Expand all Loading... | |
372 ADD_FAILURE() << name << " is not a defined value."; | 371 ADD_FAILURE() << name << " is not a defined value."; |
373 } else if (!value->GetAsString(&as_string)) { | 372 } else if (!value->GetAsString(&as_string)) { |
374 ADD_FAILURE() << name << " could not be coerced to a string."; | 373 ADD_FAILURE() << name << " could not be coerced to a string."; |
375 } else { | 374 } else { |
376 EXPECT_EQ(expected, as_string); | 375 EXPECT_EQ(expected, as_string); |
377 } | 376 } |
378 } | 377 } |
379 | 378 |
380 void ExpectValidationObserved(const std::string& pref_path) { | 379 void ExpectValidationObserved(const std::string& pref_path) { |
381 // No validations are expected for platforms that do not support tracking. | 380 // No validations are expected for platforms that do not support tracking. |
382 if (!SupportsPreferenceTracking()) | 381 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking) |
383 return; | 382 return; |
384 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) | 383 if (!mock_validation_delegate_record_->GetEventForPath(pref_path)) |
385 ADD_FAILURE() << "No validation observed for preference: " << pref_path; | 384 ADD_FAILURE() << "No validation observed for preference: " << pref_path; |
386 } | 385 } |
387 | 386 |
388 base::MessageLoop main_message_loop_; | 387 base::MessageLoop main_message_loop_; |
389 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> configuration_; | 388 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> configuration_; |
390 base::ScopedTempDir profile_dir_; | 389 base::ScopedTempDir profile_dir_; |
391 TestingPrefServiceSimple local_state_; | |
392 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; | 390 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; |
393 RegistryVerifier registry_verifier_; | 391 RegistryVerifier registry_verifier_; |
394 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; | 392 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; |
395 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> | 393 std::unique_ptr<MockValidationDelegate> mock_validation_delegate_; |
396 mock_validation_delegate_; | 394 mojo::BindingSet<prefs::mojom::TrackedPreferenceValidationDelegate> |
395 mock_validation_delegate_bindings_; | |
397 std::unique_ptr<ProfilePrefStoreManager> manager_; | 396 std::unique_ptr<ProfilePrefStoreManager> manager_; |
398 scoped_refptr<PersistentPrefStore> pref_store_; | 397 scoped_refptr<PersistentPrefStore> pref_store_; |
399 | 398 |
400 std::string seed_; | 399 std::string seed_; |
401 | 400 |
402 private: | 401 private: |
403 void RecordReset() { | 402 void OnResetOnLoad() override { |
404 // As-is |reset_recorded_| is only designed to remember a single reset, make | 403 // As-is |reset_recorded_| is only designed to remember a single reset, make |
405 // sure none was previously recorded (or that ClearResetRecorded() was | 404 // sure none was previously recorded (or that ClearResetRecorded() was |
406 // called). | 405 // called). |
407 EXPECT_FALSE(reset_recorded_); | 406 EXPECT_FALSE(reset_recorded_); |
408 reset_recorded_ = true; | 407 reset_recorded_ = true; |
409 } | 408 } |
410 | 409 |
411 void BindInterface(const std::string& interface_name, | 410 void BindInterface(const std::string& interface_name, |
412 mojo::ScopedMessagePipeHandle handle) { | 411 mojo::ScopedMessagePipeHandle handle) { |
413 service_manager::ServiceInfo source( | 412 service_manager::ServiceInfo source( |
414 service_manager::Identity(content::mojom::kBrowserServiceName, | 413 service_manager::Identity(content::mojom::kBrowserServiceName, |
415 service_manager::mojom::kRootUserID), | 414 service_manager::mojom::kRootUserID), |
416 service_manager::InterfaceProviderSpecMap()); | 415 service_manager::InterfaceProviderSpecMap()); |
417 static_cast<service_manager::mojom::Service*>(pref_service_context_.get()) | 416 static_cast<service_manager::mojom::Service*>(pref_service_context_.get()) |
418 ->OnBindInterface(source, interface_name, std::move(handle), | 417 ->OnBindInterface(source, interface_name, std::move(handle), |
419 base::Bind(&base::DoNothing)); | 418 base::Bind(&base::DoNothing)); |
420 } | 419 } |
421 | 420 |
422 base::test::ScopedFeatureList feature_list_; | 421 base::test::ScopedFeatureList feature_list_; |
423 bool reset_recorded_; | 422 bool reset_recorded_; |
424 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; | 423 std::unique_ptr<base::SequencedWorkerPoolOwner> worker_pool_; |
425 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; | 424 std::unique_ptr<service_manager::ServiceContext> pref_service_context_; |
426 std::unique_ptr<service_manager::Connector> connector_; | 425 std::unique_ptr<service_manager::Connector> connector_; |
427 service_manager::mojom::ConnectorRequest connector_request_; | 426 service_manager::mojom::ConnectorRequest connector_request_; |
427 mojo::BindingSet<prefs::mojom::ResetOnLoadObserver> | |
428 reset_on_load_observer_bindings_; | |
428 }; | 429 }; |
429 | 430 |
430 TEST_P(ProfilePrefStoreManagerTest, StoreValues) { | 431 TEST_P(ProfilePrefStoreManagerTest, StoreValues) { |
431 InitializePrefs(); | 432 InitializePrefs(); |
432 | 433 |
433 LoadExistingPrefs(); | 434 LoadExistingPrefs(); |
434 | 435 |
435 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 436 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
436 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 437 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
437 VerifyResetRecorded(false); | 438 VerifyResetRecorded(false); |
438 ExpectValidationObserved(kTrackedAtomic); | 439 ExpectValidationObserved(kTrackedAtomic); |
439 ExpectValidationObserved(kProtectedAtomic); | 440 ExpectValidationObserved(kProtectedAtomic); |
440 } | 441 } |
441 | 442 |
442 TEST_P(ProfilePrefStoreManagerTest, ProtectValues) { | 443 TEST_P(ProfilePrefStoreManagerTest, ProtectValues) { |
443 InitializePrefs(); | 444 InitializePrefs(); |
444 | 445 |
445 ReplaceStringInPrefs(kFoobar, kBarfoo); | 446 ReplaceStringInPrefs(kFoobar, kBarfoo); |
446 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | 447 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); |
447 | 448 |
448 LoadExistingPrefs(); | 449 LoadExistingPrefs(); |
449 | 450 |
450 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | 451 // kTrackedAtomic is unprotected and thus will be loaded as it appears on |
451 // disk. | 452 // disk. |
452 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | 453 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); |
453 | 454 |
454 // If preference tracking is supported, the tampered value of kProtectedAtomic | 455 // If preference tracking is supported, the tampered value of kProtectedAtomic |
455 // will be discarded at load time, leaving this preference undefined. | 456 // will be discarded at load time, leaving this preference undefined. |
456 EXPECT_NE(SupportsPreferenceTracking(), | 457 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
457 pref_store_->GetValue(kProtectedAtomic, NULL)); | 458 pref_store_->GetValue(kProtectedAtomic, NULL)); |
458 VerifyResetRecorded(SupportsPreferenceTracking()); | 459 VerifyResetRecorded( |
460 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
459 | 461 |
460 ExpectValidationObserved(kTrackedAtomic); | 462 ExpectValidationObserved(kTrackedAtomic); |
461 ExpectValidationObserved(kProtectedAtomic); | 463 ExpectValidationObserved(kProtectedAtomic); |
462 } | 464 } |
463 | 465 |
464 TEST_P(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 466 TEST_P(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
465 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); | 467 auto master_prefs = base::MakeUnique<base::DictionaryValue>(); |
466 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); | 468 master_prefs->Set(kTrackedAtomic, new base::Value(kFoobar)); |
467 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); | 469 master_prefs->Set(kProtectedAtomic, new base::Value(kHelloWorld)); |
468 EXPECT_TRUE( | 470 EXPECT_TRUE(manager_->InitializePrefsFromMasterPrefs( |
469 manager_->InitializePrefsFromMasterPrefs(std::move(master_prefs))); | 471 prefs::CloneTrackedConfiguration(configuration_), kReportingIdCount, |
472 std::move(master_prefs))); | |
470 | 473 |
471 LoadExistingPrefs(); | 474 LoadExistingPrefs(); |
472 | 475 |
473 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 476 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
474 // necessary to authenticate these values. | 477 // necessary to authenticate these values. |
475 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 478 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
476 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 479 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
477 VerifyResetRecorded(false); | 480 VerifyResetRecorded(false); |
478 } | 481 } |
479 | 482 |
(...skipping 30 matching lines...) Expand all Loading... | |
510 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); | 513 ExpectStringValueEquals(kUnprotectedPref, kBarfoo); |
511 VerifyResetRecorded(false); | 514 VerifyResetRecorded(false); |
512 | 515 |
513 // Ensure everything is written out to disk. | 516 // Ensure everything is written out to disk. |
514 DestroyPrefStore(); | 517 DestroyPrefStore(); |
515 | 518 |
516 // It's protected now, so (if the platform supports it) any tampering should | 519 // It's protected now, so (if the platform supports it) any tampering should |
517 // lead to a reset. | 520 // lead to a reset. |
518 ReplaceStringInPrefs(kBarfoo, kFoobar); | 521 ReplaceStringInPrefs(kBarfoo, kFoobar); |
519 LoadExistingPrefs(); | 522 LoadExistingPrefs(); |
520 EXPECT_NE(SupportsPreferenceTracking(), | 523 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
521 pref_store_->GetValue(kUnprotectedPref, NULL)); | 524 pref_store_->GetValue(kUnprotectedPref, NULL)); |
522 VerifyResetRecorded(SupportsPreferenceTracking()); | 525 VerifyResetRecorded( |
526 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
523 } | 527 } |
524 | 528 |
525 TEST_P(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | 529 TEST_P(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { |
526 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> | 530 std::vector<prefs::mojom::TrackedPreferenceMetadataPtr> |
527 original_configuration = prefs::CloneTrackedConfiguration(configuration_); | 531 original_configuration = prefs::CloneTrackedConfiguration(configuration_); |
528 for (const auto& metadata : configuration_) { | 532 for (const auto& metadata : configuration_) { |
529 metadata->enforcement_level = EnforcementLevel::NO_ENFORCEMENT; | 533 metadata->enforcement_level = EnforcementLevel::NO_ENFORCEMENT; |
530 } | 534 } |
531 ReloadConfiguration(); | 535 ReloadConfiguration(); |
532 | 536 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 PrefTrackingStrategy::ATOMIC}; | 574 PrefTrackingStrategy::ATOMIC}; |
571 configuration_.push_back(prefs::ConstructTrackedMetadata(new_protected)); | 575 configuration_.push_back(prefs::ConstructTrackedMetadata(new_protected)); |
572 seed_ = "new-seed-to-break-trust"; | 576 seed_ = "new-seed-to-break-trust"; |
573 ReloadConfiguration(); | 577 ReloadConfiguration(); |
574 | 578 |
575 // And try loading with the new configuration. | 579 // And try loading with the new configuration. |
576 LoadExistingPrefs(); | 580 LoadExistingPrefs(); |
577 | 581 |
578 // If preference tracking is supported, kUnprotectedPref will have been | 582 // If preference tracking is supported, kUnprotectedPref will have been |
579 // discarded because new values are not accepted without a valid super MAC. | 583 // discarded because new values are not accepted without a valid super MAC. |
580 EXPECT_NE(SupportsPreferenceTracking(), | 584 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
581 pref_store_->GetValue(kUnprotectedPref, NULL)); | 585 pref_store_->GetValue(kUnprotectedPref, NULL)); |
582 VerifyResetRecorded(SupportsPreferenceTracking()); | 586 VerifyResetRecorded( |
587 ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking); | |
583 } | 588 } |
584 | 589 |
585 // This test verifies that preference values are correctly maintained when a | 590 // This test verifies that preference values are correctly maintained when a |
586 // preference's protection state changes from protected to unprotected. | 591 // preference's protection state changes from protected to unprotected. |
587 TEST_P(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { | 592 TEST_P(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
588 InitializePrefs(); | 593 InitializePrefs(); |
589 | 594 |
590 ExpectValidationObserved(kTrackedAtomic); | 595 ExpectValidationObserved(kTrackedAtomic); |
591 ExpectValidationObserved(kProtectedAtomic); | 596 ExpectValidationObserved(kProtectedAtomic); |
592 | 597 |
(...skipping 24 matching lines...) Expand all Loading... | |
617 // Trigger the logic that migrates it back to the unprotected preferences | 622 // Trigger the logic that migrates it back to the unprotected preferences |
618 // file. | 623 // file. |
619 pref_store_->SetValue(kProtectedAtomic, | 624 pref_store_->SetValue(kProtectedAtomic, |
620 base::WrapUnique(new base::Value(kGoodbyeWorld)), | 625 base::WrapUnique(new base::Value(kGoodbyeWorld)), |
621 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 626 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
622 LoadExistingPrefs(); | 627 LoadExistingPrefs(); |
623 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 628 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
624 VerifyResetRecorded(false); | 629 VerifyResetRecorded(false); |
625 } | 630 } |
626 | 631 |
632 // The parameter controls whether the user pref store is created within a | |
633 // service. | |
627 INSTANTIATE_TEST_CASE_P(ProfilePrefStoreManagerTest, | 634 INSTANTIATE_TEST_CASE_P(ProfilePrefStoreManagerTest, |
628 ProfilePrefStoreManagerTest, | 635 ProfilePrefStoreManagerTest, |
629 testing::Bool()); | 636 testing::Bool()); |
OLD | NEW |