Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: components/safe_browsing_db/v4_store.h

Issue 2384893002: PVer4: Test checksum on startup outside the hotpath of DB load (Closed)
Patch Set: Remove all histogram related changes. That would be a separate CL Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 185
186 private: 186 private:
187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile); 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromEmptyFile);
188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); 188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile);
189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); 189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile);
190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); 190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile);
191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); 191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile);
192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); 192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile);
193 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); 193 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile);
194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); 194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 TestHashPrefixExistsInMapWithSingleSize); 239 TestHashPrefixExistsInMapWithSingleSize);
240 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 240 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
241 TestHashPrefixExistsInMapWithDifferentSizes); 241 TestHashPrefixExistsInMapWithDifferentSizes);
242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
243 TestHashPrefixDoesNotExistInMapWithDifferentSizes); 243 TestHashPrefixDoesNotExistInMapWithDifferentSizes);
244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, 244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest,
245 TestAdditionsWithRiceEncodingFailsWithInvalidInput); 245 TestAdditionsWithRiceEncodingFailsWithInvalidInput);
246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); 246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds);
247 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); 247 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds);
248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum); 248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum);
249 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestChecksumErrorOnStartup);
249 friend class V4StoreTest; 250 friend class V4StoreTest;
250 251
251 // If |prefix_size| is within expected range, and |raw_hashes_length| is a 252 // 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 253 // 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 254 // |raw_hashes_length| starting at |raw_hashes_begin| as the value at key
254 // |prefix_size| in |additions_map| 255 // |prefix_size| in |additions_map|
255 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, 256 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size,
256 const char* raw_hashes_begin, 257 const char* raw_hashes_begin,
257 const size_t raw_hashes_length, 258 const size_t raw_hashes_length,
258 HashPrefixMap* additions_map); 259 HashPrefixMap* additions_map);
(...skipping 27 matching lines...) Expand all
286 // list is exact. This ignores the space that would otherwise be released by 287 // 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 288 // 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 289 // those deletions upfront. This isn't so bad since deletions are supposed to
289 // be small and infrequent. 290 // be small and infrequent.
290 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, 291 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map,
291 HashPrefixMap* prefix_map_to_update); 292 HashPrefixMap* prefix_map_to_update);
292 293
293 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the 294 // 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. 295 // 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. 296 // 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 297 // The SHA256 checksum of the final list of hash prefixes, in
297 // sorted order, must match |expected_checksum| (if it's not empty). 298 // lexicographically sorted order, must match |expected_checksum| (if it's not
299 // empty).
298 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, 300 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map,
299 const HashPrefixMap& additions_map, 301 const HashPrefixMap& additions_map,
300 const ::google::protobuf::RepeatedField< 302 const ::google::protobuf::RepeatedField<
301 ::google::protobuf::int32>* raw_removals, 303 ::google::protobuf::int32>* raw_removals,
302 const std::string& expected_checksum); 304 const std::string& expected_checksum);
303 305
304 // Processes the FULL_UPDATE |response| from the server, and writes the 306 // Processes the FULL_UPDATE |response| from the server, and writes the
305 // merged V4Store to disk. If processing the |response| succeeds, it returns 307 // merged V4Store to disk. If processing the |response| succeeds, it returns
306 // APPLY_UPDATE_SUCCESS. 308 // APPLY_UPDATE_SUCCESS.
307 // This method is only called when we receive a FULL_UPDATE from the server. 309 // This method is only called when we receive a FULL_UPDATE from the server.
(...skipping 20 matching lines...) Expand all
328 // updates the |hash_prefix_map_| and |state_| in the V4Store. If processing 330 // updates the |hash_prefix_map_| and |state_| in the V4Store. If processing
329 // succeeds, it returns APPLY_UPDATE_SUCCESS. 331 // succeeds, it returns APPLY_UPDATE_SUCCESS.
330 ApplyUpdateResult ProcessUpdate( 332 ApplyUpdateResult ProcessUpdate(
331 const HashPrefixMap& hash_prefix_map_old, 333 const HashPrefixMap& hash_prefix_map_old,
332 const std::unique_ptr<ListUpdateResponse>& response); 334 const std::unique_ptr<ListUpdateResponse>& response);
333 335
334 // Reads the state of the store from the file on disk and returns the reason 336 // Reads the state of the store from the file on disk and returns the reason
335 // for the failure or reports success. 337 // for the failure or reports success.
336 StoreReadResult ReadFromDisk(); 338 StoreReadResult ReadFromDisk();
337 339
340 // Resets the state of the store on the IO thread.
341 void ResetOnIOThread();
342
338 // Updates the |additions_map| with the additions received in the partial 343 // Updates the |additions_map| with the additions received in the partial
339 // update from the server. 344 // update from the server.
340 ApplyUpdateResult UpdateHashPrefixMapFromAdditions( 345 ApplyUpdateResult UpdateHashPrefixMapFromAdditions(
341 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, 346 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions,
342 HashPrefixMap* additions_map); 347 HashPrefixMap* additions_map);
343 348
349 // Scheduled when reading the store file from disk on startup. When run, it
350 // ensures that the checksum of the hash prefixes in lexicographical sorted
351 // order matches the expected value. If not, it clears out the contents of
352 // the store.
353 void VerifyChecksumDelayed(const std::string& expected_checksum);
354
344 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. 355 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto.
345 StoreWriteResult WriteToDisk( 356 StoreWriteResult WriteToDisk(
346 std::unique_ptr<ListUpdateResponse> response) const; 357 std::unique_ptr<ListUpdateResponse> response) const;
347 358
348 // The state of the store as returned by the PVer4 server in the last applied 359 // The state of the store as returned by the PVer4 server in the last applied
349 // update response. 360 // update response.
350 std::string state_; 361 std::string state_;
351 const base::FilePath store_path_; 362 const base::FilePath store_path_;
352 HashPrefixMap hash_prefix_map_; 363 HashPrefixMap hash_prefix_map_;
353 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 364 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
354 }; 365 };
355 366
356 std::ostream& operator<<(std::ostream& os, const V4Store& store); 367 std::ostream& operator<<(std::ostream& os, const V4Store& store);
357 368
358 } // namespace safe_browsing 369 } // namespace safe_browsing
359 370
360 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ 371 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698