Chromium Code Reviews| 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/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/json_pref_store.h" | 16 #include "base/prefs/json_pref_store.h" |
| 16 #include "base/prefs/persistent_pref_store.h" | 17 #include "base/prefs/persistent_pref_store.h" |
| 17 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
| 19 #include "base/prefs/pref_service_factory.h" | |
| 18 #include "base/prefs/pref_store.h" | 20 #include "base/prefs/pref_store.h" |
| 19 #include "base/prefs/testing_pref_service.h" | 21 #include "base/prefs/testing_pref_service.h" |
| 20 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 21 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 22 #include "base/values.h" | 24 #include "base/values.h" |
| 23 #include "chrome/browser/prefs/pref_hash_filter.h" | 25 #include "chrome/browser/prefs/pref_hash_filter.h" |
| 24 #include "components/user_prefs/pref_registry_syncable.h" | 26 #include "components/user_prefs/pref_registry_syncable.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 53 FirstEqualsPredicate(key))) | 55 FirstEqualsPredicate(key))) |
| 54 << "Unregistered key " << key << " was changed."; | 56 << "Unregistered key " << key << " was changed."; |
| 55 } | 57 } |
| 56 | 58 |
| 57 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {} | 59 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {} |
| 58 | 60 |
| 59 private: | 61 private: |
| 60 scoped_refptr<PrefRegistry> pref_registry_; | 62 scoped_refptr<PrefRegistry> pref_registry_; |
| 61 }; | 63 }; |
| 62 | 64 |
| 65 const char kUnprotectedAtomic[] = "unprotected_atomic"; | |
| 63 const char kTrackedAtomic[] = "tracked_atomic"; | 66 const char kTrackedAtomic[] = "tracked_atomic"; |
| 64 const char kProtectedAtomic[] = "protected_atomic"; | 67 const char kProtectedAtomic[] = "protected_atomic"; |
| 65 const char kProtectedSplit[] = "protected_split"; | 68 const char kProtectedSplit[] = "protected_split"; |
| 66 | 69 |
| 67 const char kFoobar[] = "FOOBAR"; | 70 const char kFoobar[] = "FOOBAR"; |
| 68 const char kBarfoo[] = "BARFOO"; | 71 const char kBarfoo[] = "BARFOO"; |
| 69 const char kHelloWorld[] = "HELLOWORLD"; | 72 const char kHelloWorld[] = "HELLOWORLD"; |
| 70 const char kGoodbyeWorld[] = "GOODBYEWORLD"; | 73 const char kGoodbyeWorld[] = "GOODBYEWORLD"; |
| 71 | 74 |
| 72 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { | 75 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 94 it != kConfiguration + arraysize(kConfiguration); | 97 it != kConfiguration + arraysize(kConfiguration); |
| 95 ++it) { | 98 ++it) { |
| 96 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { | 99 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { |
| 97 profile_pref_registry_->RegisterStringPref( | 100 profile_pref_registry_->RegisterStringPref( |
| 98 it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 101 it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 99 } else { | 102 } else { |
| 100 profile_pref_registry_->RegisterDictionaryPref( | 103 profile_pref_registry_->RegisterDictionaryPref( |
| 101 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 104 it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 102 } | 105 } |
| 103 } | 106 } |
| 107 profile_pref_registry_->RegisterStringPref( | |
| 108 kUnprotectedAtomic, | |
| 109 std::string(), | |
| 110 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 111 | |
| 104 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); | 112 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir()); |
| 113 ReloadConfiguration(); | |
| 114 } | |
| 105 | 115 |
| 116 void ReloadConfiguration() { | |
| 106 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(), | 117 manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(), |
| 107 configuration_, | 118 configuration_, |
| 108 configuration_.size(), | 119 configuration_.size(), |
| 109 "seed", | 120 "seed", |
| 110 "device_id", | 121 "device_id", |
| 111 &local_state_)); | 122 &local_state_)); |
| 112 } | 123 } |
| 113 | 124 |
| 114 virtual void TearDown() OVERRIDE { | 125 virtual void TearDown() OVERRIDE { DestroyPrefStore(); } |
| 126 | |
| 127 protected: | |
| 128 bool ResetRecorded() { | |
| 129 base::PrefServiceFactory pref_service_factory; | |
| 130 pref_service_factory.set_user_prefs(pref_store_); | |
| 131 return !ProfilePrefStoreManager::GetResetTime( | |
| 132 pref_service_factory.Create(profile_pref_registry_).get()) | |
|
Bernhard Bauer
2014/03/27 15:10:45
This expression is a bit big. Could you extract e.
erikwright (departed)
2014/03/27 19:26:21
Done.
| |
| 133 .is_null(); | |
| 134 } | |
| 135 | |
| 136 void InitializePrefs() { | |
| 137 scoped_refptr<PersistentPrefStore> pref_store = | |
| 138 manager_->CreateProfilePrefStore( | |
| 139 main_message_loop_.message_loop_proxy()); | |
| 140 InitializePrefStore(pref_store); | |
| 141 pref_store = NULL; | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 } | |
| 144 | |
| 145 void DestroyPrefStore() { | |
| 115 if (pref_store_) { | 146 if (pref_store_) { |
| 116 // Force everything to be written to disk, triggering the PrefHashFilter | 147 // Force everything to be written to disk, triggering the PrefHashFilter |
| 117 // while our RegistryVerifier is watching. | 148 // while our RegistryVerifier is watching. |
| 118 pref_store_->CommitPendingWrite(); | 149 pref_store_->CommitPendingWrite(); |
| 119 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
| 120 | 151 |
| 121 pref_store_->RemoveObserver(®istry_verifier_); | 152 pref_store_->RemoveObserver(®istry_verifier_); |
| 122 pref_store_ = NULL; | 153 pref_store_ = NULL; |
| 123 // Nothing should have to happen on the background threads, but just in | 154 // Nothing should have to happen on the background threads, but just in |
| 124 // case... | 155 // case... |
| 125 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
| 126 } | 157 } |
| 127 } | 158 } |
| 128 | 159 |
| 129 protected: | 160 void InitializeDeprecatedCombinedProfilePrefStore() { |
| 130 void InitializePrefs() { | |
| 131 scoped_refptr<PersistentPrefStore> pref_store = | 161 scoped_refptr<PersistentPrefStore> pref_store = |
| 132 manager_->CreateProfilePrefStore( | 162 manager_->CreateDeprecatedCombinedProfilePrefStore( |
| 133 main_message_loop_.message_loop_proxy()); | 163 main_message_loop_.message_loop_proxy()); |
| 134 pref_store->AddObserver(®istry_verifier_); | 164 InitializePrefStore(pref_store); |
| 135 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | |
| 136 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | |
| 137 pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar)); | |
| 138 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld)); | |
| 139 pref_store->RemoveObserver(®istry_verifier_); | |
| 140 pref_store = NULL; | 165 pref_store = NULL; |
| 141 base::RunLoop().RunUntilIdle(); | 166 base::RunLoop().RunUntilIdle(); |
| 142 } | 167 } |
| 143 | 168 |
| 169 void InitializePrefStore(PersistentPrefStore* pref_store) { | |
| 170 pref_store->AddObserver(®istry_verifier_); | |
| 171 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); | |
| 172 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); | |
| 173 pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar)); | |
| 174 pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld)); | |
| 175 pref_store->SetValue(kUnprotectedAtomic, new base::StringValue(kFoobar)); | |
| 176 pref_store->RemoveObserver(®istry_verifier_); | |
| 177 pref_store->CommitPendingWrite(); | |
| 178 base::RunLoop().RunUntilIdle(); | |
| 179 } | |
| 180 | |
| 144 void LoadExistingPrefs() { | 181 void LoadExistingPrefs() { |
| 182 DestroyPrefStore(); | |
| 145 pref_store_ = manager_->CreateProfilePrefStore( | 183 pref_store_ = manager_->CreateProfilePrefStore( |
| 146 main_message_loop_.message_loop_proxy()); | 184 main_message_loop_.message_loop_proxy()); |
| 147 pref_store_->AddObserver(®istry_verifier_); | 185 pref_store_->AddObserver(®istry_verifier_); |
| 148 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 186 pref_store_->ReadPrefs(); |
| 149 pref_store_->ReadPrefs()); | |
| 150 } | 187 } |
| 151 | 188 |
| 152 void ReplaceStringInPrefs(const std::string& find, | 189 void ReplaceStringInPrefs(const std::string& find, |
| 153 const std::string& replace) { | 190 const std::string& replace) { |
| 154 // Tamper with the file's contents | 191 base::FileEnumerator file_enumerator( |
| 155 base::FilePath pref_file_path = | 192 profile_dir_.path(), true, base::FileEnumerator::FILES); |
| 156 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( | 193 |
| 157 profile_dir_.path()); | 194 base::FilePath pref_file_path; |
| 158 std::string pref_file_contents; | 195 while (!(pref_file_path = file_enumerator.Next()).empty()) { |
| 159 EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents)); | 196 // Tamper with the file's contents |
| 160 ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace); | 197 std::string pref_file_contents; |
| 161 EXPECT_EQ(static_cast<int>(pref_file_contents.length()), | 198 EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents)); |
| 162 base::WriteFile(pref_file_path, | 199 ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace); |
| 163 pref_file_contents.c_str(), | 200 EXPECT_EQ(static_cast<int>(pref_file_contents.length()), |
| 164 pref_file_contents.length())); | 201 base::WriteFile(pref_file_path, |
| 202 pref_file_contents.c_str(), | |
| 203 pref_file_contents.length())); | |
| 204 } | |
| 165 } | 205 } |
| 166 | 206 |
| 167 void ExpectStringValueEquals(const std::string& name, | 207 void ExpectStringValueEquals(const std::string& name, |
| 168 const std::string& expected) { | 208 const std::string& expected) { |
| 169 const base::Value* value = NULL; | 209 const base::Value* value = NULL; |
| 170 std::string as_string; | 210 std::string as_string; |
| 171 if (!pref_store_->GetValue(name, &value)) { | 211 if (!pref_store_->GetValue(name, &value)) { |
| 172 ADD_FAILURE() << name << " is not a defined value."; | 212 ADD_FAILURE() << name << " is not a defined value."; |
| 173 } else if (!value->GetAsString(&as_string)) { | 213 } else if (!value->GetAsString(&as_string)) { |
| 174 ADD_FAILURE() << name << " could not be coerced to a string."; | 214 ADD_FAILURE() << name << " could not be coerced to a string."; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 187 scoped_refptr<PersistentPrefStore> pref_store_; | 227 scoped_refptr<PersistentPrefStore> pref_store_; |
| 188 }; | 228 }; |
| 189 | 229 |
| 190 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { | 230 TEST_F(ProfilePrefStoreManagerTest, StoreValues) { |
| 191 InitializePrefs(); | 231 InitializePrefs(); |
| 192 | 232 |
| 193 LoadExistingPrefs(); | 233 LoadExistingPrefs(); |
| 194 | 234 |
| 195 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 235 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 196 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 236 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 237 EXPECT_FALSE(ResetRecorded()); | |
| 197 } | 238 } |
| 198 | 239 |
| 199 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) { | 240 TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) { |
| 200 base::FilePath pref_file_path = | 241 base::FilePath pref_file_path = |
| 201 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( | 242 ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( |
| 202 profile_dir_.path()); | 243 profile_dir_.path()); |
| 203 | 244 |
| 204 ASSERT_FALSE(base::PathExists(pref_file_path)); | 245 EXPECT_FALSE(base::PathExists(pref_file_path)); |
| 205 | 246 |
| 206 InitializePrefs(); | 247 InitializePrefs(); |
| 207 | 248 |
| 208 ASSERT_TRUE(base::PathExists(pref_file_path)); | 249 EXPECT_TRUE(base::PathExists(pref_file_path)); |
| 209 } | 250 } |
| 210 | 251 |
| 211 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { | 252 TEST_F(ProfilePrefStoreManagerTest, ProtectValues) { |
| 212 InitializePrefs(); | 253 InitializePrefs(); |
| 213 | 254 |
| 214 ReplaceStringInPrefs(kFoobar, kBarfoo); | 255 ReplaceStringInPrefs(kFoobar, kBarfoo); |
| 215 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); | 256 ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld); |
| 216 | 257 |
| 217 LoadExistingPrefs(); | 258 LoadExistingPrefs(); |
| 218 | 259 |
| 219 // kTrackedAtomic is unprotected and thus will be loaded as it appears on | 260 // kTrackedAtomic is unprotected and thus will be loaded as it appears on |
| 220 // disk. | 261 // disk. |
| 221 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); | 262 ExpectStringValueEquals(kTrackedAtomic, kBarfoo); |
| 222 | 263 |
| 223 // If preference tracking is supported, the tampered value of kProtectedAtomic | 264 // If preference tracking is supported, the tampered value of kProtectedAtomic |
| 224 // will be discarded at load time, leaving this preference undefined. | 265 // will be discarded at load time, leaving this preference undefined. |
| 225 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 266 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| 226 pref_store_->GetValue(kProtectedAtomic, NULL)); | 267 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 268 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 269 ResetRecorded()); | |
| 227 } | 270 } |
| 228 | 271 |
| 229 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) { | 272 TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) { |
| 230 InitializePrefs(); | 273 InitializePrefs(); |
| 231 | 274 |
| 232 manager_->ResetPrefHashStore(); | 275 manager_->ResetPrefHashStore(); |
| 233 | 276 |
| 234 LoadExistingPrefs(); | 277 LoadExistingPrefs(); |
| 235 | 278 |
| 236 // kTrackedAtomic is loaded as it appears on disk. | 279 // kTrackedAtomic is loaded as it appears on disk. |
| 237 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 280 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 238 // If preference tracking is supported, the tampered value of kProtectedAtomic | 281 // If preference tracking is supported, the tampered value of kProtectedAtomic |
| 239 // will be discarded at load time, leaving this preference undefined. | 282 // will be discarded at load time, leaving this preference undefined. |
| 240 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 283 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| 241 pref_store_->GetValue(kProtectedAtomic, NULL)); | 284 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 285 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 286 ResetRecorded()); | |
| 242 } | 287 } |
| 243 | 288 |
| 244 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) { | 289 TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) { |
| 245 InitializePrefs(); | 290 InitializePrefs(); |
| 246 | 291 |
| 247 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); | 292 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); |
| 248 | 293 |
| 249 LoadExistingPrefs(); | 294 LoadExistingPrefs(); |
| 250 | 295 |
| 251 // kTrackedAtomic is loaded as it appears on disk. | 296 // kTrackedAtomic is loaded as it appears on disk. |
| 252 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 297 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 253 // If preference tracking is supported, kProtectedAtomic will be undefined | 298 // If preference tracking is supported, kProtectedAtomic will be undefined |
| 254 // because the value was discarded due to loss of the hash store contents. | 299 // because the value was discarded due to loss of the hash store contents. |
| 255 EXPECT_EQ(!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | 300 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, |
| 256 pref_store_->GetValue(kProtectedAtomic, NULL)); | 301 pref_store_->GetValue(kProtectedAtomic, NULL)); |
| 302 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 303 ResetRecorded()); | |
| 304 } | |
| 305 | |
| 306 TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) { | |
| 307 InitializeDeprecatedCombinedProfilePrefStore(); | |
| 308 | |
| 309 LoadExistingPrefs(); | |
| 310 | |
| 311 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | |
| 312 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
| 313 EXPECT_FALSE(ResetRecorded()); | |
| 257 } | 314 } |
| 258 | 315 |
| 259 TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) { | 316 TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) { |
| 260 InitializePrefs(); | 317 scoped_refptr<JsonPrefStore> legacy_prefs( |
| 261 | 318 new JsonPrefStore(ProfilePrefStoreManager::GetPrefFilePathFromProfilePath( |
| 262 manager_->ResetPrefHashStore(); | 319 profile_dir_.path()), |
| 320 main_message_loop_.message_loop_proxy(), | |
| 321 scoped_ptr<PrefFilter>())); | |
| 322 legacy_prefs->SetValue(kTrackedAtomic, new base::StringValue(kFoobar)); | |
| 323 legacy_prefs->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld)); | |
| 324 legacy_prefs = NULL; | |
| 325 base::RunLoop().RunUntilIdle(); | |
| 263 | 326 |
| 264 // This is a no-op if !kPlatformSupportsPreferenceTracking. | 327 // This is a no-op if !kPlatformSupportsPreferenceTracking. |
| 265 manager_->UpdateProfileHashStoreIfRequired( | 328 manager_->UpdateProfileHashStoreIfRequired( |
| 266 main_message_loop_.message_loop_proxy()); | 329 main_message_loop_.message_loop_proxy()); |
| 267 base::RunLoop().RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
| 268 | 331 |
| 269 // At the moment, UpdateProfileHashStoreIfRequired will accept existing | 332 // At the moment, UpdateProfileHashStoreIfRequired will accept existing |
| 270 // values. | 333 // values. |
| 271 LoadExistingPrefs(); | 334 LoadExistingPrefs(); |
| 272 | 335 |
| 273 // These expectations hold whether or not tracking is supported. | 336 // These expectations hold whether or not tracking is supported. |
| 274 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 337 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 275 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 338 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 339 EXPECT_FALSE(ResetRecorded()); | |
| 276 } | 340 } |
| 277 | 341 |
| 278 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { | 342 TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) { |
| 279 scoped_ptr<base::DictionaryValue> master_prefs( | 343 scoped_ptr<base::DictionaryValue> master_prefs( |
| 280 new base::DictionaryValue); | 344 new base::DictionaryValue); |
| 281 master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar)); | 345 master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar)); |
| 282 master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); | 346 master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld)); |
| 283 ASSERT_TRUE( | 347 EXPECT_TRUE( |
| 284 manager_->InitializePrefsFromMasterPrefs(*master_prefs)); | 348 manager_->InitializePrefsFromMasterPrefs(*master_prefs)); |
| 285 | 349 |
| 286 LoadExistingPrefs(); | 350 LoadExistingPrefs(); |
| 287 | 351 |
| 288 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs | 352 // Verify that InitializePrefsFromMasterPrefs correctly applied the MACs |
| 289 // necessary to authenticate these values. | 353 // necessary to authenticate these values. |
| 290 ExpectStringValueEquals(kTrackedAtomic, kFoobar); | 354 ExpectStringValueEquals(kTrackedAtomic, kFoobar); |
| 291 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | 355 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); |
| 356 EXPECT_FALSE(ResetRecorded()); | |
| 292 } | 357 } |
| 358 | |
| 359 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) { | |
| 360 InitializePrefs(); | |
| 361 LoadExistingPrefs(); | |
| 362 ExpectStringValueEquals(kUnprotectedAtomic, kFoobar); | |
| 363 | |
| 364 // Ensure everything is written out to disk. | |
| 365 DestroyPrefStore(); | |
| 366 | |
| 367 ReplaceStringInPrefs(kFoobar, kBarfoo); | |
| 368 | |
| 369 // It's unprotected, so we can load the modified value. | |
| 370 LoadExistingPrefs(); | |
| 371 ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo); | |
| 372 | |
| 373 // Now update the configuration to protect it. | |
| 374 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | |
| 375 99u, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, | |
| 376 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | |
| 377 configuration_.push_back(new_protected); | |
| 378 ReloadConfiguration(); | |
| 379 | |
| 380 // And try loading with the new configuration. | |
| 381 LoadExistingPrefs(); | |
| 382 | |
| 383 // Since there was a valid super MAC we were able to extend the existing trust | |
| 384 // to the newly proteted preference. | |
| 385 ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo); | |
| 386 EXPECT_FALSE(ResetRecorded()); | |
| 387 | |
| 388 // Ensure everything is written out to disk. | |
| 389 DestroyPrefStore(); | |
| 390 | |
| 391 // It's protected now, so (if the platform supports it) any tampering should | |
| 392 // lead to a reset. | |
| 393 ReplaceStringInPrefs(kBarfoo, kFoobar); | |
| 394 LoadExistingPrefs(); | |
| 395 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 396 pref_store_->GetValue(kUnprotectedAtomic, NULL)); | |
| 397 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 398 ResetRecorded()); | |
| 399 } | |
| 400 | |
| 401 TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) { | |
| 402 InitializePrefs(); | |
| 403 | |
| 404 // Now update the configuration to protect it. | |
| 405 PrefHashFilter::TrackedPreferenceMetadata new_protected = { | |
| 406 99u, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, | |
| 407 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}; | |
| 408 configuration_.push_back(new_protected); | |
| 409 ReloadConfiguration(); | |
| 410 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); | |
| 411 | |
| 412 // And try loading with the new configuration. | |
| 413 LoadExistingPrefs(); | |
| 414 | |
| 415 // If preference tracking is supported, kUnprotectedAtomic will have been | |
| 416 // discarded because new values are not accepted without a valid super MAC. | |
| 417 EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 418 pref_store_->GetValue(kUnprotectedAtomic, NULL)); | |
| 419 EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking, | |
| 420 ResetRecorded()); | |
| 421 } | |
| 422 | |
| 423 // This test does not directly verify that the values are moved from one pref | |
| 424 // store to the other. segregated_pref_store_unittest.cc _does_ verify that | |
| 425 // functionality. | |
| 426 // | |
| 427 // _This_ test verifies that preference values are correctly maintained when a | |
| 428 // preference's protection state changes from protected to unprotected. | |
| 429 TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) { | |
| 430 InitializePrefs(); | |
| 431 DestroyPrefStore(); | |
| 432 | |
| 433 // Unconfigure protection for kProtectedAtomic | |
| 434 for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it = | |
| 435 configuration_.begin(); | |
| 436 it != configuration_.end(); | |
| 437 ++it) { | |
| 438 if (it->name == std::string(kProtectedAtomic)) { | |
| 439 configuration_.erase(it); | |
| 440 break; | |
| 441 } | |
| 442 } | |
| 443 ReloadConfiguration(); | |
| 444 | |
| 445 // Reset the hash stores and then try loading the prefs. | |
| 446 ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_); | |
| 447 LoadExistingPrefs(); | |
| 448 | |
| 449 // Verify that the value was not reset. | |
| 450 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
| 451 EXPECT_FALSE(ResetRecorded()); | |
| 452 | |
| 453 // Accessing the value of the previously protected pref didn't trigger its | |
| 454 // move to the unprotected preferences file, though the loading of the pref | |
| 455 // store should still have caused the MAC store to be recalculated. | |
| 456 LoadExistingPrefs(); | |
| 457 ExpectStringValueEquals(kProtectedAtomic, kHelloWorld); | |
| 458 | |
| 459 // Trigger the logic that migrates it back to the unprotected preferences | |
| 460 // file. | |
| 461 pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld)); | |
| 462 LoadExistingPrefs(); | |
| 463 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); | |
| 464 EXPECT_FALSE(ResetRecorded()); | |
| 465 } | |
| OLD | NEW |