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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "chrome/browser/safe_browsing/bloom_filter.h" | |
13 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 12 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 13 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
15 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" | 14 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" |
16 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
17 #include "crypto/sha2.h" | 16 #include "crypto/sha2.h" |
18 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
19 #include "sql/connection.h" | 18 #include "sql/connection.h" |
20 #include "sql/statement.h" | 19 #include "sql/statement.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 313 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
315 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 314 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
316 database_->UpdateFinished(true); | 315 database_->UpdateFinished(true); |
317 | 316 |
318 GetListsInfo(&lists); | 317 GetListsInfo(&lists); |
319 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 318 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
320 EXPECT_EQ(lists[0].adds, "1-3"); | 319 EXPECT_EQ(lists[0].adds, "1-3"); |
321 EXPECT_EQ(lists[0].subs, "7"); | 320 EXPECT_EQ(lists[0].subs, "7"); |
322 if (lists.size() == 2) { | 321 if (lists.size() == 2) { |
323 // Old style database won't have the second entry since it creates the lists | 322 // Old style database won't have the second entry since it creates the lists |
324 // when it receives an update containing that list. The new bloom filter | 323 // when it receives an update containing that list. The filter-based |
325 // based database has these values hard coded. | 324 // database has these values hard coded. |
326 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); | 325 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); |
327 EXPECT_TRUE(lists[1].adds.empty()); | 326 EXPECT_TRUE(lists[1].adds.empty()); |
328 EXPECT_TRUE(lists[1].subs.empty()); | 327 EXPECT_TRUE(lists[1].subs.empty()); |
329 } | 328 } |
330 | 329 |
331 // Add a phishing add chunk. | 330 // Add a phishing add chunk. |
332 chunk.hosts.clear(); | 331 chunk.hosts.clear(); |
333 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/", | 332 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/", |
334 "www.evil.com/phishing.html"); | 333 "www.evil.com/phishing.html"); |
335 chunks.clear(); | 334 chunks.clear(); |
(...skipping 1303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1639 ASSERT_FALSE(file_util::PathExists(filter_file)); | 1638 ASSERT_FALSE(file_util::PathExists(filter_file)); |
1640 database_.reset(new SafeBrowsingDatabaseNew); | 1639 database_.reset(new SafeBrowsingDatabaseNew); |
1641 database_->Init(database_filename_); | 1640 database_->Init(database_filename_); |
1642 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1641 EXPECT_FALSE(database_->ContainsBrowseUrl( |
1643 GURL("http://www.evil.com/malware.html"), | 1642 GURL("http://www.evil.com/malware.html"), |
1644 &matching_list, &prefix_hits, &full_hashes, now)); | 1643 &matching_list, &prefix_hits, &full_hashes, now)); |
1645 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1644 EXPECT_FALSE(database_->ContainsBrowseUrl( |
1646 GURL("http://www.good.com/goodware.html"), | 1645 GURL("http://www.good.com/goodware.html"), |
1647 &matching_list, &prefix_hits, &full_hashes, now)); | 1646 &matching_list, &prefix_hits, &full_hashes, now)); |
1648 } | 1647 } |
1649 | |
1650 TEST_F(SafeBrowsingDatabaseTest, PrefixSetTransition) { | |
1651 // Create a database with trivial example data and write it out. | |
1652 { | |
1653 SBChunkList chunks; | |
1654 SBChunk chunk; | |
1655 | |
1656 // Prime the database. | |
1657 std::vector<SBListChunkRanges> lists; | |
1658 EXPECT_TRUE(database_->UpdateStarted(&lists)); | |
1659 | |
1660 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", | |
1661 "www.evil.com/malware.html"); | |
1662 chunks.clear(); | |
1663 chunks.push_back(chunk); | |
1664 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | |
1665 database_->UpdateFinished(true); | |
1666 } | |
1667 | |
1668 // Some helpful paths. | |
1669 FilePath prefix_set_file = database_->PrefixSetForFilename( | |
1670 database_->BrowseDBFilename(database_filename_)); | |
1671 FilePath bloom_filter_file = database_->BloomFilterForFilename( | |
1672 database_->BrowseDBFilename(database_filename_)); | |
1673 | |
1674 // Manually create a bloom filter for the prefixes and write it out. | |
1675 { | |
1676 scoped_refptr<BloomFilter> filter( | |
1677 new BloomFilter(BloomFilter::kBloomFilterSizeRatio * | |
1678 BloomFilter::kBloomFilterMinSize)); | |
1679 filter->Insert(Sha256Prefix("www.evil.com/")); | |
1680 filter->Insert(Sha256Prefix("www.evil.com/malware.html")); | |
1681 ASSERT_TRUE(filter->WriteFile(bloom_filter_file)); | |
1682 } | |
1683 | |
1684 file_util::Delete(prefix_set_file, false); | |
1685 ASSERT_FALSE(file_util::PathExists(prefix_set_file)); | |
1686 ASSERT_TRUE(file_util::PathExists(bloom_filter_file)); | |
1687 | |
1688 // Reload the database. | |
1689 database_.reset(new SafeBrowsingDatabaseNew); | |
1690 database_->Init(database_filename_); | |
1691 | |
1692 // Should find the malware. | |
1693 const Time now = Time::Now(); | |
1694 std::vector<SBFullHashResult> full_hashes; | |
1695 std::vector<SBPrefix> prefix_hits; | |
1696 std::string matching_list; | |
1697 EXPECT_TRUE(database_->ContainsBrowseUrl( | |
1698 GURL("http://www.evil.com/malware.html"), | |
1699 &matching_list, &prefix_hits, &full_hashes, now)); | |
1700 EXPECT_FALSE(database_->ContainsBrowseUrl( | |
1701 GURL("http://www.good.com/goodware.html"), | |
1702 &matching_list, &prefix_hits, &full_hashes, now)); | |
1703 } | |
OLD | NEW |