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/safe_browsing_store_file.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_file.h" |
8 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
9 #include "base/md5.h" | 11 #include "base/md5.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "testing/platform_test.h" | 13 #include "testing/platform_test.h" |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 const int kAddChunk1 = 1; | 17 const int kAddChunk1 = 1; |
16 const int kAddChunk2 = 3; | 18 const int kAddChunk2 = 3; |
17 const int kAddChunk3 = 5; | 19 const int kAddChunk3 = 5; |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 std::vector<SBAddFullHash> pending_adds; | 408 std::vector<SBAddFullHash> pending_adds; |
407 SBAddPrefixes orig_prefixes; | 409 SBAddPrefixes orig_prefixes; |
408 std::vector<SBAddFullHash> orig_hashes; | 410 std::vector<SBAddFullHash> orig_hashes; |
409 EXPECT_TRUE(store_->BeginUpdate()); | 411 EXPECT_TRUE(store_->BeginUpdate()); |
410 EXPECT_TRUE(store_->FinishUpdate(pending_adds, &orig_prefixes, &orig_hashes)); | 412 EXPECT_TRUE(store_->FinishUpdate(pending_adds, &orig_prefixes, &orig_hashes)); |
411 EXPECT_GT(orig_prefixes.size(), 0U); | 413 EXPECT_GT(orig_prefixes.size(), 0U); |
412 EXPECT_GT(orig_hashes.size(), 0U); | 414 EXPECT_GT(orig_hashes.size(), 0U); |
413 EXPECT_FALSE(corruption_detected_); | 415 EXPECT_FALSE(corruption_detected_); |
414 | 416 |
415 // Corrupt the store. | 417 // Corrupt the store. |
416 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); | 418 base::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
417 const long kOffset = 60; | 419 const long kOffset = 60; |
418 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 420 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
419 const uint32 kZero = 0; | 421 const uint32 kZero = 0; |
420 uint32 previous = kZero; | 422 uint32 previous = kZero; |
421 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); | 423 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); |
422 EXPECT_NE(previous, kZero); | 424 EXPECT_NE(previous, kZero); |
423 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 425 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
424 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); | 426 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); |
425 file.reset(); | 427 file.reset(); |
426 | 428 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { | 474 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { |
473 PopulateStore(base::Time::Now()); | 475 PopulateStore(base::Time::Now()); |
474 EXPECT_TRUE(base::PathExists(filename_)); | 476 EXPECT_TRUE(base::PathExists(filename_)); |
475 | 477 |
476 // 37 is the most random prime number. It's also past the header, | 478 // 37 is the most random prime number. It's also past the header, |
477 // as corrupting the header would fail BeginUpdate() in which case | 479 // as corrupting the header would fail BeginUpdate() in which case |
478 // CheckValidity() cannot be called. | 480 // CheckValidity() cannot be called. |
479 const size_t kOffset = 37; | 481 const size_t kOffset = 37; |
480 | 482 |
481 { | 483 { |
482 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); | 484 base::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
483 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); | 485 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); |
484 EXPECT_GE(fputs("hello", file.get()), 0); | 486 EXPECT_GE(fputs("hello", file.get()), 0); |
485 } | 487 } |
486 ASSERT_TRUE(store_->BeginUpdate()); | 488 ASSERT_TRUE(store_->BeginUpdate()); |
487 EXPECT_FALSE(corruption_detected_); | 489 EXPECT_FALSE(corruption_detected_); |
488 EXPECT_FALSE(store_->CheckValidity()); | 490 EXPECT_FALSE(store_->CheckValidity()); |
489 EXPECT_TRUE(corruption_detected_); | 491 EXPECT_TRUE(corruption_detected_); |
490 EXPECT_TRUE(store_->CancelUpdate()); | 492 EXPECT_TRUE(store_->CancelUpdate()); |
491 } | 493 } |
492 | 494 |
493 // Corrupt the checksum. | 495 // Corrupt the checksum. |
494 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { | 496 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { |
495 PopulateStore(base::Time::Now()); | 497 PopulateStore(base::Time::Now()); |
496 EXPECT_TRUE(base::PathExists(filename_)); | 498 EXPECT_TRUE(base::PathExists(filename_)); |
497 | 499 |
498 // An offset from the end of the file which is in the checksum. | 500 // An offset from the end of the file which is in the checksum. |
499 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); | 501 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); |
500 | 502 |
501 { | 503 { |
502 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); | 504 base::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
503 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); | 505 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); |
504 EXPECT_GE(fputs("hello", file.get()), 0); | 506 EXPECT_GE(fputs("hello", file.get()), 0); |
505 } | 507 } |
506 ASSERT_TRUE(store_->BeginUpdate()); | 508 ASSERT_TRUE(store_->BeginUpdate()); |
507 EXPECT_FALSE(corruption_detected_); | 509 EXPECT_FALSE(corruption_detected_); |
508 EXPECT_FALSE(store_->CheckValidity()); | 510 EXPECT_FALSE(store_->CheckValidity()); |
509 EXPECT_TRUE(corruption_detected_); | 511 EXPECT_TRUE(corruption_detected_); |
510 EXPECT_TRUE(store_->CancelUpdate()); | 512 EXPECT_TRUE(store_->CancelUpdate()); |
511 } | 513 } |
512 | 514 |
513 } // namespace | 515 } // namespace |
OLD | NEW |