| 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(base::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(base::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(base::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. |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 CheckPrefixes(*prefix_set, prefixes); | 366 CheckPrefixes(*prefix_set, prefixes); |
| 367 } | 367 } |
| 368 } | 368 } |
| 369 | 369 |
| 370 // Check that |CleanChecksum()| makes an acceptable checksum. | 370 // Check that |CleanChecksum()| makes an acceptable checksum. |
| 371 TEST_F(PrefixSetTest, CorruptionHelpers) { | 371 TEST_F(PrefixSetTest, CorruptionHelpers) { |
| 372 base::FilePath filename; | 372 base::FilePath filename; |
| 373 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 373 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 374 | 374 |
| 375 // This will modify data in |index_|, which will fail the digest check. | 375 // This will modify data in |index_|, which will fail the digest check. |
| 376 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); | 376 file_util::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 377 IncrementIntAt(file.get(), kPayloadOffset, 1); | 377 IncrementIntAt(file.get(), kPayloadOffset, 1); |
| 378 file.reset(); | 378 file.reset(); |
| 379 scoped_ptr<safe_browsing::PrefixSet> | 379 scoped_ptr<safe_browsing::PrefixSet> |
| 380 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 380 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
| 381 ASSERT_FALSE(prefix_set.get()); | 381 ASSERT_FALSE(prefix_set.get()); |
| 382 | 382 |
| 383 // Fix up the checksum and it will read successfully (though the | 383 // Fix up the checksum and it will read successfully (though the |
| 384 // data will be wrong). | 384 // data will be wrong). |
| 385 file.reset(file_util::OpenFile(filename, "r+b")); | 385 file.reset(base::OpenFile(filename, "r+b")); |
| 386 CleanChecksum(file.get()); | 386 CleanChecksum(file.get()); |
| 387 file.reset(); | 387 file.reset(); |
| 388 prefix_set.reset(safe_browsing::PrefixSet::LoadFile(filename)); | 388 prefix_set.reset(safe_browsing::PrefixSet::LoadFile(filename)); |
| 389 ASSERT_TRUE(prefix_set.get()); | 389 ASSERT_TRUE(prefix_set.get()); |
| 390 } | 390 } |
| 391 | 391 |
| 392 // Bad |index_| size is caught by the sanity check. | 392 // Bad |index_| size is caught by the sanity check. |
| 393 TEST_F(PrefixSetTest, CorruptionMagic) { | 393 TEST_F(PrefixSetTest, CorruptionMagic) { |
| 394 base::FilePath filename; | 394 base::FilePath filename; |
| 395 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 395 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 436 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
| 437 ASSERT_FALSE(prefix_set.get()); | 437 ASSERT_FALSE(prefix_set.get()); |
| 438 } | 438 } |
| 439 | 439 |
| 440 // Test that the digest catches corruption in the middle of the file | 440 // Test that the digest catches corruption in the middle of the file |
| 441 // (in the payload between the header and the digest). | 441 // (in the payload between the header and the digest). |
| 442 TEST_F(PrefixSetTest, CorruptionPayload) { | 442 TEST_F(PrefixSetTest, CorruptionPayload) { |
| 443 base::FilePath filename; | 443 base::FilePath filename; |
| 444 ASSERT_TRUE(GetPrefixSetFile(&filename)); | 444 ASSERT_TRUE(GetPrefixSetFile(&filename)); |
| 445 | 445 |
| 446 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); | 446 file_util::ScopedFILE file(base::OpenFile(filename, "r+b")); |
| 447 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), 666, 1)); | 447 ASSERT_NO_FATAL_FAILURE(IncrementIntAt(file.get(), 666, 1)); |
| 448 file.reset(); | 448 file.reset(); |
| 449 scoped_ptr<safe_browsing::PrefixSet> | 449 scoped_ptr<safe_browsing::PrefixSet> |
| 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(base::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(base::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(base::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 |