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/file_util.h" | 8 #include "base/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" |
| 13 #include "chrome/common/chrome_paths.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 const int kAddChunk1 = 1; | 19 const int kAddChunk1 = 1; |
18 const int kAddChunk2 = 3; | 20 const int kAddChunk2 = 3; |
19 const int kAddChunk3 = 5; | 21 const int kAddChunk3 = 5; |
20 const int kAddChunk4 = 7; | 22 const int kAddChunk4 = 7; |
21 // Disjoint chunk numbers for subs to flush out typos. | 23 // Disjoint chunk numbers for subs to flush out typos. |
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 | 754 |
753 // New stride should be the same, or shifted one left. | 755 // New stride should be the same, or shifted one left. |
754 const uint32 new_shard_stride = ReadStride(); | 756 const uint32 new_shard_stride = ReadStride(); |
755 EXPECT_TRUE((new_shard_stride == shard_stride) || | 757 EXPECT_TRUE((new_shard_stride == shard_stride) || |
756 (new_shard_stride == (shard_stride << 1))); | 758 (new_shard_stride == (shard_stride << 1))); |
757 shard_stride = new_shard_stride; | 759 shard_stride = new_shard_stride; |
758 } | 760 } |
759 EXPECT_EQ(0u, shard_stride); | 761 EXPECT_EQ(0u, shard_stride); |
760 } | 762 } |
761 | 763 |
| 764 // Test that a golden v7 file can be read by the current code. All platforms |
| 765 // generating v7 files are little-endian, so there is no point to testing this |
| 766 // transition if/when a big-endian port is added. |
| 767 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 768 TEST_F(SafeBrowsingStoreFileTest, Version7) { |
| 769 store_.reset(); |
| 770 |
| 771 // Copy the golden file into temporary storage. The golden file contains: |
| 772 // - Add chunk kAddChunk1 containing kHash1.prefix and kHash2. |
| 773 // - Sub chunk kSubChunk1 containing kHash3. |
| 774 const char kBasename[] = "FileStoreVersion7"; |
| 775 base::FilePath golden_path; |
| 776 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); |
| 777 golden_path = golden_path.AppendASCII("SafeBrowsing"); |
| 778 golden_path = golden_path.AppendASCII(kBasename); |
| 779 ASSERT_TRUE(base::CopyFile(golden_path, filename_)); |
| 780 |
| 781 // Reset the store to make sure it re-reads the file. |
| 782 store_.reset(new SafeBrowsingStoreFile()); |
| 783 store_->Init(filename_, |
| 784 base::Bind(&SafeBrowsingStoreFileTest::OnCorruptionDetected, |
| 785 base::Unretained(this))); |
| 786 |
| 787 // Check that the expected prefixes and hashes are in place. |
| 788 SBAddPrefixes add_prefixes; |
| 789 EXPECT_TRUE(store_->GetAddPrefixes(&add_prefixes)); |
| 790 ASSERT_EQ(2U, add_prefixes.size()); |
| 791 EXPECT_EQ(kAddChunk1, add_prefixes[0].chunk_id); |
| 792 EXPECT_EQ(kHash1.prefix, add_prefixes[0].prefix); |
| 793 EXPECT_EQ(kAddChunk1, add_prefixes[1].chunk_id); |
| 794 EXPECT_EQ(kHash2.prefix, add_prefixes[1].prefix); |
| 795 |
| 796 std::vector<SBAddFullHash> add_hashes; |
| 797 EXPECT_TRUE(store_->GetAddFullHashes(&add_hashes)); |
| 798 ASSERT_EQ(1U, add_hashes.size()); |
| 799 EXPECT_EQ(kAddChunk1, add_hashes[0].chunk_id); |
| 800 EXPECT_TRUE(SBFullHashEqual(kHash2, add_hashes[0].full_hash)); |
| 801 |
| 802 // Attempt an update to make sure things work end-to-end. |
| 803 EXPECT_TRUE(store_->BeginUpdate()); |
| 804 |
| 805 // Still has the chunks expected in the next update. |
| 806 std::vector<int> chunks; |
| 807 store_->GetAddChunks(&chunks); |
| 808 ASSERT_EQ(1U, chunks.size()); |
| 809 EXPECT_EQ(kAddChunk1, chunks[0]); |
| 810 |
| 811 store_->GetSubChunks(&chunks); |
| 812 ASSERT_EQ(1U, chunks.size()); |
| 813 EXPECT_EQ(kSubChunk1, chunks[0]); |
| 814 |
| 815 EXPECT_TRUE(store_->CheckAddChunk(kAddChunk1)); |
| 816 EXPECT_TRUE(store_->CheckSubChunk(kSubChunk1)); |
| 817 |
| 818 // Sub chunk kAddChunk1 hash kHash2. |
| 819 store_->SetSubChunk(kSubChunk2); |
| 820 EXPECT_TRUE(store_->CheckSubChunk(kSubChunk1)); |
| 821 EXPECT_TRUE(store_->WriteSubPrefix(kSubChunk1, kAddChunk1, kHash2.prefix)); |
| 822 EXPECT_TRUE(store_->WriteSubHash(kSubChunk1, kAddChunk1, kHash2)); |
| 823 EXPECT_TRUE(store_->FinishChunk()); |
| 824 |
| 825 { |
| 826 std::vector<SBAddFullHash> pending_adds; |
| 827 safe_browsing::PrefixSetBuilder builder; |
| 828 std::vector<SBAddFullHash> add_full_hashes_result; |
| 829 EXPECT_TRUE(store_->FinishUpdate(pending_adds, |
| 830 &builder, |
| 831 &add_full_hashes_result)); |
| 832 |
| 833 // The sub'ed prefix and hash are gone. |
| 834 std::vector<SBPrefix> prefixes_result; |
| 835 builder.GetPrefixSet()->GetPrefixes(&prefixes_result); |
| 836 ASSERT_EQ(1U, prefixes_result.size()); |
| 837 EXPECT_EQ(kHash1.prefix, prefixes_result[0]); |
| 838 EXPECT_TRUE(add_full_hashes_result.empty()); |
| 839 } |
| 840 } |
| 841 #endif |
| 842 |
762 } // namespace | 843 } // namespace |
OLD | NEW |