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/macros.h" | 9 #include "base/macros.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 11 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
12 #include "chrome/browser/prefs/pref_hash_store_transaction.h" | 12 #include "chrome/browser/prefs/pref_hash_store_transaction.h" |
13 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h" | |
13 #include "chrome/browser/prefs/tracked/hash_store_contents.h" | 14 #include "chrome/browser/prefs/tracked/hash_store_contents.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 class MockHashStoreContents : public HashStoreContents { | |
17 public: | |
18 // Keep the data separate from the API implementation so that it can be owned | |
19 // by the test and reused. The API implementation is owned by the | |
20 // PrefHashStoreImpl. | |
21 struct Data { | |
22 Data() : commit_performed(false) {} | |
23 | |
24 // Returns the current value of |commit_performed| and resets it to false | |
25 // immediately after. | |
26 bool GetCommitPerformedAndReset() { | |
27 bool current_commit_performed = commit_performed; | |
28 commit_performed = false; | |
29 return current_commit_performed; | |
30 } | |
31 | |
32 scoped_ptr<base::DictionaryValue> contents; | |
33 std::string super_mac; | |
34 bool commit_performed; | |
35 }; | |
36 | |
37 explicit MockHashStoreContents(Data* data) : data_(data) {} | |
38 | |
39 // HashStoreContents implementation | |
40 virtual std::string hash_store_id() const OVERRIDE { return "store_id"; } | |
41 | |
42 virtual void Reset() OVERRIDE { | |
43 data_->contents.reset(); | |
44 data_->super_mac = ""; | |
45 } | |
46 | |
47 virtual bool IsInitialized() const OVERRIDE { return data_->contents; } | |
48 | |
49 virtual const base::DictionaryValue* GetContents() const OVERRIDE { | |
50 return data_->contents.get(); | |
51 } | |
52 | |
53 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | |
54 return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_)); | |
55 } | |
56 | |
57 virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; } | |
58 | |
59 virtual void SetSuperMac(const std::string& super_mac) OVERRIDE { | |
60 data_->super_mac = super_mac; | |
61 } | |
62 | |
63 virtual void CommitPendingWrite() OVERRIDE { | |
64 EXPECT_FALSE(data_->commit_performed); | |
65 data_->commit_performed = true; | |
66 } | |
67 | |
68 private: | |
69 class MockMutableDictionary : public MutableDictionary { | |
70 public: | |
71 explicit MockMutableDictionary(Data* data) : data_(data) {} | |
72 | |
73 // MutableDictionary implementation | |
74 virtual base::DictionaryValue* operator->() OVERRIDE { | |
75 if (!data_->contents) | |
76 data_->contents.reset(new base::DictionaryValue); | |
77 return data_->contents.get(); | |
78 } | |
79 | |
80 private: | |
81 Data* data_; | |
82 DISALLOW_COPY_AND_ASSIGN(MockMutableDictionary); | |
83 }; | |
84 | |
85 Data* data_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(MockHashStoreContents); | |
88 }; | |
89 | |
90 class PrefHashStoreImplTest : public testing::Test { | 17 class PrefHashStoreImplTest : public testing::Test { |
91 protected: | 18 protected: |
92 scoped_ptr<HashStoreContents> CreateHashStoreContents() { | 19 scoped_ptr<HashStoreContents> CreateHashStoreContents() { |
93 return scoped_ptr<HashStoreContents>( | 20 return scoped_ptr<HashStoreContents>( |
94 new MockHashStoreContents(&hash_store_data_)); | 21 new DictionaryHashStoreContents(&pref_store_contents_)); |
95 } | 22 } |
96 | 23 |
97 MockHashStoreContents::Data hash_store_data_; | 24 private: |
25 base::DictionaryValue pref_store_contents_; | |
98 }; | 26 }; |
99 | 27 |
100 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { | 28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
101 base::StringValue string_1("string1"); | 29 base::StringValue string_1("string1"); |
102 base::StringValue string_2("string2"); | 30 base::StringValue string_2("string2"); |
103 | 31 |
104 { | 32 { |
105 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 33 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
106 PrefHashStoreImpl pref_hash_store( | 34 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
107 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
108 scoped_ptr<PrefHashStoreTransaction> transaction( | 35 scoped_ptr<PrefHashStoreTransaction> transaction( |
109 pref_hash_store.BeginTransaction()); | 36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
110 | 37 |
111 // Only NULL should be trusted in the absence of a hash. | 38 // Only NULL should be trusted in the absence of a hash. |
112 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
113 transaction->CheckValue("path1", &string_1)); | 40 transaction->CheckValue("path1", &string_1)); |
114 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
115 transaction->CheckValue("path1", NULL)); | 42 transaction->CheckValue("path1", NULL)); |
116 | 43 |
117 transaction->StoreHash("path1", &string_1); | 44 transaction->StoreHash("path1", &string_1); |
118 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
119 transaction->CheckValue("path1", &string_1)); | 46 transaction->CheckValue("path1", &string_1)); |
(...skipping 17 matching lines...) Expand all Loading... | |
137 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); | 64 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"); |
138 | 65 |
139 EXPECT_EQ(PrefHashStoreTransaction::WEAK_LEGACY, | 66 EXPECT_EQ(PrefHashStoreTransaction::WEAK_LEGACY, |
140 transaction->CheckValue("path1", &dict)); | 67 transaction->CheckValue("path1", &dict)); |
141 transaction->StoreHash("path1", &dict); | 68 transaction->StoreHash("path1", &dict); |
142 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 69 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
143 transaction->CheckValue("path1", &dict)); | 70 transaction->CheckValue("path1", &dict)); |
144 | 71 |
145 // Test that the |pref_hash_store| flushes its changes on request post | 72 // Test that the |pref_hash_store| flushes its changes on request post |
146 // transaction. | 73 // transaction. |
147 transaction.reset(); | 74 transaction.reset(); |
gab
2014/06/17 02:00:05
Remove the last 3 lines as well (the reason to res
erikwright (departed)
2014/06/17 19:07:23
Done.
| |
148 pref_hash_store.CommitPendingWrite(); | |
149 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
150 } | 75 } |
151 | 76 |
152 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); | 77 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); |
153 | 78 |
154 { | 79 { |
155 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 80 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
156 // trust new unknown values. | 81 // trust new unknown values. |
157 PrefHashStoreImpl pref_hash_store2( | 82 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
158 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
159 scoped_ptr<PrefHashStoreTransaction> transaction( | 83 scoped_ptr<PrefHashStoreTransaction> transaction( |
160 pref_hash_store2.BeginTransaction()); | 84 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 85 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
162 transaction->CheckValue("new_path", &string_1)); | 86 transaction->CheckValue("new_path", &string_1)); |
163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 87 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
164 transaction->CheckValue("new_path", &string_2)); | 88 transaction->CheckValue("new_path", &string_2)); |
165 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 89 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
166 transaction->CheckValue("new_path", NULL)); | 90 transaction->CheckValue("new_path", NULL)); |
167 | 91 |
168 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 92 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
169 // didn't change. | 93 // didn't change. |
170 transaction.reset(); | 94 transaction.reset(); |
171 pref_hash_store2.CommitPendingWrite(); | |
172 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
173 } | 95 } |
174 | 96 |
175 // Manually corrupt the super MAC. | 97 // Manually corrupt the super MAC. |
176 hash_store_data_.super_mac = std::string(64, 'A'); | 98 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
177 | 99 |
178 { | 100 { |
179 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 101 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
180 // and thus shouldn't trust non-NULL unknown values. | 102 // and thus shouldn't trust non-NULL unknown values. |
181 PrefHashStoreImpl pref_hash_store3( | 103 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
182 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
183 scoped_ptr<PrefHashStoreTransaction> transaction( | 104 scoped_ptr<PrefHashStoreTransaction> transaction( |
184 pref_hash_store3.BeginTransaction()); | 105 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 106 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
186 transaction->CheckValue("new_path", &string_1)); | 107 transaction->CheckValue("new_path", &string_1)); |
187 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 108 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
188 transaction->CheckValue("new_path", &string_2)); | 109 transaction->CheckValue("new_path", &string_2)); |
189 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 110 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
190 transaction->CheckValue("new_path", NULL)); | 111 transaction->CheckValue("new_path", NULL)); |
191 | 112 |
192 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 113 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
193 // didn't change. | 114 // didn't change. |
194 transaction.reset(); | 115 transaction.reset(); |
195 pref_hash_store3.CommitPendingWrite(); | |
196 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
197 } | 116 } |
198 } | 117 } |
199 | 118 |
200 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { | 119 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { |
201 base::StringValue string_1("string1"); | 120 base::StringValue string_1("string1"); |
202 base::StringValue string_2("string2"); | 121 base::StringValue string_2("string2"); |
203 | 122 |
204 { | 123 { |
205 // Pass |use_super_mac| => false. | 124 // Pass |use_super_mac| => false. |
206 PrefHashStoreImpl pref_hash_store( | 125 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); |
207 std::string(32, 0), "device_id", CreateHashStoreContents(), false); | |
208 scoped_ptr<PrefHashStoreTransaction> transaction( | 126 scoped_ptr<PrefHashStoreTransaction> transaction( |
209 pref_hash_store.BeginTransaction()); | 127 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
210 | 128 |
211 transaction->StoreHash("path1", &string_2); | 129 transaction->StoreHash("path1", &string_2); |
212 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 130 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
213 transaction->CheckValue("path1", &string_2)); | 131 transaction->CheckValue("path1", &string_2)); |
214 } | 132 } |
215 | 133 |
216 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); | 134 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); |
217 | 135 |
218 { | 136 { |
219 PrefHashStoreImpl pref_hash_store2( | 137 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); |
220 std::string(32, 0), "device_id", CreateHashStoreContents(), false); | |
221 scoped_ptr<PrefHashStoreTransaction> transaction( | 138 scoped_ptr<PrefHashStoreTransaction> transaction( |
222 pref_hash_store2.BeginTransaction()); | 139 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
223 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 140 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
224 transaction->CheckValue("new_path", &string_1)); | 141 transaction->CheckValue("new_path", &string_1)); |
225 } | 142 } |
226 } | 143 } |
227 | 144 |
228 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { | 145 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
229 base::DictionaryValue dict; | 146 base::DictionaryValue dict; |
230 dict.Set("a", new base::StringValue("to be replaced")); | 147 dict.Set("a", new base::StringValue("to be replaced")); |
231 dict.Set("b", new base::StringValue("same")); | 148 dict.Set("b", new base::StringValue("same")); |
232 dict.Set("o", new base::StringValue("old")); | 149 dict.Set("o", new base::StringValue("old")); |
233 | 150 |
234 base::DictionaryValue modified_dict; | 151 base::DictionaryValue modified_dict; |
235 modified_dict.Set("a", new base::StringValue("replaced")); | 152 modified_dict.Set("a", new base::StringValue("replaced")); |
236 modified_dict.Set("b", new base::StringValue("same")); | 153 modified_dict.Set("b", new base::StringValue("same")); |
237 modified_dict.Set("c", new base::StringValue("new")); | 154 modified_dict.Set("c", new base::StringValue("new")); |
238 | 155 |
239 base::DictionaryValue empty_dict; | 156 base::DictionaryValue empty_dict; |
240 | 157 |
241 std::vector<std::string> invalid_keys; | 158 std::vector<std::string> invalid_keys; |
242 | 159 |
243 { | 160 { |
244 PrefHashStoreImpl pref_hash_store( | 161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
245 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
246 scoped_ptr<PrefHashStoreTransaction> transaction( | 162 scoped_ptr<PrefHashStoreTransaction> transaction( |
247 pref_hash_store.BeginTransaction()); | 163 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
248 | 164 |
249 // No hashes stored yet and hashes dictionary is empty (and thus not | 165 // No hashes stored yet and hashes dictionary is empty (and thus not |
250 // trusted). | 166 // trusted). |
251 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 167 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
252 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 168 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
253 EXPECT_TRUE(invalid_keys.empty()); | 169 EXPECT_TRUE(invalid_keys.empty()); |
254 | 170 |
255 transaction->StoreSplitHash("path1", &dict); | 171 transaction->StoreSplitHash("path1", &dict); |
256 | 172 |
257 // Verify match post storage. | 173 // Verify match post storage. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 219 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
304 std::vector<std::string> expected_invalid_keys2; | 220 std::vector<std::string> expected_invalid_keys2; |
305 expected_invalid_keys2.push_back("a"); | 221 expected_invalid_keys2.push_back("a"); |
306 expected_invalid_keys2.push_back("o"); | 222 expected_invalid_keys2.push_back("o"); |
307 expected_invalid_keys2.push_back("c"); | 223 expected_invalid_keys2.push_back("c"); |
308 EXPECT_EQ(expected_invalid_keys2, invalid_keys); | 224 EXPECT_EQ(expected_invalid_keys2, invalid_keys); |
309 invalid_keys.clear(); | 225 invalid_keys.clear(); |
310 | 226 |
311 // Test that the |pref_hash_store| flushes its changes on request. | 227 // Test that the |pref_hash_store| flushes its changes on request. |
312 transaction.reset(); | 228 transaction.reset(); |
313 pref_hash_store.CommitPendingWrite(); | |
314 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
315 } | 229 } |
316 | 230 |
317 { | 231 { |
318 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 232 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
319 // trust new unknown values. | 233 // trust new unknown values. |
320 PrefHashStoreImpl pref_hash_store2( | 234 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
321 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
322 scoped_ptr<PrefHashStoreTransaction> transaction( | 235 scoped_ptr<PrefHashStoreTransaction> transaction( |
323 pref_hash_store2.BeginTransaction()); | 236 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
324 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 237 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
325 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 238 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
326 EXPECT_TRUE(invalid_keys.empty()); | 239 EXPECT_TRUE(invalid_keys.empty()); |
327 | 240 |
328 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 241 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
329 // didn't change. | 242 // didn't change. |
330 transaction.reset(); | 243 transaction.reset(); |
331 pref_hash_store2.CommitPendingWrite(); | |
332 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
333 } | 244 } |
334 | 245 |
335 // Manually corrupt the super MAC. | 246 // Manually corrupt the super MAC. |
336 hash_store_data_.super_mac = std::string(64, 'A'); | 247 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); |
337 | 248 |
338 { | 249 { |
339 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 250 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
340 // and thus shouldn't trust unknown values. | 251 // and thus shouldn't trust unknown values. |
341 PrefHashStoreImpl pref_hash_store3( | 252 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
342 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
343 scoped_ptr<PrefHashStoreTransaction> transaction( | 253 scoped_ptr<PrefHashStoreTransaction> transaction( |
344 pref_hash_store3.BeginTransaction()); | 254 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); |
345 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 255 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
346 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 256 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
347 EXPECT_TRUE(invalid_keys.empty()); | 257 EXPECT_TRUE(invalid_keys.empty()); |
348 | 258 |
349 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 259 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
350 // didn't change. | 260 // didn't change. |
351 transaction.reset(); | 261 transaction.reset(); |
352 pref_hash_store3.CommitPendingWrite(); | |
353 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
354 } | 262 } |
355 } | 263 } |
356 | 264 |
357 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { | 265 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
358 base::DictionaryValue empty_dict; | 266 base::DictionaryValue empty_dict; |
359 | 267 |
360 std::vector<std::string> invalid_keys; | 268 std::vector<std::string> invalid_keys; |
361 | 269 |
362 { | 270 { |
363 PrefHashStoreImpl pref_hash_store( | 271 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
364 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
365 scoped_ptr<PrefHashStoreTransaction> transaction( | 272 scoped_ptr<PrefHashStoreTransaction> transaction( |
366 pref_hash_store.BeginTransaction()); | 273 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
367 | 274 |
368 // Store hashes for a random dict to be overwritten below. | 275 // Store hashes for a random dict to be overwritten below. |
369 base::DictionaryValue initial_dict; | 276 base::DictionaryValue initial_dict; |
370 initial_dict.Set("a", new base::StringValue("foo")); | 277 initial_dict.Set("a", new base::StringValue("foo")); |
371 transaction->StoreSplitHash("path1", &initial_dict); | 278 transaction->StoreSplitHash("path1", &initial_dict); |
372 | 279 |
373 // Verify stored empty dictionary matches NULL and empty dictionary back. | 280 // Verify stored empty dictionary matches NULL and empty dictionary back. |
374 transaction->StoreSplitHash("path1", &empty_dict); | 281 transaction->StoreSplitHash("path1", &empty_dict); |
375 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 282 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
376 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); | 283 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); |
(...skipping 13 matching lines...) Expand all Loading... | |
390 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys)); | 297 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys)); |
391 EXPECT_TRUE(invalid_keys.empty()); | 298 EXPECT_TRUE(invalid_keys.empty()); |
392 } | 299 } |
393 | 300 |
394 { | 301 { |
395 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 302 // |pref_hash_store2| should trust its initial hashes dictionary (and thus |
396 // trust new unknown values) even though the last action done was to clear | 303 // trust new unknown values) even though the last action done was to clear |
397 // the hashes for path1 by setting its value to NULL (this is a regression | 304 // the hashes for path1 by setting its value to NULL (this is a regression |
398 // test ensuring that the internal action of clearing some hashes does | 305 // test ensuring that the internal action of clearing some hashes does |
399 // update the stored hash of hashes). | 306 // update the stored hash of hashes). |
400 PrefHashStoreImpl pref_hash_store2( | 307 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
401 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
402 scoped_ptr<PrefHashStoreTransaction> transaction( | 308 scoped_ptr<PrefHashStoreTransaction> transaction( |
403 pref_hash_store2.BeginTransaction()); | 309 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
404 | 310 |
405 base::DictionaryValue tested_dict; | 311 base::DictionaryValue tested_dict; |
406 tested_dict.Set("a", new base::StringValue("foo")); | 312 tested_dict.Set("a", new base::StringValue("foo")); |
407 tested_dict.Set("b", new base::StringValue("bar")); | 313 tested_dict.Set("b", new base::StringValue("bar")); |
408 EXPECT_EQ( | 314 EXPECT_EQ( |
409 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 315 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
410 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 316 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
411 EXPECT_TRUE(invalid_keys.empty()); | 317 EXPECT_TRUE(invalid_keys.empty()); |
412 } | 318 } |
413 } | 319 } |
414 | 320 |
415 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for | 321 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for |
416 // a split preference even if there is an existing atomic preference's hash | 322 // a split preference even if there is an existing atomic preference's hash |
417 // stored. There is no point providing a migration path for preferences | 323 // stored. There is no point providing a migration path for preferences |
418 // switching strategies after their initial release as split preferences are | 324 // switching strategies after their initial release as split preferences are |
419 // turned into split preferences specifically because the atomic hash isn't | 325 // turned into split preferences specifically because the atomic hash isn't |
420 // considered useful. | 326 // considered useful. |
421 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { | 327 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { |
422 base::StringValue string("string1"); | 328 base::StringValue string("string1"); |
423 | 329 |
424 base::DictionaryValue dict; | 330 base::DictionaryValue dict; |
425 dict.Set("a", new base::StringValue("foo")); | 331 dict.Set("a", new base::StringValue("foo")); |
426 dict.Set("d", new base::StringValue("bad")); | 332 dict.Set("d", new base::StringValue("bad")); |
427 dict.Set("b", new base::StringValue("bar")); | 333 dict.Set("b", new base::StringValue("bar")); |
428 dict.Set("c", new base::StringValue("baz")); | 334 dict.Set("c", new base::StringValue("baz")); |
429 | 335 |
430 { | 336 { |
431 PrefHashStoreImpl pref_hash_store( | 337 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
432 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
433 scoped_ptr<PrefHashStoreTransaction> transaction( | 338 scoped_ptr<PrefHashStoreTransaction> transaction( |
434 pref_hash_store.BeginTransaction()); | 339 pref_hash_store.BeginTransaction(CreateHashStoreContents())); |
435 | 340 |
436 transaction->StoreHash("path1", &string); | 341 transaction->StoreHash("path1", &string); |
437 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 342 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
438 transaction->CheckValue("path1", &string)); | 343 transaction->CheckValue("path1", &string)); |
439 } | 344 } |
440 | 345 |
441 { | 346 { |
442 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 347 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
443 PrefHashStoreImpl pref_hash_store2( | 348 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
444 std::string(32, 0), "device_id", CreateHashStoreContents(), true); | |
445 scoped_ptr<PrefHashStoreTransaction> transaction( | 349 scoped_ptr<PrefHashStoreTransaction> transaction( |
446 pref_hash_store2.BeginTransaction()); | 350 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); |
447 std::vector<std::string> invalid_keys; | 351 std::vector<std::string> invalid_keys; |
448 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 352 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
449 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 353 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
450 EXPECT_TRUE(invalid_keys.empty()); | 354 EXPECT_TRUE(invalid_keys.empty()); |
451 } | 355 } |
452 } | 356 } |
OLD | NEW |