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

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

Issue 324493002: Move preference MACs to the protected preference stores. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self-review. Created 6 years, 6 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/pref_hash_store_impl_unittest.cc
diff --git a/chrome/browser/prefs/pref_hash_store_impl_unittest.cc b/chrome/browser/prefs/pref_hash_store_impl_unittest.cc
index 6a3152d33401b2f5dadc08b0a17b2a2f68d4245b..88cb594ca0815ee35d718ec08cbc00140fd65eaa 100644
--- a/chrome/browser/prefs/pref_hash_store_impl_unittest.cc
+++ b/chrome/browser/prefs/pref_hash_store_impl_unittest.cc
@@ -10,91 +10,19 @@
#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/dictionary_hash_store_contents.h"
#include "chrome/browser/prefs/tracked/hash_store_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
-class MockHashStoreContents : public HashStoreContents {
- public:
- // Keep the data separate from the API implementation so that it can be owned
- // by the test and reused. The API implementation is owned by the
- // PrefHashStoreImpl.
- struct Data {
- Data() : commit_performed(false) {}
-
- // Returns the current value of |commit_performed| and resets it to false
- // immediately after.
- bool GetCommitPerformedAndReset() {
- bool current_commit_performed = commit_performed;
- commit_performed = false;
- return current_commit_performed;
- }
-
- scoped_ptr<base::DictionaryValue> contents;
- std::string super_mac;
- bool commit_performed;
- };
-
- explicit MockHashStoreContents(Data* data) : data_(data) {}
-
- // HashStoreContents implementation
- virtual std::string hash_store_id() const OVERRIDE { return "store_id"; }
-
- virtual void Reset() OVERRIDE {
- data_->contents.reset();
- data_->super_mac = "";
- }
-
- virtual bool IsInitialized() const OVERRIDE { return data_->contents; }
-
- 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) {}
-
- // MutableDictionary implementation
- 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_));
+ new DictionaryHashStoreContents(&pref_store_contents_));
}
- MockHashStoreContents::Data hash_store_data_;
+ private:
+ base::DictionaryValue pref_store_contents_;
};
TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
@@ -103,10 +31,9 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
{
// 32 NULL bytes is the seed that was used to generate the legacy hash.
- PrefHashStoreImpl pref_hash_store(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store.BeginTransaction());
+ pref_hash_store.BeginTransaction(CreateHashStoreContents()));
// Only NULL should be trusted in the absence of a hash.
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
@@ -145,8 +72,6 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
// Test that the |pref_hash_store| flushes its changes on request post
// transaction.
transaction.reset();
gab 2014/06/17 02:00:05 Remove the last 3 lines as well (the reason to res
erikwright (departed) 2014/06/17 19:07:23 Done.
- pref_hash_store.CommitPendingWrite();
- EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty());
@@ -154,10 +79,9 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
{
// |pref_hash_store2| should trust its initial hashes dictionary and thus
// trust new unknown values.
- PrefHashStoreImpl pref_hash_store2(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store2.BeginTransaction());
+ pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
@@ -168,20 +92,17 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
// Test that |pref_hash_store2| doesn't flush its contents to disk when it
// didn't change.
transaction.reset();
- pref_hash_store2.CommitPendingWrite();
- EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
// Manually corrupt the super MAC.
- hash_store_data_.super_mac = std::string(64, 'A');
+ CreateHashStoreContents()->SetSuperMac(std::string(64, 'A'));
{
// |pref_hash_store3| should no longer trust its initial hashes dictionary
// and thus shouldn't trust non-NULL unknown values.
- PrefHashStoreImpl pref_hash_store3(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store3.BeginTransaction());
+ pref_hash_store3.BeginTransaction(CreateHashStoreContents()));
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_1));
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
@@ -192,8 +113,6 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
// Test that |pref_hash_store3| doesn't flush its contents to disk when it
// didn't change.
transaction.reset();
- pref_hash_store3.CommitPendingWrite();
- EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
}
@@ -203,10 +122,9 @@ TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
{
// Pass |use_super_mac| => false.
- PrefHashStoreImpl pref_hash_store(
- std::string(32, 0), "device_id", CreateHashStoreContents(), false);
+ PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store.BeginTransaction());
+ pref_hash_store.BeginTransaction(CreateHashStoreContents()));
transaction->StoreHash("path1", &string_2);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
@@ -216,10 +134,9 @@ TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty());
{
- PrefHashStoreImpl pref_hash_store2(
- std::string(32, 0), "device_id", CreateHashStoreContents(), false);
+ PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store2.BeginTransaction());
+ pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckValue("new_path", &string_1));
}
@@ -241,10 +158,9 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
std::vector<std::string> invalid_keys;
{
- PrefHashStoreImpl pref_hash_store(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store.BeginTransaction());
+ pref_hash_store.BeginTransaction(CreateHashStoreContents()));
// No hashes stored yet and hashes dictionary is empty (and thus not
// trusted).
@@ -310,17 +226,14 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
// Test that the |pref_hash_store| flushes its changes on request.
transaction.reset();
- pref_hash_store.CommitPendingWrite();
- EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
}
{
// |pref_hash_store2| should trust its initial hashes dictionary and thus
// trust new unknown values.
- PrefHashStoreImpl pref_hash_store2(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store2.BeginTransaction());
+ pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
@@ -328,20 +241,17 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
// Test that |pref_hash_store2| doesn't flush its contents to disk when it
// didn't change.
transaction.reset();
- pref_hash_store2.CommitPendingWrite();
- EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
// Manually corrupt the super MAC.
- hash_store_data_.super_mac = std::string(64, 'A');
+ CreateHashStoreContents()->SetSuperMac(std::string(64, 'A'));
{
// |pref_hash_store3| should no longer trust its initial hashes dictionary
// and thus shouldn't trust unknown values.
- PrefHashStoreImpl pref_hash_store3(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store3.BeginTransaction());
+ pref_hash_store3.BeginTransaction(CreateHashStoreContents()));
EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
EXPECT_TRUE(invalid_keys.empty());
@@ -349,8 +259,6 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
// Test that |pref_hash_store3| doesn't flush its contents to disk when it
// didn't change.
transaction.reset();
- pref_hash_store3.CommitPendingWrite();
- EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
}
}
@@ -360,10 +268,9 @@ TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
std::vector<std::string> invalid_keys;
{
- PrefHashStoreImpl pref_hash_store(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store.BeginTransaction());
+ pref_hash_store.BeginTransaction(CreateHashStoreContents()));
// Store hashes for a random dict to be overwritten below.
base::DictionaryValue initial_dict;
@@ -397,10 +304,9 @@ TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
// the hashes for path1 by setting its value to NULL (this is a regression
// test ensuring that the internal action of clearing some hashes does
// update the stored hash of hashes).
- PrefHashStoreImpl pref_hash_store2(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store2.BeginTransaction());
+ pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
base::DictionaryValue tested_dict;
tested_dict.Set("a", new base::StringValue("foo"));
@@ -428,10 +334,9 @@ TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
dict.Set("c", new base::StringValue("baz"));
{
- PrefHashStoreImpl pref_hash_store(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store.BeginTransaction());
+ pref_hash_store.BeginTransaction(CreateHashStoreContents()));
transaction->StoreHash("path1", &string);
EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
@@ -440,10 +345,9 @@ TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
{
// Load a new |pref_hash_store2| in which the hashes dictionary is trusted.
- PrefHashStoreImpl pref_hash_store2(
- std::string(32, 0), "device_id", CreateHashStoreContents(), true);
+ PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
scoped_ptr<PrefHashStoreTransaction> transaction(
- pref_hash_store2.BeginTransaction());
+ pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
std::vector<std::string> invalid_keys;
EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
transaction->CheckSplitValue("path1", &dict, &invalid_keys));

Powered by Google App Engine
This is Rietveld 408576698