diff options
author | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-09 21:41:14 +0000 |
---|---|---|
committer | shess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-09 21:41:14 +0000 |
commit | 11d288907f2fd35ed9c27ef16093bd4f1d8f35ae (patch) | |
tree | 904036295be9f1d9707382e6cf5ee3df3b8d5844 /chrome/browser/safe_browsing | |
parent | 21fc8e76330cca8eaca830bd1f3f8ee0e8889e1b (diff) | |
download | chromium_src-11d288907f2fd35ed9c27ef16093bd4f1d8f35ae.zip chromium_src-11d288907f2fd35ed9c27ef16093bd4f1d8f35ae.tar.gz chromium_src-11d288907f2fd35ed9c27ef16093bd4f1d8f35ae.tar.bz2 |
Clean up SafeBrowsingDatabaseTest setup/teardown.
Pull handling of the database setup, teardown, and memory management
up into the test class. Push a bunch of global functions into the
test class. Use helper functions for dealing with hashes.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/755001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41079 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/safe_browsing')
-rw-r--r-- | chrome/browser/safe_browsing/safe_browsing_database.h | 2 | ||||
-rw-r--r-- | chrome/browser/safe_browsing/safe_browsing_database_unittest.cc | 648 |
2 files changed, 304 insertions, 346 deletions
diff --git a/chrome/browser/safe_browsing/safe_browsing_database.h b/chrome/browser/safe_browsing/safe_browsing_database.h index c188f25..88bf58a 100644 --- a/chrome/browser/safe_browsing/safe_browsing_database.h +++ b/chrome/browser/safe_browsing/safe_browsing_database.h @@ -83,7 +83,7 @@ class SafeBrowsingDatabase { typedef base::hash_map<SBPrefix, HashList> HashCache; friend class SafeBrowsingDatabaseTest; - FRIEND_TEST(SafeBrowsingDatabase, HashCaching); + FRIEND_TEST(SafeBrowsingDatabaseTest, HashCaching); static FilePath BloomFilterFilename(const FilePath& db_filename); diff --git a/chrome/browser/safe_browsing/safe_browsing_database_unittest.cc b/chrome/browser/safe_browsing/safe_browsing_database_unittest.cc index 38c5f28..84cbc80 100644 --- a/chrome/browser/safe_browsing/safe_browsing_database_unittest.cc +++ b/chrome/browser/safe_browsing/safe_browsing_database_unittest.cc @@ -15,6 +15,7 @@ #include "base/time.h" #include "chrome/browser/safe_browsing/protocol_parser.h" #include "chrome/browser/safe_browsing/safe_browsing_database.h" +#include "chrome/browser/safe_browsing/safe_browsing_store_unittest_helper.h" #include "chrome/test/file_test_utils.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -23,94 +24,89 @@ using base::Time; static const FilePath::CharType kBloomSuffix[] = FILE_PATH_LITERAL(" Bloom"); -static const FilePath::CharType kFilterSuffix[] = FILE_PATH_LITERAL(" Filter"); static const FilePath::CharType kFolderPrefix[] = FILE_PATH_LITERAL("SafeBrowsingTestDatabase"); namespace { - SBPrefix Sha256Prefix(const std::string& str) { - SBPrefix hash; - base::SHA256HashString(str, &hash, sizeof(hash)); - return hash; - } - // Helper function to do an AddDel or SubDel command. - void DelChunk(SafeBrowsingDatabase* db, - const std::string& list, - int chunk_id, - bool is_sub_del) { - std::vector<SBChunkDelete> deletes; - SBChunkDelete chunk_delete; - chunk_delete.list_name = list; - chunk_delete.is_sub_del = is_sub_del; - chunk_delete.chunk_del.push_back(ChunkRange(chunk_id)); - deletes.push_back(chunk_delete); - db->DeleteChunks(deletes); - } +SBPrefix Sha256Prefix(const std::string& str) { + SBPrefix prefix; + base::SHA256HashString(str, &prefix, sizeof(prefix)); + return prefix; +} - void AddDelChunk(SafeBrowsingDatabase* db, - const std::string& list, - int chunk_id) { - DelChunk(db, list, chunk_id, false); - } +SBFullHash Sha256Hash(const std::string& str) { + SBFullHash hash; + base::SHA256HashString(str, &hash, sizeof(hash)); + return hash; +} - void SubDelChunk(SafeBrowsingDatabase* db, - const std::string& list, - int chunk_id) { - DelChunk(db, list, chunk_id, true); - } +} // namespace - // Creates a new test directory. - FilePath CreateTestDirectory() { - FilePath temp_dir; - EXPECT_TRUE(file_util::CreateNewTempDirectory(kFolderPrefix, &temp_dir)); - return temp_dir; - } +class SafeBrowsingDatabaseTest : public PlatformTest { + public: + virtual void SetUp() { + PlatformTest::SetUp(); - // Common database test set up code. - FilePath GetTestDatabaseName(const FilePath& test_dir) { - FilePath filename(test_dir); - filename = filename.AppendASCII("SafeBrowsingTestDatabase"); - return filename; - } + // Temporary directory for the database files. + FilePath temp_dir; + ASSERT_TRUE(file_util::CreateNewTempDirectory(kFolderPrefix, &temp_dir)); + file_deleter_.reset(new FileAutoDeleter(temp_dir)); - SafeBrowsingDatabase* SetupTestDatabase(const FilePath& test_dir) { - FilePath filename = GetTestDatabaseName(test_dir); + FilePath filename(temp_dir); + filename.AppendASCII("SafeBrowsingTestDatabase"); // In case it existed from a previous run. file_util::Delete(FilePath(filename.value() + kBloomSuffix), false); file_util::Delete(filename, false); - SafeBrowsingDatabase* database = SafeBrowsingDatabase::Create(); - database->Init(filename); + database_.reset(SafeBrowsingDatabase::Create()); + database_->Init(filename); + } + + virtual void TearDown() { + database_.reset(); + file_deleter_.reset(); - return database; + PlatformTest::TearDown(); } - void TearDownTestDatabase(SafeBrowsingDatabase* database) { - FilePath filename = database->filename(); - delete database; - file_util::Delete(filename, false); - file_util::Delete(FilePath(filename.value() + kFilterSuffix), - false); + void GetListsInfo(std::vector<SBListChunkRanges>* lists) { + EXPECT_TRUE(database_->UpdateStarted()); + database_->GetListsInfo(lists); + database_->UpdateFinished(true); } - void GetListsInfo(SafeBrowsingDatabase* database, - std::vector<SBListChunkRanges>* lists) { - EXPECT_TRUE(database->UpdateStarted()); - database->GetListsInfo(lists); - database->UpdateFinished(true); + // Helper function to do an AddDel or SubDel command. + void DelChunk(const std::string& list, + int chunk_id, + bool is_sub_del) { + std::vector<SBChunkDelete> deletes; + SBChunkDelete chunk_delete; + chunk_delete.list_name = list; + chunk_delete.is_sub_del = is_sub_del; + chunk_delete.chunk_del.push_back(ChunkRange(chunk_id)); + deletes.push_back(chunk_delete); + database_->DeleteChunks(deletes); } -} // namespace + void AddDelChunk(const std::string& list, int chunk_id) { + DelChunk(list, chunk_id, false); + } + + void SubDelChunk(const std::string& list, int chunk_id) { + DelChunk(list, chunk_id, true); + } + + // Utility function for setting up the database for the caching test. + void PopulateDatabaseForCacheTest(); -class SafeBrowsingDatabasePlatformTest : public PlatformTest { + scoped_ptr<FileAutoDeleter> file_deleter_; + scoped_ptr<SafeBrowsingDatabase> database_; }; // Tests retrieving list name information. -TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { - FileAutoDeleter file_deleter(CreateTestDirectory()); - SafeBrowsingDatabase* database = SetupTestDatabase(file_deleter.path()); +TEST_F(SafeBrowsingDatabaseTest, ListName) { SBChunkList chunks; // Insert some malware add chunks. @@ -125,8 +121,8 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateStarted(); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); host.host = Sha256Prefix("www.foo.com/"); host.entry = SBEntry::Create(SBEntry::ADD_PREFIX, 1); @@ -138,7 +134,7 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); host.host = Sha256Prefix("www.whatever.com/"); host.entry = SBEntry::Create(SBEntry::ADD_PREFIX, 1); @@ -150,11 +146,11 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); std::vector<SBListChunkRanges> lists; - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1-3"); EXPECT_TRUE(lists[0].subs.empty()); @@ -173,13 +169,13 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1-3"); EXPECT_EQ(lists[0].subs, "7"); @@ -204,9 +200,9 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kPhishingList, chunks); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); // Insert some phishing sub chunks. host.host = Sha256Prefix("www.phishy.com/"); @@ -220,7 +216,7 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kPhishingList, chunks); + database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); host.host = Sha256Prefix("www.phishy2.com/"); host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1); @@ -233,11 +229,11 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kPhishingList, chunks); - database->UpdateFinished(true); + database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1-3"); EXPECT_EQ(lists[0].subs, "7"); @@ -245,14 +241,10 @@ TEST_F(SafeBrowsingDatabasePlatformTest, ListName) { EXPECT_EQ(lists[1].adds, "47"); EXPECT_EQ(lists[1].subs, "200-201"); lists.clear(); - - TearDownTestDatabase(database); } // Checks database reading and writing. -TEST(SafeBrowsingDatabase, Database) { - FileAutoDeleter file_deleter(CreateTestDirectory()); - SafeBrowsingDatabase* database = SetupTestDatabase(file_deleter.path()); +TEST_F(SafeBrowsingDatabaseTest, Database) { SBChunkList chunks; // Add a simple chunk with one hostkey. @@ -271,9 +263,9 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); std::vector<SBListChunkRanges> lists; - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); // Add another chunk with two different hostkeys. host.host = Sha256Prefix("www.evil.com/"); @@ -297,7 +289,7 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); // and a chunk with an IP-based host host.host = Sha256Prefix("192.168.0.1/"); @@ -311,12 +303,12 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); // Make sure they were added correctly. - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1-3"); EXPECT_TRUE(lists[0].subs.empty()); @@ -326,45 +318,45 @@ TEST(SafeBrowsingDatabase, Database) { std::vector<SBFullHashResult> full_hashes; std::vector<SBPrefix> prefix_hits; std::string matching_list; - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); EXPECT_EQ(prefix_hits[0], Sha256Prefix("www.evil.com/phishing.html")); EXPECT_EQ(prefix_hits.size(), 1U); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/malware.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); - - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/notevil1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); - - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/malware.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.good.com/good1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/notevil1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.good.com/good2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://192.168.0.1/malware.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.good.com/good1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.evil.com/"), + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.good.com/good2.html"), &matching_list, &prefix_hits, &full_hashes, now)); - EXPECT_EQ(prefix_hits.size(), 0U); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.evil.com/robots.txt"), + EXPECT_TRUE(database_->ContainsUrl(GURL("http://192.168.0.1/malware.html"), &matching_list, &prefix_hits, &full_hashes, now)); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.evil.com/"), + &matching_list, &prefix_hits, + &full_hashes, now)); + EXPECT_EQ(prefix_hits.size(), 0U); + + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.evil.com/robots.txt"), + &matching_list, &prefix_hits, + &full_hashes, now)); + // Attempt to re-add the first chunk (should be a no-op). @@ -382,13 +374,13 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1-3"); EXPECT_TRUE(lists[0].subs.empty()); @@ -410,36 +402,36 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); EXPECT_EQ(prefix_hits[0], Sha256Prefix("www.evil.com/phishing.html")); EXPECT_EQ(prefix_hits.size(), 1U); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.evil.com/notevil1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.evil.com/notevil1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); EXPECT_EQ(prefix_hits.size(), 0U); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.good.com/good1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.good.com/good1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.good.com/good2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.good.com/good2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].subs, "4"); lists.clear(); @@ -460,38 +452,38 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].subs, "4"); lists.clear(); // Test removing all the prefixes from an add chunk. - database->UpdateStarted(); - database->GetListsInfo(&lists); - AddDelChunk(database, safe_browsing_util::kMalwareList, 2); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + AddDelChunk(safe_browsing_util::kMalwareList, 2); + database_->UpdateFinished(true); lists.clear(); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.evil.com/notevil2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.good.com/good1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.good.com/good1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.good.com/good2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.good.com/good2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1,3"); EXPECT_EQ(lists[0].subs, "4"); @@ -512,20 +504,20 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); // Now remove the dummy entry. If there are any problems with the // transactions, asserts will fire. - AddDelChunk(database, safe_browsing_util::kMalwareList, 44); + AddDelChunk(safe_browsing_util::kMalwareList, 44); // Test the subdel command. - SubDelChunk(database, safe_browsing_util::kMalwareList, 4); - database->UpdateFinished(true); + SubDelChunk(safe_browsing_util::kMalwareList, 4); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); EXPECT_EQ(lists[0].adds, "1,3"); EXPECT_EQ(lists[0].subs, ""); @@ -547,13 +539,13 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - EXPECT_FALSE(database->ContainsUrl( + EXPECT_FALSE(database_->ContainsUrl( GURL("http://www.notevilanymore.com/index.html"), &matching_list, &prefix_hits, &full_hashes, now)); @@ -570,28 +562,24 @@ TEST(SafeBrowsingDatabase, Database) { chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - EXPECT_FALSE(database->ContainsUrl( + EXPECT_FALSE(database_->ContainsUrl( GURL("http://www.notevilanymore.com/index.html"), &matching_list, &prefix_hits, &full_hashes, now)); - EXPECT_FALSE(database->ContainsUrl( + EXPECT_FALSE(database_->ContainsUrl( GURL("http://www.notevilanymore.com/good.html"), &matching_list, &prefix_hits, &full_hashes, now)); - - TearDownTestDatabase(database); } // Test adding zero length chunks to the database. -TEST(SafeBrowsingDatabase, ZeroSizeChunk) { - FileAutoDeleter file_deleter(CreateTestDirectory()); - SafeBrowsingDatabase* database = SetupTestDatabase(file_deleter.path()); +TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { SBChunkList chunks; // Populate with a couple of normal chunks. @@ -621,14 +609,14 @@ TEST(SafeBrowsingDatabase, ZeroSizeChunk) { chunks.push_back(chunk); std::vector<SBListChunkRanges> lists; - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); // Add an empty ADD and SUB chunk. - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_EQ(lists[0].adds, "1,10"); lists.clear(); @@ -637,18 +625,18 @@ TEST(SafeBrowsingDatabase, ZeroSizeChunk) { empty_chunk.is_add = true; chunks.clear(); chunks.push_back(empty_chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); chunks.clear(); empty_chunk.chunk_number = 7; empty_chunk.is_add = false; chunks.push_back(empty_chunk); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_EQ(lists[0].adds, "1,10,19"); EXPECT_EQ(lists[0].subs, "7"); lists.clear(); @@ -681,56 +669,54 @@ TEST(SafeBrowsingDatabase, ZeroSizeChunk) { empty_chunk.is_add = true; chunks.push_back(empty_chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); const Time now = Time::Now(); std::vector<SBFullHashResult> full_hashes; std::vector<SBPrefix> prefix_hits; std::string matching_list; - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.notempty.com/full1.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.notempty.com/full2.html"), - &matching_list, &prefix_hits, - &full_hashes, now)); - - GetListsInfo(database, &lists); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.notempty.com/full1.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.notempty.com/full2.html"), + &matching_list, &prefix_hits, + &full_hashes, now)); + + GetListsInfo(&lists); EXPECT_EQ(lists[0].adds, "1,10,19-22"); EXPECT_EQ(lists[0].subs, "7"); lists.clear(); // Handle AddDel and SubDel commands for empty chunks. - database->UpdateStarted(); - database->GetListsInfo(&lists); - AddDelChunk(database, safe_browsing_util::kMalwareList, 21); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + AddDelChunk(safe_browsing_util::kMalwareList, 21); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_EQ(lists[0].adds, "1,10,19-20,22"); EXPECT_EQ(lists[0].subs, "7"); lists.clear(); - database->UpdateStarted(); - database->GetListsInfo(&lists); - SubDelChunk(database, safe_browsing_util::kMalwareList, 7); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + SubDelChunk(safe_browsing_util::kMalwareList, 7); + database_->UpdateFinished(true); lists.clear(); - GetListsInfo(database, &lists); + GetListsInfo(&lists); EXPECT_EQ(lists[0].adds, "1,10,19-20,22"); EXPECT_EQ(lists[0].subs, ""); lists.clear(); - - TearDownTestDatabase(database); } // Utility function for setting up the database for the caching test. -void PopulateDatabaseForCacheTest(SafeBrowsingDatabase* database) { +void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { // Add a simple chunk with one hostkey and cache it. SBChunkHost host; host.host = Sha256Prefix("www.evil.com/"); @@ -747,65 +733,54 @@ void PopulateDatabaseForCacheTest(SafeBrowsingDatabase* database) { SBChunkList chunks; std::vector<SBListChunkRanges> lists; chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); // Add the GetHash results to the cache. SBFullHashResult full_hash; - base::SHA256HashString("www.evil.com/phishing.html", - &full_hash.hash, sizeof(SBFullHash)); + full_hash.hash = Sha256Hash("www.evil.com/phishing.html"); full_hash.list_name = safe_browsing_util::kMalwareList; full_hash.add_chunk_id = 1; std::vector<SBFullHashResult> results; results.push_back(full_hash); - base::SHA256HashString("www.evil.com/malware.html", - &full_hash.hash, sizeof(SBFullHash)); + full_hash.hash = Sha256Hash("www.evil.com/malware.html"); results.push_back(full_hash); std::vector<SBPrefix> prefixes; - database->CacheHashResults(prefixes, results); + database_->CacheHashResults(prefixes, results); } -TEST(SafeBrowsingDatabase, HashCaching) { - FileAutoDeleter file_deleter(CreateTestDirectory()); - SafeBrowsingDatabase* database = SetupTestDatabase(file_deleter.path()); - - PopulateDatabaseForCacheTest(database); +TEST_F(SafeBrowsingDatabaseTest, HashCaching) { + PopulateDatabaseForCacheTest(); // We should have both full hashes in the cache. - SafeBrowsingDatabase::HashCache* hash_cache = database->hash_cache(); + SafeBrowsingDatabase::HashCache* hash_cache = database_->hash_cache(); EXPECT_EQ(hash_cache->size(), 2U); // Test the cache lookup for the first prefix. std::string listname; std::vector<SBPrefix> prefixes; std::vector<SBFullHashResult> full_hashes; - database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &listname, &prefixes, &full_hashes, Time::Now()); + database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &listname, &prefixes, &full_hashes, Time::Now()); EXPECT_EQ(full_hashes.size(), 1U); - - SBFullHashResult full_hash; - base::SHA256HashString("www.evil.com/phishing.html", - &full_hash.hash, sizeof(SBFullHash)); - EXPECT_EQ(memcmp(&full_hashes[0].hash, - &full_hash.hash, sizeof(SBFullHash)), 0); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.evil.com/phishing.html"))); prefixes.clear(); full_hashes.clear(); // Test the cache lookup for the second prefix. - database->ContainsUrl(GURL("http://www.evil.com/malware.html"), - &listname, &prefixes, &full_hashes, Time::Now()); + database_->ContainsUrl(GURL("http://www.evil.com/malware.html"), + &listname, &prefixes, &full_hashes, Time::Now()); EXPECT_EQ(full_hashes.size(), 1U); - base::SHA256HashString("www.evil.com/malware.html", - &full_hash.hash, sizeof(SBFullHash)); - EXPECT_EQ(memcmp(&full_hashes[0].hash, - &full_hash.hash, sizeof(SBFullHash)), 0); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.evil.com/malware.html"))); prefixes.clear(); full_hashes.clear(); @@ -827,41 +802,39 @@ TEST(SafeBrowsingDatabase, HashCaching) { chunks.push_back(chunk); std::vector<SBListChunkRanges> lists; - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); lists.clear(); // This prefix should still be there. - database->ContainsUrl(GURL("http://www.evil.com/malware.html"), - &listname, &prefixes, &full_hashes, Time::Now()); + database_->ContainsUrl(GURL("http://www.evil.com/malware.html"), + &listname, &prefixes, &full_hashes, Time::Now()); EXPECT_EQ(full_hashes.size(), 1U); - base::SHA256HashString("www.evil.com/malware.html", - &full_hash.hash, sizeof(SBFullHash)); - EXPECT_EQ(memcmp(&full_hashes[0].hash, - &full_hash.hash, sizeof(SBFullHash)), 0); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.evil.com/malware.html"))); prefixes.clear(); full_hashes.clear(); // This prefix should be gone. - database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &listname, &prefixes, &full_hashes, Time::Now()); + database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &listname, &prefixes, &full_hashes, Time::Now()); EXPECT_EQ(full_hashes.size(), 0U); prefixes.clear(); full_hashes.clear(); // Test that an AddDel for the original chunk removes the last cached entry. - database->UpdateStarted(); - database->GetListsInfo(&lists); - AddDelChunk(database, safe_browsing_util::kMalwareList, 1); - database->UpdateFinished(true); - database->ContainsUrl(GURL("http://www.evil.com/malware.html"), - &listname, &prefixes, &full_hashes, Time::Now()); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + AddDelChunk(safe_browsing_util::kMalwareList, 1); + database_->UpdateFinished(true); + database_->ContainsUrl(GURL("http://www.evil.com/malware.html"), + &listname, &prefixes, &full_hashes, Time::Now()); EXPECT_EQ(full_hashes.size(), 0U); - EXPECT_EQ(database->hash_cache()->size(), 0U); + EXPECT_EQ(database_->hash_cache()->size(), 0U); lists.clear(); prefixes.clear(); @@ -870,53 +843,52 @@ TEST(SafeBrowsingDatabase, HashCaching) { // Test that the cache won't return expired values. First we have to adjust // the cached entries' received time to make them older, since the database // cache insert uses Time::Now(). First, store some entries. - PopulateDatabaseForCacheTest(database); - hash_cache = database->hash_cache(); + PopulateDatabaseForCacheTest(); + hash_cache = database_->hash_cache(); EXPECT_EQ(hash_cache->size(), 2U); // Now adjust one of the entries times to be in the past. base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60); - SBPrefix key; - memcpy(&key, &full_hash.hash, sizeof(SBPrefix)); + const SBPrefix key = Sha256Prefix("www.evil.com/malware.html"); SafeBrowsingDatabase::HashList& entries = (*hash_cache)[key]; SafeBrowsingDatabase::HashCacheEntry entry = entries.front(); entries.pop_front(); entry.received = expired; entries.push_back(entry); - database->ContainsUrl(GURL("http://www.evil.com/malware.html"), - &listname, &prefixes, &full_hashes, expired); + database_->ContainsUrl(GURL("http://www.evil.com/malware.html"), + &listname, &prefixes, &full_hashes, expired); EXPECT_EQ(full_hashes.size(), 0U); // This entry should still exist. - database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &listname, &prefixes, &full_hashes, expired); + database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &listname, &prefixes, &full_hashes, expired); EXPECT_EQ(full_hashes.size(), 1U); // Testing prefix miss caching. First, we clear out the existing database, // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate // chunks. - database->UpdateStarted(); - database->GetListsInfo(&lists); - AddDelChunk(database, safe_browsing_util::kMalwareList, 1); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + AddDelChunk(safe_browsing_util::kMalwareList, 1); + database_->UpdateFinished(true); lists.clear(); std::vector<SBPrefix> prefix_misses; std::vector<SBFullHashResult> empty_full_hash; prefix_misses.push_back(Sha256Prefix("http://www.bad.com/malware.html")); prefix_misses.push_back(Sha256Prefix("http://www.bad.com/phishing.html")); - database->CacheHashResults(prefix_misses, empty_full_hash); + database_->CacheHashResults(prefix_misses, empty_full_hash); // Prefixes with no full results are misses. - EXPECT_EQ(database->prefix_miss_cache()->size(), 2U); + EXPECT_EQ(database_->prefix_miss_cache()->size(), 2U); // Update the database. - PopulateDatabaseForCacheTest(database); + PopulateDatabaseForCacheTest(); // Prefix miss cache should be cleared. - EXPECT_EQ(database->prefix_miss_cache()->size(), 0U); + EXPECT_EQ(database_->prefix_miss_cache()->size(), 0U); // Cache a GetHash miss for a particular prefix, and even though the prefix is // in the database, it is flagged as a miss so looking up the associated URL @@ -926,10 +898,10 @@ TEST(SafeBrowsingDatabase, HashCaching) { prefix_misses.clear(); empty_full_hash.clear(); prefix_misses.push_back(Sha256Prefix("www.evil.com/phishing.html")); - database->CacheHashResults(prefix_misses, empty_full_hash); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.evil.com/phishing.html"), - &listname, &prefixes, - &full_hashes, Time::Now())); + database_->CacheHashResults(prefix_misses, empty_full_hash); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.evil.com/phishing.html"), + &listname, &prefixes, + &full_hashes, Time::Now())); lists.clear(); prefixes.clear(); @@ -939,14 +911,8 @@ TEST(SafeBrowsingDatabase, HashCaching) { host.host = Sha256Prefix("www.fullevil.com/"); host.entry = SBEntry::Create(SBEntry::ADD_FULL_HASH, 2); host.entry->set_chunk_id(20); - SBFullHash full_add1; - base::SHA256HashString("www.fullevil.com/bad1.html", - &full_add1.full_hash, sizeof(SBFullHash)); - host.entry->SetFullHashAt(0, full_add1); - SBFullHash full_add2; - base::SHA256HashString("www.fullevil.com/bad2.html", - &full_add2.full_hash, sizeof(SBFullHash)); - host.entry->SetFullHashAt(1, full_add2); + host.entry->SetFullHashAt(0, Sha256Hash("www.fullevil.com/bad1.html")); + host.entry->SetFullHashAt(1, Sha256Hash("www.fullevil.com/bad2.html")); chunk.chunk_number = 20; chunk.is_add = true; @@ -954,29 +920,27 @@ TEST(SafeBrowsingDatabase, HashCaching) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); EXPECT_EQ(full_hashes.size(), 1U); - EXPECT_EQ(0, memcmp(full_hashes[0].hash.full_hash, - full_add1.full_hash, - sizeof(SBFullHash))); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.fullevil.com/bad1.html"))); lists.clear(); prefixes.clear(); full_hashes.clear(); - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); EXPECT_EQ(full_hashes.size(), 1U); - EXPECT_EQ(0, memcmp(full_hashes[0].hash.full_hash, - full_add2.full_hash, - sizeof(SBFullHash))); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.fullevil.com/bad2.html"))); lists.clear(); prefixes.clear(); full_hashes.clear(); @@ -986,10 +950,7 @@ TEST(SafeBrowsingDatabase, HashCaching) { host.entry = SBEntry::Create(SBEntry::SUB_FULL_HASH, 1); host.entry->set_chunk_id(200); host.entry->SetChunkIdAtPrefix(0, 20); - SBFullHash full_sub; - base::SHA256HashString("www.fullevil.com/bad1.html", - &full_sub.full_hash, sizeof(SBFullHash)); - host.entry->SetFullHashAt(0, full_sub); + host.entry->SetFullHashAt(0, Sha256Hash("www.fullevil.com/bad1.html")); chunk.chunk_number = 200; chunk.is_add = false; @@ -997,45 +958,44 @@ TEST(SafeBrowsingDatabase, HashCaching) { chunk.hosts.push_back(host); chunks.clear(); chunks.push_back(chunk); - database->UpdateStarted(); - database->GetListsInfo(&lists); - database->InsertChunks(safe_browsing_util::kMalwareList, chunks); - database->UpdateFinished(true); - - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); + database_->UpdateFinished(true); + + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); EXPECT_EQ(full_hashes.size(), 0U); // There should be one remaining full add. - EXPECT_TRUE(database->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); + EXPECT_TRUE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); EXPECT_EQ(full_hashes.size(), 1U); - EXPECT_EQ(0, memcmp(full_hashes[0].hash.full_hash, - full_add2.full_hash, - sizeof(SBFullHash))); + EXPECT_TRUE(SBFullHashEq(full_hashes[0].hash, + Sha256Hash("www.fullevil.com/bad2.html"))); lists.clear(); prefixes.clear(); full_hashes.clear(); // Now test an AddDel for the remaining full add. - database->UpdateStarted(); - database->GetListsInfo(&lists); - AddDelChunk(database, safe_browsing_util::kMalwareList, 20); - database->UpdateFinished(true); + database_->UpdateStarted(); + database_->GetListsInfo(&lists); + AddDelChunk(safe_browsing_util::kMalwareList, 20); + database_->UpdateFinished(true); lists.clear(); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); - EXPECT_FALSE(database->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), - &listname, &prefixes, &full_hashes, - Time::Now())); - - TearDownTestDatabase(database); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad1.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); + EXPECT_FALSE(database_->ContainsUrl(GURL("http://www.fullevil.com/bad2.html"), + &listname, &prefixes, &full_hashes, + Time::Now())); } +namespace { + void PrintStat(const char* name) { int value = StatsTable::current()->GetCounterValue(name); LOG(INFO) << StringPrintf("%s %d", name, value); @@ -1201,8 +1161,6 @@ void UpdateDatabase(const FilePath& initial_db, } } -namespace { - // Construct the shared base path used by the GetOld* functions. FilePath BasePath() { return FilePath(FILE_PATH_LITERAL("old")); |