Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1456)

Unified Diff: chrome/browser/prefs/profile_pref_store_manager_unittest.cc

Issue 205813002: Separate storage for protected preferences into Protected Preferences file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pp4_profile_pref_store
Patch Set: Pre-review. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/profile_pref_store_manager_unittest.cc
diff --git a/chrome/browser/prefs/profile_pref_store_manager_unittest.cc b/chrome/browser/prefs/profile_pref_store_manager_unittest.cc
index 438f8b29b41601627cff1baa274fdacaa31fb94b..03503983ebabbee9d3c94c0972bee937a371b332 100644
--- a/chrome/browser/prefs/profile_pref_store_manager_unittest.cc
+++ b/chrome/browser/prefs/profile_pref_store_manager_unittest.cc
@@ -8,6 +8,7 @@
#include "base/compiler_specific.h"
#include "base/file_util.h"
+#include "base/files/file_enumerator.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
@@ -59,6 +60,7 @@ class RegistryVerifier : public PrefStore::Observer {
scoped_refptr<PrefRegistry> pref_registry_;
};
+const char kUnprotectedAtomic[] = "unprotected_atomic";
const char kTrackedAtomic[] = "tracked_atomic";
const char kProtectedAtomic[] = "protected_atomic";
const char kProtectedSplit[] = "protected_split";
@@ -100,8 +102,16 @@ class ProfilePrefStoreManagerTest : public testing::Test {
it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
}
+ profile_pref_registry_->RegisterStringPref(
+ kUnprotectedAtomic,
+ "",
+ user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
+
ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
+ ReloadConfiguration();
+ }
+ void ReloadConfiguration() {
manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(),
configuration_,
configuration_.size(),
@@ -110,7 +120,18 @@ class ProfilePrefStoreManagerTest : public testing::Test {
&local_state_));
}
- virtual void TearDown() OVERRIDE {
+ virtual void TearDown() OVERRIDE { DestroyPrefStore(); }
+
+ protected:
+ void InitializePrefs() {
+ scoped_refptr<PersistentPrefStore> pref_store =
+ manager_->CreateProfilePrefStore(
+ main_message_loop_.message_loop_proxy());
+ InitializePrefStore(pref_store);
+ pref_store = NULL;
+ base::RunLoop().RunUntilIdle();
+ }
+ void DestroyPrefStore() {
if (pref_store_) {
// Force everything to be written to disk, triggering the PrefHashFilter
// while our RegistryVerifier is watching.
@@ -125,42 +146,51 @@ class ProfilePrefStoreManagerTest : public testing::Test {
}
}
- protected:
- void InitializePrefs() {
+ void InitializeDeprecatedCombinedProfilePrefStore() {
scoped_refptr<PersistentPrefStore> pref_store =
- manager_->CreateProfilePrefStore(
+ manager_->CreateDeprecatedCombinedProfilePrefStore(
main_message_loop_.message_loop_proxy());
+ InitializePrefStore(pref_store);
+ pref_store = NULL;
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void InitializePrefStore(PersistentPrefStore* pref_store) {
pref_store->AddObserver(&registry_verifier_);
PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs();
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
pref_store->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
pref_store->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
+ pref_store->SetValue(kUnprotectedAtomic, new base::StringValue(kFoobar));
pref_store->RemoveObserver(&registry_verifier_);
- pref_store = NULL;
+ pref_store->CommitPendingWrite();
base::RunLoop().RunUntilIdle();
}
void LoadExistingPrefs() {
+ DestroyPrefStore();
pref_store_ = manager_->CreateProfilePrefStore(
main_message_loop_.message_loop_proxy());
pref_store_->AddObserver(&registry_verifier_);
- EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
- pref_store_->ReadPrefs());
+ pref_store_->ReadPrefs();
}
void ReplaceStringInPrefs(const std::string& find,
const std::string& replace) {
- // Tamper with the file's contents
- base::FilePath pref_file_path =
- ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
- profile_dir_.path());
- std::string pref_file_contents;
- EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents));
- ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace);
- EXPECT_EQ(static_cast<int>(pref_file_contents.length()),
- base::WriteFile(pref_file_path,
- pref_file_contents.c_str(),
- pref_file_contents.length()));
+ base::FileEnumerator file_enumerator(
+ profile_dir_.path(), true, base::FileEnumerator::FILES);
+
+ base::FilePath pref_file_path;
+ while (!(pref_file_path = file_enumerator.Next()).empty()) {
+ // Tamper with the file's contents
+ std::string pref_file_contents;
+ EXPECT_TRUE(base::ReadFileToString(pref_file_path, &pref_file_contents));
+ ReplaceSubstringsAfterOffset(&pref_file_contents, 0u, find, replace);
+ EXPECT_EQ((int)pref_file_contents.length(),
+ base::WriteFile(pref_file_path,
+ pref_file_contents.c_str(),
+ pref_file_contents.length()));
+ }
}
void ExpectStringValueEquals(const std::string& name,
@@ -255,10 +285,25 @@ TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) {
pref_store_->GetValue(kProtectedAtomic, NULL));
}
-TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) {
- InitializePrefs();
+TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) {
+ InitializeDeprecatedCombinedProfilePrefStore();
- manager_->ResetPrefHashStore();
+ LoadExistingPrefs();
+
+ ExpectStringValueEquals(kTrackedAtomic, kFoobar);
+ ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
+}
+
+TEST_F(ProfilePrefStoreManagerTest, UpdateProfileHashStoreIfRequired) {
+ scoped_refptr<JsonPrefStore> legacy_prefs(
+ new JsonPrefStore(ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
+ profile_dir_.path()),
+ main_message_loop_.message_loop_proxy(),
+ scoped_ptr<PrefFilter>()));
+ legacy_prefs->SetValue(kTrackedAtomic, new base::StringValue(kFoobar));
+ legacy_prefs->SetValue(kProtectedAtomic, new base::StringValue(kHelloWorld));
+ legacy_prefs = NULL;
+ base::RunLoop().RunUntilIdle();
// This is a no-op if !kPlatformSupportsPreferenceTracking.
manager_->UpdateProfileHashStoreIfRequired(
@@ -289,3 +334,65 @@ TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) {
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
}
+
+TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) {
+ InitializePrefs();
+ LoadExistingPrefs();
+ ExpectStringValueEquals(kUnprotectedAtomic, kFoobar);
+
+ PrefHashFilter::TrackedPreferenceMetadata new_protected = {
+ 99u, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
+ PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
+ configuration_.push_back(new_protected);
+ ReloadConfiguration();
+ LoadExistingPrefs();
+ ExpectStringValueEquals(kUnprotectedAtomic, kFoobar);
+}
+
+TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) {
robertshield 2014/03/25 03:05:12 Can we add a test that simulates partial migration
erikwright (departed) 2014/03/25 20:28:26 Yes. That's a bit trickier so I'm going to upload
+ InitializePrefs();
+ PrefHashFilter::TrackedPreferenceMetadata new_protected = {
+ 99u, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
+ PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
+ configuration_.push_back(new_protected);
+ ReloadConfiguration();
+ ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
+
+ LoadExistingPrefs();
+ ASSERT_FALSE(pref_store_->GetValue(kUnprotectedAtomic, NULL));
+}
+
+
+TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) {
+ InitializePrefs();
+ ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
+
+ for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it =
+ configuration_.begin();
+ it != configuration_.end();
+ ++it) {
+ if (it->name == std::string(kProtectedAtomic)) {
+ configuration_.erase(it);
+ break;
+ }
+ }
+
+ ReloadConfiguration();
+
+ LoadExistingPrefs();
+ ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
+
+ // Accessing the value of the previously proteted pref didn't trigger its move
+ // to the unprotected preferences file, though the loading of the pref store
+ // should still have caused the MAC store to be recalculated.
+ LoadExistingPrefs();
+ ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
+
+ // Trigger the logic that migrates it back to the unprotected preferences
+ // file.
+ pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld));
+ LoadExistingPrefs();
+ ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
+}
+
+// Add test coverage for when Reset events should and should not be recorded.

Powered by Google App Engine
This is Rietveld 408576698