Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/safe_browsing/safe_browsing_store_file.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_file.h" | 9 #include "base/files/scoped_file.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/md5.h" | 11 #include "base/md5.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/test/test_simple_task_runner.h" | |
| 13 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const int kAddChunk1 = 1; | 20 const int kAddChunk1 = 1; |
| 20 const int kAddChunk2 = 3; | 21 const int kAddChunk2 = 3; |
| 21 const int kAddChunk3 = 5; | 22 const int kAddChunk3 = 5; |
| 22 const int kAddChunk4 = 7; | 23 const int kAddChunk4 = 7; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 33 | 34 |
| 34 const SBPrefix kMinSBPrefix = 0u; | 35 const SBPrefix kMinSBPrefix = 0u; |
| 35 const SBPrefix kMaxSBPrefix = ~kMinSBPrefix; | 36 const SBPrefix kMaxSBPrefix = ~kMinSBPrefix; |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 namespace safe_browsing { | 40 namespace safe_browsing { |
| 40 | 41 |
| 41 class SafeBrowsingStoreFileTest : public PlatformTest { | 42 class SafeBrowsingStoreFileTest : public PlatformTest { |
| 42 public: | 43 public: |
| 44 SafeBrowsingStoreFileTest() | |
| 45 : task_runner_(new base::TestSimpleTaskRunner), | |
| 46 corruption_detected_(false) {} | |
| 47 | |
| 43 void SetUp() override { | 48 void SetUp() override { |
| 44 PlatformTest::SetUp(); | 49 PlatformTest::SetUp(); |
| 45 | 50 |
| 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 51 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 47 | 52 |
| 48 filename_ = temp_dir_.path(); | 53 filename_ = temp_dir_.path(); |
| 49 filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); | 54 filename_ = filename_.AppendASCII("SafeBrowsingTestStore"); |
| 50 | 55 |
| 51 store_.reset(new SafeBrowsingStoreFile()); | 56 store_.reset(new SafeBrowsingStoreFile(task_runner_)); |
| 52 store_->Init(filename_, | 57 store_->Init(filename_, |
| 53 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 58 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |
| 54 base::Unretained(this))); | 59 base::Unretained(this))); |
| 55 corruption_detected_ = false; | 60 corruption_detected_ = false; |
|
gab
2015/02/19 14:38:23
Remove this line now that it's properly taken care
Alexei Svitkine (slow)
2015/02/20 15:42:45
Done.
| |
| 56 } | 61 } |
| 57 void TearDown() override { | 62 void TearDown() override { |
| 58 if (store_.get()) | 63 if (store_.get()) |
| 59 store_->Delete(); | 64 store_->Delete(); |
| 60 store_.reset(); | 65 store_.reset(); |
| 61 | 66 |
| 62 PlatformTest::TearDown(); | 67 PlatformTest::TearDown(); |
| 63 } | 68 } |
| 64 | 69 |
| 65 void OnCorruptionDetected() { | 70 void OnCorruptionDetected() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 // Manually read the shard stride info from the file. | 110 // Manually read the shard stride info from the file. |
| 106 uint32 ReadStride() { | 111 uint32 ReadStride() { |
| 107 base::ScopedFILE file(base::OpenFile(filename_, "rb")); | 112 base::ScopedFILE file(base::OpenFile(filename_, "rb")); |
| 108 const long kOffset = 4 * sizeof(uint32); | 113 const long kOffset = 4 * sizeof(uint32); |
| 109 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 114 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
| 110 uint32 shard_stride = 0; | 115 uint32 shard_stride = 0; |
| 111 EXPECT_EQ(fread(&shard_stride, sizeof(shard_stride), 1, file.get()), 1U); | 116 EXPECT_EQ(fread(&shard_stride, sizeof(shard_stride), 1, file.get()), 1U); |
| 112 return shard_stride; | 117 return shard_stride; |
| 113 } | 118 } |
| 114 | 119 |
| 120 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 115 base::ScopedTempDir temp_dir_; | 121 base::ScopedTempDir temp_dir_; |
| 116 base::FilePath filename_; | 122 base::FilePath filename_; |
| 117 scoped_ptr<SafeBrowsingStoreFile> store_; | 123 scoped_ptr<SafeBrowsingStoreFile> store_; |
| 118 bool corruption_detected_; | 124 bool corruption_detected_; |
| 119 }; | 125 }; |
| 120 | 126 |
| 121 // Test that the empty store looks empty. | 127 // Test that the empty store looks empty. |
| 122 TEST_F(SafeBrowsingStoreFileTest, Empty) { | 128 TEST_F(SafeBrowsingStoreFileTest, Empty) { |
| 123 ASSERT_TRUE(store_->BeginUpdate()); | 129 ASSERT_TRUE(store_->BeginUpdate()); |
| 124 | 130 |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 | 460 |
| 455 EXPECT_FALSE(base::PathExists(filename_)); | 461 EXPECT_FALSE(base::PathExists(filename_)); |
| 456 EXPECT_FALSE(base::PathExists(temp_file)); | 462 EXPECT_FALSE(base::PathExists(temp_file)); |
| 457 | 463 |
| 458 // Starting a transaction creates a temporary file. | 464 // Starting a transaction creates a temporary file. |
| 459 ASSERT_TRUE(store_->BeginUpdate()); | 465 ASSERT_TRUE(store_->BeginUpdate()); |
| 460 EXPECT_TRUE(base::PathExists(temp_file)); | 466 EXPECT_TRUE(base::PathExists(temp_file)); |
| 461 | 467 |
| 462 // Pull the rug out from under the existing store, simulating a | 468 // Pull the rug out from under the existing store, simulating a |
| 463 // crash. | 469 // crash. |
| 464 store_.reset(new SafeBrowsingStoreFile()); | 470 store_.reset(new SafeBrowsingStoreFile(task_runner_)); |
| 465 store_->Init(filename_, base::Closure()); | 471 store_->Init(filename_, base::Closure()); |
| 466 EXPECT_FALSE(base::PathExists(filename_)); | 472 EXPECT_FALSE(base::PathExists(filename_)); |
| 467 EXPECT_TRUE(base::PathExists(temp_file)); | 473 EXPECT_TRUE(base::PathExists(temp_file)); |
| 468 | 474 |
| 469 // Make sure the temporary file is deleted. | 475 // Make sure the temporary file is deleted. |
| 470 EXPECT_TRUE(store_->Delete()); | 476 EXPECT_TRUE(store_->Delete()); |
| 471 EXPECT_FALSE(base::PathExists(filename_)); | 477 EXPECT_FALSE(base::PathExists(filename_)); |
| 472 EXPECT_FALSE(base::PathExists(temp_file)); | 478 EXPECT_FALSE(base::PathExists(temp_file)); |
| 473 } | 479 } |
| 474 | 480 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 741 // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. | 747 // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. |
| 742 // - Sub chunk kSubChunk1 containing kHash3. | 748 // - Sub chunk kSubChunk1 containing kHash3. |
| 743 const char kBasename[] = "FileStoreVersion7"; | 749 const char kBasename[] = "FileStoreVersion7"; |
| 744 base::FilePath golden_path; | 750 base::FilePath golden_path; |
| 745 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | 751 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); |
| 746 golden_path = golden_path.AppendASCII("SafeBrowsing"); | 752 golden_path = golden_path.AppendASCII("SafeBrowsing"); |
| 747 golden_path = golden_path.AppendASCII(kBasename); | 753 golden_path = golden_path.AppendASCII(kBasename); |
| 748 ASSERT_TRUE(base::CopyFile(golden_path, filename_)); | 754 ASSERT_TRUE(base::CopyFile(golden_path, filename_)); |
| 749 | 755 |
| 750 // Reset the store to make sure it re-reads the file. | 756 // Reset the store to make sure it re-reads the file. |
| 751 store_.reset(new SafeBrowsingStoreFile()); | 757 store_.reset(new SafeBrowsingStoreFile(task_runner_)); |
|
gab
2015/02/19 14:38:23
ASSERT_TRUE(!task_runner_->HasPendingTasks());
be
Alexei Svitkine (slow)
2015/02/20 15:42:44
Done.
| |
| 752 store_->Init(filename_, | 758 store_->Init(filename_, |
| 753 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 759 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |
| 754 base::Unretained(this))); | 760 base::Unretained(this))); |
| 755 EXPECT_FALSE(corruption_detected_); | 761 EXPECT_FALSE(corruption_detected_); |
| 756 | 762 |
| 757 // The unknown version should be encountered on the first read. | 763 // The unknown version should be encountered on the first read. |
| 758 EXPECT_FALSE(store_->BeginUpdate()); | 764 EXPECT_FALSE(store_->BeginUpdate()); |
| 759 EXPECT_TRUE(corruption_detected_); | 765 EXPECT_TRUE(corruption_detected_); |
| 760 | 766 |
| 761 // No more to test because corrupt file stores are cleaned up by the database | 767 // No more to test because corrupt file stores are cleaned up by the database |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 774 // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. | 780 // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. |
| 775 // - Sub chunk kSubChunk1 containing kHash3. | 781 // - Sub chunk kSubChunk1 containing kHash3. |
| 776 const char kBasename[] = "FileStoreVersion8"; | 782 const char kBasename[] = "FileStoreVersion8"; |
| 777 base::FilePath golden_path; | 783 base::FilePath golden_path; |
| 778 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | 784 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); |
| 779 golden_path = golden_path.AppendASCII("SafeBrowsing"); | 785 golden_path = golden_path.AppendASCII("SafeBrowsing"); |
| 780 golden_path = golden_path.AppendASCII(kBasename); | 786 golden_path = golden_path.AppendASCII(kBasename); |
| 781 ASSERT_TRUE(base::CopyFile(golden_path, filename_)); | 787 ASSERT_TRUE(base::CopyFile(golden_path, filename_)); |
| 782 | 788 |
| 783 // Reset the store to make sure it re-reads the file. | 789 // Reset the store to make sure it re-reads the file. |
| 784 store_.reset(new SafeBrowsingStoreFile()); | 790 store_.reset(new SafeBrowsingStoreFile(task_runner_)); |
| 785 store_->Init(filename_, | 791 store_->Init(filename_, |
| 786 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, | 792 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |
| 787 base::Unretained(this))); | 793 base::Unretained(this))); |
| 788 | 794 |
| 789 // Check that the expected prefixes and hashes are in place. | 795 // Check that the expected prefixes and hashes are in place. |
| 790 SBAddPrefixes add_prefixes; | 796 SBAddPrefixes add_prefixes; |
| 791 EXPECT_TRUE(store_->GetAddPrefixes(&add_prefixes)); | 797 EXPECT_TRUE(store_->GetAddPrefixes(&add_prefixes)); |
| 792 ASSERT_EQ(2U, add_prefixes.size()); | 798 ASSERT_EQ(2U, add_prefixes.size()); |
| 793 EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id); | 799 EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id); |
| 794 EXPECT_EQ(kHash1.prefix, add_prefixes[0].prefix); | 800 EXPECT_EQ(kHash1.prefix, add_prefixes[0].prefix); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 835 std::vector<SBPrefix> prefixes_result; | 841 std::vector<SBPrefix> prefixes_result; |
| 836 builder.GetPrefixSetNoHashes()->GetPrefixes(&prefixes_result); | 842 builder.GetPrefixSetNoHashes()->GetPrefixes(&prefixes_result); |
| 837 ASSERT_EQ(1U, prefixes_result.size()); | 843 ASSERT_EQ(1U, prefixes_result.size()); |
| 838 EXPECT_EQ(kHash1.prefix, prefixes_result[0]); | 844 EXPECT_EQ(kHash1.prefix, prefixes_result[0]); |
| 839 EXPECT_TRUE(add_full_hashes_result.empty()); | 845 EXPECT_TRUE(add_full_hashes_result.empty()); |
| 840 } | 846 } |
| 841 } | 847 } |
| 842 #endif | 848 #endif |
| 843 | 849 |
| 844 } // namespace safe_browsing | 850 } // namespace safe_browsing |
| OLD | NEW |