| 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 ASSERT_NE(-1, fseek(fp, digested_size, SEEK_SET)); | 142 ASSERT_NE(-1, fseek(fp, digested_size, SEEK_SET)); |
| 143 ASSERT_EQ(1U, fwrite(&new_digest, sizeof(new_digest), 1, fp)); | 143 ASSERT_EQ(1U, fwrite(&new_digest, sizeof(new_digest), 1, fp)); |
| 144 ASSERT_EQ(file_size, ftell(fp)); | 144 ASSERT_EQ(file_size, ftell(fp)); |
| 145 } | 145 } |
| 146 | 146 |
| 147 // Open |filename| and increment the int32 at |offset| by |inc|. | 147 // Open |filename| and increment the int32 at |offset| by |inc|. |
| 148 // Then re-generate the checksum to account for the new contents. | 148 // Then re-generate the checksum to account for the new contents. |
| 149 void ModifyAndCleanChecksum(const base::FilePath& filename, long offset, | 149 void ModifyAndCleanChecksum(const base::FilePath& filename, long offset, |
| 150 int inc) { | 150 int inc) { |
| 151 int64 size_64; | 151 int64 size_64; |
| 152 ASSERT_TRUE(file_util::GetFileSize(filename, &size_64)); | 152 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); |
| 153 | 153 |
| 154 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); | 154 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); |
| 155 IncrementIntAt(file.get(), offset, inc); | 155 IncrementIntAt(file.get(), offset, inc); |
| 156 CleanChecksum(file.get()); | 156 CleanChecksum(file.get()); |
| 157 file.reset(); | 157 file.reset(); |
| 158 | 158 |
| 159 int64 new_size_64; | 159 int64 new_size_64; |
| 160 ASSERT_TRUE(file_util::GetFileSize(filename, &new_size_64)); | 160 ASSERT_TRUE(base::GetFileSize(filename, &new_size_64)); |
| 161 ASSERT_EQ(new_size_64, size_64); | 161 ASSERT_EQ(new_size_64, size_64); |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Tests should not modify this shared resource. | 164 // Tests should not modify this shared resource. |
| 165 static std::vector<SBPrefix> shared_prefixes_; | 165 static std::vector<SBPrefix> shared_prefixes_; |
| 166 | 166 |
| 167 base::ScopedTempDir temp_dir_; | 167 base::ScopedTempDir temp_dir_; |
| 168 }; | 168 }; |
| 169 | 169 |
| 170 std::vector<SBPrefix> PrefixSetTest::shared_prefixes_; | 170 std::vector<SBPrefix> PrefixSetTest::shared_prefixes_; |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 450 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
| 451 ASSERT_FALSE(prefix_set.get()); | 451 ASSERT_FALSE(prefix_set.get()); |
| 452 } | 452 } |
| 453 | 453 |
| 454 // Test corruption in the digest itself. | 454 // Test corruption in the digest itself. |
| 455 TEST_F(PrefixSetTest, CorruptionDigest) { | 455 TEST_F(PrefixSetTest, CorruptionDigest) { |
| 456 base::FilePath filename; | 456 base::FilePath filename; |
| 457 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 457 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 458 | 458 |
| 459 int64 size_64; | 459 int64 size_64; |
| 460 ASSERT_TRUE(file_util::GetFileSize(filename, &size_64)); | 460 ASSERT_TRUE(base::GetFileSize(filename, &size_64)); |
| 461 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); | 461 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); |
| 462 long digest_offset = static_cast<long>(size_64 - sizeof(base::MD5Digest)); | 462 long digest_offset = static_cast<long>(size_64 - sizeof(base::MD5Digest)); |
| 463 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), digest_offset, 1)); | 463 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), digest_offset, 1)); |
| 464 file.reset(); | 464 file.reset(); |
| 465 scoped_ptr<safe_browsing::PrefixSet> | 465 scoped_ptr<safe_browsing::PrefixSet> |
| 466 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 466 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
| 467 ASSERT_FALSE(prefix_set.get()); | 467 ASSERT_FALSE(prefix_set.get()); |
| 468 } | 468 } |
| 469 | 469 |
| 470 // Test excess data after the digest (fails the size test). | 470 // Test excess data after the digest (fails the size test). |
| 471 TEST_F(PrefixSetTest, CorruptionExcess) { | 471 TEST_F(PrefixSetTest, CorruptionExcess) { |
| 472 base::FilePath filename; | 472 base::FilePath filename; |
| 473 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 473 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 474 | 474 |
| 475 // Add some junk to the trunk. | 475 // Add some junk to the trunk. |
| 476 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); | 476 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); |
| 477 const char buf[] = "im in ur base, killing ur d00dz."; | 477 const char buf[] = "im in ur base, killing ur d00dz."; |
| 478 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); | 478 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); |
| 479 file.reset(); | 479 file.reset(); |
| 480 scoped_ptr<safe_browsing::PrefixSet> | 480 scoped_ptr<safe_browsing::PrefixSet> |
| 481 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 481 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
| 482 ASSERT_FALSE(prefix_set.get()); | 482 ASSERT_FALSE(prefix_set.get()); |
| 483 } | 483 } |
| 484 | 484 |
| 485 } // namespace | 485 } // namespace |
| OLD | NEW |