| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h" | 11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h" |
| 12 #include "components/user_prefs/tracked/hash_store_contents.h" | 12 #include "components/user_prefs/tracked/hash_store_contents.h" |
| 13 #include "components/user_prefs/tracked/pref_hash_store_impl.h" | 13 #include "components/user_prefs/tracked/pref_hash_store_impl.h" |
| 14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" | 14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 class PrefHashStoreImplTest : public testing::Test { | 17 class PrefHashStoreImplTest : public testing::Test { |
| 18 protected: | 18 protected: |
| 19 scoped_ptr<HashStoreContents> CreateHashStoreContents() { | 19 std::unique_ptr<HashStoreContents> CreateHashStoreContents() { |
| 20 return scoped_ptr<HashStoreContents>( | 20 return std::unique_ptr<HashStoreContents>( |
| 21 new DictionaryHashStoreContents(&pref_store_contents_)); | 21 new DictionaryHashStoreContents(&pref_store_contents_)); |
| 22 } | 22 } |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 base::DictionaryValue pref_store_contents_; | 25 base::DictionaryValue pref_store_contents_; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { | 28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
| 29 base::StringValue string_1("string1"); | 29 base::StringValue string_1("string1"); |
| 30 base::StringValue string_2("string2"); | 30 base::StringValue string_2("string2"); |
| 31 | 31 |
| 32 { | 32 { |
| 33 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 33 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
| 34 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 34 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 35 scoped_ptr<PrefHashStoreTransaction> transaction( | 35 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 37 | 37 |
| 38 // Only NULL should be trusted in the absence of a hash. | 38 // Only NULL should be trusted in the absence of a hash. |
| 39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 40 transaction->CheckValue("path1", &string_1)); | 40 transaction->CheckValue("path1", &string_1)); |
| 41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, | 41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, |
| 42 transaction->CheckValue("path1", NULL)); | 42 transaction->CheckValue("path1", NULL)); |
| 43 | 43 |
| 44 transaction->StoreHash("path1", &string_1); | 44 transaction->StoreHash("path1", &string_1); |
| 45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 62 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 62 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 63 transaction->CheckValue("path1", &dict)); | 63 transaction->CheckValue("path1", &dict)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); | 66 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); |
| 67 | 67 |
| 68 { | 68 { |
| 69 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 69 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 70 // trust new unknown values. | 70 // trust new unknown values. |
| 71 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 71 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| 72 scoped_ptr<PrefHashStoreTransaction> transaction( | 72 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 73 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 73 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 74 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 74 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 75 transaction->CheckValue("new_path", &string_1)); | 75 transaction->CheckValue("new_path", &string_1)); |
| 76 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 76 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 77 transaction->CheckValue("new_path", &string_2)); | 77 transaction->CheckValue("new_path", &string_2)); |
| 78 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, | 78 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, |
| 79 transaction->CheckValue("new_path", NULL)); | 79 transaction->CheckValue("new_path", NULL)); |
| 80 } | 80 } |
| 81 | 81 |
| 82 // Manually corrupt the super MAC. | 82 // Manually corrupt the super MAC. |
| 83 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); | 83 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
| 84 | 84 |
| 85 { | 85 { |
| 86 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 86 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 87 // and thus shouldn't trust non-NULL unknown values. | 87 // and thus shouldn't trust non-NULL unknown values. |
| 88 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); | 88 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
| 89 scoped_ptr<PrefHashStoreTransaction> transaction( | 89 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 90 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); | 90 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
| 91 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 91 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 92 transaction->CheckValue("new_path", &string_1)); | 92 transaction->CheckValue("new_path", &string_1)); |
| 93 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 93 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 94 transaction->CheckValue("new_path", &string_2)); | 94 transaction->CheckValue("new_path", &string_2)); |
| 95 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, | 95 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, |
| 96 transaction->CheckValue("new_path", NULL)); | 96 transaction->CheckValue("new_path", NULL)); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 TEST_F(PrefHashStoreImplTest, ImportExportOperations) { | 100 TEST_F(PrefHashStoreImplTest, ImportExportOperations) { |
| 101 base::StringValue string_1("string1"); | 101 base::StringValue string_1("string1"); |
| 102 base::StringValue string_2("string2"); | 102 base::StringValue string_2("string2"); |
| 103 | 103 |
| 104 // Initial state: no super MAC. | 104 // Initial state: no super MAC. |
| 105 { | 105 { |
| 106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 107 scoped_ptr<PrefHashStoreTransaction> transaction( | 107 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 108 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 108 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 109 ASSERT_FALSE(transaction->IsSuperMACValid()); | 109 ASSERT_FALSE(transaction->IsSuperMACValid()); |
| 110 | 110 |
| 111 ASSERT_FALSE(transaction->HasHash("path1")); | 111 ASSERT_FALSE(transaction->HasHash("path1")); |
| 112 | 112 |
| 113 // Storing a hash will stamp the super MAC. | 113 // Storing a hash will stamp the super MAC. |
| 114 transaction->StoreHash("path1", &string_1); | 114 transaction->StoreHash("path1", &string_1); |
| 115 | 115 |
| 116 ASSERT_TRUE(transaction->HasHash("path1")); | 116 ASSERT_TRUE(transaction->HasHash("path1")); |
| 117 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 117 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 118 transaction->CheckValue("path1", &string_1)); | 118 transaction->CheckValue("path1", &string_1)); |
| 119 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, | 119 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, |
| 120 transaction->CheckValue("path1", &string_2)); | 120 transaction->CheckValue("path1", &string_2)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Make a copy of the stored hash for future use. | 123 // Make a copy of the stored hash for future use. |
| 124 const base::Value* hash = NULL; | 124 const base::Value* hash = NULL; |
| 125 ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash)); | 125 ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash)); |
| 126 scoped_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy()); | 126 std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy()); |
| 127 hash = NULL; | 127 hash = NULL; |
| 128 | 128 |
| 129 // Verify that the super MAC was stamped. | 129 // Verify that the super MAC was stamped. |
| 130 { | 130 { |
| 131 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 131 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 132 scoped_ptr<PrefHashStoreTransaction> transaction( | 132 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 133 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 133 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 134 ASSERT_TRUE(transaction->IsSuperMACValid()); | 134 ASSERT_TRUE(transaction->IsSuperMACValid()); |
| 135 ASSERT_TRUE(transaction->HasHash("path1")); | 135 ASSERT_TRUE(transaction->HasHash("path1")); |
| 136 | 136 |
| 137 // Clearing the hash should preserve validity. | 137 // Clearing the hash should preserve validity. |
| 138 transaction->ClearHash("path1"); | 138 transaction->ClearHash("path1"); |
| 139 | 139 |
| 140 // The effects of the clear should be immediately visible. | 140 // The effects of the clear should be immediately visible. |
| 141 ASSERT_FALSE(transaction->HasHash("path1")); | 141 ASSERT_FALSE(transaction->HasHash("path1")); |
| 142 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, | 142 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, |
| 143 transaction->CheckValue("path1", NULL)); | 143 transaction->CheckValue("path1", NULL)); |
| 144 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 144 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 145 transaction->CheckValue("path1", &string_1)); | 145 transaction->CheckValue("path1", &string_1)); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Verify that validity was preserved and that the clear took effect. | 148 // Verify that validity was preserved and that the clear took effect. |
| 149 { | 149 { |
| 150 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 150 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 151 scoped_ptr<PrefHashStoreTransaction> transaction( | 151 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 153 ASSERT_TRUE(transaction->IsSuperMACValid()); | 153 ASSERT_TRUE(transaction->IsSuperMACValid()); |
| 154 ASSERT_FALSE(transaction->HasHash("path1")); | 154 ASSERT_FALSE(transaction->HasHash("path1")); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // Invalidate the super MAC. | 157 // Invalidate the super MAC. |
| 158 CreateHashStoreContents()->SetSuperMac(std::string()); | 158 CreateHashStoreContents()->SetSuperMac(std::string()); |
| 159 | 159 |
| 160 { | 160 { |
| 161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 162 scoped_ptr<PrefHashStoreTransaction> transaction( | 162 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 163 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 163 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 164 ASSERT_FALSE(transaction->IsSuperMACValid()); | 164 ASSERT_FALSE(transaction->IsSuperMACValid()); |
| 165 ASSERT_FALSE(transaction->HasHash("path1")); | 165 ASSERT_FALSE(transaction->HasHash("path1")); |
| 166 | 166 |
| 167 // An import should preserve invalidity. | 167 // An import should preserve invalidity. |
| 168 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); | 168 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); |
| 169 | 169 |
| 170 ASSERT_TRUE(transaction->HasHash("path1")); | 170 ASSERT_TRUE(transaction->HasHash("path1")); |
| 171 | 171 |
| 172 // The imported hash should be usable for validating the original value. | 172 // The imported hash should be usable for validating the original value. |
| 173 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 173 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 174 transaction->CheckValue("path1", &string_1)); | 174 transaction->CheckValue("path1", &string_1)); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Verify that invalidity was preserved and that the import took effect. | 177 // Verify that invalidity was preserved and that the import took effect. |
| 178 { | 178 { |
| 179 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 179 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 180 scoped_ptr<PrefHashStoreTransaction> transaction( | 180 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 181 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 181 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 182 ASSERT_FALSE(transaction->IsSuperMACValid()); | 182 ASSERT_FALSE(transaction->IsSuperMACValid()); |
| 183 ASSERT_TRUE(transaction->HasHash("path1")); | 183 ASSERT_TRUE(transaction->HasHash("path1")); |
| 184 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 184 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 185 transaction->CheckValue("path1", &string_1)); | 185 transaction->CheckValue("path1", &string_1)); |
| 186 | 186 |
| 187 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN. | 187 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN. |
| 188 transaction->ClearHash("path1"); | 188 transaction->ClearHash("path1"); |
| 189 | 189 |
| 190 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, | 190 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, |
| 191 transaction->CheckValue("path1", NULL)); | 191 transaction->CheckValue("path1", NULL)); |
| 192 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 192 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 193 transaction->CheckValue("path1", &string_1)); | 193 transaction->CheckValue("path1", &string_1)); |
| 194 } | 194 } |
| 195 | 195 |
| 196 { | 196 { |
| 197 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 197 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 198 scoped_ptr<PrefHashStoreTransaction> transaction( | 198 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 199 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 199 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 200 ASSERT_FALSE(transaction->IsSuperMACValid()); | 200 ASSERT_FALSE(transaction->IsSuperMACValid()); |
| 201 | 201 |
| 202 // Test StampSuperMac. | 202 // Test StampSuperMac. |
| 203 transaction->StampSuperMac(); | 203 transaction->StampSuperMac(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Verify that the store is now valid. | 206 // Verify that the store is now valid. |
| 207 { | 207 { |
| 208 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 208 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 209 scoped_ptr<PrefHashStoreTransaction> transaction( | 209 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 210 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 210 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 211 ASSERT_TRUE(transaction->IsSuperMACValid()); | 211 ASSERT_TRUE(transaction->IsSuperMACValid()); |
| 212 | 212 |
| 213 // Store the hash of a different value to test an "over-import". | 213 // Store the hash of a different value to test an "over-import". |
| 214 transaction->StoreHash("path1", &string_2); | 214 transaction->StoreHash("path1", &string_2); |
| 215 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, | 215 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, |
| 216 transaction->CheckValue("path1", &string_1)); | 216 transaction->CheckValue("path1", &string_1)); |
| 217 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 217 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 218 transaction->CheckValue("path1", &string_2)); | 218 transaction->CheckValue("path1", &string_2)); |
| 219 } | 219 } |
| 220 | 220 |
| 221 { | 221 { |
| 222 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 222 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 223 scoped_ptr<PrefHashStoreTransaction> transaction( | 223 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 224 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 224 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 225 ASSERT_TRUE(transaction->IsSuperMACValid()); | 225 ASSERT_TRUE(transaction->IsSuperMACValid()); |
| 226 | 226 |
| 227 // "Over-import". An import should preserve validity. | 227 // "Over-import". An import should preserve validity. |
| 228 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); | 228 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); |
| 229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 230 transaction->CheckValue("path1", &string_1)); | 230 transaction->CheckValue("path1", &string_1)); |
| 231 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, | 231 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, |
| 232 transaction->CheckValue("path1", &string_2)); | 232 transaction->CheckValue("path1", &string_2)); |
| 233 } | 233 } |
| 234 | 234 |
| 235 // Verify that validity was preserved and the "over-import" took effect. | 235 // Verify that validity was preserved and the "over-import" took effect. |
| 236 { | 236 { |
| 237 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 237 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 238 scoped_ptr<PrefHashStoreTransaction> transaction( | 238 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 239 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 239 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 240 ASSERT_TRUE(transaction->IsSuperMACValid()); | 240 ASSERT_TRUE(transaction->IsSuperMACValid()); |
| 241 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 241 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 242 transaction->CheckValue("path1", &string_1)); | 242 transaction->CheckValue("path1", &string_1)); |
| 243 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, | 243 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, |
| 244 transaction->CheckValue("path1", &string_2)); | 244 transaction->CheckValue("path1", &string_2)); |
| 245 } | 245 } |
| 246 } | 246 } |
| 247 | 247 |
| 248 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { | 248 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { |
| 249 base::StringValue string_1("string1"); | 249 base::StringValue string_1("string1"); |
| 250 base::StringValue string_2("string2"); | 250 base::StringValue string_2("string2"); |
| 251 | 251 |
| 252 { | 252 { |
| 253 // Pass |use_super_mac| => false. | 253 // Pass |use_super_mac| => false. |
| 254 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); | 254 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); |
| 255 scoped_ptr<PrefHashStoreTransaction> transaction( | 255 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 256 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 256 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 257 | 257 |
| 258 transaction->StoreHash("path1", &string_2); | 258 transaction->StoreHash("path1", &string_2); |
| 259 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 259 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 260 transaction->CheckValue("path1", &string_2)); | 260 transaction->CheckValue("path1", &string_2)); |
| 261 } | 261 } |
| 262 | 262 |
| 263 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); | 263 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); |
| 264 | 264 |
| 265 { | 265 { |
| 266 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); | 266 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); |
| 267 scoped_ptr<PrefHashStoreTransaction> transaction( | 267 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 268 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 268 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 269 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 269 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 270 transaction->CheckValue("new_path", &string_1)); | 270 transaction->CheckValue("new_path", &string_1)); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 | 273 |
| 274 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { | 274 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
| 275 base::DictionaryValue dict; | 275 base::DictionaryValue dict; |
| 276 dict.Set("a", new base::StringValue("to be replaced")); | 276 dict.Set("a", new base::StringValue("to be replaced")); |
| 277 dict.Set("b", new base::StringValue("same")); | 277 dict.Set("b", new base::StringValue("same")); |
| 278 dict.Set("o", new base::StringValue("old")); | 278 dict.Set("o", new base::StringValue("old")); |
| 279 | 279 |
| 280 base::DictionaryValue modified_dict; | 280 base::DictionaryValue modified_dict; |
| 281 modified_dict.Set("a", new base::StringValue("replaced")); | 281 modified_dict.Set("a", new base::StringValue("replaced")); |
| 282 modified_dict.Set("b", new base::StringValue("same")); | 282 modified_dict.Set("b", new base::StringValue("same")); |
| 283 modified_dict.Set("c", new base::StringValue("new")); | 283 modified_dict.Set("c", new base::StringValue("new")); |
| 284 | 284 |
| 285 base::DictionaryValue empty_dict; | 285 base::DictionaryValue empty_dict; |
| 286 | 286 |
| 287 std::vector<std::string> invalid_keys; | 287 std::vector<std::string> invalid_keys; |
| 288 | 288 |
| 289 { | 289 { |
| 290 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 290 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 291 scoped_ptr<PrefHashStoreTransaction> transaction( | 291 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 292 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 292 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 293 | 293 |
| 294 // No hashes stored yet and hashes dictionary is empty (and thus not | 294 // No hashes stored yet and hashes dictionary is empty (and thus not |
| 295 // trusted). | 295 // trusted). |
| 296 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 296 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 297 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 297 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 298 EXPECT_TRUE(invalid_keys.empty()); | 298 EXPECT_TRUE(invalid_keys.empty()); |
| 299 | 299 |
| 300 transaction->StoreSplitHash("path1", &dict); | 300 transaction->StoreSplitHash("path1", &dict); |
| 301 | 301 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 expected_invalid_keys2.push_back("o"); | 351 expected_invalid_keys2.push_back("o"); |
| 352 expected_invalid_keys2.push_back("c"); | 352 expected_invalid_keys2.push_back("c"); |
| 353 EXPECT_EQ(expected_invalid_keys2, invalid_keys); | 353 EXPECT_EQ(expected_invalid_keys2, invalid_keys); |
| 354 invalid_keys.clear(); | 354 invalid_keys.clear(); |
| 355 } | 355 } |
| 356 | 356 |
| 357 { | 357 { |
| 358 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 358 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 359 // trust new unknown values. | 359 // trust new unknown values. |
| 360 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 360 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| 361 scoped_ptr<PrefHashStoreTransaction> transaction( | 361 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 362 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 362 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 363 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 363 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 364 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 364 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 365 EXPECT_TRUE(invalid_keys.empty()); | 365 EXPECT_TRUE(invalid_keys.empty()); |
| 366 } | 366 } |
| 367 | 367 |
| 368 // Manually corrupt the super MAC. | 368 // Manually corrupt the super MAC. |
| 369 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); | 369 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
| 370 | 370 |
| 371 { | 371 { |
| 372 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 372 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 373 // and thus shouldn't trust unknown values. | 373 // and thus shouldn't trust unknown values. |
| 374 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); | 374 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
| 375 scoped_ptr<PrefHashStoreTransaction> transaction( | 375 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 376 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); | 376 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
| 377 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 377 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 378 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 378 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 379 EXPECT_TRUE(invalid_keys.empty()); | 379 EXPECT_TRUE(invalid_keys.empty()); |
| 380 } | 380 } |
| 381 } | 381 } |
| 382 | 382 |
| 383 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { | 383 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
| 384 base::DictionaryValue empty_dict; | 384 base::DictionaryValue empty_dict; |
| 385 | 385 |
| 386 std::vector<std::string> invalid_keys; | 386 std::vector<std::string> invalid_keys; |
| 387 | 387 |
| 388 { | 388 { |
| 389 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 389 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 390 scoped_ptr<PrefHashStoreTransaction> transaction( | 390 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 391 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 391 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 392 | 392 |
| 393 // Store hashes for a random dict to be overwritten below. | 393 // Store hashes for a random dict to be overwritten below. |
| 394 base::DictionaryValue initial_dict; | 394 base::DictionaryValue initial_dict; |
| 395 initial_dict.Set("a", new base::StringValue("foo")); | 395 initial_dict.Set("a", new base::StringValue("foo")); |
| 396 transaction->StoreSplitHash("path1", &initial_dict); | 396 transaction->StoreSplitHash("path1", &initial_dict); |
| 397 | 397 |
| 398 // Verify stored empty dictionary matches NULL and empty dictionary back. | 398 // Verify stored empty dictionary matches NULL and empty dictionary back. |
| 399 transaction->StoreSplitHash("path1", &empty_dict); | 399 transaction->StoreSplitHash("path1", &empty_dict); |
| 400 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 400 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 416 EXPECT_TRUE(invalid_keys.empty()); | 416 EXPECT_TRUE(invalid_keys.empty()); |
| 417 } | 417 } |
| 418 | 418 |
| 419 { | 419 { |
| 420 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 420 // |pref_hash_store2| should trust its initial hashes dictionary (and thus |
| 421 // trust new unknown values) even though the last action done was to clear | 421 // trust new unknown values) even though the last action done was to clear |
| 422 // the hashes for path1 by setting its value to NULL (this is a regression | 422 // the hashes for path1 by setting its value to NULL (this is a regression |
| 423 // test ensuring that the internal action of clearing some hashes does | 423 // test ensuring that the internal action of clearing some hashes does |
| 424 // update the stored hash of hashes). | 424 // update the stored hash of hashes). |
| 425 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 425 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| 426 scoped_ptr<PrefHashStoreTransaction> transaction( | 426 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 427 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 427 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 428 | 428 |
| 429 base::DictionaryValue tested_dict; | 429 base::DictionaryValue tested_dict; |
| 430 tested_dict.Set("a", new base::StringValue("foo")); | 430 tested_dict.Set("a", new base::StringValue("foo")); |
| 431 tested_dict.Set("b", new base::StringValue("bar")); | 431 tested_dict.Set("b", new base::StringValue("bar")); |
| 432 EXPECT_EQ( | 432 EXPECT_EQ( |
| 433 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 433 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 434 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 434 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
| 435 EXPECT_TRUE(invalid_keys.empty()); | 435 EXPECT_TRUE(invalid_keys.empty()); |
| 436 } | 436 } |
| 437 } | 437 } |
| 438 | 438 |
| 439 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for | 439 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for |
| 440 // a split preference even if there is an existing atomic preference's hash | 440 // a split preference even if there is an existing atomic preference's hash |
| 441 // stored. There is no point providing a migration path for preferences | 441 // stored. There is no point providing a migration path for preferences |
| 442 // switching strategies after their initial release as split preferences are | 442 // switching strategies after their initial release as split preferences are |
| 443 // turned into split preferences specifically because the atomic hash isn't | 443 // turned into split preferences specifically because the atomic hash isn't |
| 444 // considered useful. | 444 // considered useful. |
| 445 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { | 445 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { |
| 446 base::StringValue string("string1"); | 446 base::StringValue string("string1"); |
| 447 | 447 |
| 448 base::DictionaryValue dict; | 448 base::DictionaryValue dict; |
| 449 dict.Set("a", new base::StringValue("foo")); | 449 dict.Set("a", new base::StringValue("foo")); |
| 450 dict.Set("d", new base::StringValue("bad")); | 450 dict.Set("d", new base::StringValue("bad")); |
| 451 dict.Set("b", new base::StringValue("bar")); | 451 dict.Set("b", new base::StringValue("bar")); |
| 452 dict.Set("c", new base::StringValue("baz")); | 452 dict.Set("c", new base::StringValue("baz")); |
| 453 | 453 |
| 454 { | 454 { |
| 455 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 455 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
| 456 scoped_ptr<PrefHashStoreTransaction> transaction( | 456 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 457 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 457 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| 458 | 458 |
| 459 transaction->StoreHash("path1", &string); | 459 transaction->StoreHash("path1", &string); |
| 460 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 460 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 461 transaction->CheckValue("path1", &string)); | 461 transaction->CheckValue("path1", &string)); |
| 462 } | 462 } |
| 463 | 463 |
| 464 { | 464 { |
| 465 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 465 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
| 466 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 466 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| 467 scoped_ptr<PrefHashStoreTransaction> transaction( | 467 std::unique_ptr<PrefHashStoreTransaction> transaction( |
| 468 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 468 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 469 std::vector<std::string> invalid_keys; | 469 std::vector<std::string> invalid_keys; |
| 470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 471 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 471 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 472 EXPECT_TRUE(invalid_keys.empty()); | 472 EXPECT_TRUE(invalid_keys.empty()); |
| 473 } | 473 } |
| 474 } | 474 } |
| OLD | NEW |