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