| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 std::vector<SBAddFullHash> pending_adds; | 83 std::vector<SBAddFullHash> pending_adds; |
| 84 SBAddPrefixes orig_prefixes; | 84 SBAddPrefixes orig_prefixes; |
| 85 std::vector<SBAddFullHash> orig_hashes; | 85 std::vector<SBAddFullHash> orig_hashes; |
| 86 EXPECT_TRUE(store_->BeginUpdate()); | 86 EXPECT_TRUE(store_->BeginUpdate()); |
| 87 EXPECT_TRUE(store_->FinishUpdate(pending_adds, &orig_prefixes, &orig_hashes)); | 87 EXPECT_TRUE(store_->FinishUpdate(pending_adds, &orig_prefixes, &orig_hashes)); |
| 88 EXPECT_GT(orig_prefixes.size(), 0U); | 88 EXPECT_GT(orig_prefixes.size(), 0U); |
| 89 EXPECT_GT(orig_hashes.size(), 0U); | 89 EXPECT_GT(orig_hashes.size(), 0U); |
| 90 EXPECT_FALSE(corruption_detected_); | 90 EXPECT_FALSE(corruption_detected_); |
| 91 | 91 |
| 92 // Corrupt the store. | 92 // Corrupt the store. |
| 93 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 93 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
| 94 const long kOffset = 60; | 94 const long kOffset = 60; |
| 95 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 95 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
| 96 const int32 kZero = 0; | 96 const int32 kZero = 0; |
| 97 int32 previous = kZero; | 97 int32 previous = kZero; |
| 98 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); | 98 EXPECT_EQ(fread(&previous, sizeof(previous), 1, file.get()), 1U); |
| 99 EXPECT_NE(previous, kZero); | 99 EXPECT_NE(previous, kZero); |
| 100 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); | 100 EXPECT_EQ(fseek(file.get(), kOffset, SEEK_SET), 0); |
| 101 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); | 101 EXPECT_EQ(fwrite(&kZero, sizeof(kZero), 1, file.get()), 1U); |
| 102 file.reset(); | 102 file.reset(); |
| 103 | 103 |
| 104 // Update fails and corruption callback is called. | 104 // Update fails and corruption callback is called. |
| 105 SBAddPrefixes add_prefixes; | 105 SBAddPrefixes add_prefixes; |
| 106 std::vector<SBAddFullHash> add_hashes; | 106 std::vector<SBAddFullHash> add_hashes; |
| 107 corruption_detected_ = false; | 107 corruption_detected_ = false; |
| 108 EXPECT_TRUE(store_->BeginUpdate()); | 108 EXPECT_TRUE(store_->BeginUpdate()); |
| 109 EXPECT_FALSE(store_->FinishUpdate(pending_adds, &add_prefixes, &add_hashes)); | 109 EXPECT_FALSE(store_->FinishUpdate(pending_adds, &add_prefixes, &add_hashes)); |
| 110 EXPECT_TRUE(corruption_detected_); | 110 EXPECT_TRUE(corruption_detected_); |
| 111 EXPECT_EQ(add_prefixes.size(), 0U); | 111 EXPECT_EQ(add_prefixes.size(), 0U); |
| 112 EXPECT_EQ(add_hashes.size(), 0U); | 112 EXPECT_EQ(add_hashes.size(), 0U); |
| 113 | 113 |
| 114 // 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. |
| 115 const long kAddChunkCountOffset = 2 * sizeof(int32); | 115 const long kAddChunkCountOffset = 2 * sizeof(int32); |
| 116 const int32 kLargeCount = 1000 * 1000 * 1000; | 116 const int32 kLargeCount = 1000 * 1000 * 1000; |
| 117 file.reset(file_util::OpenFile(filename_, "rb+")); | 117 file.reset(base::OpenFile(filename_, "rb+")); |
| 118 EXPECT_EQ(fseek(file.get(), kAddChunkCountOffset, SEEK_SET), 0); | 118 EXPECT_EQ(fseek(file.get(), kAddChunkCountOffset, SEEK_SET), 0); |
| 119 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); | 119 EXPECT_EQ(fwrite(&kLargeCount, sizeof(kLargeCount), 1, file.get()), 1U); |
| 120 file.reset(); | 120 file.reset(); |
| 121 | 121 |
| 122 // Detects corruption and fails to even begin the update. | 122 // Detects corruption and fails to even begin the update. |
| 123 corruption_detected_ = false; | 123 corruption_detected_ = false; |
| 124 EXPECT_FALSE(store_->BeginUpdate()); | 124 EXPECT_FALSE(store_->BeginUpdate()); |
| 125 EXPECT_TRUE(corruption_detected_); | 125 EXPECT_TRUE(corruption_detected_); |
| 126 } | 126 } |
| 127 | 127 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 149 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { | 149 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { |
| 150 SafeBrowsingStoreTestStorePrefix(store_.get()); | 150 SafeBrowsingStoreTestStorePrefix(store_.get()); |
| 151 EXPECT_TRUE(base::PathExists(filename_)); | 151 EXPECT_TRUE(base::PathExists(filename_)); |
| 152 | 152 |
| 153 // 37 is the most random prime number. It's also past the header, | 153 // 37 is the most random prime number. It's also past the header, |
| 154 // as corrupting the header would fail BeginUpdate() in which case | 154 // as corrupting the header would fail BeginUpdate() in which case |
| 155 // CheckValidity() cannot be called. | 155 // CheckValidity() cannot be called. |
| 156 const size_t kOffset = 37; | 156 const size_t kOffset = 37; |
| 157 | 157 |
| 158 { | 158 { |
| 159 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 159 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
| 160 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); | 160 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); |
| 161 EXPECT_GE(fputs("hello", file.get()), 0); | 161 EXPECT_GE(fputs("hello", file.get()), 0); |
| 162 } | 162 } |
| 163 ASSERT_TRUE(store_->BeginUpdate()); | 163 ASSERT_TRUE(store_->BeginUpdate()); |
| 164 EXPECT_FALSE(corruption_detected_); | 164 EXPECT_FALSE(corruption_detected_); |
| 165 EXPECT_FALSE(store_->CheckValidity()); | 165 EXPECT_FALSE(store_->CheckValidity()); |
| 166 EXPECT_TRUE(corruption_detected_); | 166 EXPECT_TRUE(corruption_detected_); |
| 167 EXPECT_TRUE(store_->CancelUpdate()); | 167 EXPECT_TRUE(store_->CancelUpdate()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Corrupt the checksum. | 170 // Corrupt the checksum. |
| 171 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { | 171 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { |
| 172 SafeBrowsingStoreTestStorePrefix(store_.get()); | 172 SafeBrowsingStoreTestStorePrefix(store_.get()); |
| 173 EXPECT_TRUE(base::PathExists(filename_)); | 173 EXPECT_TRUE(base::PathExists(filename_)); |
| 174 | 174 |
| 175 // An offset from the end of the file which is in the checksum. | 175 // An offset from the end of the file which is in the checksum. |
| 176 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); | 176 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); |
| 177 | 177 |
| 178 { | 178 { |
| 179 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 179 file_util::ScopedFILE file(base::OpenFile(filename_, "rb+")); |
| 180 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); | 180 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); |
| 181 EXPECT_GE(fputs("hello", file.get()), 0); | 181 EXPECT_GE(fputs("hello", file.get()), 0); |
| 182 } | 182 } |
| 183 ASSERT_TRUE(store_->BeginUpdate()); | 183 ASSERT_TRUE(store_->BeginUpdate()); |
| 184 EXPECT_FALSE(corruption_detected_); | 184 EXPECT_FALSE(corruption_detected_); |
| 185 EXPECT_FALSE(store_->CheckValidity()); | 185 EXPECT_FALSE(store_->CheckValidity()); |
| 186 EXPECT_TRUE(corruption_detected_); | 186 EXPECT_TRUE(corruption_detected_); |
| 187 EXPECT_TRUE(store_->CancelUpdate()); | 187 EXPECT_TRUE(store_->CancelUpdate()); |
| 188 } | 188 } |
| 189 | 189 |
| 190 } // namespace | 190 } // namespace |
| OLD | NEW |