| 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 "chrome/browser/safe_browsing/safe_browsing_database.h" | 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 chunks.push_back(AddChunkPrefix2Value(1, | 823 chunks.push_back(AddChunkPrefix2Value(1, |
| 824 "www.evil.com/phishing.html", | 824 "www.evil.com/phishing.html", |
| 825 "www.evil.com/malware.html")); | 825 "www.evil.com/malware.html")); |
| 826 | 826 |
| 827 std::vector<SBListChunkRanges> lists; | 827 std::vector<SBListChunkRanges> lists; |
| 828 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 828 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 829 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 829 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 830 database_->UpdateFinished(true); | 830 database_->UpdateFinished(true); |
| 831 | 831 |
| 832 // Cache should be cleared after updating. | 832 // Cache should be cleared after updating. |
| 833 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); | 833 EXPECT_TRUE( |
| 834 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 834 | 835 |
| 835 SBFullHashResult full_hash; | 836 SBFullHashResult full_hash; |
| 836 full_hash.list_id = safe_browsing_util::MALWARE; | 837 full_hash.list_id = safe_browsing_util::MALWARE; |
| 837 | 838 |
| 838 std::vector<SBFullHashResult> results; | 839 std::vector<SBFullHashResult> results; |
| 839 std::vector<SBPrefix> prefixes; | 840 std::vector<SBPrefix> prefixes; |
| 840 | 841 |
| 841 // Add a fullhash result for each prefix. | 842 // Add a fullhash result for each prefix. |
| 842 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); | 843 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); |
| 843 results.push_back(full_hash); | 844 results.push_back(full_hash); |
| 844 prefixes.push_back(full_hash.hash.prefix); | 845 prefixes.push_back(full_hash.hash.prefix); |
| 845 | 846 |
| 846 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); | 847 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); |
| 847 results.push_back(full_hash); | 848 results.push_back(full_hash); |
| 848 prefixes.push_back(full_hash.hash.prefix); | 849 prefixes.push_back(full_hash.hash.prefix); |
| 849 | 850 |
| 850 database_->CacheHashResults(prefixes, results, kCacheLifetime); | 851 database_->CacheHashResults(prefixes, results, kCacheLifetime); |
| 851 } | 852 } |
| 852 | 853 |
| 853 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 854 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 854 PopulateDatabaseForCacheTest(); | 855 PopulateDatabaseForCacheTest(); |
| 855 | 856 |
| 856 // We should have both full hashes in the cache. | 857 // We should have both full hashes in the cache. |
| 857 EXPECT_EQ(2U, database_->prefix_gethash_cache_.size()); | 858 EXPECT_EQ(2U, |
| 859 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->size()); |
| 858 | 860 |
| 859 // Test the cache lookup for the first prefix. | 861 // Test the cache lookup for the first prefix. |
| 860 std::vector<SBPrefix> prefix_hits; | 862 std::vector<SBPrefix> prefix_hits; |
| 861 std::vector<SBFullHashResult> cache_hits; | 863 std::vector<SBFullHashResult> cache_hits; |
| 862 EXPECT_TRUE(database_->ContainsBrowseUrl( | 864 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 863 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 865 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 864 EXPECT_TRUE(prefix_hits.empty()); | 866 EXPECT_TRUE(prefix_hits.empty()); |
| 865 ASSERT_EQ(1U, cache_hits.size()); | 867 ASSERT_EQ(1U, cache_hits.size()); |
| 866 EXPECT_TRUE(SBFullHashEqual( | 868 EXPECT_TRUE(SBFullHashEqual( |
| 867 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); | 869 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 905 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 904 prefix_hits.clear(); | 906 prefix_hits.clear(); |
| 905 cache_hits.clear(); | 907 cache_hits.clear(); |
| 906 | 908 |
| 907 // Test that an AddDel for the original chunk removes the last cached entry. | 909 // Test that an AddDel for the original chunk removes the last cached entry. |
| 908 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 910 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 909 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 911 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 910 database_->UpdateFinished(true); | 912 database_->UpdateFinished(true); |
| 911 EXPECT_FALSE(database_->ContainsBrowseUrl( | 913 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 912 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 914 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 913 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); | 915 EXPECT_TRUE( |
| 916 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 914 prefix_hits.clear(); | 917 prefix_hits.clear(); |
| 915 cache_hits.clear(); | 918 cache_hits.clear(); |
| 916 | 919 |
| 917 // Test that the cache won't return expired values. First we have to adjust | 920 // Test that the cache won't return expired values. First we have to adjust |
| 918 // the cached entries' received time to make them older, since the database | 921 // the cached entries' received time to make them older, since the database |
| 919 // cache insert uses Time::Now(). First, store some entries. | 922 // cache insert uses Time::Now(). First, store some entries. |
| 920 PopulateDatabaseForCacheTest(); | 923 PopulateDatabaseForCacheTest(); |
| 921 | 924 |
| 922 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache = | 925 SafeBrowsingDatabaseNew::PrefixGetHashCache* hash_cache = |
| 923 &database_->prefix_gethash_cache_; | 926 database_->GetUnsynchronizedPrefixGetHashCacheForTesting(); |
| 924 EXPECT_EQ(2U, hash_cache->size()); | 927 EXPECT_EQ(2U, hash_cache->size()); |
| 925 | 928 |
| 926 // Now adjust one of the entries times to be in the past. | 929 // Now adjust one of the entries times to be in the past. |
| 927 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); | 930 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); |
| 928 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter = | 931 SafeBrowsingDatabaseNew::PrefixGetHashCache::iterator iter = |
| 929 hash_cache->find(key); | 932 hash_cache->find(key); |
| 930 ASSERT_TRUE(iter != hash_cache->end()); | 933 ASSERT_TRUE(iter != hash_cache->end()); |
| 931 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1); | 934 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1); |
| 932 | 935 |
| 933 EXPECT_TRUE(database_->ContainsBrowseUrl( | 936 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 934 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 937 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 935 EXPECT_EQ(1U, prefix_hits.size()); | 938 EXPECT_EQ(1U, prefix_hits.size()); |
| 936 EXPECT_TRUE(cache_hits.empty()); | 939 EXPECT_TRUE(cache_hits.empty()); |
| 937 // Expired entry should have been removed from cache. | 940 // Expired entry should have been removed from cache. |
| 938 EXPECT_EQ(1U, hash_cache->size()); | 941 EXPECT_EQ(1U, hash_cache->size()); |
| (...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1878 | 1881 |
| 1879 // Remove kFullHash1_1 from the database. | 1882 // Remove kFullHash1_1 from the database. |
| 1880 chunks.clear(); | 1883 chunks.clear(); |
| 1881 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 1884 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 1882 | 1885 |
| 1883 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1886 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1884 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1887 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1885 database_->UpdateFinished(true); | 1888 database_->UpdateFinished(true); |
| 1886 | 1889 |
| 1887 // Cache should be cleared after updating. | 1890 // Cache should be cleared after updating. |
| 1888 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); | 1891 EXPECT_TRUE( |
| 1892 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 1889 | 1893 |
| 1890 { | 1894 { |
| 1891 // Now the database doesn't contain kFullHash1_1. | 1895 // Now the database doesn't contain kFullHash1_1. |
| 1892 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1896 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1893 std::vector<SBPrefix> prefix_hits; | 1897 std::vector<SBPrefix> prefix_hits; |
| 1894 std::vector<SBFullHashResult> cache_hits; | 1898 std::vector<SBFullHashResult> cache_hits; |
| 1895 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1899 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1896 full_hashes, &prefix_hits, &cache_hits)); | 1900 full_hashes, &prefix_hits, &cache_hits)); |
| 1897 | 1901 |
| 1898 // Nor kFullHash1_3. | 1902 // Nor kFullHash1_3. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1915 | 1919 |
| 1916 // Remove kFullHash1_2 from the database. | 1920 // Remove kFullHash1_2 from the database. |
| 1917 chunks.clear(); | 1921 chunks.clear(); |
| 1918 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); | 1922 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); |
| 1919 | 1923 |
| 1920 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1924 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1921 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1925 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1922 database_->UpdateFinished(true); | 1926 database_->UpdateFinished(true); |
| 1923 | 1927 |
| 1924 // Cache should be cleared after updating. | 1928 // Cache should be cleared after updating. |
| 1925 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); | 1929 EXPECT_TRUE( |
| 1930 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 1926 | 1931 |
| 1927 { | 1932 { |
| 1928 // None are present. | 1933 // None are present. |
| 1929 std::vector<SBFullHash> full_hashes; | 1934 std::vector<SBFullHash> full_hashes; |
| 1930 std::vector<SBPrefix> prefix_hits; | 1935 std::vector<SBPrefix> prefix_hits; |
| 1931 std::vector<SBFullHashResult> cache_hits; | 1936 std::vector<SBFullHashResult> cache_hits; |
| 1932 full_hashes.push_back(kFullHash1_1); | 1937 full_hashes.push_back(kFullHash1_1); |
| 1933 full_hashes.push_back(kFullHash1_2); | 1938 full_hashes.push_back(kFullHash1_2); |
| 1934 full_hashes.push_back(kFullHash1_3); | 1939 full_hashes.push_back(kFullHash1_3); |
| 1935 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1940 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2130 GURL(std::string("http://") + kExampleCollision), | 2135 GURL(std::string("http://") + kExampleCollision), |
| 2131 &prefix_hits, &cache_hits)); | 2136 &prefix_hits, &cache_hits)); |
| 2132 ASSERT_EQ(1U, prefix_hits.size()); | 2137 ASSERT_EQ(1U, prefix_hits.size()); |
| 2133 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2138 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 2134 EXPECT_TRUE(cache_hits.empty()); | 2139 EXPECT_TRUE(cache_hits.empty()); |
| 2135 | 2140 |
| 2136 // This prefix collides, but no full hash match. | 2141 // This prefix collides, but no full hash match. |
| 2137 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2142 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 2138 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2143 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 2139 } | 2144 } |
| OLD | NEW |