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

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: Serialize the store's hash_prefix_map_ to file in WriteToDisk() 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 #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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 EXPECT_EQ(READ_SUCCESS, V4Store(task_runner_, store_path_).ReadFromDisk());
130 } 130 }
131 131
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 }
160
161 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { 132 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) {
162 HashPrefixMap prefix_map; 133 HashPrefixMap prefix_map;
163 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, 134 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE,
164 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); 135 V4Store::AddUnlumpedHashes(5, "a", &prefix_map));
165 EXPECT_TRUE(prefix_map.empty()); 136 EXPECT_TRUE(prefix_map.empty());
166 } 137 }
167 138
168 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { 139 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) {
169 HashPrefixMap prefix_map; 140 HashPrefixMap prefix_map;
170 EXPECT_EQ(APPLY_UPDATE_SUCCESS, 141 EXPECT_EQ(APPLY_UPDATE_SUCCESS,
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 store.MergeUpdate(prefix_map_old, prefix_map_additions, 532 store.MergeUpdate(prefix_map_old, prefix_map_additions,
562 &raw_removals, expected_checksum)); 533 &raw_removals, expected_checksum));
563 534
564 const HashPrefixMap& prefix_map = store.hash_prefix_map_; 535 const HashPrefixMap& prefix_map = store.hash_prefix_map_;
565 // The size is 2 since we reserve space anyway. 536 // The size is 2 since we reserve space anyway.
566 EXPECT_EQ(2u, prefix_map.size()); 537 EXPECT_EQ(2u, prefix_map.size());
567 EXPECT_EQ("2222aaaa", prefix_map.at(4)); 538 EXPECT_EQ("2222aaaa", prefix_map.at(4));
568 EXPECT_EQ("1111133333bbbbb", prefix_map.at(5)); 539 EXPECT_EQ("1111133333bbbbb", prefix_map.at(5));
569 } 540 }
570 541
571 TEST_F(V4StoreTest, TestReadFullResponseWithValidHashPrefixMap) { 542 TEST_F(V4StoreTest, TestReadFullResponseWithValidHashPrefixMap) {
Nathan Parker 2016/10/11 20:29:42 How about a test for when a write fails (like, per
vakh (use Gerrit instead) 2016/10/11 21:50:39 Done.
572 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); 543 V4Store write_store(task_runner_, store_path_);
573 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); 544 write_store.hash_prefix_map_[4] = "00000abc";
574 lur->set_new_client_state("test_client_state"); 545 write_store.hash_prefix_map_[5] = "00000abcde";
575 lur->set_platform_type(WINDOWS_PLATFORM); 546 write_store.state_ = "test_client_state";
576 lur->set_threat_entry_type(URL);
577 lur->set_threat_type(MALWARE_THREAT);
578 ThreatEntrySet* additions = lur->add_additions();
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, 547 EXPECT_EQ(WRITE_SUCCESS,
587 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur))); 548 write_store.WriteToDisk(GetUrlMalwareId(), Checksum()));
588 549
589 V4Store read_store (task_runner_, store_path_); 550 V4Store read_store (task_runner_, store_path_);
590 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk()); 551 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk());
591 EXPECT_EQ("test_client_state", read_store.state_); 552 EXPECT_EQ("test_client_state", read_store.state_);
592 ASSERT_EQ(2u, read_store.hash_prefix_map_.size()); 553 ASSERT_EQ(2u, read_store.hash_prefix_map_.size());
Nathan Parker 2016/10/11 20:29:42 This should verify the store id too, ya?
vakh (use Gerrit instead) 2016/10/11 21:50:39 I realized that there's no need to set the list_id
593 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]); 554 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]);
594 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]); 555 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]);
595 } 556 }
596 557
597 // This tests fails to read the prefix map from the disk because the file on 558 // 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 559 // 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 560 // size is 5 so the parser isn't able to split the hash prefixes list
600 // completely. 561 // completely.
601 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) { 562 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) {
602 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); 563 V4Store write_store(task_runner_, store_path_);
603 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); 564 write_store.hash_prefix_map_[5] = "abcdef";
604 lur->set_new_client_state("test_client_state"); 565 write_store.state_ = "test_client_state";
605 lur->set_platform_type(WINDOWS_PLATFORM);
606 lur->set_threat_entry_type(URL);
607 lur->set_threat_type(MALWARE_THREAT);
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, 566 EXPECT_EQ(WRITE_SUCCESS,
613 V4Store(task_runner_, store_path_).WriteToDisk(std::move(lur))); 567 write_store.WriteToDisk(GetUrlMalwareId(), Checksum()));
614 568
615 V4Store read_store(task_runner_, store_path_); 569 V4Store read_store(task_runner_, store_path_);
616 EXPECT_EQ(HASH_PREFIX_MAP_GENERATION_FAILURE, read_store.ReadFromDisk()); 570 EXPECT_EQ(HASH_PREFIX_MAP_GENERATION_FAILURE, read_store.ReadFromDisk());
617 EXPECT_TRUE(read_store.state_.empty()); 571 EXPECT_TRUE(read_store.state_.empty());
618 EXPECT_TRUE(read_store.hash_prefix_map_.empty()); 572 EXPECT_TRUE(read_store.hash_prefix_map_.empty());
619 } 573 }
620 574
621 TEST_F(V4StoreTest, TestHashPrefixExistsAtTheBeginning) { 575 TEST_F(V4StoreTest, TestHashPrefixExistsAtTheBeginning) {
622 HashPrefixes hash_prefixes = "abcdebbbbbccccc"; 576 HashPrefixes hash_prefixes = "abcdebbbbbccccc";
623 HashPrefix hash_prefix = "abcde"; 577 HashPrefix hash_prefix = "abcde";
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 EXPECT_TRUE(another_store.expected_checksum_.empty()); 795 EXPECT_TRUE(another_store.expected_checksum_.empty());
842 796
843 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk()); 797 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk());
844 EXPECT_TRUE(!another_store.expected_checksum_.empty()); 798 EXPECT_TRUE(!another_store.expected_checksum_.empty());
845 EXPECT_EQ("test_client_state", another_store.state()); 799 EXPECT_EQ("test_client_state", another_store.state());
846 800
847 EXPECT_TRUE(another_store.VerifyChecksum()); 801 EXPECT_TRUE(another_store.VerifyChecksum());
848 } 802 }
849 803
850 } // namespace safe_browsing 804 } // namespace safe_browsing
OLDNEW
« components/safe_browsing_db/v4_store.cc ('K') | « 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