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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/prefs/json_pref_store.h" | 16 #include "base/prefs/json_pref_store.h" |
17 #include "base/prefs/persistent_pref_store.h" | 17 #include "base/prefs/persistent_pref_store.h" |
18 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
19 #include "base/prefs/pref_service_factory.h" | 19 #include "base/prefs/pref_service_factory.h" |
20 #include "base/prefs/pref_store.h" | 20 #include "base/prefs/pref_store.h" |
21 #include "base/prefs/testing_pref_service.h" | 21 #include "base/prefs/testing_pref_service.h" |
22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
24 #include "base/values.h" | 24 #include "base/values.h" |
25 #include "chrome/browser/prefs/mock_validation_delegate.h" | 25 #include "chrome/browser/prefs/mock_validation_delegate.h" |
26 #include "chrome/browser/prefs/pref_hash_filter.h" | 26 #include "chrome/browser/prefs/pref_hash_filter.h" |
27 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h" | |
27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
28 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 class FirstEqualsPredicate { | 34 class FirstEqualsPredicate { |
34 public: | 35 public: |
35 explicit FirstEqualsPredicate(const std::string& expected) | 36 explicit FirstEqualsPredicate(const std::string& expected) |
36 : expected_(expected) {} | 37 : expected_(expected) {} |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 bool WasResetRecorded() { | 145 bool WasResetRecorded() { |
145 base::PrefServiceFactory pref_service_factory; | 146 base::PrefServiceFactory pref_service_factory; |
146 pref_service_factory.set_user_prefs(pref_store_); | 147 pref_service_factory.set_user_prefs(pref_store_); |
147 | 148 |
148 scoped_ptr<PrefService> pref_service( | 149 scoped_ptr<PrefService> pref_service( |
149 pref_service_factory.Create(profile_pref_registry_)); | 150 pref_service_factory.Create(profile_pref_registry_)); |
150 | 151 |
151 return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null(); | 152 return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null(); |
152 } | 153 } |
153 | 154 |
155 void ClearResetRecorded() { | |
156 base::PrefServiceFactory pref_service_factory; | |
157 pref_service_factory.set_user_prefs(pref_store_); | |
158 | |
159 scoped_ptr<PrefService> pref_service( | |
160 pref_service_factory.Create(profile_pref_registry_)); | |
161 | |
162 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); | |
163 } | |
164 | |
154 void InitializePrefs() { | 165 void InitializePrefs() { |
155 // According to the implementation of ProfilePrefStoreManager, this is | 166 // According to the implementation of ProfilePrefStoreManager, this is |
156 // actually a SegregatedPrefStore backed by two underlying pref stores. | 167 // actually a SegregatedPrefStore backed by two underlying pref stores. |
157 scoped_refptr<PersistentPrefStore> pref_store = | 168 scoped_refptr<PersistentPrefStore> pref_store = |
158 manager_->CreateProfilePrefStore( | 169 manager_->CreateProfilePrefStore( |
159 main_message_loop_.message_loop_proxy(), | 170 main_message_loop_.message_loop_proxy(), |
160 &mock_validation_delegate_); | 171 &mock_validation_delegate_); |
161 InitializePrefStore(pref_store); | 172 InitializePrefStore(pref_store); |
162 pref_store = NULL; | 173 pref_store = NULL; |
163 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
164 } | 175 } |
165 | 176 |
166 void DestroyPrefStore() { | 177 void DestroyPrefStore() { |
167 if (pref_store_) { | 178 if (pref_store_) { |
179 ClearResetRecorded(); | |
168 // Force everything to be written to disk, triggering the PrefHashFilter | 180 // Force everything to be written to disk, triggering the PrefHashFilter |
169 // while our RegistryVerifier is watching. | 181 // while our RegistryVerifier is watching. |
170 pref_store_->CommitPendingWrite(); | 182 pref_store_->CommitPendingWrite(); |
171 base::RunLoop().RunUntilIdle(); | 183 base::RunLoop().RunUntilIdle(); |
172 | 184 |
173 pref_store_->RemoveObserver(®istry_verifier_); | 185 pref_store_->RemoveObserver(®istry_verifier_); |
174 pref_store_ = NULL; | 186 pref_store_ = NULL; |
175 // Nothing should have to happen on the background threads, but just in | 187 // Nothing should have to happen on the background threads, but just in |
176 // case... | 188 // case... |
177 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 312 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
301 WasResetRecorded()); | 313 WasResetRecorded()); |
302 | 314 |
303 ExpectValidationObserved(kTrackedAtomic); | 315 ExpectValidationObserved(kTrackedAtomic); |
304 ExpectValidationObserved(kProtectedAtomic); | 316 ExpectValidationObserved(kProtectedAtomic); |
305 } | 317 } |
306 | 318 |
307 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) { | 319 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) { |
308 InitializeDeprecatedCombinedProfilePrefStore(); | 320 InitializeDeprecatedCombinedProfilePrefStore(); |
309 | 321 |
322 // The deprecated model stores hashes in local state. | |
323 ASSERT_TRUE(local_state_.GetUserPrefValue( | |
gab
2014/06/17 22:08:22
Tests appear to be choking on this; as if there wa
erikwright (departed)
2014/06/18 15:10:46
Done.
| |
324 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
325 | |
310 LoadExistingPrefs(); | 326 LoadExistingPrefs(); |
311 | 327 |
328 // After a first migration, the hashes were copied to the two user preference | |
329 // files but were not cleaned. | |
330 ASSERT_TRUE(local_state_.GetUserPrefValue( | |
331 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
332 | |
333 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | |
334 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
335 EXPECT_FALSE(WasResetRecorded()); | |
336 | |
337 LoadExistingPrefs(); | |
338 | |
339 // In a subsequent launch, the local state hash store would be reset. | |
340 ASSERT_FALSE(local_state_.GetUserPrefValue( | |
341 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
342 | |
312 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 343 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
313 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 344 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
314 EXPECT_FALSE(WasResetRecorded()); | 345 EXPECT_FALSE(WasResetRecorded()); |
315 } | 346 } |
316 | 347 |
348 TEST_F(ProfilePrefStoreManagerTest, MigrateWithTampering) { | |
349 InitializeDeprecatedCombinedProfilePrefStore(); | |
350 | |
351 ReplaceStringInPrefs(kFoobar, kBarfoo); | |
352 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | |
353 | |
354 // The deprecated model stores hashes in local state. | |
355 ASSERT_TRUE(local_state_.GetUserPrefValue( | |
356 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
357 | |
358 LoadExistingPrefs(); | |
359 | |
360 // After a first migration, the hashes were copied to the two user preference | |
361 // files but were not cleaned. | |
362 ASSERT_TRUE(local_state_.GetUserPrefValue( | |
363 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
364 | |
365 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | |
366 // disk. | |
367 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | |
368 | |
369 // If preference tracking is supported, the tampered value of kProtectedAtomic | |
370 // will be discarded at load time, leaving this preference undefined. | |
371 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
372 pref_store_->GetValue(kProtectedAtomic, NULL)); | |
373 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
374 WasResetRecorded()); | |
375 | |
376 LoadExistingPrefs(); | |
377 | |
378 // In a subsequent launch, the local state hash store would be reset. | |
379 ASSERT_FALSE(local_state_.GetUserPrefValue( | |
380 PrefServiceHashStoreContents::kProfilePreferenceHashes)); | |
381 | |
382 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | |
383 EXPECT_FALSE(WasResetRecorded()); | |
384 } | |
385 | |
317 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 386 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
318 base::DictionaryValue master_prefs; | 387 base::DictionaryValue master_prefs; |
319 master_prefs.Set(kTrackedAtomic, new base::StringValue(kFoobar)); | 388 master_prefs.Set(kTrackedAtomic, new base::StringValue(kFoobar)); |
320 master_prefs.Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); | 389 master_prefs.Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); |
321 EXPECT_TRUE(manager_->InitializePrefsFromMasterPrefs(master_prefs)); | 390 EXPECT_TRUE(manager_->InitializePrefsFromMasterPrefs(master_prefs)); |
322 | 391 |
323 LoadExistingPrefs(); | 392 LoadExistingPrefs(); |
324 | 393 |
325 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 394 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
326 // necessary to authenticate these values. | 395 // necessary to authenticate these values. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 // It's protected now, so (if the platform supports it) any tampering should | 437 // It's protected now, so (if the platform supports it) any tampering should |
369 // lead to a reset. | 438 // lead to a reset. |
370 ReplaceStringInPrefs(kBarfoo, kFoobar); | 439 ReplaceStringInPrefs(kBarfoo, kFoobar); |
371 LoadExistingPrefs(); | 440 LoadExistingPrefs(); |
372 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 441 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
373 pref_store_->GetValue(kUnprotectedPref, NULL)); | 442 pref_store_->GetValue(kUnprotectedPref, NULL)); |
374 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 443 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
375 WasResetRecorded()); | 444 WasResetRecorded()); |
376 } | 445 } |
377 | 446 |
447 TEST_F(ProfilePrefStoreManagerTest, NewPrefWhenFirstProtecting) { | |
448 std::vector<PrefHashFilter::TrackedPreferenceMetadata> | |
449 original_configuration = configuration_; | |
450 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | |
451 configuration_.begin(); | |
452 it != configuration_.end(); | |
453 ++it) { | |
454 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; | |
455 } | |
456 ReloadConfiguration(); | |
457 | |
458 InitializePrefs(); | |
459 | |
460 ExpectValidationObserved(kTrackedAtomic); | |
461 ExpectValidationObserved(kProtectedAtomic); | |
462 | |
463 LoadExistingPrefs(); | |
464 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | |
465 | |
466 // Ensure everything is written out to disk. | |
467 DestroyPrefStore(); | |
468 | |
469 // Now introduce protection, including the never-before tracked "new_pref". | |
470 configuration_ = original_configuration; | |
471 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | |
472 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | |
473 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | |
474 configuration_.push_back(new_protected); | |
475 ReloadConfiguration(); | |
476 | |
477 // And try loading with the new configuration. | |
478 LoadExistingPrefs(); | |
479 | |
480 // Since there was a valid super MAC we were able to extend the existing trust | |
481 // to the newly tracked & protected preference. | |
482 ExpectStringValueEquals(kUnprotectedPref, kFoobar); | |
483 EXPECT_FALSE(WasResetRecorded()); | |
484 } | |
485 | |
378 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | 486 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { |
379 InitializePrefs(); | 487 InitializePrefs(); |
380 | 488 |
381 ExpectValidationObserved(kTrackedAtomic); | 489 ExpectValidationObserved(kTrackedAtomic); |
382 ExpectValidationObserved(kProtectedAtomic); | 490 ExpectValidationObserved(kProtectedAtomic); |
383 | 491 |
384 // Now update the configuration to protect it. | 492 // Now update the configuration to protect it. |
385 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | 493 PrefHashFilter::TrackedPreferenceMetadata new_protected = { |
386 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, | 494 kExtraReportingId, kUnprotectedPref, PrefHashFilter::ENFORCE_ON_LOAD, |
387 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | 495 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 LoadExistingPrefs(); | 543 LoadExistingPrefs(); |
436 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 544 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
437 | 545 |
438 // Trigger the logic that migrates it back to the unprotected preferences | 546 // Trigger the logic that migrates it back to the unprotected preferences |
439 // file. | 547 // file. |
440 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); | 548 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); |
441 LoadExistingPrefs(); | 549 LoadExistingPrefs(); |
442 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 550 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
443 EXPECT_FALSE(WasResetRecorded()); | 551 EXPECT_FALSE(WasResetRecorded()); |
444 } | 552 } |
OLD | NEW |