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/pref_hash_filter.h" | 25 #include "chrome/browser/prefs/pref_hash_filter.h" |
| 26 #include "chrome/common/pref_names.h" |
26 #include "components/user_prefs/pref_registry_syncable.h" | 27 #include "components/user_prefs/pref_registry_syncable.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 class FirstEqualsPredicate { | 32 class FirstEqualsPredicate { |
32 public: | 33 public: |
33 explicit FirstEqualsPredicate(const std::string& expected) | 34 explicit FirstEqualsPredicate(const std::string& expected) |
34 : expected_(expected) {} | 35 : expected_(expected) {} |
35 bool operator()(const std::pair<std::string, base::Value*>& pair) { | 36 bool operator()(const std::pair<std::string, base::Value*>& pair) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } else { | 105 } else { |
105 profile_pref_registry_->RegisterDictionaryPref( | 106 profile_pref_registry_->RegisterDictionaryPref( |
106 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 107 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
107 } | 108 } |
108 } | 109 } |
109 profile_pref_registry_->RegisterStringPref( | 110 profile_pref_registry_->RegisterStringPref( |
110 kUnprotectedPref, | 111 kUnprotectedPref, |
111 std::string(), | 112 std::string(), |
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
113 | 114 |
| 115 // As in chrome_pref_service_factory.cc, kPreferencesResetTime needs to be |
| 116 // declared as protected in order to be read from the proper store by the |
| 117 // SegregatedPrefStore. Only declare it after configured prefs have been |
| 118 // registered above for this test as kPreferenceResetTime is already |
| 119 // registered in ProfilePrefStoreManager::RegisterProfilePrefs. |
| 120 PrefHashFilter::TrackedPreferenceMetadata pref_reset_time_config = |
| 121 {configuration_.rbegin()->reporting_id + 1, prefs::kPreferenceResetTime, |
| 122 PrefHashFilter::ENFORCE_ON_LOAD, |
| 123 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; |
| 124 configuration_.push_back(pref_reset_time_config); |
| 125 |
114 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 126 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
115 ReloadConfiguration(); | 127 ReloadConfiguration(); |
116 } | 128 } |
117 | 129 |
118 void ReloadConfiguration() { | 130 void ReloadConfiguration() { |
119 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(), | 131 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(), |
120 configuration_, | 132 configuration_, |
121 kReportingIdCount, | 133 kReportingIdCount, |
122 "seed", | 134 "seed", |
123 "device_id", | 135 "device_id", |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 LoadExistingPrefs(); | 427 LoadExistingPrefs(); |
416 | 428 |
417 // If preference tracking is supported, kUnprotectedPref will have been | 429 // If preference tracking is supported, kUnprotectedPref will have been |
418 // discarded because new values are not accepted without a valid super MAC. | 430 // discarded because new values are not accepted without a valid super MAC. |
419 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 431 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
420 pref_store_->GetValue(kUnprotectedPref, NULL)); | 432 pref_store_->GetValue(kUnprotectedPref, NULL)); |
421 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 433 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
422 WasResetRecorded()); | 434 WasResetRecorded()); |
423 } | 435 } |
424 | 436 |
425 // This test does not directly verify that the values are moved from one pref | 437 // This test verifies that preference values are correctly maintained when a |
426 // store to the other. segregated_pref_store_unittest.cc _does_ verify that | |
427 // functionality. | |
428 // | |
429 // _This_ test verifies that preference values are correctly maintained when a | |
430 // preference's protection state changes from protected to unprotected. | 438 // preference's protection state changes from protected to unprotected. |
431 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { | 439 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { |
432 InitializePrefs(); | 440 InitializePrefs(); |
433 DestroyPrefStore(); | 441 DestroyPrefStore(); |
434 | 442 |
435 // Unconfigure protection for kProtectedAtomic | 443 // Unconfigure protection for kProtectedAtomic |
436 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | 444 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = |
437 configuration_.begin(); | 445 configuration_.begin(); |
438 it != configuration_.end(); | 446 it != configuration_.end(); |
439 ++it) { | 447 ++it) { |
440 if (it->name == kProtectedAtomic) { | 448 if (it->name == kProtectedAtomic) { |
441 configuration_.erase(it); | 449 it->enforcement_level = PrefHashFilter::NO_ENFORCEMENT; |
442 break; | 450 break; |
443 } | 451 } |
444 } | 452 } |
445 ReloadConfiguration(); | 453 ReloadConfiguration(); |
446 | 454 |
447 // Reset the hash stores and then try loading the prefs. | 455 // Reset the hash stores and then try loading the prefs. |
448 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); | 456 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); |
449 LoadExistingPrefs(); | 457 LoadExistingPrefs(); |
450 | 458 |
451 // Verify that the value was not reset. | 459 // Verify that the value was not reset. |
452 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 460 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
453 EXPECT_FALSE(WasResetRecorded()); | 461 EXPECT_FALSE(WasResetRecorded()); |
454 | 462 |
455 // Accessing the value of the previously protected pref didn't trigger its | 463 // Accessing the value of the previously protected pref didn't trigger its |
456 // move to the unprotected preferences file, though the loading of the pref | 464 // move to the unprotected preferences file, though the loading of the pref |
457 // store should still have caused the MAC store to be recalculated. | 465 // store should still have caused the MAC store to be recalculated. |
458 LoadExistingPrefs(); | 466 LoadExistingPrefs(); |
459 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 467 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
460 | 468 |
461 // Trigger the logic that migrates it back to the unprotected preferences | 469 // Trigger the logic that migrates it back to the unprotected preferences |
462 // file. | 470 // file. |
463 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); | 471 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); |
464 LoadExistingPrefs(); | 472 LoadExistingPrefs(); |
465 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | 473 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); |
466 EXPECT_FALSE(WasResetRecorded()); | 474 EXPECT_FALSE(WasResetRecorded()); |
467 } | 475 } |
OLD | NEW |