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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // Returns the current value of |commit_performed| and resets it to false | 24 // Returns the current value of |commit_performed| and resets it to false |
25 // immediately after. | 25 // immediately after. |
26 bool GetCommitPerformedAndReset() { | 26 bool GetCommitPerformedAndReset() { |
27 bool current_commit_performed = commit_performed; | 27 bool current_commit_performed = commit_performed; |
28 commit_performed = false; | 28 commit_performed = false; |
29 return current_commit_performed; | 29 return current_commit_performed; |
30 } | 30 } |
31 | 31 |
32 scoped_ptr<base::DictionaryValue> contents; | 32 scoped_ptr<base::DictionaryValue> contents; |
33 std::string super_mac; | 33 std::string super_mac; |
34 scoped_ptr<int> version; | |
35 bool commit_performed; | 34 bool commit_performed; |
36 }; | 35 }; |
37 | 36 |
38 explicit MockHashStoreContents(Data* data) : data_(data) {} | 37 explicit MockHashStoreContents(Data* data) : data_(data) {} |
39 | 38 |
40 // HashStoreContents implementation | 39 // HashStoreContents implementation |
41 virtual std::string hash_store_id() const OVERRIDE { return "store_id"; } | 40 virtual std::string hash_store_id() const OVERRIDE { return "store_id"; } |
42 | 41 |
43 virtual void Reset() OVERRIDE { | 42 virtual void Reset() OVERRIDE { |
44 data_->contents.reset(); | 43 data_->contents.reset(); |
45 data_->super_mac = ""; | 44 data_->super_mac = ""; |
46 data_->version.reset(); | |
47 } | 45 } |
48 | 46 |
49 virtual bool IsInitialized() const OVERRIDE { return data_->contents; } | 47 virtual bool IsInitialized() const OVERRIDE { return data_->contents; } |
50 | 48 |
51 virtual bool GetVersion(int* version) const OVERRIDE { | |
52 if (data_->version) | |
53 *version = *data_->version; | |
54 return data_->version; | |
55 } | |
56 | |
57 virtual void SetVersion(int version) OVERRIDE { | |
58 data_->version.reset(new int(version)); | |
59 } | |
60 | |
61 virtual const base::DictionaryValue* GetContents() const OVERRIDE { | 49 virtual const base::DictionaryValue* GetContents() const OVERRIDE { |
62 return data_->contents.get(); | 50 return data_->contents.get(); |
63 } | 51 } |
64 | 52 |
65 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | 53 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { |
66 return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_)); | 54 return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_)); |
67 } | 55 } |
68 | 56 |
69 virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; } | 57 virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; } |
70 | 58 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 96 |
109 MockHashStoreContents::Data hash_store_data_; | 97 MockHashStoreContents::Data hash_store_data_; |
110 }; | 98 }; |
111 | 99 |
112 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { | 100 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
113 base::StringValue string_1("string1"); | 101 base::StringValue string_1("string1"); |
114 base::StringValue string_2("string2"); | 102 base::StringValue string_2("string2"); |
115 | 103 |
116 { | 104 { |
117 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 105 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
118 PrefHashStoreImpl pref_hash_store( | 106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
119 std::string(32, 0), "device_id", CreateHashStoreContents()); | 107 pref_hash_store.SetHashStoreContents(CreateHashStoreContents()); |
120 scoped_ptr<PrefHashStoreTransaction> transaction( | 108 scoped_ptr<PrefHashStoreTransaction> transaction( |
121 pref_hash_store.BeginTransaction()); | 109 pref_hash_store.BeginTransaction()); |
122 | 110 |
123 // Only NULL should be trusted in the absence of a hash. | 111 // Only NULL should be trusted in the absence of a hash. |
124 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 112 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
125 transaction->CheckValue("path1", &string_1)); | 113 transaction->CheckValue("path1", &string_1)); |
126 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 114 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
127 transaction->CheckValue("path1", NULL)); | 115 transaction->CheckValue("path1", NULL)); |
128 | 116 |
129 transaction->StoreHash("path1", &string_1); | 117 transaction->StoreHash("path1", &string_1); |
(...skipping 27 matching lines...) Expand all Loading... |
157 // Test that the |pref_hash_store| flushes its changes on request post | 145 // Test that the |pref_hash_store| flushes its changes on request post |
158 // transaction. | 146 // transaction. |
159 transaction.reset(); | 147 transaction.reset(); |
160 pref_hash_store.CommitPendingWrite(); | 148 pref_hash_store.CommitPendingWrite(); |
161 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | 149 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); |
162 } | 150 } |
163 | 151 |
164 { | 152 { |
165 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 153 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
166 // trust new unknown values. | 154 // trust new unknown values. |
167 PrefHashStoreImpl pref_hash_store2( | 155 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
168 std::string(32, 0), "device_id", CreateHashStoreContents()); | 156 pref_hash_store2.SetHashStoreContents(CreateHashStoreContents()); |
169 scoped_ptr<PrefHashStoreTransaction> transaction( | 157 scoped_ptr<PrefHashStoreTransaction> transaction( |
170 pref_hash_store2.BeginTransaction()); | 158 pref_hash_store2.BeginTransaction()); |
171 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 159 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
172 transaction->CheckValue("new_path", &string_1)); | 160 transaction->CheckValue("new_path", &string_1)); |
173 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
174 transaction->CheckValue("new_path", &string_2)); | 162 transaction->CheckValue("new_path", &string_2)); |
175 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
176 transaction->CheckValue("new_path", NULL)); | 164 transaction->CheckValue("new_path", NULL)); |
177 | 165 |
178 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 166 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
179 // didn't change. | 167 // didn't change. |
180 transaction.reset(); | 168 transaction.reset(); |
181 pref_hash_store2.CommitPendingWrite(); | 169 pref_hash_store2.CommitPendingWrite(); |
182 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 170 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
183 } | 171 } |
184 | 172 |
185 // Manually corrupt the super MAC. | 173 // Manually corrupt the super MAC. |
186 hash_store_data_.super_mac = std::string(64, 'A'); | 174 hash_store_data_.super_mac = std::string(64, 'A'); |
187 | 175 |
188 { | 176 { |
189 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 177 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
190 // and thus shouldn't trust non-NULL unknown values. | 178 // and thus shouldn't trust non-NULL unknown values. |
191 PrefHashStoreImpl pref_hash_store3( | 179 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
192 std::string(32, 0), "device_id", CreateHashStoreContents()); | 180 pref_hash_store3.SetHashStoreContents(CreateHashStoreContents()); |
193 scoped_ptr<PrefHashStoreTransaction> transaction( | 181 scoped_ptr<PrefHashStoreTransaction> transaction( |
194 pref_hash_store3.BeginTransaction()); | 182 pref_hash_store3.BeginTransaction()); |
195 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 183 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
196 transaction->CheckValue("new_path", &string_1)); | 184 transaction->CheckValue("new_path", &string_1)); |
197 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
198 transaction->CheckValue("new_path", &string_2)); | 186 transaction->CheckValue("new_path", &string_2)); |
199 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 187 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
200 transaction->CheckValue("new_path", NULL)); | 188 transaction->CheckValue("new_path", NULL)); |
201 | 189 |
202 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 190 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
(...skipping 13 matching lines...) Expand all Loading... |
216 base::DictionaryValue modified_dict; | 204 base::DictionaryValue modified_dict; |
217 modified_dict.Set("a", new base::StringValue("replaced")); | 205 modified_dict.Set("a", new base::StringValue("replaced")); |
218 modified_dict.Set("b", new base::StringValue("same")); | 206 modified_dict.Set("b", new base::StringValue("same")); |
219 modified_dict.Set("c", new base::StringValue("new")); | 207 modified_dict.Set("c", new base::StringValue("new")); |
220 | 208 |
221 base::DictionaryValue empty_dict; | 209 base::DictionaryValue empty_dict; |
222 | 210 |
223 std::vector<std::string> invalid_keys; | 211 std::vector<std::string> invalid_keys; |
224 | 212 |
225 { | 213 { |
226 PrefHashStoreImpl pref_hash_store( | 214 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
227 std::string(32, 0), "device_id", CreateHashStoreContents()); | 215 pref_hash_store.SetHashStoreContents(CreateHashStoreContents()); |
228 scoped_ptr<PrefHashStoreTransaction> transaction( | 216 scoped_ptr<PrefHashStoreTransaction> transaction( |
229 pref_hash_store.BeginTransaction()); | 217 pref_hash_store.BeginTransaction()); |
230 | 218 |
231 // No hashes stored yet and hashes dictionary is empty (and thus not | 219 // No hashes stored yet and hashes dictionary is empty (and thus not |
232 // trusted). | 220 // trusted). |
233 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 221 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
234 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 222 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
235 EXPECT_TRUE(invalid_keys.empty()); | 223 EXPECT_TRUE(invalid_keys.empty()); |
236 | 224 |
237 transaction->StoreSplitHash("path1", &dict); | 225 transaction->StoreSplitHash("path1", &dict); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 280 |
293 // Test that the |pref_hash_store| flushes its changes on request. | 281 // Test that the |pref_hash_store| flushes its changes on request. |
294 transaction.reset(); | 282 transaction.reset(); |
295 pref_hash_store.CommitPendingWrite(); | 283 pref_hash_store.CommitPendingWrite(); |
296 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | 284 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); |
297 } | 285 } |
298 | 286 |
299 { | 287 { |
300 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 288 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
301 // trust new unknown values. | 289 // trust new unknown values. |
302 PrefHashStoreImpl pref_hash_store2( | 290 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
303 std::string(32, 0), "device_id", CreateHashStoreContents()); | 291 pref_hash_store2.SetHashStoreContents(CreateHashStoreContents()); |
304 scoped_ptr<PrefHashStoreTransaction> transaction( | 292 scoped_ptr<PrefHashStoreTransaction> transaction( |
305 pref_hash_store2.BeginTransaction()); | 293 pref_hash_store2.BeginTransaction()); |
306 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 294 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
307 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 295 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
308 EXPECT_TRUE(invalid_keys.empty()); | 296 EXPECT_TRUE(invalid_keys.empty()); |
309 | 297 |
310 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 298 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
311 // didn't change. | 299 // didn't change. |
312 transaction.reset(); | 300 transaction.reset(); |
313 pref_hash_store2.CommitPendingWrite(); | 301 pref_hash_store2.CommitPendingWrite(); |
314 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 302 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
315 } | 303 } |
316 | 304 |
317 // Manually corrupt the super MAC. | 305 // Manually corrupt the super MAC. |
318 hash_store_data_.super_mac = std::string(64, 'A'); | 306 hash_store_data_.super_mac = std::string(64, 'A'); |
319 | 307 |
320 { | 308 { |
321 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 309 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
322 // and thus shouldn't trust unknown values. | 310 // and thus shouldn't trust unknown values. |
323 PrefHashStoreImpl pref_hash_store3( | 311 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); |
324 std::string(32, 0), "device_id", CreateHashStoreContents()); | 312 pref_hash_store3.SetHashStoreContents(CreateHashStoreContents()); |
325 scoped_ptr<PrefHashStoreTransaction> transaction( | 313 scoped_ptr<PrefHashStoreTransaction> transaction( |
326 pref_hash_store3.BeginTransaction()); | 314 pref_hash_store3.BeginTransaction()); |
327 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 315 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
328 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 316 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
329 EXPECT_TRUE(invalid_keys.empty()); | 317 EXPECT_TRUE(invalid_keys.empty()); |
330 | 318 |
331 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 319 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
332 // didn't change. | 320 // didn't change. |
333 transaction.reset(); | 321 transaction.reset(); |
334 pref_hash_store3.CommitPendingWrite(); | 322 pref_hash_store3.CommitPendingWrite(); |
335 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 323 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
336 } | 324 } |
337 } | 325 } |
338 | 326 |
339 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { | 327 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
340 base::DictionaryValue empty_dict; | 328 base::DictionaryValue empty_dict; |
341 | 329 |
342 std::vector<std::string> invalid_keys; | 330 std::vector<std::string> invalid_keys; |
343 | 331 |
344 { | 332 { |
345 PrefHashStoreImpl pref_hash_store( | 333 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
346 std::string(32, 0), "device_id", CreateHashStoreContents()); | 334 pref_hash_store.SetHashStoreContents(CreateHashStoreContents()); |
347 scoped_ptr<PrefHashStoreTransaction> transaction( | 335 scoped_ptr<PrefHashStoreTransaction> transaction( |
348 pref_hash_store.BeginTransaction()); | 336 pref_hash_store.BeginTransaction()); |
349 | 337 |
350 // Store hashes for a random dict to be overwritten below. | 338 // Store hashes for a random dict to be overwritten below. |
351 base::DictionaryValue initial_dict; | 339 base::DictionaryValue initial_dict; |
352 initial_dict.Set("a", new base::StringValue("foo")); | 340 initial_dict.Set("a", new base::StringValue("foo")); |
353 transaction->StoreSplitHash("path1", &initial_dict); | 341 transaction->StoreSplitHash("path1", &initial_dict); |
354 | 342 |
355 // Verify stored empty dictionary matches NULL and empty dictionary back. | 343 // Verify stored empty dictionary matches NULL and empty dictionary back. |
356 transaction->StoreSplitHash("path1", &empty_dict); | 344 transaction->StoreSplitHash("path1", &empty_dict); |
(...skipping 15 matching lines...) Expand all Loading... |
372 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys)); | 360 transaction->CheckSplitValue("path1", &empty_dict, &invalid_keys)); |
373 EXPECT_TRUE(invalid_keys.empty()); | 361 EXPECT_TRUE(invalid_keys.empty()); |
374 } | 362 } |
375 | 363 |
376 { | 364 { |
377 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 365 // |pref_hash_store2| should trust its initial hashes dictionary (and thus |
378 // trust new unknown values) even though the last action done was to clear | 366 // trust new unknown values) even though the last action done was to clear |
379 // the hashes for path1 by setting its value to NULL (this is a regression | 367 // the hashes for path1 by setting its value to NULL (this is a regression |
380 // test ensuring that the internal action of clearing some hashes does | 368 // test ensuring that the internal action of clearing some hashes does |
381 // update the stored hash of hashes). | 369 // update the stored hash of hashes). |
382 PrefHashStoreImpl pref_hash_store2( | 370 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
383 std::string(32, 0), "device_id", CreateHashStoreContents()); | 371 pref_hash_store2.SetHashStoreContents(CreateHashStoreContents()); |
384 scoped_ptr<PrefHashStoreTransaction> transaction( | 372 scoped_ptr<PrefHashStoreTransaction> transaction( |
385 pref_hash_store2.BeginTransaction()); | 373 pref_hash_store2.BeginTransaction()); |
386 | 374 |
387 base::DictionaryValue tested_dict; | 375 base::DictionaryValue tested_dict; |
388 tested_dict.Set("a", new base::StringValue("foo")); | 376 tested_dict.Set("a", new base::StringValue("foo")); |
389 tested_dict.Set("b", new base::StringValue("bar")); | 377 tested_dict.Set("b", new base::StringValue("bar")); |
390 EXPECT_EQ( | 378 EXPECT_EQ( |
391 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 379 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
392 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 380 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
393 EXPECT_TRUE(invalid_keys.empty()); | 381 EXPECT_TRUE(invalid_keys.empty()); |
394 } | 382 } |
395 } | 383 } |
396 | 384 |
397 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for | 385 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for |
398 // a split preference even if there is an existing atomic preference's hash | 386 // a split preference even if there is an existing atomic preference's hash |
399 // stored. There is no point providing a migration path for preferences | 387 // stored. There is no point providing a migration path for preferences |
400 // switching strategies after their initial release as split preferences are | 388 // switching strategies after their initial release as split preferences are |
401 // turned into split preferences specifically because the atomic hash isn't | 389 // turned into split preferences specifically because the atomic hash isn't |
402 // considered useful. | 390 // considered useful. |
403 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { | 391 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { |
404 base::StringValue string("string1"); | 392 base::StringValue string("string1"); |
405 | 393 |
406 base::DictionaryValue dict; | 394 base::DictionaryValue dict; |
407 dict.Set("a", new base::StringValue("foo")); | 395 dict.Set("a", new base::StringValue("foo")); |
408 dict.Set("d", new base::StringValue("bad")); | 396 dict.Set("d", new base::StringValue("bad")); |
409 dict.Set("b", new base::StringValue("bar")); | 397 dict.Set("b", new base::StringValue("bar")); |
410 dict.Set("c", new base::StringValue("baz")); | 398 dict.Set("c", new base::StringValue("baz")); |
411 | 399 |
412 { | 400 { |
413 PrefHashStoreImpl pref_hash_store( | 401 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); |
414 std::string(32, 0), "device_id", CreateHashStoreContents()); | 402 pref_hash_store.SetHashStoreContents(CreateHashStoreContents()); |
415 scoped_ptr<PrefHashStoreTransaction> transaction( | 403 scoped_ptr<PrefHashStoreTransaction> transaction( |
416 pref_hash_store.BeginTransaction()); | 404 pref_hash_store.BeginTransaction()); |
417 | 405 |
418 transaction->StoreHash("path1", &string); | 406 transaction->StoreHash("path1", &string); |
419 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 407 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
420 transaction->CheckValue("path1", &string)); | 408 transaction->CheckValue("path1", &string)); |
421 } | 409 } |
422 | 410 |
423 { | 411 { |
424 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 412 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
425 PrefHashStoreImpl pref_hash_store2( | 413 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); |
426 std::string(32, 0), "device_id", CreateHashStoreContents()); | 414 pref_hash_store2.SetHashStoreContents(CreateHashStoreContents()); |
427 scoped_ptr<PrefHashStoreTransaction> transaction( | 415 scoped_ptr<PrefHashStoreTransaction> transaction( |
428 pref_hash_store2.BeginTransaction()); | 416 pref_hash_store2.BeginTransaction()); |
429 std::vector<std::string> invalid_keys; | 417 std::vector<std::string> invalid_keys; |
430 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 418 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
431 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 419 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
432 EXPECT_TRUE(invalid_keys.empty()); | 420 EXPECT_TRUE(invalid_keys.empty()); |
433 } | 421 } |
434 } | 422 } |
435 | |
436 TEST_F(PrefHashStoreImplTest, GetCurrentVersion) { | |
437 COMPILE_ASSERT(PrefHashStoreImpl::VERSION_LATEST == 2, | |
438 new_versions_should_be_tested_here); | |
439 { | |
440 PrefHashStoreImpl pref_hash_store( | |
441 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
442 | |
443 // VERSION_UNINITIALIZED when no hashes are stored. | |
444 EXPECT_EQ(PrefHashStoreImpl::VERSION_UNINITIALIZED, | |
445 pref_hash_store.GetCurrentVersion()); | |
446 | |
447 scoped_ptr<PrefHashStoreTransaction> transaction( | |
448 pref_hash_store.BeginTransaction()); | |
449 base::StringValue string_value("foo"); | |
450 transaction->StoreHash("path1", &string_value); | |
451 | |
452 // Test that |pref_hash_store| flushes its content to disk when it | |
453 // initializes its version. | |
454 transaction.reset(); | |
455 pref_hash_store.CommitPendingWrite(); | |
456 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
457 } | |
458 { | |
459 PrefHashStoreImpl pref_hash_store( | |
460 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
461 | |
462 // VERSION_LATEST after storing a hash. | |
463 EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST, | |
464 pref_hash_store.GetCurrentVersion()); | |
465 | |
466 // Test that |pref_hash_store| doesn't flush its contents to disk when it | |
467 // didn't change. | |
468 pref_hash_store.CommitPendingWrite(); | |
469 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
470 } | |
471 | |
472 // Manually clear the version number. | |
473 hash_store_data_.version.reset(); | |
474 | |
475 { | |
476 PrefHashStoreImpl pref_hash_store( | |
477 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
478 | |
479 // VERSION_PRE_MIGRATION when no version is stored. | |
480 EXPECT_EQ(PrefHashStoreImpl::VERSION_PRE_MIGRATION, | |
481 pref_hash_store.GetCurrentVersion()); | |
482 | |
483 scoped_ptr<PrefHashStoreTransaction> transaction( | |
484 pref_hash_store.BeginTransaction()); | |
485 | |
486 // Test that |pref_hash_store| flushes its content to disk when it | |
487 // re-initializes its version. | |
488 transaction.reset(); | |
489 pref_hash_store.CommitPendingWrite(); | |
490 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
491 } | |
492 { | |
493 PrefHashStoreImpl pref_hash_store( | |
494 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
495 | |
496 // Back to VERSION_LATEST after performing a transaction from | |
497 // VERSION_PRE_MIGRATION (the presence of an existing hash should be | |
498 // sufficient, no need for the transaction itself to perform any work). | |
499 EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST, | |
500 pref_hash_store.GetCurrentVersion()); | |
501 | |
502 // Test that |pref_hash_store| doesn't flush its contents to disk when it | |
503 // didn't change (i.e., its version was already up-to-date and the only | |
504 // transaction performed was empty). | |
505 scoped_ptr<PrefHashStoreTransaction> transaction( | |
506 pref_hash_store.BeginTransaction()); | |
507 transaction.reset(); | |
508 pref_hash_store.CommitPendingWrite(); | |
509 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
510 } | |
511 } | |
OLD | NEW |