| 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 #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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 128 } |
| 129 | 129 |
| 130 TEST_F(V4StoreTest, TestReadFromNoHashPrefixesFile) { | 130 TEST_F(V4StoreTest, TestReadFromNoHashPrefixesFile) { |
| 131 ListUpdateResponse list_update_response; | 131 ListUpdateResponse list_update_response; |
| 132 list_update_response.set_platform_type(LINUX_PLATFORM); | 132 list_update_response.set_platform_type(LINUX_PLATFORM); |
| 133 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); | 133 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 134 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); | 134 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); |
| 135 V4Store store(task_runner_, store_path_); | 135 V4Store store(task_runner_, store_path_); |
| 136 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); | 136 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); |
| 137 EXPECT_TRUE(store.hash_prefix_map_.empty()); | 137 EXPECT_TRUE(store.hash_prefix_map_.empty()); |
| 138 EXPECT_EQ(14ul, store.file_size_); |
| 138 } | 139 } |
| 139 | 140 |
| 140 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { | 141 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithInvalidAddition) { |
| 141 HashPrefixMap prefix_map; | 142 HashPrefixMap prefix_map; |
| 142 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, | 143 EXPECT_EQ(ADDITIONS_SIZE_UNEXPECTED_FAILURE, |
| 143 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); | 144 V4Store::AddUnlumpedHashes(5, "a", &prefix_map)); |
| 144 EXPECT_TRUE(prefix_map.empty()); | 145 EXPECT_TRUE(prefix_map.empty()); |
| 145 } | 146 } |
| 146 | 147 |
| 147 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { | 148 TEST_F(V4StoreTest, TestAddUnlumpedHashesWithEmptyString) { |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 EXPECT_FALSE(base::PathExists(write_store.store_path_)); | 556 EXPECT_FALSE(base::PathExists(write_store.store_path_)); |
| 556 EXPECT_EQ(WRITE_SUCCESS, write_store.WriteToDisk(Checksum())); | 557 EXPECT_EQ(WRITE_SUCCESS, write_store.WriteToDisk(Checksum())); |
| 557 EXPECT_TRUE(base::PathExists(write_store.store_path_)); | 558 EXPECT_TRUE(base::PathExists(write_store.store_path_)); |
| 558 | 559 |
| 559 V4Store read_store (task_runner_, store_path_); | 560 V4Store read_store (task_runner_, store_path_); |
| 560 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk()); | 561 EXPECT_EQ(READ_SUCCESS, read_store.ReadFromDisk()); |
| 561 EXPECT_EQ("test_client_state", read_store.state_); | 562 EXPECT_EQ("test_client_state", read_store.state_); |
| 562 ASSERT_EQ(2u, read_store.hash_prefix_map_.size()); | 563 ASSERT_EQ(2u, read_store.hash_prefix_map_.size()); |
| 563 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]); | 564 EXPECT_EQ("00000abc", read_store.hash_prefix_map_[4]); |
| 564 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]); | 565 EXPECT_EQ("00000abcde", read_store.hash_prefix_map_[5]); |
| 566 EXPECT_EQ(71ul, read_store.file_size_); |
| 565 } | 567 } |
| 566 | 568 |
| 567 // This tests fails to read the prefix map from the disk because the file on | 569 // This tests fails to read the prefix map from the disk because the file on |
| 568 // disk is invalid. The hash prefixes string is 6 bytes long, but the prefix | 570 // disk is invalid. The hash prefixes string is 6 bytes long, but the prefix |
| 569 // size is 5 so the parser isn't able to split the hash prefixes list | 571 // size is 5 so the parser isn't able to split the hash prefixes list |
| 570 // completely. | 572 // completely. |
| 571 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) { | 573 TEST_F(V4StoreTest, TestReadFullResponseWithInvalidHashPrefixMap) { |
| 572 V4Store write_store(task_runner_, store_path_); | 574 V4Store write_store(task_runner_, store_path_); |
| 573 write_store.hash_prefix_map_[5] = "abcdef"; | 575 write_store.hash_prefix_map_[5] = "abcdef"; |
| 574 write_store.state_ = "test_client_state"; | 576 write_store.state_ = "test_client_state"; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 list_update_response.set_new_client_state("test_client_state"); | 778 list_update_response.set_new_client_state("test_client_state"); |
| 777 list_update_response.set_platform_type(LINUX_PLATFORM); | 779 list_update_response.set_platform_type(LINUX_PLATFORM); |
| 778 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); | 780 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 779 list_update_response.mutable_checksum()->set_sha256( | 781 list_update_response.mutable_checksum()->set_sha256( |
| 780 std::string(crypto::kSHA256Length, 0)); | 782 std::string(crypto::kSHA256Length, 0)); |
| 781 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); | 783 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); |
| 782 V4Store store(task_runner_, store_path_); | 784 V4Store store(task_runner_, store_path_); |
| 783 EXPECT_TRUE(store.expected_checksum_.empty()); | 785 EXPECT_TRUE(store.expected_checksum_.empty()); |
| 784 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); | 786 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); |
| 785 EXPECT_TRUE(!store.expected_checksum_.empty()); | 787 EXPECT_TRUE(!store.expected_checksum_.empty()); |
| 788 EXPECT_EQ(69ul, store.file_size_); |
| 786 EXPECT_EQ("test_client_state", store.state()); | 789 EXPECT_EQ("test_client_state", store.state()); |
| 787 | 790 |
| 788 EXPECT_FALSE(store.VerifyChecksum()); | 791 EXPECT_FALSE(store.VerifyChecksum()); |
| 789 | 792 |
| 790 // Now the case of checksum matching after reading from disk. | 793 // Now the case of checksum matching after reading from disk. |
| 791 // Proof of checksum mismatch using python: | 794 // Proof of checksum mismatch using python: |
| 792 // >>> import hashlib | 795 // >>> import hashlib |
| 793 // >>> m = hashlib.sha256() | 796 // >>> m = hashlib.sha256() |
| 794 // >>> m.update("abcde") | 797 // >>> m.update("abcde") |
| 795 // >>> import base64 | 798 // >>> import base64 |
| 796 // >>> encoded = base64.b64encode(m.digest()) | 799 // >>> encoded = base64.b64encode(m.digest()) |
| 797 // >>> encoded | 800 // >>> encoded |
| 798 // 'NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=' | 801 // 'NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=' |
| 799 std::string expected_checksum; | 802 std::string expected_checksum; |
| 800 base::Base64Decode("NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=", | 803 base::Base64Decode("NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=", |
| 801 &expected_checksum); | 804 &expected_checksum); |
| 802 ThreatEntrySet* additions = list_update_response.add_additions(); | 805 ThreatEntrySet* additions = list_update_response.add_additions(); |
| 803 additions->set_compression_type(RAW); | 806 additions->set_compression_type(RAW); |
| 804 additions->mutable_raw_hashes()->set_prefix_size(5); | 807 additions->mutable_raw_hashes()->set_prefix_size(5); |
| 805 additions->mutable_raw_hashes()->set_raw_hashes("abcde"); | 808 additions->mutable_raw_hashes()->set_raw_hashes("abcde"); |
| 806 list_update_response.mutable_checksum()->set_sha256(expected_checksum); | 809 list_update_response.mutable_checksum()->set_sha256(expected_checksum); |
| 807 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); | 810 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); |
| 808 V4Store another_store(task_runner_, store_path_); | 811 V4Store another_store(task_runner_, store_path_); |
| 809 EXPECT_TRUE(another_store.expected_checksum_.empty()); | 812 EXPECT_TRUE(another_store.expected_checksum_.empty()); |
| 810 | 813 |
| 811 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk()); | 814 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk()); |
| 812 EXPECT_TRUE(!another_store.expected_checksum_.empty()); | 815 EXPECT_TRUE(!another_store.expected_checksum_.empty()); |
| 813 EXPECT_EQ("test_client_state", another_store.state()); | 816 EXPECT_EQ("test_client_state", another_store.state()); |
| 817 EXPECT_EQ(69ul, store.file_size_); |
| 814 | 818 |
| 815 EXPECT_TRUE(another_store.VerifyChecksum()); | 819 EXPECT_TRUE(another_store.VerifyChecksum()); |
| 816 } | 820 } |
| 817 | 821 |
| 818 TEST_F(V4StoreTest, WriteToDiskFails) { | 822 TEST_F(V4StoreTest, WriteToDiskFails) { |
| 819 // Pass the directory name as file name so that when the code tries to rename | 823 // Pass the directory name as file name so that when the code tries to rename |
| 820 // the temp store file to |store_path_| it fails. | 824 // the temp store file to |store_path_| it fails. |
| 821 EXPECT_EQ(UNABLE_TO_RENAME_FAILURE, | 825 EXPECT_EQ(UNABLE_TO_RENAME_FAILURE, |
| 822 V4Store(task_runner_, temp_dir_.GetPath()).WriteToDisk(Checksum())); | 826 V4Store(task_runner_, temp_dir_.GetPath()).WriteToDisk(Checksum())); |
| 823 } | 827 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 842 task_runner_->RunPendingTasks(); | 846 task_runner_->RunPendingTasks(); |
| 843 base::RunLoop().RunUntilIdle(); | 847 base::RunLoop().RunUntilIdle(); |
| 844 | 848 |
| 845 // This ensures that the callback was called. | 849 // This ensures that the callback was called. |
| 846 EXPECT_TRUE(called_back); | 850 EXPECT_TRUE(called_back); |
| 847 // Ensure that the file is still not created. | 851 // Ensure that the file is still not created. |
| 848 EXPECT_FALSE(base::PathExists(store.store_path_)); | 852 EXPECT_FALSE(base::PathExists(store.store_path_)); |
| 849 } | 853 } |
| 850 | 854 |
| 851 } // namespace safe_browsing | 855 } // namespace safe_browsing |
| OLD | NEW |