Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / user_prefs / tracked / pref_hash_store_impl_unittest.cc
blob8ec8ffaf277e921a34bc4f48113d5f8fe395fcb7
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
3 // found in the LICENSE file.
5 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
7 #include <string>
9 #include "base/macros.h"
10 #include "base/values.h"
11 #include "components/user_prefs/tracked/dictionary_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"
14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 class PrefHashStoreImplTest : public testing::Test {
18 protected:
19 scoped_ptr<HashStoreContents> CreateHashStoreContents() {
20 return scoped_ptr<HashStoreContents>(
21 new DictionaryHashStoreContents(&pref_store_contents_));
24 private:
25 base::DictionaryValue pref_store_contents_;
28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
29 base::StringValue string_1("string1");
30 base::StringValue string_2("string2");
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);
35 scoped_ptr<PrefHashStoreTransaction> transaction(
36 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
38 // Only NULL should be trusted in the absence of a hash.
39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
40 transaction->CheckValue("path1", &string_1));
41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
42 transaction->CheckValue("path1", NULL));
44 transaction->StoreHash("path1", &string_1);
45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
46 transaction->CheckValue("path1", &string_1));
47 EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
48 transaction->CheckValue("path1", NULL));
49 transaction->StoreHash("path1", NULL);
50 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
51 transaction->CheckValue("path1", NULL));
52 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
53 transaction->CheckValue("path1", &string_2));
55 base::DictionaryValue dict;
56 dict.Set("a", new base::StringValue("foo"));
57 dict.Set("d", new base::StringValue("bad"));
58 dict.Set("b", new base::StringValue("bar"));
59 dict.Set("c", new base::StringValue("baz"));
61 transaction->StoreHash("path1", &dict);
62 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
63 transaction->CheckValue("path1", &dict));
66 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty());
69 // |pref_hash_store2| should trust its initial hashes dictionary and thus
70 // trust new unknown values.
71 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
72 scoped_ptr<PrefHashStoreTransaction> transaction(
73 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
74 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
75 transaction->CheckValue("new_path", &string_1));
76 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
77 transaction->CheckValue("new_path", &string_2));
78 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
79 transaction->CheckValue("new_path", NULL));
82 // Manually corrupt the super MAC.
83 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A'));
86 // |pref_hash_store3| should no longer trust its initial hashes dictionary
87 // and thus shouldn't trust non-NULL unknown values.
88 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
89 scoped_ptr<PrefHashStoreTransaction> transaction(
90 pref_hash_store3.BeginTransaction(CreateHashStoreContents()));
91 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
92 transaction->CheckValue("new_path", &string_1));
93 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
94 transaction->CheckValue("new_path", &string_2));
95 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
96 transaction->CheckValue("new_path", NULL));
100 TEST_F(PrefHashStoreImplTest, ImportExportOperations) {
101 base::StringValue string_1("string1");
102 base::StringValue string_2("string2");
104 // Initial state: no super MAC.
106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
107 scoped_ptr<PrefHashStoreTransaction> transaction(
108 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
109 ASSERT_FALSE(transaction->IsSuperMACValid());
111 ASSERT_FALSE(transaction->HasHash("path1"));
113 // Storing a hash will stamp the super MAC.
114 transaction->StoreHash("path1", &string_1);
116 ASSERT_TRUE(transaction->HasHash("path1"));
117 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
118 transaction->CheckValue("path1", &string_1));
119 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
120 transaction->CheckValue("path1", &string_2));
123 // Make a copy of the stored hash for future use.
124 const base::Value* hash = NULL;
125 ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash));
126 scoped_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy());
127 hash = NULL;
129 // Verify that the super MAC was stamped.
131 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
132 scoped_ptr<PrefHashStoreTransaction> transaction(
133 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
134 ASSERT_TRUE(transaction->IsSuperMACValid());
135 ASSERT_TRUE(transaction->HasHash("path1"));
137 // Clearing the hash should preserve validity.
138 transaction->ClearHash("path1");
140 // The effects of the clear should be immediately visible.
141 ASSERT_FALSE(transaction->HasHash("path1"));
142 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
143 transaction->CheckValue("path1", NULL));
144 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
145 transaction->CheckValue("path1", &string_1));
148 // Verify that validity was preserved and that the clear took effect.
150 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
151 scoped_ptr<PrefHashStoreTransaction> transaction(
152 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
153 ASSERT_TRUE(transaction->IsSuperMACValid());
154 ASSERT_FALSE(transaction->HasHash("path1"));
157 // Invalidate the super MAC.
158 CreateHashStoreContents()->SetSuperMac(std::string());
161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
162 scoped_ptr<PrefHashStoreTransaction> transaction(
163 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
164 ASSERT_FALSE(transaction->IsSuperMACValid());
165 ASSERT_FALSE(transaction->HasHash("path1"));
167 // An import should preserve invalidity.
168 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
170 ASSERT_TRUE(transaction->HasHash("path1"));
172 // The imported hash should be usable for validating the original value.
173 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
174 transaction->CheckValue("path1", &string_1));
177 // Verify that invalidity was preserved and that the import took effect.
179 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
180 scoped_ptr<PrefHashStoreTransaction> transaction(
181 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
182 ASSERT_FALSE(transaction->IsSuperMACValid());
183 ASSERT_TRUE(transaction->HasHash("path1"));
184 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
185 transaction->CheckValue("path1", &string_1));
187 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN.
188 transaction->ClearHash("path1");
190 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
191 transaction->CheckValue("path1", NULL));
192 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
193 transaction->CheckValue("path1", &string_1));
197 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
198 scoped_ptr<PrefHashStoreTransaction> transaction(
199 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
200 ASSERT_FALSE(transaction->IsSuperMACValid());
202 // Test StampSuperMac.
203 transaction->StampSuperMac();
206 // Verify that the store is now valid.
208 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
209 scoped_ptr<PrefHashStoreTransaction> transaction(
210 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
211 ASSERT_TRUE(transaction->IsSuperMACValid());
213 // Store the hash of a different value to test an "over-import".
214 transaction->StoreHash("path1", &string_2);
215 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
216 transaction->CheckValue("path1", &string_1));
217 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
218 transaction->CheckValue("path1", &string_2));
222 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
223 scoped_ptr<PrefHashStoreTransaction> transaction(
224 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
225 ASSERT_TRUE(transaction->IsSuperMACValid());
227 // "Over-import". An import should preserve validity.
228 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
230 transaction->CheckValue("path1", &string_1));
231 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
232 transaction->CheckValue("path1", &string_2));
235 // Verify that validity was preserved and the "over-import" took effect.
237 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
238 scoped_ptr<PrefHashStoreTransaction> transaction(
239 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
240 ASSERT_TRUE(transaction->IsSuperMACValid());
241 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
242 transaction->CheckValue("path1", &string_1));
243 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
244 transaction->CheckValue("path1", &string_2));
248 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
249 base::StringValue string_1("string1");
250 base::StringValue string_2("string2");
253 // Pass |use_super_mac| => false.
254 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false);
255 scoped_ptr<PrefHashStoreTransaction> transaction(
256 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
258 transaction->StoreHash("path1", &string_2);
259 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
260 transaction->CheckValue("path1", &string_2));
263 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty());
266 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false);
267 scoped_ptr<PrefHashStoreTransaction> transaction(
268 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
269 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
270 transaction->CheckValue("new_path", &string_1));
274 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
275 base::DictionaryValue dict;
276 dict.Set("a", new base::StringValue("to be replaced"));
277 dict.Set("b", new base::StringValue("same"));
278 dict.Set("o", new base::StringValue("old"));
280 base::DictionaryValue modified_dict;
281 modified_dict.Set("a", new base::StringValue("replaced"));
282 modified_dict.Set("b", new base::StringValue("same"));
283 modified_dict.Set("c", new base::StringValue("new"));
285 base::DictionaryValue empty_dict;
287 std::vector<std::string> invalid_keys;
290 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
291 scoped_ptr<PrefHashStoreTransaction> transaction(
292 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
294 // No hashes stored yet and hashes dictionary is empty (and thus not
295 // trusted).
296 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
297 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
298 EXPECT_TRUE(invalid_keys.empty());
300 transaction->StoreSplitHash("path1", &dict);
302 // Verify match post storage.
303 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
304 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
305 EXPECT_TRUE(invalid_keys.empty());
307 // Verify new path is still unknown.
308 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
309 transaction->CheckSplitValue("path2", &dict, &invalid_keys));
310 EXPECT_TRUE(invalid_keys.empty());
312 // Verify NULL or empty dicts are declared as having been cleared.
313 EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
314 transaction->CheckSplitValue("path1", NULL, &invalid_keys));
315 EXPECT_TRUE(invalid_keys.empty());
316 EXPECT_EQ(
317 PrefHashStoreTransaction::CLEARED,
318 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
319 EXPECT_TRUE(invalid_keys.empty());
321 // Verify changes are properly detected.
322 EXPECT_EQ(
323 PrefHashStoreTransaction::CHANGED,
324 transaction->CheckSplitValue("path1", &modified_dict, &invalid_keys));
325 std::vector<std::string> expected_invalid_keys1;
326 expected_invalid_keys1.push_back("a");
327 expected_invalid_keys1.push_back("c");
328 expected_invalid_keys1.push_back("o");
329 EXPECT_EQ(expected_invalid_keys1, invalid_keys);
330 invalid_keys.clear();
332 // Verify |dict| still matches post check.
333 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
334 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
335 EXPECT_TRUE(invalid_keys.empty());
337 // Store hash for |modified_dict|.
338 transaction->StoreSplitHash("path1", &modified_dict);
340 // Verify |modified_dict| is now the one that verifies correctly.
341 EXPECT_EQ(
342 PrefHashStoreTransaction::UNCHANGED,
343 transaction->CheckSplitValue("path1", &modified_dict, &invalid_keys));
344 EXPECT_TRUE(invalid_keys.empty());
346 // Verify old dict no longer matches.
347 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
348 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
349 std::vector<std::string> expected_invalid_keys2;
350 expected_invalid_keys2.push_back("a");
351 expected_invalid_keys2.push_back("o");
352 expected_invalid_keys2.push_back("c");
353 EXPECT_EQ(expected_invalid_keys2, invalid_keys);
354 invalid_keys.clear();
358 // |pref_hash_store2| should trust its initial hashes dictionary and thus
359 // trust new unknown values.
360 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
361 scoped_ptr<PrefHashStoreTransaction> transaction(
362 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
363 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
364 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
365 EXPECT_TRUE(invalid_keys.empty());
368 // Manually corrupt the super MAC.
369 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A'));
372 // |pref_hash_store3| should no longer trust its initial hashes dictionary
373 // and thus shouldn't trust unknown values.
374 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
375 scoped_ptr<PrefHashStoreTransaction> transaction(
376 pref_hash_store3.BeginTransaction(CreateHashStoreContents()));
377 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
378 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
379 EXPECT_TRUE(invalid_keys.empty());
383 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
384 base::DictionaryValue empty_dict;
386 std::vector<std::string> invalid_keys;
389 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
390 scoped_ptr<PrefHashStoreTransaction> transaction(
391 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
393 // Store hashes for a random dict to be overwritten below.
394 base::DictionaryValue initial_dict;
395 initial_dict.Set("a", new base::StringValue("foo"));
396 transaction->StoreSplitHash("path1", &initial_dict);
398 // Verify stored empty dictionary matches NULL and empty dictionary back.
399 transaction->StoreSplitHash("path1", &empty_dict);
400 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
401 transaction->CheckSplitValue("path1", NULL, &invalid_keys));
402 EXPECT_TRUE(invalid_keys.empty());
403 EXPECT_EQ(
404 PrefHashStoreTransaction::UNCHANGED,
405 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
406 EXPECT_TRUE(invalid_keys.empty());
408 // Same when storing NULL directly.
409 transaction->StoreSplitHash("path1", NULL);
410 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
411 transaction->CheckSplitValue("path1", NULL, &invalid_keys));
412 EXPECT_TRUE(invalid_keys.empty());
413 EXPECT_EQ(
414 PrefHashStoreTransaction::UNCHANGED,
415 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys));
416 EXPECT_TRUE(invalid_keys.empty());
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
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
424 // update the stored hash of hashes).
425 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
426 scoped_ptr<PrefHashStoreTransaction> transaction(
427 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
429 base::DictionaryValue tested_dict;
430 tested_dict.Set("a", new base::StringValue("foo"));
431 tested_dict.Set("b", new base::StringValue("bar"));
432 EXPECT_EQ(
433 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
434 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys));
435 EXPECT_TRUE(invalid_keys.empty());
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
441 // stored. There is no point providing a migration path for preferences
442 // switching strategies after their initial release as split preferences are
443 // turned into split preferences specifically because the atomic hash isn't
444 // considered useful.
445 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
446 base::StringValue string("string1");
448 base::DictionaryValue dict;
449 dict.Set("a", new base::StringValue("foo"));
450 dict.Set("d", new base::StringValue("bad"));
451 dict.Set("b", new base::StringValue("bar"));
452 dict.Set("c", new base::StringValue("baz"));
455 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
456 scoped_ptr<PrefHashStoreTransaction> transaction(
457 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
459 transaction->StoreHash("path1", &string);
460 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
461 transaction->CheckValue("path1", &string));
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);
467 scoped_ptr<PrefHashStoreTransaction> transaction(
468 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
469 std::vector<std::string> invalid_keys;
470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
471 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
472 EXPECT_TRUE(invalid_keys.empty());