This source file includes following definitions.
- OnPrefValueChanged
- OnInitializationCompleted
- registry_verifier_
- SetUp
- ReloadConfiguration
- TearDown
- WasResetRecorded
- InitializePrefs
- DestroyPrefStore
- InitializeDeprecatedCombinedProfilePrefStore
- InitializePrefStore
- LoadExistingPrefs
- ReplaceStringInPrefs
- ExpectStringValueEquals
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/prefs/profile_pref_store_manager.h"
#include <vector>
#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"
#include "base/message_loop/message_loop.h"
#include "base/prefs/json_pref_store.h"
#include "base/prefs/persistent_pref_store.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_service_factory.h"
#include "base/prefs/pref_store.h"
#include "base/prefs/testing_pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_hash_filter.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class FirstEqualsPredicate {
public:
explicit FirstEqualsPredicate(const std::string& expected)
: expected_(expected) {}
bool operator()(const std::pair<std::string, base::Value*>& pair) {
return pair.first == expected_;
}
private:
const std::string expected_;
};
class RegistryVerifier : public PrefStore::Observer {
public:
explicit RegistryVerifier(PrefRegistry* pref_registry)
: pref_registry_(pref_registry) {}
virtual void OnPrefValueChanged(const std::string& key) OVERRIDE {
EXPECT_TRUE(pref_registry_->end() !=
std::find_if(pref_registry_->begin(),
pref_registry_->end(),
FirstEqualsPredicate(key)))
<< "Unregistered key " << key << " was changed.";
}
virtual void OnInitializationCompleted(bool succeeded) OVERRIDE {}
private:
scoped_refptr<PrefRegistry> pref_registry_;
};
const char kUnprotectedAtomic[] = "unprotected_atomic";
const char kTrackedAtomic[] = "tracked_atomic";
const char kProtectedAtomic[] = "protected_atomic";
const char kFoobar[] = "FOOBAR";
const char kBarfoo[] = "BARFOO";
const char kHelloWorld[] = "HELLOWORLD";
const char kGoodbyeWorld[] = "GOODBYEWORLD";
const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = {
{0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC},
{1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC}};
const size_t kExtraReportingId = 2u;
const size_t kReportingIdCount = 3u;
}
class ProfilePrefStoreManagerTest : public testing::Test {
public:
ProfilePrefStoreManagerTest()
: configuration_(kConfiguration,
kConfiguration + arraysize(kConfiguration)),
profile_pref_registry_(new user_prefs::PrefRegistrySyncable),
registry_verifier_(profile_pref_registry_) {}
virtual void SetUp() OVERRIDE {
ProfilePrefStoreManager::RegisterPrefs(local_state_.registry());
ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_);
for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration;
it != kConfiguration + arraysize(kConfiguration);
++it) {
if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) {
profile_pref_registry_->RegisterStringPref(
it->name, "", user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
} else {
profile_pref_registry_->RegisterDictionaryPref(
it->name, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
}
profile_pref_registry_->RegisterStringPref(
kUnprotectedAtomic,
std::string(),
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
ReloadConfiguration();
}
void ReloadConfiguration() {
manager_.reset(new ProfilePrefStoreManager(profile_dir_.path(),
configuration_,
kReportingIdCount,
"seed",
"device_id",
&local_state_));
}
virtual void TearDown() OVERRIDE { DestroyPrefStore(); }
protected:
bool WasResetRecorded() {
base::PrefServiceFactory pref_service_factory;
pref_service_factory.set_user_prefs(pref_store_);
scoped_ptr<PrefService> pref_service(
pref_service_factory.Create(profile_pref_registry_));
return !ProfilePrefStoreManager::GetResetTime(pref_service.get()).is_null();
}
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_) {
pref_store_->CommitPendingWrite();
base::RunLoop().RunUntilIdle();
pref_store_->RemoveObserver(®istry_verifier_);
pref_store_ = NULL;
base::RunLoop().RunUntilIdle();
}
}
void InitializeDeprecatedCombinedProfilePrefStore() {
scoped_refptr<PersistentPrefStore> pref_store =
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(®istry_verifier_);
PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs();
EXPECT_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(®istry_verifier_);
pref_store->CommitPendingWrite();
base::RunLoop().RunUntilIdle();
}
void LoadExistingPrefs() {
DestroyPrefStore();
pref_store_ = manager_->CreateProfilePrefStore(
main_message_loop_.message_loop_proxy());
pref_store_->AddObserver(®istry_verifier_);
pref_store_->ReadPrefs();
}
void ReplaceStringInPrefs(const std::string& find,
const std::string& replace) {
base::FileEnumerator file_enum(
profile_dir_.path(), true, base::FileEnumerator::FILES);
for (base::FilePath path = file_enum.Next(); !path.empty();
path = file_enum.Next()) {
std::string contents;
EXPECT_TRUE(base::ReadFileToString(path, &contents));
ReplaceSubstringsAfterOffset(&contents, 0u, find, replace);
EXPECT_EQ(static_cast<int>(contents.length()),
base::WriteFile(path, contents.c_str(), contents.length()));
}
}
void ExpectStringValueEquals(const std::string& name,
const std::string& expected) {
const base::Value* value = NULL;
std::string as_string;
if (!pref_store_->GetValue(name, &value)) {
ADD_FAILURE() << name << " is not a defined value.";
} else if (!value->GetAsString(&as_string)) {
ADD_FAILURE() << name << " could not be coerced to a string.";
} else {
EXPECT_EQ(expected, as_string);
}
}
base::MessageLoop main_message_loop_;
std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_;
base::ScopedTempDir profile_dir_;
TestingPrefServiceSimple local_state_;
scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_;
RegistryVerifier registry_verifier_;
scoped_ptr<ProfilePrefStoreManager> manager_;
scoped_refptr<PersistentPrefStore> pref_store_;
};
TEST_F(ProfilePrefStoreManagerTest, StoreValues) {
InitializePrefs();
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
EXPECT_FALSE(WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, GetPrefFilePathFromProfilePath) {
base::FilePath pref_file_path =
ProfilePrefStoreManager::GetPrefFilePathFromProfilePath(
profile_dir_.path());
EXPECT_FALSE(base::PathExists(pref_file_path));
InitializePrefs();
EXPECT_TRUE(base::PathExists(pref_file_path));
}
TEST_F(ProfilePrefStoreManagerTest, ProtectValues) {
InitializePrefs();
ReplaceStringInPrefs(kFoobar, kBarfoo);
ReplaceStringInPrefs(kHelloWorld, kGoodbyeWorld);
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kBarfoo);
EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, ResetPrefHashStore) {
InitializePrefs();
manager_->ResetPrefHashStore();
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, ResetAllPrefHashStores) {
InitializePrefs();
ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kProtectedAtomic, NULL));
EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, MigrateFromOneFile) {
InitializeDeprecatedCombinedProfilePrefStore();
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
EXPECT_FALSE(WasResetRecorded());
}
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();
manager_->UpdateProfileHashStoreIfRequired(
main_message_loop_.message_loop_proxy());
base::RunLoop().RunUntilIdle();
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
EXPECT_FALSE(WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, InitializePrefsFromMasterPrefs) {
scoped_ptr<base::DictionaryValue> master_prefs(
new base::DictionaryValue);
master_prefs->Set(kTrackedAtomic, new base::StringValue(kFoobar));
master_prefs->Set(kProtectedAtomic, new base::StringValue(kHelloWorld));
EXPECT_TRUE(
manager_->InitializePrefsFromMasterPrefs(*master_prefs));
LoadExistingPrefs();
ExpectStringValueEquals(kTrackedAtomic, kFoobar);
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
EXPECT_FALSE(WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtected) {
InitializePrefs();
LoadExistingPrefs();
ExpectStringValueEquals(kUnprotectedAtomic, kFoobar);
DestroyPrefStore();
ReplaceStringInPrefs(kFoobar, kBarfoo);
LoadExistingPrefs();
ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo);
PrefHashFilter::TrackedPreferenceMetadata new_protected = {
kExtraReportingId, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
configuration_.push_back(new_protected);
ReloadConfiguration();
LoadExistingPrefs();
ExpectStringValueEquals(kUnprotectedAtomic, kBarfoo);
EXPECT_FALSE(WasResetRecorded());
DestroyPrefStore();
ReplaceStringInPrefs(kBarfoo, kFoobar);
LoadExistingPrefs();
EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kUnprotectedAtomic, NULL));
EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, UnprotectedToProtectedWithoutTrust) {
InitializePrefs();
PrefHashFilter::TrackedPreferenceMetadata new_protected = {
kExtraReportingId, kUnprotectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
PrefHashFilter::TRACKING_STRATEGY_ATOMIC};
configuration_.push_back(new_protected);
ReloadConfiguration();
ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
LoadExistingPrefs();
EXPECT_NE(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
pref_store_->GetValue(kUnprotectedAtomic, NULL));
EXPECT_EQ(ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking,
WasResetRecorded());
}
TEST_F(ProfilePrefStoreManagerTest, ProtectedToUnprotected) {
InitializePrefs();
DestroyPrefStore();
for (std::vector<PrefHashFilter::TrackedPreferenceMetadata>::iterator it =
configuration_.begin();
it != configuration_.end();
++it) {
if (it->name == kProtectedAtomic) {
configuration_.erase(it);
break;
}
}
ReloadConfiguration();
ProfilePrefStoreManager::ResetAllPrefHashStores(&local_state_);
LoadExistingPrefs();
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
EXPECT_FALSE(WasResetRecorded());
LoadExistingPrefs();
ExpectStringValueEquals(kProtectedAtomic, kHelloWorld);
pref_store_->SetValue(kProtectedAtomic, new base::StringValue(kGoodbyeWorld));
LoadExistingPrefs();
ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
EXPECT_FALSE(WasResetRecorded());
}