| 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 "components/safe_browsing_db/prefix_set.h" | 5 #include "components/safe_browsing_db/prefix_set.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 #include <string.h> |
| 10 |
| 7 #include <algorithm> | 11 #include <algorithm> |
| 8 #include <iterator> | 12 #include <iterator> |
| 9 #include <set> | 13 #include <set> |
| 10 #include <string> | 14 #include <string> |
| 11 | 15 |
| 12 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_file.h" | 17 #include "base/files/scoped_file.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 18 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/logging.h" | 19 #include "base/logging.h" |
| 16 #include "base/md5.h" | 20 #include "base/md5.h" |
| 17 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/path_service.h" | 22 #include "base/path_service.h" |
| 19 #include "base/rand_util.h" | 23 #include "base/rand_util.h" |
| 20 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
| 26 #include "build/build_config.h" |
| 22 #include "components/safe_browsing_db/util.h" | 27 #include "components/safe_browsing_db/util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/platform_test.h" | 29 #include "testing/platform_test.h" |
| 25 | 30 |
| 26 namespace safe_browsing { | 31 namespace safe_browsing { |
| 27 | 32 |
| 28 namespace { | 33 namespace { |
| 29 | 34 |
| 30 const SBPrefix kHighBitClear = 1000u * 1000u * 1000u; | 35 const SBPrefix kHighBitClear = 1000u * 1000u * 1000u; |
| 31 const SBPrefix kHighBitSet = 3u * 1000u * 1000u * 1000u; | 36 const SBPrefix kHighBitSet = 3u * 1000u * 1000u * 1000u; |
| 32 | 37 |
| 33 } // namespace | 38 } // namespace |
| 34 | 39 |
| 35 class PrefixSetTest : public PlatformTest { | 40 class PrefixSetTest : public PlatformTest { |
| 36 protected: | 41 protected: |
| 37 // Constants for the v1 format. | 42 // Constants for the v1 format. |
| 38 static const size_t kMagicOffset = 0 * sizeof(uint32); | 43 static const size_t kMagicOffset = 0 * sizeof(uint32_t); |
| 39 static const size_t kVersionOffset = 1 * sizeof(uint32); | 44 static const size_t kVersionOffset = 1 * sizeof(uint32_t); |
| 40 static const size_t kIndexSizeOffset = 2 * sizeof(uint32); | 45 static const size_t kIndexSizeOffset = 2 * sizeof(uint32_t); |
| 41 static const size_t kDeltasSizeOffset = 3 * sizeof(uint32); | 46 static const size_t kDeltasSizeOffset = 3 * sizeof(uint32_t); |
| 42 static const size_t kFullHashesSizeOffset = 4 * sizeof(uint32); | 47 static const size_t kFullHashesSizeOffset = 4 * sizeof(uint32_t); |
| 43 static const size_t kPayloadOffset = 5 * sizeof(uint32); | 48 static const size_t kPayloadOffset = 5 * sizeof(uint32_t); |
| 44 | 49 |
| 45 // Generate a set of random prefixes to share between tests. For | 50 // Generate a set of random prefixes to share between tests. For |
| 46 // most tests this generation was a large fraction of the test time. | 51 // most tests this generation was a large fraction of the test time. |
| 47 // | 52 // |
| 48 // The set should contain sparse areas where adjacent items are more | 53 // The set should contain sparse areas where adjacent items are more |
| 49 // than 2^16 apart, and dense areas where adjacent items are less | 54 // than 2^16 apart, and dense areas where adjacent items are less |
| 50 // than 2^16 apart. | 55 // than 2^16 apart. |
| 51 static void SetUpTestCase() { | 56 static void SetUpTestCase() { |
| 52 // Distribute clusters of prefixes. | 57 // Distribute clusters of prefixes. |
| 53 for (size_t i = 0; i < 250; ++i) { | 58 for (size_t i = 0; i < 250; ++i) { |
| 54 // Unsigned for overflow characteristics. | 59 // Unsigned for overflow characteristics. |
| 55 const uint32 base = static_cast<uint32>(base::RandUint64()); | 60 const uint32_t base = static_cast<uint32_t>(base::RandUint64()); |
| 56 for (size_t j = 0; j < 10; ++j) { | 61 for (size_t j = 0; j < 10; ++j) { |
| 57 const uint32 delta = static_cast<uint32>(base::RandUint64() & 0xFFFF); | 62 const uint32_t delta = |
| 63 static_cast<uint32_t>(base::RandUint64() & 0xFFFF); |
| 58 const SBPrefix prefix = static_cast<SBPrefix>(base + delta); | 64 const SBPrefix prefix = static_cast<SBPrefix>(base + delta); |
| 59 shared_prefixes_.push_back(prefix); | 65 shared_prefixes_.push_back(prefix); |
| 60 } | 66 } |
| 61 } | 67 } |
| 62 | 68 |
| 63 // Lay down a sparsely-distributed layer. | 69 // Lay down a sparsely-distributed layer. |
| 64 const size_t count = shared_prefixes_.size(); | 70 const size_t count = shared_prefixes_.size(); |
| 65 for (size_t i = 0; i < count; ++i) { | 71 for (size_t i = 0; i < count; ++i) { |
| 66 const SBPrefix prefix = static_cast<SBPrefix>(base::RandUint64()); | 72 const SBPrefix prefix = static_cast<SBPrefix>(base::RandUint64()); |
| 67 shared_prefixes_.push_back(prefix); | 73 shared_prefixes_.push_back(prefix); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 base::FilePath filename = temp_dir_.path().AppendASCII("PrefixSetTest"); | 113 base::FilePath filename = temp_dir_.path().AppendASCII("PrefixSetTest"); |
| 108 | 114 |
| 109 PrefixSetBuilder builder(shared_prefixes_); | 115 PrefixSetBuilder builder(shared_prefixes_); |
| 110 if (!builder.GetPrefixSetNoHashes()->WriteFile(filename)) | 116 if (!builder.GetPrefixSetNoHashes()->WriteFile(filename)) |
| 111 return false; | 117 return false; |
| 112 | 118 |
| 113 *filenamep = filename; | 119 *filenamep = filename; |
| 114 return true; | 120 return true; |
| 115 } | 121 } |
| 116 | 122 |
| 117 // Helper function to read the uint32 value at |offset|, increment it | 123 // Helper function to read the uint32_t value at |offset|, increment it |
| 118 // by |inc|, and write it back in place. |fp| should be opened in | 124 // by |inc|, and write it back in place. |fp| should be opened in |
| 119 // r+ mode. | 125 // r+ mode. |
| 120 static void IncrementIntAt(FILE* fp, long offset, int inc) { | 126 static void IncrementIntAt(FILE* fp, long offset, int inc) { |
| 121 uint32 value = 0; | 127 uint32_t value = 0; |
| 122 | 128 |
| 123 ASSERT_NE(-1, fseek(fp, offset, SEEK_SET)); | 129 ASSERT_NE(-1, fseek(fp, offset, SEEK_SET)); |
| 124 ASSERT_EQ(1U, fread(&value, sizeof(value), 1, fp)); | 130 ASSERT_EQ(1U, fread(&value, sizeof(value), 1, fp)); |
| 125 | 131 |
| 126 value += inc; | 132 value += inc; |
| 127 | 133 |
| 128 ASSERT_NE(-1, fseek(fp, offset, SEEK_SET)); | 134 ASSERT_NE(-1, fseek(fp, offset, SEEK_SET)); |
| 129 ASSERT_EQ(1U, fwrite(&value, sizeof(value), 1, fp)); | 135 ASSERT_EQ(1U, fwrite(&value, sizeof(value), 1, fp)); |
| 130 } | 136 } |
| 131 | 137 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 152 ASSERT_EQ(digested_size, payload_size); | 158 ASSERT_EQ(digested_size, payload_size); |
| 153 ASSERT_EQ(static_cast<long>(digested_size), ftell(fp)); | 159 ASSERT_EQ(static_cast<long>(digested_size), ftell(fp)); |
| 154 | 160 |
| 155 base::MD5Digest new_digest; | 161 base::MD5Digest new_digest; |
| 156 base::MD5Final(&new_digest, &context); | 162 base::MD5Final(&new_digest, &context); |
| 157 ASSERT_NE(-1, fseek(fp, digested_size, SEEK_SET)); | 163 ASSERT_NE(-1, fseek(fp, digested_size, SEEK_SET)); |
| 158 ASSERT_EQ(1U, fwrite(&new_digest, sizeof(new_digest), 1, fp)); | 164 ASSERT_EQ(1U, fwrite(&new_digest, sizeof(new_digest), 1, fp)); |
| 159 ASSERT_EQ(file_size, ftell(fp)); | 165 ASSERT_EQ(file_size, ftell(fp)); |
| 160 } | 166 } |
| 161 | 167 |
| 162 // Open |filename| and increment the uint32 at |offset| by |inc|. | 168 // Open |filename| and increment the uint32_t at |offset| by |inc|. |
| 163 // Then re-generate the checksum to account for the new contents. | 169 // Then re-generate the checksum to account for the new contents. |
| 164 void ModifyAndCleanChecksum(const base::FilePath& filename, long offset, | 170 void ModifyAndCleanChecksum(const base::FilePath& filename, long offset, |
| 165 int inc) { | 171 int inc) { |
| 166 int64 size_64; | 172 int64_t size_64; |
| 167 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); | 173 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); |
| 168 | 174 |
| 169 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 175 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 170 IncrementIntAt(file.get(), offset, inc); | 176 IncrementIntAt(file.get(), offset, inc); |
| 171 CleanChecksum(file.get()); | 177 CleanChecksum(file.get()); |
| 172 file.reset(); | 178 file.reset(); |
| 173 | 179 |
| 174 int64 new_size_64; | 180 int64_t new_size_64; |
| 175 ASSERT_TRUE(base::GetFileSize(filename, &new_size_64)); | 181 ASSERT_TRUE(base::GetFileSize(filename, &new_size_64)); |
| 176 ASSERT_EQ(new_size_64, size_64); | 182 ASSERT_EQ(new_size_64, size_64); |
| 177 } | 183 } |
| 178 | 184 |
| 179 base::FilePath TestFilePath() { | 185 base::FilePath TestFilePath() { |
| 180 base::FilePath path; | 186 base::FilePath path; |
| 181 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 187 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 182 return path.AppendASCII("components") | 188 return path.AppendASCII("components") |
| 183 .AppendASCII("test") | 189 .AppendASCII("test") |
| 184 .AppendASCII("data") | 190 .AppendASCII("data") |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 file.reset(); | 534 file.reset(); |
| 529 scoped_ptr<const PrefixSet> prefix_set = PrefixSet::LoadFile(filename); | 535 scoped_ptr<const PrefixSet> prefix_set = PrefixSet::LoadFile(filename); |
| 530 ASSERT_FALSE(prefix_set.get()); | 536 ASSERT_FALSE(prefix_set.get()); |
| 531 } | 537 } |
| 532 | 538 |
| 533 // Test corruption in the digest itself. | 539 // Test corruption in the digest itself. |
| 534 TEST_F(PrefixSetTest, CorruptionDigest) { | 540 TEST_F(PrefixSetTest, CorruptionDigest) { |
| 535 base::FilePath filename; | 541 base::FilePath filename; |
| 536 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 542 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 537 | 543 |
| 538 int64 size_64; | 544 int64_t size_64; |
| 539 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); | 545 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); |
| 540 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 546 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 541 long digest_offset = static_cast<long>(size_64 - sizeof(base::MD5Digest)); | 547 long digest_offset = static_cast<long>(size_64 - sizeof(base::MD5Digest)); |
| 542 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), digest_offset, 1)); | 548 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), digest_offset, 1)); |
| 543 file.reset(); | 549 file.reset(); |
| 544 scoped_ptr<const PrefixSet> prefix_set = PrefixSet::LoadFile(filename); | 550 scoped_ptr<const PrefixSet> prefix_set = PrefixSet::LoadFile(filename); |
| 545 ASSERT_FALSE(prefix_set.get()); | 551 ASSERT_FALSE(prefix_set.get()); |
| 546 } | 552 } |
| 547 | 553 |
| 548 // Test excess data after the digest (fails the size test). | 554 // Test excess data after the digest (fails the size test). |
| (...skipping 12 matching lines...) Expand all Loading... |
| 561 | 567 |
| 562 // Test that files which had 64-bit size_t are discarded. | 568 // Test that files which had 64-bit size_t are discarded. |
| 563 TEST_F(PrefixSetTest, SizeTRecovery) { | 569 TEST_F(PrefixSetTest, SizeTRecovery) { |
| 564 base::FilePath filename; | 570 base::FilePath filename; |
| 565 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 571 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 566 | 572 |
| 567 // Open the file for rewrite. | 573 // Open the file for rewrite. |
| 568 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 574 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 569 | 575 |
| 570 // Leave existing magic and version. | 576 // Leave existing magic and version. |
| 571 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32) * 2, SEEK_SET)); | 577 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32_t) * 2, SEEK_SET)); |
| 572 | 578 |
| 573 // Indicate two index values and two deltas. | 579 // Indicate two index values and two deltas. |
| 574 uint32 val = 2; | 580 uint32_t val = 2; |
| 575 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); | 581 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); |
| 576 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); | 582 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); |
| 577 | 583 |
| 578 // Write two index values with 64-bit "size_t". | 584 // Write two index values with 64-bit "size_t". |
| 579 std::pair<SBPrefix, uint64> item; | 585 std::pair<SBPrefix, uint64_t> item; |
| 580 memset(&item, 0, sizeof(item)); // Includes any padding. | 586 memset(&item, 0, sizeof(item)); // Includes any padding. |
| 581 item.first = 17; | 587 item.first = 17; |
| 582 item.second = 0; | 588 item.second = 0; |
| 583 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); | 589 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); |
| 584 item.first = 100042; | 590 item.first = 100042; |
| 585 item.second = 1; | 591 item.second = 1; |
| 586 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); | 592 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); |
| 587 | 593 |
| 588 // Write two delta values. | 594 // Write two delta values. |
| 589 uint16 delta = 23; | 595 uint16_t delta = 23; |
| 590 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 596 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
| 591 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 597 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
| 592 | 598 |
| 593 // Leave space for the digest at the end, and regenerate it. | 599 // Leave space for the digest at the end, and regenerate it. |
| 594 base::MD5Digest dummy = { { 0 } }; | 600 base::MD5Digest dummy = { { 0 } }; |
| 595 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); | 601 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); |
| 596 ASSERT_TRUE(base::TruncateFile(file.get())); | 602 ASSERT_TRUE(base::TruncateFile(file.get())); |
| 597 CleanChecksum(file.get()); | 603 CleanChecksum(file.get()); |
| 598 file.reset(); // Flush updates. | 604 file.reset(); // Flush updates. |
| 599 | 605 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 | 645 |
| 640 // Test that a version 1 file is discarded on read. | 646 // Test that a version 1 file is discarded on read. |
| 641 TEST_F(PrefixSetTest, ReadSigned) { | 647 TEST_F(PrefixSetTest, ReadSigned) { |
| 642 base::FilePath filename; | 648 base::FilePath filename; |
| 643 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 649 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 644 | 650 |
| 645 // Open the file for rewrite. | 651 // Open the file for rewrite. |
| 646 base::ScopedFILE file(base::OpenFile(filename, "r+b")); | 652 base::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 647 | 653 |
| 648 // Leave existing magic. | 654 // Leave existing magic. |
| 649 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32), SEEK_SET)); | 655 ASSERT_NE(-1, fseek(file.get(), sizeof(uint32_t), SEEK_SET)); |
| 650 | 656 |
| 651 // Version 1. | 657 // Version 1. |
| 652 uint32 version = 1; | 658 uint32_t version = 1; |
| 653 ASSERT_EQ(sizeof(version), fwrite(&version, 1, sizeof(version), file.get())); | 659 ASSERT_EQ(sizeof(version), fwrite(&version, 1, sizeof(version), file.get())); |
| 654 | 660 |
| 655 // Indicate two index values and two deltas. | 661 // Indicate two index values and two deltas. |
| 656 uint32 val = 2; | 662 uint32_t val = 2; |
| 657 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); | 663 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); |
| 658 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); | 664 ASSERT_EQ(sizeof(val), fwrite(&val, 1, sizeof(val), file.get())); |
| 659 | 665 |
| 660 std::pair<int32, uint32> item; | 666 std::pair<int32_t, uint32_t> item; |
| 661 memset(&item, 0, sizeof(item)); // Includes any padding. | 667 memset(&item, 0, sizeof(item)); // Includes any padding. |
| 662 item.first = -1000; | 668 item.first = -1000; |
| 663 item.second = 0; | 669 item.second = 0; |
| 664 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); | 670 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); |
| 665 item.first = 1000; | 671 item.first = 1000; |
| 666 item.second = 1; | 672 item.second = 1; |
| 667 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); | 673 ASSERT_EQ(sizeof(item), fwrite(&item, 1, sizeof(item), file.get())); |
| 668 | 674 |
| 669 // Write two delta values. | 675 // Write two delta values. |
| 670 uint16 delta = 23; | 676 uint16_t delta = 23; |
| 671 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 677 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
| 672 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); | 678 ASSERT_EQ(sizeof(delta), fwrite(&delta, 1, sizeof(delta), file.get())); |
| 673 | 679 |
| 674 // Leave space for the digest at the end, and regenerate it. | 680 // Leave space for the digest at the end, and regenerate it. |
| 675 base::MD5Digest dummy = { { 0 } }; | 681 base::MD5Digest dummy = { { 0 } }; |
| 676 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); | 682 ASSERT_EQ(sizeof(dummy), fwrite(&dummy, 1, sizeof(dummy), file.get())); |
| 677 ASSERT_TRUE(base::TruncateFile(file.get())); | 683 ASSERT_TRUE(base::TruncateFile(file.get())); |
| 678 CleanChecksum(file.get()); | 684 CleanChecksum(file.get()); |
| 679 file.reset(); // Flush updates. | 685 file.reset(); // Flush updates. |
| 680 | 686 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); | 725 const SBFullHash kHash2 = SBFullHashForString("www.evil.com/phishing.html"); |
| 720 | 726 |
| 721 EXPECT_TRUE(prefix_set->Exists(kHash1)); | 727 EXPECT_TRUE(prefix_set->Exists(kHash1)); |
| 722 EXPECT_TRUE(prefix_set->Exists(kHash2)); | 728 EXPECT_TRUE(prefix_set->Exists(kHash2)); |
| 723 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); | 729 EXPECT_FALSE(prefix_set->PrefixExists(kHash1.prefix)); |
| 724 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); | 730 EXPECT_FALSE(prefix_set->PrefixExists(kHash2.prefix)); |
| 725 } | 731 } |
| 726 #endif | 732 #endif |
| 727 | 733 |
| 728 } // namespace safe_browsing | 734 } // namespace safe_browsing |
| OLD | NEW |