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 1799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1853 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); | 1866 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); |
1854 | 1867 |
1855 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); | 1868 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); |
1856 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); | 1869 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); |
1857 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); | 1870 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); |
1858 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); | 1871 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); |
1859 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); | 1872 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); |
1860 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); | 1873 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); |
1861 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); | 1874 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); |
1862 } | 1875 } |
| 1876 |
| 1877 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { |
| 1878 std::vector<SBListChunkRanges> lists; |
| 1879 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1880 |
| 1881 // Add a host-level hit. |
| 1882 { |
| 1883 SBChunkList chunks; |
| 1884 SBChunk chunk; |
| 1885 InsertAddChunkHostPrefix(&chunk, 1, "www.evil.com/"); |
| 1886 chunks.push_back(chunk); |
| 1887 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1888 } |
| 1889 |
| 1890 // Add a specific fullhash. |
| 1891 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; |
| 1892 { |
| 1893 SBChunkList chunks; |
| 1894 SBChunk chunk; |
| 1895 InsertAddChunkHostFullHashes(&chunk, 2, "www.whatever.com/", |
| 1896 kWhateverMalware); |
| 1897 chunks.push_back(chunk); |
| 1898 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1899 } |
| 1900 |
| 1901 // Add a fullhash which has a prefix collision for a known url. |
| 1902 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 1903 static const char kExampleCollision[] = |
| 1904 "www.example.com/3123364814/malware.htm"; |
| 1905 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 1906 SBPrefixForString(kExampleCollision)); |
| 1907 { |
| 1908 SBChunkList chunks; |
| 1909 SBChunk chunk; |
| 1910 InsertAddChunkHostFullHashes(&chunk, 3, "www.example.com/", |
| 1911 kExampleCollision); |
| 1912 chunks.push_back(chunk); |
| 1913 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1914 } |
| 1915 |
| 1916 database_->UpdateFinished(true); |
| 1917 |
| 1918 const Time now = Time::Now(); |
| 1919 std::vector<SBFullHashResult> cached_hashes; |
| 1920 std::vector<SBPrefix> prefix_hits; |
| 1921 |
| 1922 // Anything will hit the host prefix. |
| 1923 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1924 GURL("http://www.evil.com/malware.html"), |
| 1925 &prefix_hits, &cached_hashes, now)); |
| 1926 ASSERT_EQ(1U, prefix_hits.size()); |
| 1927 EXPECT_EQ(SBPrefixForString("www.evil.com/"), prefix_hits[0]); |
| 1928 EXPECT_TRUE(cached_hashes.empty()); |
| 1929 |
| 1930 // Hit the specific URL prefix. |
| 1931 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1932 GURL(std::string("http://") + kWhateverMalware), |
| 1933 &prefix_hits, &cached_hashes, now)); |
| 1934 ASSERT_EQ(1U, prefix_hits.size()); |
| 1935 EXPECT_EQ(SBPrefixForString(kWhateverMalware), prefix_hits[0]); |
| 1936 EXPECT_TRUE(cached_hashes.empty()); |
| 1937 |
| 1938 // Other URLs at that host are fine. |
| 1939 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1940 GURL("http://www.whatever.com/fine.html"), |
| 1941 &prefix_hits, &cached_hashes, now)); |
| 1942 EXPECT_TRUE(prefix_hits.empty()); |
| 1943 EXPECT_TRUE(cached_hashes.empty()); |
| 1944 |
| 1945 // Hit the specific URL full hash. |
| 1946 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1947 GURL(std::string("http://") + kExampleCollision), |
| 1948 &prefix_hits, &cached_hashes, now)); |
| 1949 ASSERT_EQ(1U, prefix_hits.size()); |
| 1950 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 1951 EXPECT_TRUE(cached_hashes.empty()); |
| 1952 |
| 1953 // This prefix collides, but no full hash match. |
| 1954 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1955 GURL(std::string("http://") + kExampleFine), |
| 1956 &prefix_hits, &cached_hashes, now)); |
| 1957 } |
OLD | NEW |