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

Side by Side Diff: components/safe_browsing_db/v4_store_unittest.cc

Issue 2403913004: Small: Serialize the store's hash_prefix_map_ to file in WriteToDisk() (Closed)
Patch Set: rebase. and temporary scope to make sure that |contents| get destroyed as soon as we're done using … 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
« no previous file with comments | « components/safe_browsing_db/v4_store.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "base/base64.h" 5 #include "base/base64.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 WriteFileFormatProtoToFile(0x600D71FE, 9); 119 WriteFileFormatProtoToFile(0x600D71FE, 9);
120 EXPECT_EQ(HASH_PREFIX_INFO_MISSING_FAILURE, 120 EXPECT_EQ(HASH_PREFIX_INFO_MISSING_FAILURE,
121 V4Store(task_runner_, store_path_).ReadFromDisk()); 121 V4Store(task_runner_, store_path_).ReadFromDisk());
122 } 122 }
123 123
124 TEST_F(V4StoreTest, TestReadFromNoHashPrefixesFile) { 124 TEST_F(V4StoreTest, TestReadFromNoHashPrefixesFile) {
125 ListUpdateResponse list_update_response; 125 ListUpdateResponse list_update_response;
126 list_update_response.set_platform_type(LINUX_PLATFORM); 126 list_update_response.set_platform_type(LINUX_PLATFORM);
127 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); 127 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE);
128 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); 128 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response);
129 EXPECT_EQ(READ_SUCCESS, V4Store(task_runner_, store_path_).ReadFromDisk()); 129 V4Store store(task_runner_, store_path_);
130 } 130 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk());
131 131 EXPECT_TRUE(store.hash_prefix_map_.empty());
132 TEST_F(V4StoreTest, TestWriteNoResponseType) {
133 EXPECT_EQ(INVALID_RESPONSE_TYPE_FAILURE,
134 V4Store(task_runner_, store_path_)
135 .WriteToDisk(base::WrapUnique(new ListUpdateResponse)));
136 }
137
138 TEST_F(V4StoreTest, TestWritePartialResponseType) {
139 std::unique_ptr<ListUpdateResponse> list_update_response(
140 new ListUpdateResponse);
141 list_update_response->set_response_type(ListUpdateResponse::PARTIAL_UPDATE);
142 EXPECT_EQ(INVALID_RESPONSE_TYPE_FAILURE,
143 V4Store(task_runner_, store_path_)
144 .WriteToDisk(std::move(list_update_response)));
145 }
146
147 TEST_F(V4StoreTest, TestWriteFullResponseType) {
148 std::unique_ptr<ListUpdateResponse> list_update_response(
149 new ListUpdateResponse);
150 list_update_response->set_response_type(ListUpdateResponse::FULL_UPDATE);
151 list_update_response->set_new_client_state("test_client_state");
152 EXPECT_EQ(WRITE_SUCCESS, V4Store(task_runner_, store_path_)
153 .WriteToDisk(std::move(list_update_response)));
154
155 V4Store read_store(task_runner_, store_path_);
156 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk());
157 EXPECT_EQ("test_client_state", read_store.state_);
158 EXPECT_TRUE(read_store.hash_prefix_map_.empty());
159 } 132 }
160 133
161 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { 134 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) {
162 HashPrefixMap prefix_map; 135 HashPrefixMap prefix_map;
163 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, 136 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE,
164 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); 137 V4Store::AddUnlumpedHashes(5, "a", &prefix_map));
165 EXPECT_TRUE(prefix_map.empty()); 138 EXPECT_TRUE(prefix_map.empty());
166 } 139 }
167 140
168 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { 141 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) {
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 &raw_removals, expected_checksum)); 535 &raw_removals, expected_checksum));
563 536
564 const HashPrefixMap& prefix_map = store.hash_prefix_map_; 537 const HashPrefixMap& prefix_map = store.hash_prefix_map_;
565 // The size is 2 since we reserve space anyway. 538 // The size is 2 since we reserve space anyway.
566 EXPECT_EQ(2u, prefix_map.size()); 539 EXPECT_EQ(2u, prefix_map.size());
567 EXPECT_EQ("2222aaaa", prefix_map.at(4)); 540 EXPECT_EQ("2222aaaa", prefix_map.at(4));
568 EXPECT_EQ("1111133333bbbbb", prefix_map.at(5)); 541 EXPECT_EQ("1111133333bbbbb", prefix_map.at(5));
569 } 542 }
570 543
571 TEST_F(V4StoreTest, TestReadFullResponseWithValidHashPrefixMap) { 544 TEST_F(V4StoreTest, TestReadFullResponseWithValidHashPrefixMap) {
572 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); 545 V4Store write_store(task_runner_, store_path_);
573 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); 546 write_store.hash_prefix_map_[4] = "00000abc";
574 lur->set_new_client_state("test_client_state"); 547 write_store.hash_prefix_map_[5] = "00000abcde";
575 lur->set_platform_type(WINDOWS_PLATFORM); 548 write_store.state_ = "test_client_state";
576 lur->set_threat_entry_type(URL); 549 EXPECT_FALSE(base::PathExists(write_store.store_path_));
577 lur->set_threat_type(MALWARE_THREAT); 550 EXPECT_EQ(WRITE_SUCCESS, write_store.WriteToDisk(Checksum()));
578 ThreatEntrySet* additions = lur->add_additions(); 551 EXPECT_TRUE(base::PathExists(write_store.store_path_));
579 additions->set_compression_type(RAW);
580 additions->mutable_raw_hashes()->set_prefix_size(5);
581 additions->mutable_raw_hashes()->set_raw_hashes("00000abcde");
582 additions = lur->add_additions();
583 additions->set_compression_type(RAW);
584 additions->mutable_raw_hashes()->set_prefix_size(4);
585 additions->mutable_raw_hashes()->set_raw_hashes("00000abc");
586 EXPECT_EQ(WRITE_SUCCESS,
587 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur)));
588 552
589 V4Store read_store (task_runner_, store_path_); 553 V4Store read_store (task_runner_, store_path_);
590 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk()); 554 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk());
591 EXPECT_EQ("test_client_state", read_store.state_); 555 EXPECT_EQ("test_client_state", read_store.state_);
592 ASSERT_EQ(2u, read_store.hash_prefix_map_.size()); 556 ASSERT_EQ(2u, read_store.hash_prefix_map_.size());
593 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]); 557 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]);
594 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]); 558 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]);
595 } 559 }
596 560
597 // This tests fails to read the prefix map from the disk because the file on 561 // This tests fails to read the prefix map from the disk because the file on
598 // disk is invalid. The hash prefixes string is 6 bytes long, but the prefix 562 // disk is invalid. The hash prefixes string is 6 bytes long, but the prefix
599 // size is 5 so the parser isn't able to split the hash prefixes list 563 // size is 5 so the parser isn't able to split the hash prefixes list
600 // completely. 564 // completely.
601 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) { 565 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) {
602 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); 566 V4Store write_store(task_runner_, store_path_);
603 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); 567 write_store.hash_prefix_map_[5] = "abcdef";
604 lur->set_new_client_state("test_client_state"); 568 write_store.state_ = "test_client_state";
605 lur->set_platform_type(WINDOWS_PLATFORM); 569 EXPECT_FALSE(base::PathExists(write_store.store_path_));
606 lur->set_threat_entry_type(URL); 570 EXPECT_EQ(WRITE_SUCCESS, write_store.WriteToDisk(Checksum()));
607 lur->set_threat_type(MALWARE_THREAT); 571 EXPECT_TRUE(base::PathExists(write_store.store_path_));
608 ThreatEntrySet* additions = lur->add_additions();
609 additions->set_compression_type(RAW);
610 additions->mutable_raw_hashes()->set_prefix_size(5);
611 additions->mutable_raw_hashes()->set_raw_hashes("abcdef");
612 EXPECT_EQ(WRITE_SUCCESS,
613 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur)));
614 572
615 V4Store read_store(task_runner_, store_path_); 573 V4Store read_store(task_runner_, store_path_);
616 EXPECT_EQ(HASH_PREFIX_MAP_GENERATION_FAILURE, read_store.ReadFromDisk()); 574 EXPECT_EQ(HASH_PREFIX_MAP_GENERATION_FAILURE, read_store.ReadFromDisk());
617 EXPECT_TRUE(read_store.state_.empty()); 575 EXPECT_TRUE(read_store.state_.empty());
618 EXPECT_TRUE(read_store.hash_prefix_map_.empty()); 576 EXPECT_TRUE(read_store.hash_prefix_map_.empty());
619 } 577 }
620 578
621 TEST_F(V4StoreTest, TestHashPrefixExistsAtTheBeginning) { 579 TEST_F(V4StoreTest, TestHashPrefixExistsAtTheBeginning) {
622 HashPrefixes hash_prefixes = "abcdebbbbbccccc"; 580 HashPrefixes hash_prefixes = "abcdebbbbbccccc";
623 HashPrefix hash_prefix = "abcde"; 581 HashPrefix hash_prefix = "abcde";
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 RiceDeltaEncoding* rice_indices = removal->mutable_rice_indices(); 729 RiceDeltaEncoding* rice_indices = removal->mutable_rice_indices();
772 rice_indices->set_first_value(0); 730 rice_indices->set_first_value(0);
773 rice_indices->set_num_entries(2); 731 rice_indices->set_num_entries(2);
774 rice_indices->set_rice_parameter(2); 732 rice_indices->set_rice_parameter(2);
775 rice_indices->set_encoded_data("\x16"); 733 rice_indices->set_encoded_data("\x16");
776 734
777 bool called_back = false; 735 bool called_back = false;
778 UpdatedStoreReadyCallback store_ready_callback = 736 UpdatedStoreReadyCallback store_ready_callback =
779 base::Bind(&V4StoreTest::UpdatedStoreReadyAfterRiceRemovals, 737 base::Bind(&V4StoreTest::UpdatedStoreReadyAfterRiceRemovals,
780 base::Unretained(this), &called_back); 738 base::Unretained(this), &called_back);
739 EXPECT_FALSE(base::PathExists(store.store_path_));
781 store.ApplyUpdate(std::move(lur), task_runner_, store_ready_callback); 740 store.ApplyUpdate(std::move(lur), task_runner_, store_ready_callback);
741 EXPECT_TRUE(base::PathExists(store.store_path_));
742
782 task_runner_->RunPendingTasks(); 743 task_runner_->RunPendingTasks();
783 base::RunLoop().RunUntilIdle(); 744 base::RunLoop().RunUntilIdle();
784 745
785 // This ensures that the callback was called. 746 // This ensures that the callback was called.
786 EXPECT_TRUE(called_back); 747 EXPECT_TRUE(called_back);
787 } 748 }
788 749
789 TEST_F(V4StoreTest, TestMergeUpdatesFailsChecksum) { 750 TEST_F(V4StoreTest, TestMergeUpdatesFailsChecksum) {
790 // Proof of checksum mismatch using python: 751 // Proof of checksum mismatch using python:
791 // >>> import hashlib 752 // >>> import hashlib
(...skipping 15 matching lines...) Expand all
807 // First the case of checksum not matching after reading from disk. 768 // First the case of checksum not matching after reading from disk.
808 ListUpdateResponse list_update_response; 769 ListUpdateResponse list_update_response;
809 list_update_response.set_new_client_state("test_client_state"); 770 list_update_response.set_new_client_state("test_client_state");
810 list_update_response.set_platform_type(LINUX_PLATFORM); 771 list_update_response.set_platform_type(LINUX_PLATFORM);
811 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); 772 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE);
812 list_update_response.mutable_checksum()->set_sha256( 773 list_update_response.mutable_checksum()->set_sha256(
813 std::string(crypto::kSHA256Length, 0)); 774 std::string(crypto::kSHA256Length, 0));
814 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); 775 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response);
815 V4Store store(task_runner_, store_path_); 776 V4Store store(task_runner_, store_path_);
816 EXPECT_TRUE(store.expected_checksum_.empty()); 777 EXPECT_TRUE(store.expected_checksum_.empty());
817
818 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); 778 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk());
819 EXPECT_TRUE(!store.expected_checksum_.empty()); 779 EXPECT_TRUE(!store.expected_checksum_.empty());
820 EXPECT_EQ("test_client_state", store.state()); 780 EXPECT_EQ("test_client_state", store.state());
821 781
822 EXPECT_FALSE(store.VerifyChecksum()); 782 EXPECT_FALSE(store.VerifyChecksum());
823 783
824 // Now the case of checksum matching after reading from disk. 784 // Now the case of checksum matching after reading from disk.
825 // Proof of checksum mismatch using python: 785 // Proof of checksum mismatch using python:
826 // >>> import hashlib 786 // >>> import hashlib
827 // >>> m = hashlib.sha256() 787 // >>> m = hashlib.sha256()
(...skipping 14 matching lines...) Expand all
842 V4Store another_store(task_runner_, store_path_); 802 V4Store another_store(task_runner_, store_path_);
843 EXPECT_TRUE(another_store.expected_checksum_.empty()); 803 EXPECT_TRUE(another_store.expected_checksum_.empty());
844 804
845 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk()); 805 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk());
846 EXPECT_TRUE(!another_store.expected_checksum_.empty()); 806 EXPECT_TRUE(!another_store.expected_checksum_.empty());
847 EXPECT_EQ("test_client_state", another_store.state()); 807 EXPECT_EQ("test_client_state", another_store.state());
848 808
849 EXPECT_TRUE(another_store.VerifyChecksum()); 809 EXPECT_TRUE(another_store.VerifyChecksum());
850 } 810 }
851 811
812 TEST_F(V4StoreTest, WriteToDiskFails) {
813 // Pass the directory name as file name so that when the code tries to rename
814 // the temp store file to |store_path_| it fails.
815 EXPECT_EQ(UNABLE_TO_RENAME_FAILURE,
816 V4Store(task_runner_, temp_dir_.GetPath()).WriteToDisk(Checksum()));
817 }
818
852 } // namespace safe_browsing 819 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_store.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698