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, AtomicHashStoreAndCheck) { |
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 ASSERT_EQ(PrefHashStore::UNKNOWN_VALUE, |
29 pref_hash_store.CheckValue("path1", &string_1)); | 29 pref_hash_store.CheckValue("path1", &string_1)); |
30 pref_hash_store.StoreHash("path1", &string_1); | 30 pref_hash_store.StoreHash("path1", &string_1); |
31 ASSERT_EQ(PrefHashStore::UNCHANGED, | 31 ASSERT_EQ(PrefHashStore::UNCHANGED, |
32 pref_hash_store.CheckValue("path1", &string_1)); | 32 pref_hash_store.CheckValue("path1", &string_1)); |
33 ASSERT_EQ(PrefHashStore::CLEARED, pref_hash_store.CheckValue("path1", NULL)); | 33 ASSERT_EQ(PrefHashStore::CLEARED, pref_hash_store.CheckValue("path1", NULL)); |
34 pref_hash_store.StoreHash("path1", NULL); | 34 pref_hash_store.StoreHash("path1", NULL); |
35 ASSERT_EQ(PrefHashStore::UNCHANGED, | 35 ASSERT_EQ(PrefHashStore::UNCHANGED, |
36 pref_hash_store.CheckValue("path1", NULL)); | 36 pref_hash_store.CheckValue("path1", NULL)); |
37 ASSERT_EQ(PrefHashStore::CHANGED, | 37 ASSERT_EQ(PrefHashStore::CHANGED, |
38 pref_hash_store.CheckValue("path1", &string_2)); | 38 pref_hash_store.CheckValue("path1", &string_2)); |
39 | 39 |
40 DictionaryValue dict; | 40 base::DictionaryValue dict; |
41 dict.Set("a", new StringValue("foo")); | 41 dict.Set("a", new StringValue("foo")); |
42 dict.Set("d", new StringValue("bad")); | 42 dict.Set("d", new StringValue("bad")); |
43 dict.Set("b", new StringValue("bar")); | 43 dict.Set("b", new StringValue("bar")); |
44 dict.Set("c", new StringValue("baz")); | 44 dict.Set("c", new StringValue("baz")); |
45 | 45 |
46 // Manually shove in a legacy hash. | 46 // Manually shove in a legacy hash. |
47 DictionaryPrefUpdate update(&local_state, prefs::kProfilePreferenceHashes); | 47 DictionaryPrefUpdate update(&local_state, prefs::kProfilePreferenceHashes); |
48 DictionaryValue* child_dictionary = NULL; | 48 base::DictionaryValue* child_dictionary = NULL; |
49 ASSERT_TRUE(update->GetDictionary("store_id", &child_dictionary)); | 49 ASSERT_TRUE(update->GetDictionary("store_id", &child_dictionary)); |
50 child_dictionary->SetString( | 50 child_dictionary->SetString( |
51 "path1", | 51 "path1", |
52 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); | 52 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); |
53 | 53 |
54 ASSERT_EQ(PrefHashStore::MIGRATED, | 54 ASSERT_EQ(PrefHashStore::MIGRATED, |
55 pref_hash_store.CheckValue("path1", &dict)); | 55 pref_hash_store.CheckValue("path1", &dict)); |
56 pref_hash_store.StoreHash("path1", &dict); | 56 pref_hash_store.StoreHash("path1", &dict); |
57 ASSERT_EQ(PrefHashStore::UNCHANGED, | 57 ASSERT_EQ(PrefHashStore::UNCHANGED, |
58 pref_hash_store.CheckValue("path1", &dict)); | 58 pref_hash_store.CheckValue("path1", &dict)); |
59 } | 59 } |
| 60 |
| 61 TEST(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
| 62 base::DictionaryValue dict; |
| 63 dict.Set("a", new StringValue("to be replaced")); |
| 64 dict.Set("b", new StringValue("same")); |
| 65 dict.Set("o", new StringValue("old")); |
| 66 |
| 67 base::DictionaryValue modified_dict; |
| 68 modified_dict.Set("a", new StringValue("replaced")); |
| 69 modified_dict.Set("b", new StringValue("same")); |
| 70 modified_dict.Set("c", new StringValue("new")); |
| 71 |
| 72 base::DictionaryValue empty_dict; |
| 73 |
| 74 TestingPrefServiceSimple local_state; |
| 75 PrefHashStoreImpl::RegisterPrefs(local_state.registry()); |
| 76 |
| 77 PrefHashStoreImpl pref_hash_store( |
| 78 "store_id", std::string(32, 0), "device_id", &local_state); |
| 79 |
| 80 std::vector<std::string> invalid_keys; |
| 81 |
| 82 // No hashes stored yet. |
| 83 ASSERT_EQ(PrefHashStore::UNKNOWN_VALUE, |
| 84 pref_hash_store.CheckSplitValue("path1", &dict, &invalid_keys)); |
| 85 ASSERT_TRUE(invalid_keys.empty()); |
| 86 |
| 87 pref_hash_store.StoreSplitHash("path1", &dict); |
| 88 |
| 89 // Verify match post storage. |
| 90 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 91 pref_hash_store.CheckSplitValue("path1", &dict, &invalid_keys)); |
| 92 ASSERT_TRUE(invalid_keys.empty()); |
| 93 |
| 94 // Verify new path is still unknown. |
| 95 ASSERT_EQ(PrefHashStore::UNKNOWN_VALUE, |
| 96 pref_hash_store.CheckSplitValue("path2", &dict, &invalid_keys)); |
| 97 ASSERT_TRUE(invalid_keys.empty()); |
| 98 |
| 99 // Verify NULL or empty dicts are declared as having been cleared. |
| 100 ASSERT_EQ(PrefHashStore::CLEARED, |
| 101 pref_hash_store.CheckSplitValue("path1", NULL, &invalid_keys)); |
| 102 ASSERT_TRUE(invalid_keys.empty()); |
| 103 ASSERT_EQ(PrefHashStore::CLEARED, |
| 104 pref_hash_store.CheckSplitValue("path1", &empty_dict, |
| 105 &invalid_keys)); |
| 106 ASSERT_TRUE(invalid_keys.empty()); |
| 107 |
| 108 // Verify changes are properly detected. |
| 109 ASSERT_EQ(PrefHashStore::CHANGED, |
| 110 pref_hash_store.CheckSplitValue("path1", &modified_dict, |
| 111 &invalid_keys)); |
| 112 std::vector<std::string> expected_invalid_keys1; |
| 113 expected_invalid_keys1.push_back("a"); |
| 114 expected_invalid_keys1.push_back("c"); |
| 115 ASSERT_EQ(expected_invalid_keys1, invalid_keys); |
| 116 invalid_keys.clear(); |
| 117 |
| 118 // Verify still match post check. |
| 119 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 120 pref_hash_store.CheckSplitValue("path1", &dict, &invalid_keys)); |
| 121 ASSERT_TRUE(invalid_keys.empty()); |
| 122 |
| 123 pref_hash_store.StoreSplitHash("path1", &modified_dict); |
| 124 |
| 125 // Verify |modified_dict| is now the one that verifies correctly. |
| 126 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 127 pref_hash_store.CheckSplitValue("path1", &modified_dict, |
| 128 &invalid_keys)); |
| 129 ASSERT_TRUE(invalid_keys.empty()); |
| 130 |
| 131 // Verify old dict no longer matches. |
| 132 ASSERT_EQ(PrefHashStore::CHANGED, |
| 133 pref_hash_store.CheckSplitValue("path1", &dict, &invalid_keys)); |
| 134 std::vector<std::string> expected_invalid_keys2; |
| 135 expected_invalid_keys2.push_back("a"); |
| 136 expected_invalid_keys2.push_back("o"); |
| 137 ASSERT_EQ(expected_invalid_keys2, invalid_keys); |
| 138 invalid_keys.clear(); |
| 139 |
| 140 // Verify stored empty dictionary matches NULL and empty dictionary back. |
| 141 pref_hash_store.StoreSplitHash("path1", &empty_dict); |
| 142 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 143 pref_hash_store.CheckSplitValue("path1", NULL, &invalid_keys)); |
| 144 ASSERT_TRUE(invalid_keys.empty()); |
| 145 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 146 pref_hash_store.CheckSplitValue("path1", &empty_dict, |
| 147 &invalid_keys)); |
| 148 ASSERT_TRUE(invalid_keys.empty()); |
| 149 |
| 150 // Same when storing NULL directly. |
| 151 pref_hash_store.StoreSplitHash("path1", NULL); |
| 152 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 153 pref_hash_store.CheckSplitValue("path1", NULL, &invalid_keys)); |
| 154 ASSERT_TRUE(invalid_keys.empty()); |
| 155 ASSERT_EQ(PrefHashStore::UNCHANGED, |
| 156 pref_hash_store.CheckSplitValue("path1", &empty_dict, |
| 157 &invalid_keys)); |
| 158 ASSERT_TRUE(invalid_keys.empty()); |
| 159 } |
OLD | NEW |