| 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/prefix_set.h" | 5 #include "components/safe_browsing_db/prefix_set.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/md5.h" | 16 #include "base/md5.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 19 #include "base/rand_util.h" | 19 #include "base/rand_util.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "components/safe_browsing_db/safe_browsing_db_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const SBPrefix kHighBitClear = 1000u * 1000u * 1000u; | 28 const SBPrefix kHighBitClear = 1000u * 1000u * 1000u; |
| 29 const SBPrefix kHighBitSet = 3u * 1000u * 1000u * 1000u; | 29 const SBPrefix kHighBitSet = 3u * 1000u * 1000u * 1000u; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 169 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 170 IncrementIntAt(file.get(), offset, inc); | 170 IncrementIntAt(file.get(), offset, inc); |
| 171 CleanChecksum(file.get()); | 171 CleanChecksum(file.get()); |
| 172 file.reset(); | 172 file.reset(); |
| 173 | 173 |
| 174 int64 new_size_64; | 174 int64 new_size_64; |
| 175 ASSERT_TRUE(base::GetFileSize(filename, &new_size_64)); | 175 ASSERT_TRUE(base::GetFileSize(filename, &new_size_64)); |
| 176 ASSERT_EQ(new_size_64, size_64); | 176 ASSERT_EQ(new_size_64, size_64); |
| 177 } | 177 } |
| 178 | 178 |
| 179 base::FilePath TestFilePath() { |
| 180 base::FilePath path; |
| 181 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 182 return path.AppendASCII("components") |
| 183 .AppendASCII("test") |
| 184 .AppendASCII("data") |
| 185 .AppendASCII("SafeBrowsingDb"); |
| 186 } |
| 187 |
| 179 // Fill |prefixes| with values read from a reference file. The reference file | 188 // Fill |prefixes| with values read from a reference file. The reference file |
| 180 // was generated from a specific |shared_prefixes_|. | 189 // was generated from a specific |shared_prefixes_|. |
| 181 bool ReadReferencePrefixes(std::vector<SBPrefix>* prefixes) { | 190 bool ReadReferencePrefixes(std::vector<SBPrefix>* prefixes) { |
| 182 const char kRefname[] = "PrefixSetRef"; | 191 const char kRefname[] = "PrefixSetRef"; |
| 183 base::FilePath ref_path; | 192 base::FilePath ref_path = TestFilePath(); |
| 184 if (!PathService::Get(chrome::DIR_TEST_DATA, &ref_path)) | |
| 185 return false; | |
| 186 ref_path = ref_path.AppendASCII("SafeBrowsing"); | |
| 187 ref_path = ref_path.AppendASCII(kRefname); | 193 ref_path = ref_path.AppendASCII(kRefname); |
| 188 | 194 |
| 189 base::ScopedFILE file(base::OpenFile(ref_path, "r")); | 195 base::ScopedFILE file(base::OpenFile(ref_path, "r")); |
| 190 if (!file.get()) | 196 if (!file.get()) |
| 191 return false; | 197 return false; |
| 192 char buf[1024]; | 198 char buf[1024]; |
| 193 while (fgets(buf, sizeof(buf), file.get())) { | 199 while (fgets(buf, sizeof(buf), file.get())) { |
| 194 std::string trimmed; | 200 std::string trimmed; |
| 195 if (base::TRIM_TRAILING != | 201 if (base::TRIM_TRAILING != |
| 196 base::TrimWhitespace(buf, base::TRIM_ALL, &trimmed)) | 202 base::TrimWhitespace(buf, base::TRIM_ALL, &trimmed)) |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 | 684 |
| 679 // Test that a golden v2 file is discarded on read. All platforms generating v2 | 685 // Test that a golden v2 file is discarded on read. All platforms generating v2 |
| 680 // files are little-endian, so there is no point to testing this transition | 686 // files are little-endian, so there is no point to testing this transition |
| 681 // if/when a big-endian port is added. | 687 // if/when a big-endian port is added. |
| 682 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 688 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 683 TEST_F(PrefixSetTest, Version2) { | 689 TEST_F(PrefixSetTest, Version2) { |
| 684 std::vector<SBPrefix> ref_prefixes; | 690 std::vector<SBPrefix> ref_prefixes; |
| 685 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); | 691 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); |
| 686 | 692 |
| 687 const char kBasename[] = "PrefixSetVersion2"; | 693 const char kBasename[] = "PrefixSetVersion2"; |
| 688 base::FilePath golden_path; | 694 base::FilePath golden_path = TestFilePath(); |
| 689 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | |
| 690 golden_path = golden_path.AppendASCII("SafeBrowsing"); | |
| 691 golden_path = golden_path.AppendASCII(kBasename); | 695 golden_path = golden_path.AppendASCII(kBasename); |
| 692 | 696 |
| 693 scoped_ptr<const PrefixSet> prefix_set(PrefixSet::LoadFile(golden_path)); | 697 scoped_ptr<const PrefixSet> prefix_set(PrefixSet::LoadFile(golden_path)); |
| 694 ASSERT_FALSE(prefix_set.get()); | 698 ASSERT_FALSE(prefix_set.get()); |
| 695 } | 699 } |
| 696 #endif | 700 #endif |
| 697 | 701 |
| 698 // Test that a golden v3 file can be read by the current code. All platforms | 702 // Test that a golden v3 file can be read by the current code. All platforms |
| 699 // generating v3 files are little-endian, so there is no point to testing this | 703 // generating v3 files are little-endian, so there is no point to testing this |
| 700 // transition if/when a big-endian port is added. | 704 // transition if/when a big-endian port is added. |
| 701 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 705 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 702 TEST_F(PrefixSetTest, Version3) { | 706 TEST_F(PrefixSetTest, Version3) { |
| 703 std::vector<SBPrefix> ref_prefixes; | 707 std::vector<SBPrefix> ref_prefixes; |
| 704 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); | 708 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); |
| 705 | 709 |
| 706 const char kBasename[] = "PrefixSetVersion3"; | 710 const char kBasename[] = "PrefixSetVersion3"; |
| 707 base::FilePath golden_path; | 711 base::FilePath golden_path = TestFilePath(); |
| 708 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &golden_path)); | |
| 709 golden_path = golden_path.AppendASCII("SafeBrowsing"); | |
| 710 golden_path = golden_path.AppendASCII(kBasename); | 712 golden_path = golden_path.AppendASCII(kBasename); |
| 711 | 713 |
| 712 scoped_ptr<const PrefixSet> prefix_set(PrefixSet::LoadFile(golden_path)); | 714 scoped_ptr<const PrefixSet> prefix_set(PrefixSet::LoadFile(golden_path)); |
| 713 ASSERT_TRUE(prefix_set.get()); | 715 ASSERT_TRUE(prefix_set.get()); |
| 714 CheckPrefixes(*prefix_set, ref_prefixes); | 716 CheckPrefixes(*prefix_set, ref_prefixes); |
| 715 | 717 |
| 716 const SBFullHash kHash1 = SBFullHashForString("www.evil.com/malware.html"); | 718 const SBFullHash kHash1 = SBFullHashForString("www.evil.com/malware.html"); |
| 717 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); | 719 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); |
| 718 | 720 |
| 719 EXPECT_TRUE(prefix_set->Exists(kHash1)); | 721 EXPECT_TRUE(prefix_set->Exists(kHash1)); |
| 720 EXPECT_TRUE(prefix_set->Exists(kHash2)); | 722 EXPECT_TRUE(prefix_set->Exists(kHash2)); |
| 721 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); | 723 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); |
| 722 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); | 724 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); |
| 723 } | 725 } |
| 724 #endif | 726 #endif |
| 725 | 727 |
| 726 } // namespace safe_browsing | 728 } // namespace safe_browsing |
| OLD | NEW |