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