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/pref_hash_store_impl.h" | 5 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/prefs/pref_hash_store.h" | 13 #include "chrome/browser/prefs/pref_hash_store.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 TEST(PrefHashStoreImplTest, TestCase) { | 17 TEST(PrefHashStoreImplTest, TestCase) { |
18 base::StringValue string_1("string1"); | 18 base::StringValue string_1("string1"); |
19 base::StringValue string_2("string2"); | 19 base::StringValue string_2("string2"); |
20 | 20 |
21 TestingPrefServiceSimple local_state; | 21 TestingPrefServiceSimple local_state; |
22 PrefHashStoreImpl::RegisterPrefs(local_state.registry()); | 22 PrefHashStoreImpl::RegisterPrefs(local_state.registry()); |
23 | 23 |
24 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 24 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
25 PrefHashStoreImpl pref_hash_store( | 25 PrefHashStoreImpl pref_hash_store( |
26 "store_id", std::string(32,0), "device_id", &local_state); | 26 "store_id", std::string(32,0), "device_id", &local_state); |
27 | 27 |
28 ASSERT_EQ(PrefHashStore::UNKNOWN_VALUE, | 28 // Only NULL should be trusted in the absence of a hash. |
| 29 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE, |
29 pref_hash_store.CheckValue("path1", &string_1)); | 30 pref_hash_store.CheckValue("path1", &string_1)); |
| 31 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE, |
| 32 pref_hash_store.CheckValue("path1", NULL)); |
| 33 |
30 pref_hash_store.StoreHash("path1", &string_1); | 34 pref_hash_store.StoreHash("path1", &string_1); |
31 ASSERT_EQ(PrefHashStore::UNCHANGED, | 35 EXPECT_EQ(PrefHashStore::UNCHANGED, |
32 pref_hash_store.CheckValue("path1", &string_1)); | 36 pref_hash_store.CheckValue("path1", &string_1)); |
33 ASSERT_EQ(PrefHashStore::CLEARED, pref_hash_store.CheckValue("path1", NULL)); | 37 EXPECT_EQ(PrefHashStore::CLEARED, pref_hash_store.CheckValue("path1", NULL)); |
34 pref_hash_store.StoreHash("path1", NULL); | 38 pref_hash_store.StoreHash("path1", NULL); |
35 ASSERT_EQ(PrefHashStore::UNCHANGED, | 39 EXPECT_EQ(PrefHashStore::UNCHANGED, |
36 pref_hash_store.CheckValue("path1", NULL)); | 40 pref_hash_store.CheckValue("path1", NULL)); |
37 ASSERT_EQ(PrefHashStore::CHANGED, | 41 EXPECT_EQ(PrefHashStore::CHANGED, |
38 pref_hash_store.CheckValue("path1", &string_2)); | 42 pref_hash_store.CheckValue("path1", &string_2)); |
39 | 43 |
40 base::DictionaryValue dict; | 44 base::DictionaryValue dict; |
41 dict.Set("a", new base::StringValue("foo")); | 45 dict.Set("a", new base::StringValue("foo")); |
42 dict.Set("d", new base::StringValue("bad")); | 46 dict.Set("d", new base::StringValue("bad")); |
43 dict.Set("b", new base::StringValue("bar")); | 47 dict.Set("b", new base::StringValue("bar")); |
44 dict.Set("c", new base::StringValue("baz")); | 48 dict.Set("c", new base::StringValue("baz")); |
45 | 49 |
46 // Manually shove in a legacy hash. | 50 { |
47 DictionaryPrefUpdate update(&local_state, prefs::kProfilePreferenceHashes); | 51 // Manually shove in a legacy hash. |
48 base::DictionaryValue* child_dictionary = NULL; | 52 DictionaryPrefUpdate update(&local_state, prefs::kProfilePreferenceHashes); |
49 ASSERT_TRUE(update->GetDictionary("store_id", &child_dictionary)); | 53 base::DictionaryValue* child_dictionary = NULL; |
50 child_dictionary->SetString( | 54 ASSERT_TRUE(update->GetDictionary("store_id", &child_dictionary)); |
51 "path1", | 55 child_dictionary->SetString( |
52 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); | 56 "path1", |
53 | 57 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); |
54 ASSERT_EQ(PrefHashStore::MIGRATED, | 58 } |
| 59 EXPECT_EQ(PrefHashStore::MIGRATED, |
55 pref_hash_store.CheckValue("path1", &dict)); | 60 pref_hash_store.CheckValue("path1", &dict)); |
56 pref_hash_store.StoreHash("path1", &dict); | 61 pref_hash_store.StoreHash("path1", &dict); |
57 ASSERT_EQ(PrefHashStore::UNCHANGED, | 62 EXPECT_EQ(PrefHashStore::UNCHANGED, |
58 pref_hash_store.CheckValue("path1", &dict)); | 63 pref_hash_store.CheckValue("path1", &dict)); |
| 64 |
| 65 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 66 // trust new unknown values. |
| 67 PrefHashStoreImpl pref_hash_store2( |
| 68 "store_id", std::string(32, 0), "device_id", &local_state); |
| 69 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE, |
| 70 pref_hash_store2.CheckValue("new_path", &string_1)); |
| 71 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE, |
| 72 pref_hash_store2.CheckValue("new_path", &string_2)); |
| 73 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE, |
| 74 pref_hash_store2.CheckValue("new_path", NULL)); |
| 75 |
| 76 { |
| 77 // Manually corrupt the hash of hashes for "store_id". |
| 78 DictionaryPrefUpdate update(&local_state, prefs::kProfilePreferenceHashes); |
| 79 base::DictionaryValue* hash_of_hashes_dict = NULL; |
| 80 ASSERT_TRUE(update->GetDictionaryWithoutPathExpansion( |
| 81 internals::kHashOfHashesPref, &hash_of_hashes_dict)); |
| 82 hash_of_hashes_dict->SetString("store_id", std::string(64, 'A')); |
| 83 // This shouldn't have increased the number of existing hash of hashes. |
| 84 ASSERT_EQ(1U, hash_of_hashes_dict->size()); |
| 85 } |
| 86 |
| 87 // |pref_hash_store3| should no longer trust its initial hashes dictionary and |
| 88 // thus shouldn't trust non-NULL unknown values. |
| 89 PrefHashStoreImpl pref_hash_store3( |
| 90 "store_id", std::string(32, 0), "device_id", &local_state); |
| 91 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE, |
| 92 pref_hash_store3.CheckValue("new_path", &string_1)); |
| 93 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE, |
| 94 pref_hash_store3.CheckValue("new_path", &string_2)); |
| 95 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE, |
| 96 pref_hash_store3.CheckValue("new_path", NULL)); |
59 } | 97 } |
OLD | NEW |