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 "services/preferences/tracked/pref_hash_store_impl.h" | 5 #include "services/preferences/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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 // Quick sanity checks here, see pref_hash_calculator_unittest.cc for more | 44 // Quick sanity checks here, see pref_hash_calculator_unittest.cc for more |
45 // complete tests. | 45 // complete tests. |
46 EXPECT_EQ(computed_mac_1, pref_hash_store.ComputeMac("path1", &string_1)); | 46 EXPECT_EQ(computed_mac_1, pref_hash_store.ComputeMac("path1", &string_1)); |
47 EXPECT_NE(computed_mac_1, computed_mac_2); | 47 EXPECT_NE(computed_mac_1, computed_mac_2); |
48 EXPECT_NE(computed_mac_1, computed_mac_3); | 48 EXPECT_NE(computed_mac_1, computed_mac_3); |
49 EXPECT_EQ(64U, computed_mac_1.size()); | 49 EXPECT_EQ(64U, computed_mac_1.size()); |
50 } | 50 } |
51 | 51 |
52 TEST_F(PrefHashStoreImplTest, ComputeSplitMacs) { | 52 TEST_F(PrefHashStoreImplTest, ComputeSplitMacs) { |
53 base::DictionaryValue dict; | 53 base::DictionaryValue dict; |
54 dict.Set("a", new base::Value("string1")); | 54 dict.SetString("a", "string1"); |
55 dict.Set("b", new base::Value("string2")); | 55 dict.SetString("b", "string2"); |
56 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 56 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
57 | 57 |
58 std::unique_ptr<base::DictionaryValue> computed_macs = | 58 std::unique_ptr<base::DictionaryValue> computed_macs = |
59 pref_hash_store.ComputeSplitMacs("foo.bar", &dict); | 59 pref_hash_store.ComputeSplitMacs("foo.bar", &dict); |
60 | 60 |
61 std::string mac_1; | 61 std::string mac_1; |
62 std::string mac_2; | 62 std::string mac_2; |
63 ASSERT_TRUE(computed_macs->GetString("a", &mac_1)); | 63 ASSERT_TRUE(computed_macs->GetString("a", &mac_1)); |
64 ASSERT_TRUE(computed_macs->GetString("b", &mac_2)); | 64 ASSERT_TRUE(computed_macs->GetString("b", &mac_2)); |
65 | 65 |
(...skipping 23 matching lines...) Expand all Loading... |
89 | 89 |
90 transaction->StoreHash("path1", &string_1); | 90 transaction->StoreHash("path1", &string_1); |
91 EXPECT_EQ(ValueState::UNCHANGED, | 91 EXPECT_EQ(ValueState::UNCHANGED, |
92 transaction->CheckValue("path1", &string_1)); | 92 transaction->CheckValue("path1", &string_1)); |
93 EXPECT_EQ(ValueState::CLEARED, transaction->CheckValue("path1", NULL)); | 93 EXPECT_EQ(ValueState::CLEARED, transaction->CheckValue("path1", NULL)); |
94 transaction->StoreHash("path1", NULL); | 94 transaction->StoreHash("path1", NULL); |
95 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", NULL)); | 95 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", NULL)); |
96 EXPECT_EQ(ValueState::CHANGED, transaction->CheckValue("path1", &string_2)); | 96 EXPECT_EQ(ValueState::CHANGED, transaction->CheckValue("path1", &string_2)); |
97 | 97 |
98 base::DictionaryValue dict; | 98 base::DictionaryValue dict; |
99 dict.Set("a", new base::Value("foo")); | 99 dict.SetString("a", "foo"); |
100 dict.Set("d", new base::Value("bad")); | 100 dict.SetString("d", "bad"); |
101 dict.Set("b", new base::Value("bar")); | 101 dict.SetString("b", "bar"); |
102 dict.Set("c", new base::Value("baz")); | 102 dict.SetString("c", "baz"); |
103 | 103 |
104 transaction->StoreHash("path1", &dict); | 104 transaction->StoreHash("path1", &dict); |
105 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", &dict)); | 105 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", &dict)); |
106 } | 106 } |
107 | 107 |
108 ASSERT_FALSE(GetHashStoreContents()->GetSuperMac().empty()); | 108 ASSERT_FALSE(GetHashStoreContents()->GetSuperMac().empty()); |
109 | 109 |
110 { | 110 { |
111 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 111 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
112 // trust new unknown values. | 112 // trust new unknown values. |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); | 304 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); |
305 std::unique_ptr<PrefHashStoreTransaction> transaction( | 305 std::unique_ptr<PrefHashStoreTransaction> transaction( |
306 pref_hash_store2.BeginTransaction(GetHashStoreContents())); | 306 pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
307 EXPECT_EQ(ValueState::UNTRUSTED_UNKNOWN_VALUE, | 307 EXPECT_EQ(ValueState::UNTRUSTED_UNKNOWN_VALUE, |
308 transaction->CheckValue("new_path", &string_1)); | 308 transaction->CheckValue("new_path", &string_1)); |
309 } | 309 } |
310 } | 310 } |
311 | 311 |
312 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { | 312 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
313 base::DictionaryValue dict; | 313 base::DictionaryValue dict; |
314 dict.Set("a", new base::Value("to be replaced")); | 314 dict.SetString("a", "to be replaced"); |
315 dict.Set("b", new base::Value("same")); | 315 dict.SetString("b", "same"); |
316 dict.Set("o", new base::Value("old")); | 316 dict.SetString("o", "old"); |
317 | 317 |
318 base::DictionaryValue modified_dict; | 318 base::DictionaryValue modified_dict; |
319 modified_dict.Set("a", new base::Value("replaced")); | 319 modified_dict.SetString("a", "replaced"); |
320 modified_dict.Set("b", new base::Value("same")); | 320 modified_dict.SetString("b", "same"); |
321 modified_dict.Set("c", new base::Value("new")); | 321 modified_dict.SetString("c", "new"); |
322 | 322 |
323 base::DictionaryValue empty_dict; | 323 base::DictionaryValue empty_dict; |
324 | 324 |
325 std::vector<std::string> invalid_keys; | 325 std::vector<std::string> invalid_keys; |
326 | 326 |
327 { | 327 { |
328 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 328 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
329 std::unique_ptr<PrefHashStoreTransaction> transaction( | 329 std::unique_ptr<PrefHashStoreTransaction> transaction( |
330 pref_hash_store.BeginTransaction(GetHashStoreContents())); | 330 pref_hash_store.BeginTransaction(GetHashStoreContents())); |
331 | 331 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 | 421 |
422 std::vector<std::string> invalid_keys; | 422 std::vector<std::string> invalid_keys; |
423 | 423 |
424 { | 424 { |
425 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 425 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
426 std::unique_ptr<PrefHashStoreTransaction> transaction( | 426 std::unique_ptr<PrefHashStoreTransaction> transaction( |
427 pref_hash_store.BeginTransaction(GetHashStoreContents())); | 427 pref_hash_store.BeginTransaction(GetHashStoreContents())); |
428 | 428 |
429 // Store hashes for a random dict to be overwritten below. | 429 // Store hashes for a random dict to be overwritten below. |
430 base::DictionaryValue initial_dict; | 430 base::DictionaryValue initial_dict; |
431 initial_dict.Set("a", new base::Value("foo")); | 431 initial_dict.SetString("a", "foo"); |
432 transaction->StoreSplitHash("path1", &initial_dict); | 432 transaction->StoreSplitHash("path1", &initial_dict); |
433 | 433 |
434 // Verify stored empty dictionary matches NULL and empty dictionary back. | 434 // Verify stored empty dictionary matches NULL and empty dictionary back. |
435 transaction->StoreSplitHash("path1", &empty_dict); | 435 transaction->StoreSplitHash("path1", &empty_dict); |
436 EXPECT_EQ(ValueState::UNCHANGED, | 436 EXPECT_EQ(ValueState::UNCHANGED, |
437 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); | 437 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); |
438 EXPECT_TRUE(invalid_keys.empty()); | 438 EXPECT_TRUE(invalid_keys.empty()); |
439 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckSplitValue( | 439 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckSplitValue( |
440 "path1", &empty_dict, &invalid_keys)); | 440 "path1", &empty_dict, &invalid_keys)); |
441 EXPECT_TRUE(invalid_keys.empty()); | 441 EXPECT_TRUE(invalid_keys.empty()); |
(...skipping 12 matching lines...) Expand all Loading... |
454 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 454 // |pref_hash_store2| should trust its initial hashes dictionary (and thus |
455 // trust new unknown values) even though the last action done was to clear | 455 // trust new unknown values) even though the last action done was to clear |
456 // the hashes for path1 by setting its value to NULL (this is a regression | 456 // the hashes for path1 by setting its value to NULL (this is a regression |
457 // test ensuring that the internal action of clearing some hashes does | 457 // test ensuring that the internal action of clearing some hashes does |
458 // update the stored hash of hashes). | 458 // update the stored hash of hashes). |
459 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 459 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
460 std::unique_ptr<PrefHashStoreTransaction> transaction( | 460 std::unique_ptr<PrefHashStoreTransaction> transaction( |
461 pref_hash_store2.BeginTransaction(GetHashStoreContents())); | 461 pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
462 | 462 |
463 base::DictionaryValue tested_dict; | 463 base::DictionaryValue tested_dict; |
464 tested_dict.Set("a", new base::Value("foo")); | 464 tested_dict.SetString("a", "foo"); |
465 tested_dict.Set("b", new base::Value("bar")); | 465 tested_dict.SetString("b", "bar"); |
466 EXPECT_EQ( | 466 EXPECT_EQ( |
467 ValueState::TRUSTED_UNKNOWN_VALUE, | 467 ValueState::TRUSTED_UNKNOWN_VALUE, |
468 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 468 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
469 EXPECT_TRUE(invalid_keys.empty()); | 469 EXPECT_TRUE(invalid_keys.empty()); |
470 } | 470 } |
471 } | 471 } |
472 | 472 |
473 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for | 473 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for |
474 // a split preference even if there is an existing atomic preference's hash | 474 // a split preference even if there is an existing atomic preference's hash |
475 // stored. There is no point providing a migration path for preferences | 475 // stored. There is no point providing a migration path for preferences |
476 // switching strategies after their initial release as split preferences are | 476 // switching strategies after their initial release as split preferences are |
477 // turned into split preferences specifically because the atomic hash isn't | 477 // turned into split preferences specifically because the atomic hash isn't |
478 // considered useful. | 478 // considered useful. |
479 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { | 479 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { |
480 base::Value string("string1"); | 480 base::Value string("string1"); |
481 | 481 |
482 base::DictionaryValue dict; | 482 base::DictionaryValue dict; |
483 dict.Set("a", new base::Value("foo")); | 483 dict.SetString("a", "foo"); |
484 dict.Set("d", new base::Value("bad")); | 484 dict.SetString("d", "bad"); |
485 dict.Set("b", new base::Value("bar")); | 485 dict.SetString("b", "bar"); |
486 dict.Set("c", new base::Value("baz")); | 486 dict.SetString("c", "baz"); |
487 | 487 |
488 { | 488 { |
489 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); | 489 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
490 std::unique_ptr<PrefHashStoreTransaction> transaction( | 490 std::unique_ptr<PrefHashStoreTransaction> transaction( |
491 pref_hash_store.BeginTransaction(GetHashStoreContents())); | 491 pref_hash_store.BeginTransaction(GetHashStoreContents())); |
492 | 492 |
493 transaction->StoreHash("path1", &string); | 493 transaction->StoreHash("path1", &string); |
494 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", &string)); | 494 EXPECT_EQ(ValueState::UNCHANGED, transaction->CheckValue("path1", &string)); |
495 } | 495 } |
496 | 496 |
497 { | 497 { |
498 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 498 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
499 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); | 499 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
500 std::unique_ptr<PrefHashStoreTransaction> transaction( | 500 std::unique_ptr<PrefHashStoreTransaction> transaction( |
501 pref_hash_store2.BeginTransaction(GetHashStoreContents())); | 501 pref_hash_store2.BeginTransaction(GetHashStoreContents())); |
502 std::vector<std::string> invalid_keys; | 502 std::vector<std::string> invalid_keys; |
503 EXPECT_EQ(ValueState::TRUSTED_UNKNOWN_VALUE, | 503 EXPECT_EQ(ValueState::TRUSTED_UNKNOWN_VALUE, |
504 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 504 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
505 EXPECT_TRUE(invalid_keys.empty()); | 505 EXPECT_TRUE(invalid_keys.empty()); |
506 } | 506 } |
507 } | 507 } |
OLD | NEW |