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" |
12 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 13 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
13 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
14 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" | 15 #include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" |
15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
16 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
17 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
18 #include "sql/connection.h" | 19 #include "sql/connection.h" |
19 #include "sql/statement.h" | 20 #include "sql/statement.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
(...skipping 1556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1578 | 1579 |
1579 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not | 1580 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not |
1580 // update the database file. | 1581 // update the database file. |
1581 ASSERT_TRUE(file_util::SetLastModifiedTime(filename, old_last_modified)); | 1582 ASSERT_TRUE(file_util::SetLastModifiedTime(filename, old_last_modified)); |
1582 ASSERT_TRUE(file_util::GetFileInfo(filename, &before_info)); | 1583 ASSERT_TRUE(file_util::GetFileInfo(filename, &before_info)); |
1583 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1584 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
1584 database_->UpdateFinished(true); | 1585 database_->UpdateFinished(true); |
1585 ASSERT_TRUE(file_util::GetFileInfo(filename, &after_info)); | 1586 ASSERT_TRUE(file_util::GetFileInfo(filename, &after_info)); |
1586 EXPECT_EQ(before_info.last_modified, after_info.last_modified); | 1587 EXPECT_EQ(before_info.last_modified, after_info.last_modified); |
1587 } | 1588 } |
| 1589 |
| 1590 // Test that a filter file is written out during update and read back |
| 1591 // in during setup. |
| 1592 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { |
| 1593 // Create a database with trivial example data and write it out. |
| 1594 { |
| 1595 SBChunkList chunks; |
| 1596 SBChunk chunk; |
| 1597 |
| 1598 // Prime the database. |
| 1599 std::vector<SBListChunkRanges> lists; |
| 1600 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1601 |
| 1602 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", |
| 1603 "www.evil.com/malware.html"); |
| 1604 chunks.clear(); |
| 1605 chunks.push_back(chunk); |
| 1606 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1607 database_->UpdateFinished(true); |
| 1608 } |
| 1609 |
| 1610 // Find the malware url in the database, don't find a good url. |
| 1611 const Time now = Time::Now(); |
| 1612 std::vector<SBFullHashResult> full_hashes; |
| 1613 std::vector<SBPrefix> prefix_hits; |
| 1614 std::string matching_list; |
| 1615 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1616 GURL("http://www.evil.com/malware.html"), |
| 1617 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1618 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1619 GURL("http://www.good.com/goodware.html"), |
| 1620 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1621 |
| 1622 FilePath filter_file = database_->PrefixSetForFilename( |
| 1623 database_->BrowseDBFilename(database_filename_)); |
| 1624 |
| 1625 // After re-creating the database, it should have a filter read from |
| 1626 // a file, so it should find the same results. |
| 1627 ASSERT_TRUE(file_util::PathExists(filter_file)); |
| 1628 database_.reset(new SafeBrowsingDatabaseNew); |
| 1629 database_->Init(database_filename_); |
| 1630 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1631 GURL("http://www.evil.com/malware.html"), |
| 1632 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1633 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1634 GURL("http://www.good.com/goodware.html"), |
| 1635 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1636 |
| 1637 // If there is no filter file, the database cannot find malware urls. |
| 1638 file_util::Delete(filter_file, false); |
| 1639 ASSERT_FALSE(file_util::PathExists(filter_file)); |
| 1640 database_.reset(new SafeBrowsingDatabaseNew); |
| 1641 database_->Init(database_filename_); |
| 1642 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1643 GURL("http://www.evil.com/malware.html"), |
| 1644 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1645 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1646 GURL("http://www.good.com/goodware.html"), |
| 1647 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1648 } |
| 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 |