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/bind.h" | 6 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/test/test_simple_task_runner.h" | 11 #include "base/test/test_simple_task_runner.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "components/safe_browsing_db/v4_store.h" | 13 #include "components/safe_browsing_db/v4_store.h" |
13 #include "components/safe_browsing_db/v4_store.pb.h" | 14 #include "components/safe_browsing_db/v4_store.pb.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 // \xb2m\x0e\xe0\xe7\xe9P9\x9b\x1cB"\xf5\xde\x05\xe0d%\xb4\xc9\x95\xe9" | 794 // \xb2m\x0e\xe0\xe7\xe9P9\x9b\x1cB"\xf5\xde\x05\xe0d%\xb4\xc9\x95\xe9" |
794 | 795 |
795 HashPrefixMap prefix_map_old; | 796 HashPrefixMap prefix_map_old; |
796 EXPECT_EQ(APPLY_UPDATE_SUCCESS, | 797 EXPECT_EQ(APPLY_UPDATE_SUCCESS, |
797 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_old)); | 798 V4Store::AddUnlumpedHashes(4, "2222", &prefix_map_old)); |
798 EXPECT_EQ(CHECKSUM_MISMATCH_FAILURE, | 799 EXPECT_EQ(CHECKSUM_MISMATCH_FAILURE, |
799 V4Store(task_runner_, store_path_) | 800 V4Store(task_runner_, store_path_) |
800 .MergeUpdate(prefix_map_old, HashPrefixMap(), nullptr, "aawc")); | 801 .MergeUpdate(prefix_map_old, HashPrefixMap(), nullptr, "aawc")); |
801 } | 802 } |
802 | 803 |
| 804 TEST_F(V4StoreTest, TestChecksumErrorOnStartup) { |
| 805 // First the case of checksum not matching after reading from disk. |
| 806 ListUpdateResponse list_update_response; |
| 807 list_update_response.set_new_client_state("test_client_state"); |
| 808 list_update_response.set_platform_type(LINUX_PLATFORM); |
| 809 list_update_response.set_response_type(ListUpdateResponse::FULL_UPDATE); |
| 810 list_update_response.mutable_checksum()->set_sha256( |
| 811 std::string(crypto::kSHA256Length, 0)); |
| 812 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); |
| 813 V4Store store(task_runner_, store_path_); |
| 814 EXPECT_TRUE(store.expected_checksum_.empty()); |
| 815 |
| 816 EXPECT_EQ(READ_SUCCESS, store.ReadFromDisk()); |
| 817 EXPECT_TRUE(!store.expected_checksum_.empty()); |
| 818 EXPECT_EQ("test_client_state", store.state()); |
| 819 |
| 820 EXPECT_FALSE(store.VerifyChecksum()); |
| 821 |
| 822 // Now the case of checksum matching after reading from disk. |
| 823 // Proof of checksum mismatch using python: |
| 824 // >>> import hashlib |
| 825 // >>> m = hashlib.sha256() |
| 826 // >>> m.update("abcde") |
| 827 // >>> import base64 |
| 828 // >>> encoded = base64.b64encode(m.digest()) |
| 829 // >>> encoded |
| 830 // 'NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=' |
| 831 std::string expected_checksum; |
| 832 base::Base64Decode("NrvlDtloQdEEQ7y2cNZVTwo0t2G+Z+ycSorSwMRMpCw=", |
| 833 &expected_checksum); |
| 834 ThreatEntrySet* additions = list_update_response.add_additions(); |
| 835 additions->set_compression_type(RAW); |
| 836 additions->mutable_raw_hashes()->set_prefix_size(5); |
| 837 additions->mutable_raw_hashes()->set_raw_hashes("abcde"); |
| 838 list_update_response.mutable_checksum()->set_sha256(expected_checksum); |
| 839 WriteFileFormatProtoToFile(0x600D71FE, 9, &list_update_response); |
| 840 V4Store another_store(task_runner_, store_path_); |
| 841 EXPECT_TRUE(another_store.expected_checksum_.empty()); |
| 842 |
| 843 EXPECT_EQ(READ_SUCCESS, another_store.ReadFromDisk()); |
| 844 EXPECT_TRUE(!another_store.expected_checksum_.empty()); |
| 845 EXPECT_EQ("test_client_state", another_store.state()); |
| 846 |
| 847 EXPECT_TRUE(another_store.VerifyChecksum()); |
| 848 } |
| 849 |
803 } // namespace safe_browsing | 850 } // namespace safe_browsing |
OLD | NEW |