OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
7 | 7 |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 // If a hash prefix in this store matches |full_hash|, returns that hash | 173 // If a hash prefix in this store matches |full_hash|, returns that hash |
174 // prefix; otherwise returns an empty hash prefix. | 174 // prefix; otherwise returns an empty hash prefix. |
175 virtual HashPrefix GetMatchingHashPrefix(const FullHash& full_hash); | 175 virtual HashPrefix GetMatchingHashPrefix(const FullHash& full_hash); |
176 | 176 |
177 std::string DebugString() const; | 177 std::string DebugString() const; |
178 | 178 |
179 // Reads the store file from disk and populates the in-memory representation | 179 // Reads the store file from disk and populates the in-memory representation |
180 // of the hash prefixes. | 180 // of the hash prefixes. |
181 void Initialize(); | 181 void Initialize(); |
182 | 182 |
183 // Reset internal state and delete the backing file. | 183 // Reset internal state. |
184 virtual bool Reset(); | 184 void Reset(); |
185 | |
186 // Scheduled after reading the store file from disk on startup. When run, it | |
187 // ensures that the checksum of the hash prefixes in lexicographical sorted | |
188 // order matches the expected value in |expected_checksum_|. Returns ture if | |
Nathan Parker
2016/10/07 23:24:29
nit: s/ture/true
I'd comment on why it's run afte
vakh (use Gerrit instead)
2016/10/10 17:42:34
Done.
| |
189 // it matches; false otherwise. | |
190 bool VerifyChecksum(); | |
185 | 191 |
186 private: | 192 private: |
187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile); | 193 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile); |
188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); | 194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); |
189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); | 195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); |
190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); | 196 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); |
191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); | 197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); |
192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); | 198 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); |
193 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 200 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 TestHashPrefixExistsInMapWithSingleSize); | 245 TestHashPrefixExistsInMapWithSingleSize); |
240 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
241 TestHashPrefixExistsInMapWithDifferentSizes); | 247 TestHashPrefixExistsInMapWithDifferentSizes); |
242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
243 TestHashPrefixDoesNotExistInMapWithDifferentSizes); | 249 TestHashPrefixDoesNotExistInMapWithDifferentSizes); |
244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 250 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
245 TestAdditionsWithRiceEncodingFailsWithInvalidInput); | 251 TestAdditionsWithRiceEncodingFailsWithInvalidInput); |
246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); | 252 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); |
247 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); | 253 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); |
248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum); | 254 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum); |
255 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestChecksumErrorOnStartup); | |
249 friend class V4StoreTest; | 256 friend class V4StoreTest; |
250 | 257 |
251 // If |prefix_size| is within expected range, and |raw_hashes_length| is a | 258 // If |prefix_size| is within expected range, and |raw_hashes_length| is a |
252 // multiple of prefix_size, then it sets the string of length | 259 // multiple of prefix_size, then it sets the string of length |
253 // |raw_hashes_length| starting at |raw_hashes_begin| as the value at key | 260 // |raw_hashes_length| starting at |raw_hashes_begin| as the value at key |
254 // |prefix_size| in |additions_map| | 261 // |prefix_size| in |additions_map| |
255 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | 262 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, |
256 const char* raw_hashes_begin, | 263 const char* raw_hashes_begin, |
257 const size_t raw_hashes_length, | 264 const size_t raw_hashes_length, |
258 HashPrefixMap* additions_map); | 265 HashPrefixMap* additions_map); |
(...skipping 27 matching lines...) Expand all Loading... | |
286 // list is exact. This ignores the space that would otherwise be released by | 293 // list is exact. This ignores the space that would otherwise be released by |
287 // deletions specified in the update because it is non-trivial to calculate | 294 // deletions specified in the update because it is non-trivial to calculate |
288 // those deletions upfront. This isn't so bad since deletions are supposed to | 295 // those deletions upfront. This isn't so bad since deletions are supposed to |
289 // be small and infrequent. | 296 // be small and infrequent. |
290 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, | 297 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, |
291 HashPrefixMap* prefix_map_to_update); | 298 HashPrefixMap* prefix_map_to_update); |
292 | 299 |
293 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the | 300 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the |
294 // update (additions_map) to populate the prefix map for the current store. | 301 // update (additions_map) to populate the prefix map for the current store. |
295 // The indices in the |raw_removals| list, which may be NULL, are not merged. | 302 // The indices in the |raw_removals| list, which may be NULL, are not merged. |
296 // The SHA256 checksum of the final list of hash prefixes, in lexographically | 303 // The SHA256 checksum of the final list of hash prefixes, in |
297 // sorted order, must match |expected_checksum| (if it's not empty). | 304 // lexicographically sorted order, must match |expected_checksum| (if it's not |
305 // empty). | |
298 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, | 306 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, |
299 const HashPrefixMap& additions_map, | 307 const HashPrefixMap& additions_map, |
300 const ::google::protobuf::RepeatedField< | 308 const ::google::protobuf::RepeatedField< |
301 ::google::protobuf::int32>* raw_removals, | 309 ::google::protobuf::int32>* raw_removals, |
302 const std::string& expected_checksum); | 310 const std::string& expected_checksum); |
303 | 311 |
304 // Processes the FULL_UPDATE |response| from the server, and writes the | 312 // Processes the FULL_UPDATE |response| from the server, and writes the |
305 // merged V4Store to disk. If processing the |response| succeeds, it returns | 313 // merged V4Store to disk. If processing the |response| succeeds, it returns |
306 // APPLY_UPDATE_SUCCESS. | 314 // APPLY_UPDATE_SUCCESS. |
307 // This method is only called when we receive a FULL_UPDATE from the server. | 315 // This method is only called when we receive a FULL_UPDATE from the server. |
(...skipping 30 matching lines...) Expand all Loading... | |
338 // Updates the |additions_map| with the additions received in the partial | 346 // Updates the |additions_map| with the additions received in the partial |
339 // update from the server. | 347 // update from the server. |
340 ApplyUpdateResult UpdateHashPrefixMapFromAdditions( | 348 ApplyUpdateResult UpdateHashPrefixMapFromAdditions( |
341 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, | 349 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, |
342 HashPrefixMap* additions_map); | 350 HashPrefixMap* additions_map); |
343 | 351 |
344 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. | 352 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
345 StoreWriteResult WriteToDisk( | 353 StoreWriteResult WriteToDisk( |
346 std::unique_ptr<ListUpdateResponse> response) const; | 354 std::unique_ptr<ListUpdateResponse> response) const; |
347 | 355 |
356 // The checksum value as read from the disk, until it is verified. Once | |
357 // verified, it is cleared. | |
358 std::string expected_checksum_; | |
359 | |
348 // The state of the store as returned by the PVer4 server in the last applied | 360 // The state of the store as returned by the PVer4 server in the last applied |
349 // update response. | 361 // update response. |
350 std::string state_; | 362 std::string state_; |
351 const base::FilePath store_path_; | 363 const base::FilePath store_path_; |
352 HashPrefixMap hash_prefix_map_; | 364 HashPrefixMap hash_prefix_map_; |
353 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 365 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
354 }; | 366 }; |
355 | 367 |
356 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 368 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
357 | 369 |
358 } // namespace safe_browsing | 370 } // namespace safe_browsing |
359 | 371 |
360 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 372 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
OLD | NEW |