| 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_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/md5.h" | 9 #include "base/md5.h" |
| 10 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" | 10 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 EXPECT_FALSE(base::PathExists(temp_file)); | 74 EXPECT_FALSE(base::PathExists(temp_file)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Test basic corruption-handling. | 77 // Test basic corruption-handling. |
| 78 TEST_F(SafeBrowsingStoreFileTest, DetectsCorruption) { | 78 TEST_F(SafeBrowsingStoreFileTest, DetectsCorruption) { |
| 79 // Load a store with some data. | 79 // Load a store with some data. |
| 80 SafeBrowsingStoreTestStorePrefix(store_.get()); | 80 SafeBrowsingStoreTestStorePrefix(store_.get()); |
| 81 | 81 |
| 82 // Can successfully open and read the store. | 82 // Can successfully open and read the store. |
| 83 std::vector<SBAddFullHash> pending_adds; | 83 std::vector<SBAddFullHash> pending_adds; |
| 84 std::set<SBPrefix> prefix_misses; | |
| 85 SBAddPrefixes orig_prefixes; | 84 SBAddPrefixes orig_prefixes; |
| 86 std::vector<SBAddFullHash> orig_hashes; | 85 std::vector<SBAddFullHash> orig_hashes; |
| 87 EXPECT_TRUE(store_->BeginUpdate()); | 86 EXPECT_TRUE(store_->BeginUpdate()); |
| 88 EXPECT_TRUE(store_->FinishUpdate(pending_adds, prefix_misses, | 87 EXPECT_TRUE(store_->FinishUpdate(pending_adds, &orig_prefixes, &orig_hashes)); |
| 89 &orig_prefixes, &orig_hashes)); | |
| 90 EXPECT_GT(orig_prefixes.size(), 0U); | 88 EXPECT_GT(orig_prefixes.size(), 0U); |
| 91 EXPECT_GT(orig_hashes.size(), 0U); | 89 EXPECT_GT(orig_hashes.size(), 0U); |
| 92 EXPECT_FALSE(corruption_detected_); | 90 EXPECT_FALSE(corruption_detected_); |
| 93 | 91 |
| 94 // Corrupt the store. | 92 // Corrupt the store. |
| 95 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 93 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); |
| 96 const long kOffset = 60; | 94 const long kOffset = 60; |
| 97 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 95 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
| 98 const int32 kZero = 0; | 96 const int32 kZero = 0; |
| 99 int32 previous = kZero; | 97 int32 previous = kZero; |
| 100 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); | 98 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); |
| 101 EXPECT_NE(previous, kZero); | 99 EXPECT_NE(previous, kZero); |
| 102 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 100 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
| 103 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); | 101 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); |
| 104 file.reset(); | 102 file.reset(); |
| 105 | 103 |
| 106 // Update fails and corruption callback is called. | 104 // Update fails and corruption callback is called. |
| 107 SBAddPrefixes add_prefixes; | 105 SBAddPrefixes add_prefixes; |
| 108 std::vector<SBAddFullHash> add_hashes; | 106 std::vector<SBAddFullHash> add_hashes; |
| 109 corruption_detected_ = false; | 107 corruption_detected_ = false; |
| 110 EXPECT_TRUE(store_->BeginUpdate()); | 108 EXPECT_TRUE(store_->BeginUpdate()); |
| 111 EXPECT_FALSE(store_->FinishUpdate(pending_adds, prefix_misses, | 109 EXPECT_FALSE(store_->FinishUpdate(pending_adds, &add_prefixes, &add_hashes)); |
| 112 &add_prefixes, &add_hashes)); | |
| 113 EXPECT_TRUE(corruption_detected_); | 110 EXPECT_TRUE(corruption_detected_); |
| 114 EXPECT_EQ(add_prefixes.size(), 0U); | 111 EXPECT_EQ(add_prefixes.size(), 0U); |
| 115 EXPECT_EQ(add_hashes.size(), 0U); | 112 EXPECT_EQ(add_hashes.size(), 0U); |
| 116 | 113 |
| 117 // Make it look like there is a lot of add-chunks-seen data. | 114 // Make it look like there is a lot of add-chunks-seen data. |
| 118 const long kAddChunkCountOffset = 2 * sizeof(int32); | 115 const long kAddChunkCountOffset = 2 * sizeof(int32); |
| 119 const int32 kLargeCount = 1000 * 1000 * 1000; | 116 const int32 kLargeCount = 1000 * 1000 * 1000; |
| 120 file.reset(file_util::OpenFile(filename_, "rb+")); | 117 file.reset(file_util::OpenFile(filename_, "rb+")); |
| 121 EXPECT_EQ(fseek(file.get(), kAddChunkCountOffset, SEEK_SET), 0); | 118 EXPECT_EQ(fseek(file.get(), kAddChunkCountOffset, SEEK_SET), 0); |
| 122 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); | 119 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 EXPECT_GE(fputs("hello", file.get()), 0); | 181 EXPECT_GE(fputs("hello", file.get()), 0); |
| 185 } | 182 } |
| 186 ASSERT_TRUE(store_->BeginUpdate()); | 183 ASSERT_TRUE(store_->BeginUpdate()); |
| 187 EXPECT_FALSE(corruption_detected_); | 184 EXPECT_FALSE(corruption_detected_); |
| 188 EXPECT_FALSE(store_->CheckValidity()); | 185 EXPECT_FALSE(store_->CheckValidity()); |
| 189 EXPECT_TRUE(corruption_detected_); | 186 EXPECT_TRUE(corruption_detected_); |
| 190 EXPECT_TRUE(store_->CancelUpdate()); | 187 EXPECT_TRUE(store_->CancelUpdate()); |
| 191 } | 188 } |
| 192 | 189 |
| 193 } // namespace | 190 } // namespace |
| OLD | NEW |