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 "chrome/browser/safe_browsing/prefix_set.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 EXPECT_FALSE(prefix_set->Exists(kHash6)); | 622 EXPECT_FALSE(prefix_set->Exists(kHash6)); |
623 | 623 |
624 EXPECT_TRUE(prefix_set->PrefixExists(kHash1.prefix)); | 624 EXPECT_TRUE(prefix_set->PrefixExists(kHash1.prefix)); |
625 EXPECT_TRUE(prefix_set->PrefixExists(kHash2.prefix)); | 625 EXPECT_TRUE(prefix_set->PrefixExists(kHash2.prefix)); |
626 EXPECT_FALSE(prefix_set->PrefixExists(kHash3.prefix)); | 626 EXPECT_FALSE(prefix_set->PrefixExists(kHash3.prefix)); |
627 EXPECT_FALSE(prefix_set->PrefixExists(kHash4.prefix)); | 627 EXPECT_FALSE(prefix_set->PrefixExists(kHash4.prefix)); |
628 EXPECT_FALSE(prefix_set->PrefixExists(kHash5.prefix)); | 628 EXPECT_FALSE(prefix_set->PrefixExists(kHash5.prefix)); |
629 EXPECT_FALSE(prefix_set->PrefixExists(kHash6.prefix)); | 629 EXPECT_FALSE(prefix_set->PrefixExists(kHash6.prefix)); |
630 } | 630 } |
631 | 631 |
632 // Test that a version 1 file is re-ordered correctly on read. | 632 // Test that a version 1 file is discarded on read. |
633 TEST_F(PrefixSetTest, ReadWriteSigned) { | 633 TEST_F(PrefixSetTest, ReadSigned) { |
634 base::FilePath filename; | 634 base::FilePath filename; |
635 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 635 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
636 | 636 |
637 // Open the file for rewrite. | 637 // Open the file for rewrite. |
638 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 638 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
639 | 639 |
640 // Leave existing magic. | 640 // Leave existing magic. |
641 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32), SEEK_SET)); | 641 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32), SEEK_SET)); |
642 | 642 |
643 // Version 1. | 643 // Version 1. |
(...skipping 19 matching lines...) Expand all Loading... |
663 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 663 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
664 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 664 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
665 | 665 |
666 // Leave space for the digest at the end, and regenerate it. | 666 // Leave space for the digest at the end, and regenerate it. |
667 base::MD5Digest dummy = { { 0 } }; | 667 base::MD5Digest dummy = { { 0 } }; |
668 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); | 668 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); |
669 ASSERT_TRUE(base::TruncateFile(file.get())); | 669 ASSERT_TRUE(base::TruncateFile(file.get())); |
670 CleanChecksum(file.get()); | 670 CleanChecksum(file.get()); |
671 file.reset(); // Flush updates. | 671 file.reset(); // Flush updates. |
672 | 672 |
673 scoped_ptr<PrefixSet> prefix_set = PrefixSet::LoadFile(filename); | 673 scoped_ptr<safe_browsing::PrefixSet> |
674 ASSERT_TRUE(prefix_set.get()); | 674 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
675 | 675 ASSERT_FALSE(prefix_set.get()); |
676 // |PrefixExists()| uses |std::upper_bound()| to find a starting point, which | |
677 // assumes |index_| is sorted. Depending on how |upper_bound()| is | |
678 // implemented, if the actual list is sorted by |int32|, then one of these | |
679 // test pairs should fail. | |
680 EXPECT_TRUE(prefix_set->PrefixExists(1000u)); | |
681 EXPECT_TRUE(prefix_set->PrefixExists(1023u)); | |
682 EXPECT_TRUE(prefix_set->PrefixExists(static_cast<uint32>(-1000))); | |
683 EXPECT_TRUE(prefix_set->PrefixExists(static_cast<uint32>(-1000 + 23))); | |
684 | |
685 std::vector<SBPrefix> prefixes_copy; | |
686 prefix_set->GetPrefixes(&prefixes_copy); | |
687 EXPECT_EQ(prefixes_copy.size(), 4u); | |
688 EXPECT_EQ(prefixes_copy[0], 1000u); | |
689 EXPECT_EQ(prefixes_copy[1], 1023u); | |
690 EXPECT_EQ(prefixes_copy[2], static_cast<uint32>(-1000)); | |
691 EXPECT_EQ(prefixes_copy[3], static_cast<uint32>(-1000 + 23)); | |
692 } | 676 } |
693 | 677 |
694 // Test that a golden v2 file can be read by the current code. All platforms | 678 // Test that a golden v2 file can be read by the current code. All platforms |
695 // generating v2 files are little-endian, so there is no point to testing this | 679 // generating v2 files are little-endian, so there is no point to testing this |
696 // transition if/when a big-endian port is added. | 680 // transition if/when a big-endian port is added. |
697 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 681 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
698 TEST_F(PrefixSetTest, Version2) { | 682 TEST_F(PrefixSetTest, Version2) { |
699 std::vector<SBPrefix> ref_prefixes; | 683 std::vector<SBPrefix> ref_prefixes; |
700 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); | 684 ASSERT_TRUE(ReadReferencePrefixes(&ref_prefixes)); |
701 | 685 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); | 717 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); |
734 | 718 |
735 EXPECT_TRUE(prefix_set->Exists(kHash1)); | 719 EXPECT_TRUE(prefix_set->Exists(kHash1)); |
736 EXPECT_TRUE(prefix_set->Exists(kHash2)); | 720 EXPECT_TRUE(prefix_set->Exists(kHash2)); |
737 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); | 721 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); |
738 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); | 722 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); |
739 } | 723 } |
740 #endif | 724 #endif |
741 | 725 |
742 } // namespace safe_browsing | 726 } // namespace safe_browsing |
OLD | NEW |