| 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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 PrefHashStoreImpl pref_hash_store2( | 413 PrefHashStoreImpl pref_hash_store2( |
| 426 std::string(32, 0), "device_id", CreateHashStoreContents()); | 414 std::string(32, 0), "device_id", 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 |