OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Unit tests for the SafeBrowsing storage system. | 5 // Unit tests for the SafeBrowsing storage system. |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); | 34 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); |
35 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); | 35 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); |
36 const std::string hashed_ip_prefix = base::SHA1HashString( | 36 const std::string hashed_ip_prefix = base::SHA1HashString( |
37 net::IPAddressToPackedString(ip_number)); | 37 net::IPAddressToPackedString(ip_number)); |
38 std::string hash(crypto::kSHA256Length, '\0'); | 38 std::string hash(crypto::kSHA256Length, '\0'); |
39 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); | 39 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); |
40 hash[base::kSHA1Length] = static_cast<char>(prefix_size); | 40 hash[base::kSHA1Length] = static_cast<char>(prefix_size); |
41 return hash; | 41 return hash; |
42 } | 42 } |
43 | 43 |
| 44 // Add a host-level entry. |
| 45 void InsertAddChunkHostPrefix(SBChunk* chunk, |
| 46 int chunk_number, |
| 47 const std::string& host_name) { |
| 48 chunk->chunk_number = chunk_number; |
| 49 chunk->is_add = true; |
| 50 SBChunkHost host; |
| 51 host.host = SBPrefixForString(host_name); |
| 52 host.entry = SBEntry::Create(SBEntry::ADD_PREFIX, 0); |
| 53 host.entry->set_chunk_id(chunk->chunk_number); |
| 54 chunk->hosts.push_back(host); |
| 55 } |
| 56 |
44 // Same as InsertAddChunkHostPrefixUrl, but with pre-computed | 57 // Same as InsertAddChunkHostPrefixUrl, but with pre-computed |
45 // prefix values. | 58 // prefix values. |
46 void InsertAddChunkHostPrefixValue(SBChunk* chunk, | 59 void InsertAddChunkHostPrefixValue(SBChunk* chunk, |
47 int chunk_number, | 60 int chunk_number, |
48 const SBPrefix& host_prefix, | 61 const SBPrefix& host_prefix, |
49 const SBPrefix& url_prefix) { | 62 const SBPrefix& url_prefix) { |
50 chunk->chunk_number = chunk_number; | 63 chunk->chunk_number = chunk_number; |
51 chunk->is_add = true; | 64 chunk->is_add = true; |
52 SBChunkHost host; | 65 SBChunkHost host; |
53 host.host = host_prefix; | 66 host.host = host_prefix; |
(...skipping 1761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1815 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); | 1828 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); |
1816 | 1829 |
1817 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); | 1830 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); |
1818 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); | 1831 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); |
1819 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); | 1832 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); |
1820 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); | 1833 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); |
1821 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); | 1834 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); |
1822 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); | 1835 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); |
1823 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); | 1836 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); |
1824 } | 1837 } |
| 1838 |
| 1839 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { |
| 1840 std::vector<SBListChunkRanges> lists; |
| 1841 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1842 |
| 1843 // Add a host-level hit. |
| 1844 { |
| 1845 SBChunkList chunks; |
| 1846 SBChunk chunk; |
| 1847 InsertAddChunkHostPrefix(&chunk, 1, "www.evil.com/"); |
| 1848 chunks.push_back(chunk); |
| 1849 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1850 } |
| 1851 |
| 1852 // Add a specific fullhash. |
| 1853 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; |
| 1854 { |
| 1855 SBChunkList chunks; |
| 1856 SBChunk chunk; |
| 1857 InsertAddChunkHostFullHashes(&chunk, 2, "www.whatever.com/", |
| 1858 kWhateverMalware); |
| 1859 chunks.push_back(chunk); |
| 1860 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1861 } |
| 1862 |
| 1863 // Add a fullhash which has a prefix collision for a known url. |
| 1864 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 1865 static const char kExampleCollision[] = |
| 1866 "www.example.com/3123364814/malware.htm"; |
| 1867 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 1868 SBPrefixForString(kExampleCollision)); |
| 1869 { |
| 1870 SBChunkList chunks; |
| 1871 SBChunk chunk; |
| 1872 InsertAddChunkHostFullHashes(&chunk, 3, "www.example.com/", |
| 1873 kExampleCollision); |
| 1874 chunks.push_back(chunk); |
| 1875 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1876 } |
| 1877 |
| 1878 database_->UpdateFinished(true); |
| 1879 |
| 1880 const Time now = Time::Now(); |
| 1881 std::vector<SBFullHashResult> full_hashes; |
| 1882 std::vector<SBPrefix> prefix_hits; |
| 1883 std::string matching_list; |
| 1884 |
| 1885 // Anything will hit the host prefix. |
| 1886 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1887 GURL("http://www.evil.com/malware.html"), |
| 1888 &matching_list, &prefix_hits, |
| 1889 &full_hashes, now)); |
| 1890 ASSERT_EQ(1U, prefix_hits.size()); |
| 1891 EXPECT_EQ(SBPrefixForString("www.evil.com/"), prefix_hits[0]); |
| 1892 EXPECT_TRUE(full_hashes.empty()); |
| 1893 |
| 1894 // Hit the specific URL prefix. |
| 1895 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1896 GURL(std::string("http://") + kWhateverMalware), |
| 1897 &matching_list, &prefix_hits, |
| 1898 &full_hashes, now)); |
| 1899 ASSERT_EQ(1U, prefix_hits.size()); |
| 1900 EXPECT_EQ(SBPrefixForString(kWhateverMalware), prefix_hits[0]); |
| 1901 ASSERT_EQ(1U, full_hashes.size()); |
| 1902 EXPECT_TRUE(SBFullHashEqual(full_hashes[0].hash, |
| 1903 SBFullHashForString(kWhateverMalware))); |
| 1904 |
| 1905 // Other URLs at that host are fine. |
| 1906 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1907 GURL("http://www.whatever.com/fine.html"), |
| 1908 &matching_list, &prefix_hits, |
| 1909 &full_hashes, now)); |
| 1910 EXPECT_TRUE(prefix_hits.empty()); |
| 1911 EXPECT_TRUE(full_hashes.empty()); |
| 1912 |
| 1913 // Hit the prefix, which returns a fullhash hit for something else. |
| 1914 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1915 GURL(std::string("http://") + kExampleFine), |
| 1916 &matching_list, &prefix_hits, |
| 1917 &full_hashes, now)); |
| 1918 ASSERT_EQ(1U, prefix_hits.size()); |
| 1919 EXPECT_EQ(SBPrefixForString(kExampleFine), prefix_hits[0]); |
| 1920 ASSERT_EQ(1U, full_hashes.size()); |
| 1921 EXPECT_TRUE(SBFullHashEqual(full_hashes[0].hash, |
| 1922 SBFullHashForString(kExampleCollision))); |
| 1923 } |
OLD | NEW |