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