Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(362)

Side by Side Diff: components/user_prefs/tracked/pref_hash_store_impl_unittest.cc

Issue 2204943002: Integrate registry_hash_store_contents with the rest of tracked prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove a lost include statement Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 protected: 18 public:
19 std::unique_ptr<HashStoreContents> CreateHashStoreContents() { 19 void SetUp() override {
20 return std::unique_ptr<HashStoreContents>( 20 pref_store_contents_.reset(new base::DictionaryValue);
21 new DictionaryHashStoreContents(&pref_store_contents_)); 21 contents_.reset(
22 new DictionaryHashStoreContents(pref_store_contents_.get()));
22 } 23 }
23 24
25 protected:
26 HashStoreContents* GetHashStoreContents() { return contents_.get(); }
27
24 private: 28 private:
25 base::DictionaryValue pref_store_contents_; 29 std::unique_ptr<HashStoreContents> contents_;
30 std::unique_ptr<base::DictionaryValue> pref_store_contents_;
gab 2016/08/08 04:37:46 |pref_store_contents_| doesn't have to be in a uni
proberge 2016/08/31 17:30:17 Done.
26 }; 31 };
27 32
33 TEST_F(PrefHashStoreImplTest, ComputeMac) {
34 base::StringValue string_1("string1");
35 base::StringValue string_2("string2");
36 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
37
38 std::string computed_mac_1 = pref_hash_store.ComputeMac("path1", &string_1);
39 std::string computed_mac_2 = pref_hash_store.ComputeMac("path1", &string_2);
40 std::string computed_mac_3 = pref_hash_store.ComputeMac("path2", &string_1);
41
42 // Quick sanity checks here, see pref_hash_calculator_unittest.cc for more
43 // complete tests.
44 EXPECT_NE(computed_mac_1, computed_mac_2);
45 EXPECT_NE(computed_mac_1, computed_mac_3);
46 EXPECT_EQ(64, computed_mac_1.size());
47 }
48
49 TEST_F(PrefHashStoreImplTest, ComputeSplitMacs) {
50 base::DictionaryValue dict;
51 dict.Set("a", new base::StringValue("string1"));
52 dict.Set("b", new base::StringValue("string2"));
53 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
54
55 std::unique_ptr<base::DictionaryValue> computed_macs =
56 pref_hash_store.ComputeSplitMacs("extension.settings", &dict);
gab 2016/08/08 04:37:46 "foo.bar" (no need to refer to extension.settings"
proberge 2016/08/31 17:30:17 Done.
57
58 std::string mac_1;
59 std::string mac_2;
60 ASSERT_TRUE(computed_macs->GetString("a", &mac_1));
61 ASSERT_TRUE(computed_macs->GetString("b", &mac_2));
62
63 EXPECT_EQ(2, computed_macs->size());
64
65 base::StringValue string_1("string1");
66 base::StringValue string_2("string2");
67 EXPECT_EQ(pref_hash_store.ComputeMac("extension.settings.a", &string_1),
68 mac_1);
69 EXPECT_EQ(pref_hash_store.ComputeMac("extension.settings.b", &string_2),
70 mac_2);
71 }
72
28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { 73 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
29 base::StringValue string_1("string1"); 74 base::StringValue string_1("string1");
30 base::StringValue string_2("string2"); 75 base::StringValue string_2("string2");
31 76
32 { 77 {
33 // 32 NULL bytes is the seed that was used to generate the legacy hash. 78 // 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); 79 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
35 std::unique_ptr<PrefHashStoreTransaction> transaction( 80 std::unique_ptr<PrefHashStoreTransaction> transaction(
36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 81 pref_hash_store.BeginTransaction(GetHashStoreContents()));
37 82
38 // Only NULL should be trusted in the absence of a hash. 83 // Only NULL should be trusted in the absence of a hash.
39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 84 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
40 transaction->CheckValue("path1", &string_1)); 85 transaction->CheckValue("path1", &string_1));
41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 86 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
42 transaction->CheckValue("path1", NULL)); 87 transaction->CheckValue("path1", NULL));
43 88
44 transaction->StoreHash("path1", &string_1); 89 transaction->StoreHash("path1", &string_1);
45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 90 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
46 transaction->CheckValue("path1", &string_1)); 91 transaction->CheckValue("path1", &string_1));
47 EXPECT_EQ(PrefHashStoreTransaction::CLEARED, 92 EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
48 transaction->CheckValue("path1", NULL)); 93 transaction->CheckValue("path1", NULL));
49 transaction->StoreHash("path1", NULL); 94 transaction->StoreHash("path1", NULL);
50 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 95 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
51 transaction->CheckValue("path1", NULL)); 96 transaction->CheckValue("path1", NULL));
52 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 97 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
53 transaction->CheckValue("path1", &string_2)); 98 transaction->CheckValue("path1", &string_2));
54 99
55 base::DictionaryValue dict; 100 base::DictionaryValue dict;
56 dict.Set("a", new base::StringValue("foo")); 101 dict.Set("a", new base::StringValue("foo"));
57 dict.Set("d", new base::StringValue("bad")); 102 dict.Set("d", new base::StringValue("bad"));
58 dict.Set("b", new base::StringValue("bar")); 103 dict.Set("b", new base::StringValue("bar"));
59 dict.Set("c", new base::StringValue("baz")); 104 dict.Set("c", new base::StringValue("baz"));
60 105
61 transaction->StoreHash("path1", &dict); 106 transaction->StoreHash("path1", &dict);
62 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 107 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
63 transaction->CheckValue("path1", &dict)); 108 transaction->CheckValue("path1", &dict));
64 } 109 }
65 110
66 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); 111 ASSERT_FALSE(GetHashStoreContents()->GetSuperMac().empty());
67 112
68 { 113 {
69 // |pref_hash_store2| should trust its initial hashes dictionary and thus 114 // |pref_hash_store2| should trust its initial hashes dictionary and thus
70 // trust new unknown values. 115 // trust new unknown values.
71 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 116 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
72 std::unique_ptr<PrefHashStoreTransaction> transaction( 117 std::unique_ptr<PrefHashStoreTransaction> transaction(
73 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 118 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
74 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 119 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
75 transaction->CheckValue("new_path", &string_1)); 120 transaction->CheckValue("new_path", &string_1));
76 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 121 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
77 transaction->CheckValue("new_path", &string_2)); 122 transaction->CheckValue("new_path", &string_2));
78 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 123 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
79 transaction->CheckValue("new_path", NULL)); 124 transaction->CheckValue("new_path", NULL));
80 } 125 }
81 126
82 // Manually corrupt the super MAC. 127 // Manually corrupt the super MAC.
83 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); 128 GetHashStoreContents()->SetSuperMac(std::string(64, 'A'));
84 129
85 { 130 {
86 // |pref_hash_store3| should no longer trust its initial hashes dictionary 131 // |pref_hash_store3| should no longer trust its initial hashes dictionary
87 // and thus shouldn't trust non-NULL unknown values. 132 // and thus shouldn't trust non-NULL unknown values.
88 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); 133 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
89 std::unique_ptr<PrefHashStoreTransaction> transaction( 134 std::unique_ptr<PrefHashStoreTransaction> transaction(
90 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); 135 pref_hash_store3.BeginTransaction(GetHashStoreContents()));
91 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 136 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
92 transaction->CheckValue("new_path", &string_1)); 137 transaction->CheckValue("new_path", &string_1));
93 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 138 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
94 transaction->CheckValue("new_path", &string_2)); 139 transaction->CheckValue("new_path", &string_2));
95 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 140 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
96 transaction->CheckValue("new_path", NULL)); 141 transaction->CheckValue("new_path", NULL));
97 } 142 }
98 } 143 }
99 144
100 TEST_F(PrefHashStoreImplTest, ImportExportOperations) { 145 TEST_F(PrefHashStoreImplTest, ImportExportOperations) {
101 base::StringValue string_1("string1"); 146 base::StringValue string_1("string1");
102 base::StringValue string_2("string2"); 147 base::StringValue string_2("string2");
103 148
104 // Initial state: no super MAC. 149 // Initial state: no super MAC.
105 { 150 {
106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 151 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
107 std::unique_ptr<PrefHashStoreTransaction> transaction( 152 std::unique_ptr<PrefHashStoreTransaction> transaction(
108 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 153 pref_hash_store.BeginTransaction(GetHashStoreContents()));
109 ASSERT_FALSE(transaction->IsSuperMACValid()); 154 ASSERT_FALSE(transaction->IsSuperMACValid());
110 155
111 ASSERT_FALSE(transaction->HasHash("path1")); 156 ASSERT_FALSE(transaction->HasHash("path1"));
112 157
113 // Storing a hash will stamp the super MAC. 158 // Storing a hash will stamp the super MAC.
114 transaction->StoreHash("path1", &string_1); 159 transaction->StoreHash("path1", &string_1);
115 160
116 ASSERT_TRUE(transaction->HasHash("path1")); 161 ASSERT_TRUE(transaction->HasHash("path1"));
117 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 162 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
118 transaction->CheckValue("path1", &string_1)); 163 transaction->CheckValue("path1", &string_1));
119 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 164 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
120 transaction->CheckValue("path1", &string_2)); 165 transaction->CheckValue("path1", &string_2));
121 } 166 }
122 167
123 // Make a copy of the stored hash for future use. 168 // Make a copy of the stored hash for future use.
124 const base::Value* hash = NULL; 169 const base::Value* hash = NULL;
125 ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash)); 170 ASSERT_TRUE(GetHashStoreContents()->GetContents()->Get("path1", &hash));
126 std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy()); 171 std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy());
127 hash = NULL; 172 hash = NULL;
128 173
129 // Verify that the super MAC was stamped. 174 // Verify that the super MAC was stamped.
130 { 175 {
131 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 176 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
132 std::unique_ptr<PrefHashStoreTransaction> transaction( 177 std::unique_ptr<PrefHashStoreTransaction> transaction(
133 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 178 pref_hash_store.BeginTransaction(GetHashStoreContents()));
134 ASSERT_TRUE(transaction->IsSuperMACValid()); 179 ASSERT_TRUE(transaction->IsSuperMACValid());
135 ASSERT_TRUE(transaction->HasHash("path1")); 180 ASSERT_TRUE(transaction->HasHash("path1"));
136 181
137 // Clearing the hash should preserve validity. 182 // Clearing the hash should preserve validity.
138 transaction->ClearHash("path1"); 183 transaction->ClearHash("path1");
139 184
140 // The effects of the clear should be immediately visible. 185 // The effects of the clear should be immediately visible.
141 ASSERT_FALSE(transaction->HasHash("path1")); 186 ASSERT_FALSE(transaction->HasHash("path1"));
142 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 187 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
143 transaction->CheckValue("path1", NULL)); 188 transaction->CheckValue("path1", NULL));
144 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 189 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
145 transaction->CheckValue("path1", &string_1)); 190 transaction->CheckValue("path1", &string_1));
146 } 191 }
147 192
148 // Verify that validity was preserved and that the clear took effect. 193 // Verify that validity was preserved and that the clear took effect.
149 { 194 {
150 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 195 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
151 std::unique_ptr<PrefHashStoreTransaction> transaction( 196 std::unique_ptr<PrefHashStoreTransaction> transaction(
152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 197 pref_hash_store.BeginTransaction(GetHashStoreContents()));
153 ASSERT_TRUE(transaction->IsSuperMACValid()); 198 ASSERT_TRUE(transaction->IsSuperMACValid());
154 ASSERT_FALSE(transaction->HasHash("path1")); 199 ASSERT_FALSE(transaction->HasHash("path1"));
155 } 200 }
156 201
157 // Invalidate the super MAC. 202 // Invalidate the super MAC.
158 CreateHashStoreContents()->SetSuperMac(std::string()); 203 GetHashStoreContents()->SetSuperMac(std::string());
159 204
160 { 205 {
161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 206 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
162 std::unique_ptr<PrefHashStoreTransaction> transaction( 207 std::unique_ptr<PrefHashStoreTransaction> transaction(
163 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 208 pref_hash_store.BeginTransaction(GetHashStoreContents()));
164 ASSERT_FALSE(transaction->IsSuperMACValid()); 209 ASSERT_FALSE(transaction->IsSuperMACValid());
165 ASSERT_FALSE(transaction->HasHash("path1")); 210 ASSERT_FALSE(transaction->HasHash("path1"));
166 211
167 // An import should preserve invalidity. 212 // An import should preserve invalidity.
168 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); 213 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
169 214
170 ASSERT_TRUE(transaction->HasHash("path1")); 215 ASSERT_TRUE(transaction->HasHash("path1"));
171 216
172 // The imported hash should be usable for validating the original value. 217 // The imported hash should be usable for validating the original value.
173 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 218 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
174 transaction->CheckValue("path1", &string_1)); 219 transaction->CheckValue("path1", &string_1));
175 } 220 }
176 221
177 // Verify that invalidity was preserved and that the import took effect. 222 // Verify that invalidity was preserved and that the import took effect.
178 { 223 {
179 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 224 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
180 std::unique_ptr<PrefHashStoreTransaction> transaction( 225 std::unique_ptr<PrefHashStoreTransaction> transaction(
181 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 226 pref_hash_store.BeginTransaction(GetHashStoreContents()));
182 ASSERT_FALSE(transaction->IsSuperMACValid()); 227 ASSERT_FALSE(transaction->IsSuperMACValid());
183 ASSERT_TRUE(transaction->HasHash("path1")); 228 ASSERT_TRUE(transaction->HasHash("path1"));
184 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
185 transaction->CheckValue("path1", &string_1)); 230 transaction->CheckValue("path1", &string_1));
186 231
187 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN. 232 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN.
188 transaction->ClearHash("path1"); 233 transaction->ClearHash("path1");
189 234
190 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 235 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
191 transaction->CheckValue("path1", NULL)); 236 transaction->CheckValue("path1", NULL));
192 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 237 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
193 transaction->CheckValue("path1", &string_1)); 238 transaction->CheckValue("path1", &string_1));
194 } 239 }
195 240
196 { 241 {
197 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 242 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
198 std::unique_ptr<PrefHashStoreTransaction> transaction( 243 std::unique_ptr<PrefHashStoreTransaction> transaction(
199 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 244 pref_hash_store.BeginTransaction(GetHashStoreContents()));
200 ASSERT_FALSE(transaction->IsSuperMACValid()); 245 ASSERT_FALSE(transaction->IsSuperMACValid());
201 246
202 // Test StampSuperMac. 247 // Test StampSuperMac.
203 transaction->StampSuperMac(); 248 transaction->StampSuperMac();
204 } 249 }
205 250
206 // Verify that the store is now valid. 251 // Verify that the store is now valid.
207 { 252 {
208 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 253 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
209 std::unique_ptr<PrefHashStoreTransaction> transaction( 254 std::unique_ptr<PrefHashStoreTransaction> transaction(
210 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 255 pref_hash_store.BeginTransaction(GetHashStoreContents()));
211 ASSERT_TRUE(transaction->IsSuperMACValid()); 256 ASSERT_TRUE(transaction->IsSuperMACValid());
212 257
213 // Store the hash of a different value to test an "over-import". 258 // Store the hash of a different value to test an "over-import".
214 transaction->StoreHash("path1", &string_2); 259 transaction->StoreHash("path1", &string_2);
215 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 260 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
216 transaction->CheckValue("path1", &string_1)); 261 transaction->CheckValue("path1", &string_1));
217 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 262 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
218 transaction->CheckValue("path1", &string_2)); 263 transaction->CheckValue("path1", &string_2));
219 } 264 }
220 265
221 { 266 {
222 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 267 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
223 std::unique_ptr<PrefHashStoreTransaction> transaction( 268 std::unique_ptr<PrefHashStoreTransaction> transaction(
224 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 269 pref_hash_store.BeginTransaction(GetHashStoreContents()));
225 ASSERT_TRUE(transaction->IsSuperMACValid()); 270 ASSERT_TRUE(transaction->IsSuperMACValid());
226 271
227 // "Over-import". An import should preserve validity. 272 // "Over-import". An import should preserve validity.
228 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); 273 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 274 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
230 transaction->CheckValue("path1", &string_1)); 275 transaction->CheckValue("path1", &string_1));
231 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 276 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
232 transaction->CheckValue("path1", &string_2)); 277 transaction->CheckValue("path1", &string_2));
233 } 278 }
234 279
235 // Verify that validity was preserved and the "over-import" took effect. 280 // Verify that validity was preserved and the "over-import" took effect.
236 { 281 {
237 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 282 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
238 std::unique_ptr<PrefHashStoreTransaction> transaction( 283 std::unique_ptr<PrefHashStoreTransaction> transaction(
239 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 284 pref_hash_store.BeginTransaction(GetHashStoreContents()));
240 ASSERT_TRUE(transaction->IsSuperMACValid()); 285 ASSERT_TRUE(transaction->IsSuperMACValid());
241 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 286 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
242 transaction->CheckValue("path1", &string_1)); 287 transaction->CheckValue("path1", &string_1));
243 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 288 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
244 transaction->CheckValue("path1", &string_2)); 289 transaction->CheckValue("path1", &string_2));
245 } 290 }
246 } 291 }
247 292
248 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { 293 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
249 base::StringValue string_1("string1"); 294 base::StringValue string_1("string1");
250 base::StringValue string_2("string2"); 295 base::StringValue string_2("string2");
251 296
252 { 297 {
253 // Pass |use_super_mac| => false. 298 // Pass |use_super_mac| => false.
254 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); 299 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false);
255 std::unique_ptr<PrefHashStoreTransaction> transaction( 300 std::unique_ptr<PrefHashStoreTransaction> transaction(
256 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 301 pref_hash_store.BeginTransaction(GetHashStoreContents()));
257 302
258 transaction->StoreHash("path1", &string_2); 303 transaction->StoreHash("path1", &string_2);
259 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 304 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
260 transaction->CheckValue("path1", &string_2)); 305 transaction->CheckValue("path1", &string_2));
261 } 306 }
262 307
263 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); 308 ASSERT_TRUE(GetHashStoreContents()->GetSuperMac().empty());
264 309
265 { 310 {
266 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); 311 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false);
267 std::unique_ptr<PrefHashStoreTransaction> transaction( 312 std::unique_ptr<PrefHashStoreTransaction> transaction(
268 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 313 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
269 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 314 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
270 transaction->CheckValue("new_path", &string_1)); 315 transaction->CheckValue("new_path", &string_1));
271 } 316 }
272 } 317 }
273 318
274 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { 319 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
275 base::DictionaryValue dict; 320 base::DictionaryValue dict;
276 dict.Set("a", new base::StringValue("to be replaced")); 321 dict.Set("a", new base::StringValue("to be replaced"));
277 dict.Set("b", new base::StringValue("same")); 322 dict.Set("b", new base::StringValue("same"));
278 dict.Set("o", new base::StringValue("old")); 323 dict.Set("o", new base::StringValue("old"));
279 324
280 base::DictionaryValue modified_dict; 325 base::DictionaryValue modified_dict;
281 modified_dict.Set("a", new base::StringValue("replaced")); 326 modified_dict.Set("a", new base::StringValue("replaced"));
282 modified_dict.Set("b", new base::StringValue("same")); 327 modified_dict.Set("b", new base::StringValue("same"));
283 modified_dict.Set("c", new base::StringValue("new")); 328 modified_dict.Set("c", new base::StringValue("new"));
284 329
285 base::DictionaryValue empty_dict; 330 base::DictionaryValue empty_dict;
286 331
287 std::vector<std::string> invalid_keys; 332 std::vector<std::string> invalid_keys;
288 333
289 { 334 {
290 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 335 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
291 std::unique_ptr<PrefHashStoreTransaction> transaction( 336 std::unique_ptr<PrefHashStoreTransaction> transaction(
292 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 337 pref_hash_store.BeginTransaction(GetHashStoreContents()));
293 338
294 // No hashes stored yet and hashes dictionary is empty (and thus not 339 // No hashes stored yet and hashes dictionary is empty (and thus not
295 // trusted). 340 // trusted).
296 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 341 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
297 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 342 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
298 EXPECT_TRUE(invalid_keys.empty()); 343 EXPECT_TRUE(invalid_keys.empty());
299 344
300 transaction->StoreSplitHash("path1", &dict); 345 transaction->StoreSplitHash("path1", &dict);
301 346
302 // Verify match post storage. 347 // Verify match post storage.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 expected_invalid_keys2.push_back("c"); 397 expected_invalid_keys2.push_back("c");
353 EXPECT_EQ(expected_invalid_keys2, invalid_keys); 398 EXPECT_EQ(expected_invalid_keys2, invalid_keys);
354 invalid_keys.clear(); 399 invalid_keys.clear();
355 } 400 }
356 401
357 { 402 {
358 // |pref_hash_store2| should trust its initial hashes dictionary and thus 403 // |pref_hash_store2| should trust its initial hashes dictionary and thus
359 // trust new unknown values. 404 // trust new unknown values.
360 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 405 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
361 std::unique_ptr<PrefHashStoreTransaction> transaction( 406 std::unique_ptr<PrefHashStoreTransaction> transaction(
362 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 407 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
363 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 408 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
364 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 409 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
365 EXPECT_TRUE(invalid_keys.empty()); 410 EXPECT_TRUE(invalid_keys.empty());
366 } 411 }
367 412
368 // Manually corrupt the super MAC. 413 // Manually corrupt the super MAC.
369 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); 414 GetHashStoreContents()->SetSuperMac(std::string(64, 'A'));
370 415
371 { 416 {
372 // |pref_hash_store3| should no longer trust its initial hashes dictionary 417 // |pref_hash_store3| should no longer trust its initial hashes dictionary
373 // and thus shouldn't trust unknown values. 418 // and thus shouldn't trust unknown values.
374 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); 419 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
375 std::unique_ptr<PrefHashStoreTransaction> transaction( 420 std::unique_ptr<PrefHashStoreTransaction> transaction(
376 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); 421 pref_hash_store3.BeginTransaction(GetHashStoreContents()));
377 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 422 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
378 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 423 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
379 EXPECT_TRUE(invalid_keys.empty()); 424 EXPECT_TRUE(invalid_keys.empty());
380 } 425 }
381 } 426 }
382 427
383 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { 428 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
384 base::DictionaryValue empty_dict; 429 base::DictionaryValue empty_dict;
385 430
386 std::vector<std::string> invalid_keys; 431 std::vector<std::string> invalid_keys;
387 432
388 { 433 {
389 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 434 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
390 std::unique_ptr<PrefHashStoreTransaction> transaction( 435 std::unique_ptr<PrefHashStoreTransaction> transaction(
391 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 436 pref_hash_store.BeginTransaction(GetHashStoreContents()));
392 437
393 // Store hashes for a random dict to be overwritten below. 438 // Store hashes for a random dict to be overwritten below.
394 base::DictionaryValue initial_dict; 439 base::DictionaryValue initial_dict;
395 initial_dict.Set("a", new base::StringValue("foo")); 440 initial_dict.Set("a", new base::StringValue("foo"));
396 transaction->StoreSplitHash("path1", &initial_dict); 441 transaction->StoreSplitHash("path1", &initial_dict);
397 442
398 // Verify stored empty dictionary matches NULL and empty dictionary back. 443 // Verify stored empty dictionary matches NULL and empty dictionary back.
399 transaction->StoreSplitHash("path1", &empty_dict); 444 transaction->StoreSplitHash("path1", &empty_dict);
400 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 445 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
401 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); 446 transaction->CheckSplitValue("path1", NULL, &invalid_keys));
(...skipping 15 matching lines...) Expand all
417 } 462 }
418 463
419 { 464 {
420 // |pref_hash_store2| should trust its initial hashes dictionary (and thus 465 // |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 466 // 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 467 // 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 468 // test ensuring that the internal action of clearing some hashes does
424 // update the stored hash of hashes). 469 // update the stored hash of hashes).
425 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 470 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
426 std::unique_ptr<PrefHashStoreTransaction> transaction( 471 std::unique_ptr<PrefHashStoreTransaction> transaction(
427 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 472 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
428 473
429 base::DictionaryValue tested_dict; 474 base::DictionaryValue tested_dict;
430 tested_dict.Set("a", new base::StringValue("foo")); 475 tested_dict.Set("a", new base::StringValue("foo"));
431 tested_dict.Set("b", new base::StringValue("bar")); 476 tested_dict.Set("b", new base::StringValue("bar"));
432 EXPECT_EQ( 477 EXPECT_EQ(
433 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 478 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
434 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); 479 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys));
435 EXPECT_TRUE(invalid_keys.empty()); 480 EXPECT_TRUE(invalid_keys.empty());
436 } 481 }
437 } 482 }
438 483
439 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for 484 // 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 485 // 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 486 // stored. There is no point providing a migration path for preferences
442 // switching strategies after their initial release as split preferences are 487 // switching strategies after their initial release as split preferences are
443 // turned into split preferences specifically because the atomic hash isn't 488 // turned into split preferences specifically because the atomic hash isn't
444 // considered useful. 489 // considered useful.
445 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { 490 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
446 base::StringValue string("string1"); 491 base::StringValue string("string1");
447 492
448 base::DictionaryValue dict; 493 base::DictionaryValue dict;
449 dict.Set("a", new base::StringValue("foo")); 494 dict.Set("a", new base::StringValue("foo"));
450 dict.Set("d", new base::StringValue("bad")); 495 dict.Set("d", new base::StringValue("bad"));
451 dict.Set("b", new base::StringValue("bar")); 496 dict.Set("b", new base::StringValue("bar"));
452 dict.Set("c", new base::StringValue("baz")); 497 dict.Set("c", new base::StringValue("baz"));
453 498
454 { 499 {
455 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 500 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
456 std::unique_ptr<PrefHashStoreTransaction> transaction( 501 std::unique_ptr<PrefHashStoreTransaction> transaction(
457 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 502 pref_hash_store.BeginTransaction(GetHashStoreContents()));
458 503
459 transaction->StoreHash("path1", &string); 504 transaction->StoreHash("path1", &string);
460 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 505 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
461 transaction->CheckValue("path1", &string)); 506 transaction->CheckValue("path1", &string));
462 } 507 }
463 508
464 { 509 {
465 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. 510 // 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); 511 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
467 std::unique_ptr<PrefHashStoreTransaction> transaction( 512 std::unique_ptr<PrefHashStoreTransaction> transaction(
468 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 513 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
469 std::vector<std::string> invalid_keys; 514 std::vector<std::string> invalid_keys;
470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 515 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
471 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 516 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
472 EXPECT_TRUE(invalid_keys.empty()); 517 EXPECT_TRUE(invalid_keys.empty());
473 } 518 }
474 } 519 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698