Chromium Code Reviews| Index: components/user_prefs/tracked/pref_hash_store_impl_unittest.cc |
| diff --git a/components/user_prefs/tracked/pref_hash_store_impl_unittest.cc b/components/user_prefs/tracked/pref_hash_store_impl_unittest.cc |
| index 10867e66d4510dc12500d0b526becc70392073be..1aadd1a05762e357aad1a2c3367472ce0a65bb58 100644 |
| --- a/components/user_prefs/tracked/pref_hash_store_impl_unittest.cc |
| +++ b/components/user_prefs/tracked/pref_hash_store_impl_unittest.cc |
| @@ -15,16 +15,61 @@ |
| #include "testing/gtest/include/gtest/gtest.h" |
| class PrefHashStoreImplTest : public testing::Test { |
| - protected: |
| - std::unique_ptr<HashStoreContents> CreateHashStoreContents() { |
| - return std::unique_ptr<HashStoreContents>( |
| - new DictionaryHashStoreContents(&pref_store_contents_)); |
| + public: |
| + void SetUp() override { |
| + pref_store_contents_.reset(new base::DictionaryValue); |
| + contents_.reset( |
| + new DictionaryHashStoreContents(pref_store_contents_.get())); |
| } |
| + protected: |
| + HashStoreContents* GetHashStoreContents() { return contents_.get(); } |
| + |
| private: |
| - base::DictionaryValue pref_store_contents_; |
| + std::unique_ptr<HashStoreContents> contents_; |
| + std::unique_ptr<base::DictionaryValue> pref_store_contents_; |
|
gab
2016/08/08 04:37:46
|pref_store_contents_| doesn't have to be in a uni
proberge
2016/08/31 17:30:17
Done.
|
| }; |
| +TEST_F(PrefHashStoreImplTest, ComputeMac) { |
| + base::StringValue string_1("string1"); |
| + base::StringValue string_2("string2"); |
| + PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| + |
| + std::string computed_mac_1 = pref_hash_store.ComputeMac("path1", &string_1); |
| + std::string computed_mac_2 = pref_hash_store.ComputeMac("path1", &string_2); |
| + std::string computed_mac_3 = pref_hash_store.ComputeMac("path2", &string_1); |
| + |
| + // Quick sanity checks here, see pref_hash_calculator_unittest.cc for more |
| + // complete tests. |
| + EXPECT_NE(computed_mac_1, computed_mac_2); |
| + EXPECT_NE(computed_mac_1, computed_mac_3); |
| + EXPECT_EQ(64, computed_mac_1.size()); |
| +} |
| + |
| +TEST_F(PrefHashStoreImplTest, ComputeSplitMacs) { |
| + base::DictionaryValue dict; |
| + dict.Set("a", new base::StringValue("string1")); |
| + dict.Set("b", new base::StringValue("string2")); |
| + PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| + |
| + std::unique_ptr<base::DictionaryValue> computed_macs = |
| + pref_hash_store.ComputeSplitMacs("extension.settings", &dict); |
|
gab
2016/08/08 04:37:46
"foo.bar" (no need to refer to extension.settings"
proberge
2016/08/31 17:30:17
Done.
|
| + |
| + std::string mac_1; |
| + std::string mac_2; |
| + ASSERT_TRUE(computed_macs->GetString("a", &mac_1)); |
| + ASSERT_TRUE(computed_macs->GetString("b", &mac_2)); |
| + |
| + EXPECT_EQ(2, computed_macs->size()); |
| + |
| + base::StringValue string_1("string1"); |
| + base::StringValue string_2("string2"); |
| + EXPECT_EQ(pref_hash_store.ComputeMac("extension.settings.a", &string_1), |
| + mac_1); |
| + EXPECT_EQ(pref_hash_store.ComputeMac("extension.settings.b", &string_2), |
| + mac_2); |
| +} |
| + |
| TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
| base::StringValue string_1("string1"); |
| base::StringValue string_2("string2"); |
| @@ -33,7 +78,7 @@ 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", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| // Only NULL should be trusted in the absence of a hash. |
| EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| @@ -63,14 +108,14 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
| transaction->CheckValue("path1", &dict)); |
| } |
| - ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); |
| + ASSERT_FALSE(GetHashStoreContents()->GetSuperMac().empty()); |
| { |
| // |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", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
| EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| transaction->CheckValue("new_path", &string_1)); |
| EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| @@ -80,14 +125,14 @@ TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
| } |
| // Manually corrupt the super MAC. |
| - CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
| + GetHashStoreContents()->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", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store3.BeginTransaction(GetHashStoreContents())); |
| EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| transaction->CheckValue("new_path", &string_1)); |
| EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| @@ -105,7 +150,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_FALSE(transaction->IsSuperMACValid()); |
| ASSERT_FALSE(transaction->HasHash("path1")); |
| @@ -122,7 +167,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| // Make a copy of the stored hash for future use. |
| const base::Value* hash = NULL; |
| - ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash)); |
| + ASSERT_TRUE(GetHashStoreContents()->GetContents()->Get("path1", &hash)); |
| std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy()); |
| hash = NULL; |
| @@ -130,7 +175,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_TRUE(transaction->IsSuperMACValid()); |
| ASSERT_TRUE(transaction->HasHash("path1")); |
| @@ -149,18 +194,18 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_TRUE(transaction->IsSuperMACValid()); |
| ASSERT_FALSE(transaction->HasHash("path1")); |
| } |
| // Invalidate the super MAC. |
| - CreateHashStoreContents()->SetSuperMac(std::string()); |
| + GetHashStoreContents()->SetSuperMac(std::string()); |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_FALSE(transaction->IsSuperMACValid()); |
| ASSERT_FALSE(transaction->HasHash("path1")); |
| @@ -178,7 +223,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_FALSE(transaction->IsSuperMACValid()); |
| ASSERT_TRUE(transaction->HasHash("path1")); |
| EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| @@ -196,7 +241,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_FALSE(transaction->IsSuperMACValid()); |
| // Test StampSuperMac. |
| @@ -207,7 +252,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_TRUE(transaction->IsSuperMACValid()); |
| // Store the hash of a different value to test an "over-import". |
| @@ -221,7 +266,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_TRUE(transaction->IsSuperMACValid()); |
| // "Over-import". An import should preserve validity. |
| @@ -236,7 +281,7 @@ TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| ASSERT_TRUE(transaction->IsSuperMACValid()); |
| EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| transaction->CheckValue("path1", &string_1)); |
| @@ -253,19 +298,19 @@ TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { |
| // Pass |use_super_mac| => false. |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| transaction->StoreHash("path1", &string_2); |
| EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| transaction->CheckValue("path1", &string_2)); |
| } |
| - ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); |
| + ASSERT_TRUE(GetHashStoreContents()->GetSuperMac().empty()); |
| { |
| PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
| EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| transaction->CheckValue("new_path", &string_1)); |
| } |
| @@ -289,7 +334,7 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| // No hashes stored yet and hashes dictionary is empty (and thus not |
| // trusted). |
| @@ -359,21 +404,21 @@ TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
| // trust new unknown values. |
| PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
| EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| EXPECT_TRUE(invalid_keys.empty()); |
| } |
| // Manually corrupt the super MAC. |
| - CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
| + GetHashStoreContents()->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", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store3.BeginTransaction(GetHashStoreContents())); |
| EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| EXPECT_TRUE(invalid_keys.empty()); |
| @@ -388,7 +433,7 @@ TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| // Store hashes for a random dict to be overwritten below. |
| base::DictionaryValue initial_dict; |
| @@ -424,7 +469,7 @@ TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
| // update the stored hash of hashes). |
| PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
| base::DictionaryValue tested_dict; |
| tested_dict.Set("a", new base::StringValue("foo")); |
| @@ -454,7 +499,7 @@ TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { |
| { |
| PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store.BeginTransaction(GetHashStoreContents())); |
| transaction->StoreHash("path1", &string); |
| EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| @@ -465,7 +510,7 @@ 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", true); |
| std::unique_ptr<PrefHashStoreTransaction> transaction( |
| - pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| + pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
| std::vector<std::string> invalid_keys; |
| EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |