This source file includes following definitions.
- hash_store_id
- Reset
- IsInitialized
- GetVersion
- SetVersion
- GetContents
- GetMutableContents
- GetSuperMac
- SetSuperMac
- CommitPendingWrite
- CreateHashStoreContents
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/prefs/pref_hash_store_impl.h"
#include <string>
#include "base/macros.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_hash_store_impl.h"
#include "chrome/browser/prefs/pref_hash_store_transaction.h"
#include "chrome/browser/prefs/tracked/hash_store_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
class MockHashStoreContents : public HashStoreContents {
public:
struct Data {
Data() : commit_performed(false) {}
bool GetCommitPerformedAndReset() {
bool current_commit_performed = commit_performed;
commit_performed = false;
return current_commit_performed;
}
scoped_ptr<base::DictionaryValue> contents;
std::string super_mac;
scoped_ptr<int> version;
bool commit_performed;
};
explicit MockHashStoreContents(Data* data) : data_(data) {}
virtual std::string hash_store_id() const OVERRIDE { return "store_id"; }
virtual void Reset() OVERRIDE {
data_->contents.reset();
data_->super_mac = "";
data_->version.reset();
}
virtual bool IsInitialized() const OVERRIDE { return data_->contents; }
virtual bool GetVersion(int* version) const OVERRIDE {
if (data_->version)
*version = *data_->version;
return data_->version;
}
virtual void SetVersion(int version) OVERRIDE {
data_->version.reset(new int(version));
}
virtual const base::DictionaryValue* GetContents() const OVERRIDE {
return data_->contents.get();
}
virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE {
return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_));
}
virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; }
virtual void SetSuperMac(const std::string& super_mac) OVERRIDE {
data_->super_mac = super_mac;
}
virtual void CommitPendingWrite() OVERRIDE {
EXPECT_FALSE(data_->commit_performed);
data_->commit_performed = true;
}
private:
class MockMutableDictionary : public MutableDictionary {
public:
explicit MockMutableDictionary(Data* data) : data_(data) {}
virtual base::DictionaryValue* operator->() OVERRIDE {
if (!data_->contents)
data_->contents.reset(new base::DictionaryValue);
return data_->contents.get();
}
private:
Data* data_;
DISALLOW_COPY_AND_ASSIGN(MockMutableDictionary);
};
Data* data_;
DISALLOW_COPY_AND_ASSIGN(MockHashStoreContents);
};
class PrefHashStoreImplTest : public testing::Test {
protected:
scoped_ptr<HashStoreContents> CreateHashStoreContents() {
return scoped_ptr<HashStoreContents>(
new MockHashStoreContents(&hash_store_data_));
}
MockHashStoreContents::Data hash_store_data_;
};
TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
base::StringValue string_1("string1");
base::StringValue string_2("string2");
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("path1", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("path1", NULL));
transaction->StoreHash("path1", &string_1);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckValue("path1", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
transaction->CheckValue("path1", NULL));
transaction->StoreHash("path1", NULL);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckValue("path1", NULL));
EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
transaction->CheckValue("path1", &string_2));
base::DictionaryValue dict;
dict.Set("a", new base::StringValue("foo"));
dict.Set("d", new base::StringValue("bad"));
dict.Set("b", new base::StringValue("bar"));
dict.Set("c", new base::StringValue("baz"));
(*CreateHashStoreContents()->GetMutableContents())->SetString(
"path1",
"C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2");
EXPECT_EQ(PrefHashStoreTransaction::WEAK_LEGACY,
transaction->CheckValue("path1", &dict));
transaction->StoreHash("path1", &dict);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckValue("path1", &dict));
transaction.reset();
pref_hash_store.CommitPendingWrite();
EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
{
PrefHashStoreImpl pref_hash_store2(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store2.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_2));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", NULL));
transaction.reset();
pref_hash_store2.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
hash_store_data_.super_mac = std::string(64, 'A');
{
PrefHashStoreImpl pref_hash_store3(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store3.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_2));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", NULL));
transaction.reset();
pref_hash_store3.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
}
TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
base::DictionaryValue dict;
dict.Set("a", new base::StringValue("to be replaced"));
dict.Set("b", new base::StringValue("same"));
dict.Set("o", new base::StringValue("old"));
base::DictionaryValue modified_dict;
modified_dict.Set("a", new base::StringValue("replaced"));
modified_dict.Set("b", new base::StringValue("same"));
modified_dict.Set("c", new base::StringValue("new"));
base::DictionaryValue empty_dict;
std::vector<std::string> invalid_keys;
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
transaction->StoreSplitHash("path1", &dict);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("path2", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
transaction->CheckSplitValue("path1", NULL, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(
PrefHashStoreTransaction::CLEARED,
transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(
PrefHashStoreTransaction::CHANGED,
transaction->CheckSplitValue("path1", &modified_dict, &invalid_keys));
std::vector<std::string> expected_invalid_keys1;
expected_invalid_keys1.push_back("a");
expected_invalid_keys1.push_back("c");
expected_invalid_keys1.push_back("o");
EXPECT_EQ(expected_invalid_keys1, invalid_keys);
invalid_keys.clear();
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
transaction->StoreSplitHash("path1", &modified_dict);
EXPECT_EQ(
PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", &modified_dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));
std::vector<std::string> expected_invalid_keys2;
expected_invalid_keys2.push_back("a");
expected_invalid_keys2.push_back("o");
expected_invalid_keys2.push_back("c");
EXPECT_EQ(expected_invalid_keys2, invalid_keys);
invalid_keys.clear();
transaction.reset();
pref_hash_store.CommitPendingWrite();
EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
{
PrefHashStoreImpl pref_hash_store2(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store2.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
transaction.reset();
pref_hash_store2.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
hash_store_data_.super_mac = std::string(64, 'A');
{
PrefHashStoreImpl pref_hash_store3(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store3.BeginTransaction());
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
transaction.reset();
pref_hash_store3.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
}
TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
base::DictionaryValue empty_dict;
std::vector<std::string> invalid_keys;
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
base::DictionaryValue initial_dict;
initial_dict.Set("a", new base::StringValue("foo"));
transaction->StoreSplitHash("path1", &initial_dict);
transaction->StoreSplitHash("path1", &empty_dict);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", NULL, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(
PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
transaction->StoreSplitHash("path1", NULL);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", NULL, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
EXPECT_EQ(
PrefHashStoreTransaction::UNCHANGED,
transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
}
{
PrefHashStoreImpl pref_hash_store2(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store2.BeginTransaction());
base::DictionaryValue tested_dict;
tested_dict.Set("a", new base::StringValue("foo"));
tested_dict.Set("b", new base::StringValue("bar"));
EXPECT_EQ(
PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
}
}
TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
base::StringValue string("string1");
base::DictionaryValue dict;
dict.Set("a", new base::StringValue("foo"));
dict.Set("d", new base::StringValue("bad"));
dict.Set("b", new base::StringValue("bar"));
dict.Set("c", new base::StringValue("baz"));
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
transaction->StoreHash("path1", &string);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
transaction->CheckValue("path1", &string));
}
{
PrefHashStoreImpl pref_hash_store2(
std::string(32, 0), "device_id", CreateHashStoreContents());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store2.BeginTransaction());
std::vector<std::string> invalid_keys;
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
}
}
TEST_F(PrefHashStoreImplTest, GetCurrentVersion) {
COMPILE_ASSERT(PrefHashStoreImpl::VERSION_LATEST == 2,
new_versions_should_be_tested_here);
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
EXPECT_EQ(PrefHashStoreImpl::VERSION_UNINITIALIZED,
pref_hash_store.GetCurrentVersion());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
base::StringValue string_value("foo");
transaction->StoreHash("path1", &string_value);
transaction.reset();
pref_hash_store.CommitPendingWrite();
EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST,
pref_hash_store.GetCurrentVersion());
pref_hash_store.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
hash_store_data_.version.reset();
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
EXPECT_EQ(PrefHashStoreImpl::VERSION_PRE_MIGRATION,
pref_hash_store.GetCurrentVersion());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
transaction.reset();
pref_hash_store.CommitPendingWrite();
EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
{
PrefHashStoreImpl pref_hash_store(
std::string(32, 0), "device_id", CreateHashStoreContents());
EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST,
pref_hash_store.GetCurrentVersion());
scoped_ptr<PrefHashStoreTransaction> transaction(
pref_hash_store.BeginTransaction());
transaction.reset();
pref_hash_store.CommitPendingWrite();
EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
}