| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 file_format.mutable_list_update_response(); | 50 file_format.mutable_list_update_response(); |
| 51 *list_update_response = *response; | 51 *list_update_response = *response; |
| 52 } | 52 } |
| 53 | 53 |
| 54 std::string file_format_string; | 54 std::string file_format_string; |
| 55 file_format.SerializeToString(&file_format_string); | 55 file_format.SerializeToString(&file_format_string); |
| 56 base::WriteFile(store_path_, file_format_string.data(), | 56 base::WriteFile(store_path_, file_format_string.data(), |
| 57 file_format_string.size()); | 57 file_format_string.size()); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void UpdatedStoreReadyAfterRiceRemovals(bool* called_back, | 60 void UpdatedStoreReady(bool* called_back, |
| 61 std::unique_ptr<V4Store> new_store) { | 61 bool expect_store, |
| 62 std::unique_ptr<V4Store> store) { |
| 62 *called_back = true; | 63 *called_back = true; |
| 63 EXPECT_EQ(2u, new_store->hash_prefix_map_.size()); | 64 if (expect_store) { |
| 64 EXPECT_EQ("22222", new_store->hash_prefix_map_[5]); | 65 ASSERT_TRUE(store); |
| 65 EXPECT_EQ("abcd", new_store->hash_prefix_map_[4]); | 66 EXPECT_EQ(2u, store->hash_prefix_map_.size()); |
| 67 EXPECT_EQ("22222", store->hash_prefix_map_[5]); |
| 68 EXPECT_EQ("abcd", store->hash_prefix_map_[4]); |
| 69 } else { |
| 70 ASSERT_FALSE(store); |
| 71 } |
| 66 } | 72 } |
| 67 | 73 |
| 68 base::ScopedTempDir temp_dir_; | 74 base::ScopedTempDir temp_dir_; |
| 69 base::FilePath store_path_; | 75 base::FilePath store_path_; |
| 70 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 76 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 71 content::TestBrowserThreadBundle thread_bundle_; | 77 content::TestBrowserThreadBundle thread_bundle_; |
| 72 }; | 78 }; |
| 73 | 79 |
| 74 TEST_F(V4StoreTest, TestReadFromEmptyFile) { | 80 TEST_F(V4StoreTest, TestReadFromEmptyFile) { |
| 75 base::CloseFile(base::OpenFile(store_path_, "wb+")); | 81 base::CloseFile(base::OpenFile(store_path_, "wb+")); |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 ThreatEntrySet* removal = lur->add_removals(); | 733 ThreatEntrySet* removal = lur->add_removals(); |
| 728 removal->set_compression_type(RICE); | 734 removal->set_compression_type(RICE); |
| 729 RiceDeltaEncoding* rice_indices = removal->mutable_rice_indices(); | 735 RiceDeltaEncoding* rice_indices = removal->mutable_rice_indices(); |
| 730 rice_indices->set_first_value(0); | 736 rice_indices->set_first_value(0); |
| 731 rice_indices->set_num_entries(2); | 737 rice_indices->set_num_entries(2); |
| 732 rice_indices->set_rice_parameter(2); | 738 rice_indices->set_rice_parameter(2); |
| 733 rice_indices->set_encoded_data("\x16"); | 739 rice_indices->set_encoded_data("\x16"); |
| 734 | 740 |
| 735 bool called_back = false; | 741 bool called_back = false; |
| 736 UpdatedStoreReadyCallback store_ready_callback = | 742 UpdatedStoreReadyCallback store_ready_callback = |
| 737 base::Bind(&V4StoreTest::UpdatedStoreReadyAfterRiceRemovals, | 743 base::Bind(&V4StoreTest::UpdatedStoreReady, base::Unretained(this), |
| 738 base::Unretained(this), &called_back); | 744 &called_back, true /* expect_store */); |
| 739 EXPECT_FALSE(base::PathExists(store.store_path_)); | 745 EXPECT_FALSE(base::PathExists(store.store_path_)); |
| 740 store.ApplyUpdate(std::move(lur), task_runner_, store_ready_callback); | 746 store.ApplyUpdate(std::move(lur), task_runner_, store_ready_callback); |
| 741 EXPECT_TRUE(base::PathExists(store.store_path_)); | 747 EXPECT_TRUE(base::PathExists(store.store_path_)); |
| 742 | 748 |
| 743 task_runner_->RunPendingTasks(); | 749 task_runner_->RunPendingTasks(); |
| 744 base::RunLoop().RunUntilIdle(); | 750 base::RunLoop().RunUntilIdle(); |
| 745 | 751 |
| 746 // This ensures that the callback was called. | 752 // This ensures that the callback was called. |
| 747 EXPECT_TRUE(called_back); | 753 EXPECT_TRUE(called_back); |
| 748 } | 754 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 EXPECT_TRUE(another_store.VerifyChecksum()); | 815 EXPECT_TRUE(another_store.VerifyChecksum()); |
| 810 } | 816 } |
| 811 | 817 |
| 812 TEST_F(V4StoreTest, WriteToDiskFails) { | 818 TEST_F(V4StoreTest, WriteToDiskFails) { |
| 813 // Pass the directory name as file name so that when the code tries to rename | 819 // 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. | 820 // the temp store file to |store_path_| it fails. |
| 815 EXPECT_EQ(UNABLE_TO_RENAME_FAILURE, | 821 EXPECT_EQ(UNABLE_TO_RENAME_FAILURE, |
| 816 V4Store(task_runner_, temp_dir_.GetPath()).WriteToDisk(Checksum())); | 822 V4Store(task_runner_, temp_dir_.GetPath()).WriteToDisk(Checksum())); |
| 817 } | 823 } |
| 818 | 824 |
| 825 TEST_F(V4StoreTest, FullUpdateFailsChecksumSynchronously) { |
| 826 V4Store store(task_runner_, store_path_); |
| 827 bool called_back = false; |
| 828 UpdatedStoreReadyCallback store_ready_callback = |
| 829 base::Bind(&V4StoreTest::UpdatedStoreReady, base::Unretained(this), |
| 830 &called_back, false /* expect_store */); |
| 831 EXPECT_FALSE(base::PathExists(store.store_path_)); |
| 832 |
| 833 // Now create a response with invalid checksum. |
| 834 std::unique_ptr<ListUpdateResponse> lur(new ListUpdateResponse); |
| 835 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 836 lur->mutable_checksum()->set_sha256(std::string(crypto::kSHA256Length, 0)); |
| 837 store.ApplyUpdate(std::move(lur), task_runner_, store_ready_callback); |
| 838 // The update should fail synchronously and not create a store file. |
| 839 EXPECT_FALSE(base::PathExists(store.store_path_)); |
| 840 |
| 841 // Run everything on the task runner to ensure there are no pending tasks. |
| 842 task_runner_->RunPendingTasks(); |
| 843 base::RunLoop().RunUntilIdle(); |
| 844 |
| 845 // This ensures that the callback was called. |
| 846 EXPECT_TRUE(called_back); |
| 847 // Ensure that the file is still not created. |
| 848 EXPECT_FALSE(base::PathExists(store.store_path_)); |
| 849 } |
| 850 |
| 819 } // namespace safe_browsing | 851 } // namespace safe_browsing |
| OLD | NEW |