| 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 "chrome/browser/prefs/tracked/pref_hash_store_impl.h" | 5 #include "chrome/browser/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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 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 scoped_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_UNKNOWN_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, |
| 46 transaction->CheckValue("path1", &string_1)); | 46 transaction->CheckValue("path1", &string_1)); |
| 47 EXPECT_EQ(PrefHashStoreTransaction::CLEARED, | 47 EXPECT_EQ(PrefHashStoreTransaction::CLEARED, |
| 48 transaction->CheckValue("path1", NULL)); | 48 transaction->CheckValue("path1", NULL)); |
| 49 transaction->StoreHash("path1", NULL); | 49 transaction->StoreHash("path1", NULL); |
| 50 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 50 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 51 transaction->CheckValue("path1", NULL)); | 51 transaction->CheckValue("path1", NULL)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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 scoped_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_UNKNOWN_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 scoped_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_UNKNOWN_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 { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 132 scoped_ptr<PrefHashStoreTransaction> transaction( | 132 scoped_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_UNKNOWN_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 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); | 152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 180 scoped_ptr<PrefHashStoreTransaction> transaction( | 180 scoped_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_UNKNOWN_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 scoped_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()); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 466 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
| 467 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 467 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
| 468 scoped_ptr<PrefHashStoreTransaction> transaction( | 468 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 469 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); | 469 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
| 470 std::vector<std::string> invalid_keys; | 470 std::vector<std::string> invalid_keys; |
| 471 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 471 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 472 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 472 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 473 EXPECT_TRUE(invalid_keys.empty()); | 473 EXPECT_TRUE(invalid_keys.empty()); |
| 474 } | 474 } |
| 475 } | 475 } |
| OLD | NEW |