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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 bool corruption_detected_; | 50 bool corruption_detected_; |
51 }; | 51 }; |
52 | 52 |
53 TEST_STORE(SafeBrowsingStoreFileTest, store_.get(), filename_); | 53 TEST_STORE(SafeBrowsingStoreFileTest, store_.get(), filename_); |
54 | 54 |
55 // Test that Delete() deletes the temporary store, if present. | 55 // Test that Delete() deletes the temporary store, if present. |
56 TEST_F(SafeBrowsingStoreFileTest, DeleteTemp) { | 56 TEST_F(SafeBrowsingStoreFileTest, DeleteTemp) { |
57 const base::FilePath temp_file = | 57 const base::FilePath temp_file = |
58 SafeBrowsingStoreFile::TemporaryFileForFilename(filename_); | 58 SafeBrowsingStoreFile::TemporaryFileForFilename(filename_); |
59 | 59 |
60 EXPECT_FALSE(file_util::PathExists(filename_)); | 60 EXPECT_FALSE(base::PathExists(filename_)); |
61 EXPECT_FALSE(file_util::PathExists(temp_file)); | 61 EXPECT_FALSE(base::PathExists(temp_file)); |
62 | 62 |
63 // Starting a transaction creates a temporary file. | 63 // Starting a transaction creates a temporary file. |
64 EXPECT_TRUE(store_->BeginUpdate()); | 64 EXPECT_TRUE(store_->BeginUpdate()); |
65 EXPECT_TRUE(file_util::PathExists(temp_file)); | 65 EXPECT_TRUE(base::PathExists(temp_file)); |
66 | 66 |
67 // Pull the rug out from under the existing store, simulating a | 67 // Pull the rug out from under the existing store, simulating a |
68 // crash. | 68 // crash. |
69 store_.reset(new SafeBrowsingStoreFile()); | 69 store_.reset(new SafeBrowsingStoreFile()); |
70 store_->Init(filename_, base::Closure()); | 70 store_->Init(filename_, base::Closure()); |
71 EXPECT_FALSE(file_util::PathExists(filename_)); | 71 EXPECT_FALSE(base::PathExists(filename_)); |
72 EXPECT_TRUE(file_util::PathExists(temp_file)); | 72 EXPECT_TRUE(base::PathExists(temp_file)); |
73 | 73 |
74 // Make sure the temporary file is deleted. | 74 // Make sure the temporary file is deleted. |
75 EXPECT_TRUE(store_->Delete()); | 75 EXPECT_TRUE(store_->Delete()); |
76 EXPECT_FALSE(file_util::PathExists(filename_)); | 76 EXPECT_FALSE(base::PathExists(filename_)); |
77 EXPECT_FALSE(file_util::PathExists(temp_file)); | 77 EXPECT_FALSE(base::PathExists(temp_file)); |
78 } | 78 } |
79 | 79 |
80 // Test basic corruption-handling. | 80 // Test basic corruption-handling. |
81 TEST_F(SafeBrowsingStoreFileTest, DetectsCorruption) { | 81 TEST_F(SafeBrowsingStoreFileTest, DetectsCorruption) { |
82 // Load a store with some data. | 82 // Load a store with some data. |
83 SafeBrowsingStoreTestStorePrefix(store_.get()); | 83 SafeBrowsingStoreTestStorePrefix(store_.get()); |
84 | 84 |
85 // Can successfully open and read the store. | 85 // Can successfully open and read the store. |
86 std::vector<SBAddFullHash> pending_adds; | 86 std::vector<SBAddFullHash> pending_adds; |
87 std::set<SBPrefix> prefix_misses; | 87 std::set<SBPrefix> prefix_misses; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 file.reset(); | 126 file.reset(); |
127 | 127 |
128 // Detects corruption and fails to even begin the update. | 128 // Detects corruption and fails to even begin the update. |
129 corruption_detected_ = false; | 129 corruption_detected_ = false; |
130 EXPECT_FALSE(store_->BeginUpdate()); | 130 EXPECT_FALSE(store_->BeginUpdate()); |
131 EXPECT_TRUE(corruption_detected_); | 131 EXPECT_TRUE(corruption_detected_); |
132 } | 132 } |
133 | 133 |
134 TEST_F(SafeBrowsingStoreFileTest, CheckValidity) { | 134 TEST_F(SafeBrowsingStoreFileTest, CheckValidity) { |
135 // Empty store is valid. | 135 // Empty store is valid. |
136 EXPECT_FALSE(file_util::PathExists(filename_)); | 136 EXPECT_FALSE(base::PathExists(filename_)); |
137 ASSERT_TRUE(store_->BeginUpdate()); | 137 ASSERT_TRUE(store_->BeginUpdate()); |
138 EXPECT_FALSE(corruption_detected_); | 138 EXPECT_FALSE(corruption_detected_); |
139 EXPECT_TRUE(store_->CheckValidity()); | 139 EXPECT_TRUE(store_->CheckValidity()); |
140 EXPECT_FALSE(corruption_detected_); | 140 EXPECT_FALSE(corruption_detected_); |
141 EXPECT_TRUE(store_->CancelUpdate()); | 141 EXPECT_TRUE(store_->CancelUpdate()); |
142 | 142 |
143 // A store with some data is valid. | 143 // A store with some data is valid. |
144 EXPECT_FALSE(file_util::PathExists(filename_)); | 144 EXPECT_FALSE(base::PathExists(filename_)); |
145 SafeBrowsingStoreTestStorePrefix(store_.get()); | 145 SafeBrowsingStoreTestStorePrefix(store_.get()); |
146 EXPECT_TRUE(file_util::PathExists(filename_)); | 146 EXPECT_TRUE(base::PathExists(filename_)); |
147 ASSERT_TRUE(store_->BeginUpdate()); | 147 ASSERT_TRUE(store_->BeginUpdate()); |
148 EXPECT_FALSE(corruption_detected_); | 148 EXPECT_FALSE(corruption_detected_); |
149 EXPECT_TRUE(store_->CheckValidity()); | 149 EXPECT_TRUE(store_->CheckValidity()); |
150 EXPECT_FALSE(corruption_detected_); | 150 EXPECT_FALSE(corruption_detected_); |
151 EXPECT_TRUE(store_->CancelUpdate()); | 151 EXPECT_TRUE(store_->CancelUpdate()); |
152 } | 152 } |
153 | 153 |
154 // Corrupt the payload. | 154 // Corrupt the payload. |
155 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { | 155 TEST_F(SafeBrowsingStoreFileTest, CheckValidityPayload) { |
156 SafeBrowsingStoreTestStorePrefix(store_.get()); | 156 SafeBrowsingStoreTestStorePrefix(store_.get()); |
157 EXPECT_TRUE(file_util::PathExists(filename_)); | 157 EXPECT_TRUE(base::PathExists(filename_)); |
158 | 158 |
159 // 37 is the most random prime number. It's also past the header, | 159 // 37 is the most random prime number. It's also past the header, |
160 // as corrupting the header would fail BeginUpdate() in which case | 160 // as corrupting the header would fail BeginUpdate() in which case |
161 // CheckValidity() cannot be called. | 161 // CheckValidity() cannot be called. |
162 const size_t kOffset = 37; | 162 const size_t kOffset = 37; |
163 | 163 |
164 { | 164 { |
165 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 165 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); |
166 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); | 166 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_SET)); |
167 EXPECT_GE(fputs("hello", file.get()), 0); | 167 EXPECT_GE(fputs("hello", file.get()), 0); |
168 } | 168 } |
169 ASSERT_TRUE(store_->BeginUpdate()); | 169 ASSERT_TRUE(store_->BeginUpdate()); |
170 EXPECT_FALSE(corruption_detected_); | 170 EXPECT_FALSE(corruption_detected_); |
171 EXPECT_FALSE(store_->CheckValidity()); | 171 EXPECT_FALSE(store_->CheckValidity()); |
172 EXPECT_TRUE(corruption_detected_); | 172 EXPECT_TRUE(corruption_detected_); |
173 EXPECT_TRUE(store_->CancelUpdate()); | 173 EXPECT_TRUE(store_->CancelUpdate()); |
174 } | 174 } |
175 | 175 |
176 // Corrupt the checksum. | 176 // Corrupt the checksum. |
177 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { | 177 TEST_F(SafeBrowsingStoreFileTest, CheckValidityChecksum) { |
178 SafeBrowsingStoreTestStorePrefix(store_.get()); | 178 SafeBrowsingStoreTestStorePrefix(store_.get()); |
179 EXPECT_TRUE(file_util::PathExists(filename_)); | 179 EXPECT_TRUE(base::PathExists(filename_)); |
180 | 180 |
181 // An offset from the end of the file which is in the checksum. | 181 // An offset from the end of the file which is in the checksum. |
182 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); | 182 const int kOffset = -static_cast<int>(sizeof(base::MD5Digest)); |
183 | 183 |
184 { | 184 { |
185 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); | 185 file_util::ScopedFILE file(file_util::OpenFile(filename_, "rb+")); |
186 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); | 186 EXPECT_EQ(0, fseek(file.get(), kOffset, SEEK_END)); |
187 EXPECT_GE(fputs("hello", file.get()), 0); | 187 EXPECT_GE(fputs("hello", file.get()), 0); |
188 } | 188 } |
189 ASSERT_TRUE(store_->BeginUpdate()); | 189 ASSERT_TRUE(store_->BeginUpdate()); |
190 EXPECT_FALSE(corruption_detected_); | 190 EXPECT_FALSE(corruption_detected_); |
191 EXPECT_FALSE(store_->CheckValidity()); | 191 EXPECT_FALSE(store_->CheckValidity()); |
192 EXPECT_TRUE(corruption_detected_); | 192 EXPECT_TRUE(corruption_detected_); |
193 EXPECT_TRUE(store_->CancelUpdate()); | 193 EXPECT_TRUE(store_->CancelUpdate()); |
194 } | 194 } |
195 | 195 |
196 } // namespace | 196 } // namespace |
OLD | NEW |