| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 InsertAddChunkHostFullHashes(&chunk, 6, "www.download.com/", | 417 InsertAddChunkHostFullHashes(&chunk, 6, "www.download.com/", |
| 418 "www.download.com/"); | 418 "www.download.com/"); |
| 419 | 419 |
| 420 chunks.clear(); | 420 chunks.clear(); |
| 421 chunks.push_back(chunk); | 421 chunks.push_back(chunk); |
| 422 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, chunks); | 422 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, chunks); |
| 423 | 423 |
| 424 database_->UpdateFinished(true); | 424 database_->UpdateFinished(true); |
| 425 | 425 |
| 426 GetListsInfo(&lists); | 426 GetListsInfo(&lists); |
| 427 EXPECT_EQ(6U, lists.size()); | 427 ASSERT_EQ(5U, lists.size()); |
| 428 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 428 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 429 EXPECT_EQ(lists[0].adds, "1"); | 429 EXPECT_EQ(lists[0].adds, "1"); |
| 430 EXPECT_TRUE(lists[0].subs.empty()); | 430 EXPECT_TRUE(lists[0].subs.empty()); |
| 431 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); | 431 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); |
| 432 EXPECT_EQ(lists[1].adds, "2"); | 432 EXPECT_EQ(lists[1].adds, "2"); |
| 433 EXPECT_TRUE(lists[1].subs.empty()); | 433 EXPECT_TRUE(lists[1].subs.empty()); |
| 434 EXPECT_TRUE(lists[2].name == safe_browsing_util::kBinUrlList); | 434 EXPECT_TRUE(lists[2].name == safe_browsing_util::kBinUrlList); |
| 435 EXPECT_EQ(lists[2].adds, "3"); | 435 EXPECT_EQ(lists[2].adds, "3"); |
| 436 EXPECT_TRUE(lists[2].subs.empty()); | 436 EXPECT_TRUE(lists[2].subs.empty()); |
| 437 EXPECT_TRUE(lists[3].name == safe_browsing_util::kBinHashList); | 437 // kBinHashList is ignored. (http://crbug.com/108130) |
| 438 EXPECT_EQ(lists[3].adds, "4"); | 438 EXPECT_TRUE(lists[3].name == safe_browsing_util::kCsdWhiteList); |
| 439 EXPECT_EQ(lists[3].adds, "5"); |
| 439 EXPECT_TRUE(lists[3].subs.empty()); | 440 EXPECT_TRUE(lists[3].subs.empty()); |
| 440 EXPECT_TRUE(lists[4].name == safe_browsing_util::kCsdWhiteList); | 441 EXPECT_TRUE(lists[4].name == safe_browsing_util::kDownloadWhiteList); |
| 441 EXPECT_EQ(lists[4].adds, "5"); | 442 EXPECT_EQ(lists[4].adds, "6"); |
| 442 EXPECT_TRUE(lists[4].subs.empty()); | 443 EXPECT_TRUE(lists[4].subs.empty()); |
| 443 EXPECT_TRUE(lists[5].name == safe_browsing_util::kDownloadWhiteList); | |
| 444 EXPECT_EQ(lists[5].adds, "6"); | |
| 445 EXPECT_TRUE(lists[5].subs.empty()); | |
| 446 database_.reset(); | 444 database_.reset(); |
| 447 } | 445 } |
| 448 | 446 |
| 449 // Checks database reading and writing for browse. | 447 // Checks database reading and writing for browse. |
| 450 TEST_F(SafeBrowsingDatabaseTest, BrowseDatabase) { | 448 TEST_F(SafeBrowsingDatabaseTest, BrowseDatabase) { |
| 451 SBChunkList chunks; | 449 SBChunkList chunks; |
| 452 SBChunk chunk; | 450 SBChunk chunk; |
| 453 | 451 |
| 454 // Add a simple chunk with one hostkey. | 452 // Add a simple chunk with one hostkey. |
| 455 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", | 453 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", |
| (...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1519 GURL("http://www.evil.com/malware2.html"), | 1517 GURL("http://www.evil.com/malware2.html"), |
| 1520 &listname, &prefixes, &full_hashes, now)); | 1518 &listname, &prefixes, &full_hashes, now)); |
| 1521 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1519 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1522 GURL("http://www.evil.com/phishing1.html"), | 1520 GURL("http://www.evil.com/phishing1.html"), |
| 1523 &listname, &prefixes, &full_hashes, now)); | 1521 &listname, &prefixes, &full_hashes, now)); |
| 1524 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1522 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1525 GURL("http://www.evil.com/phishing2.html"), | 1523 GURL("http://www.evil.com/phishing2.html"), |
| 1526 &listname, &prefixes, &full_hashes, now)); | 1524 &listname, &prefixes, &full_hashes, now)); |
| 1527 } | 1525 } |
| 1528 | 1526 |
| 1529 TEST_F(SafeBrowsingDatabaseTest, BinHashInsertLookup) { | |
| 1530 const SBPrefix kPrefix1 = 0x31313131; | |
| 1531 const SBPrefix kPrefix2 = 0x32323232; | |
| 1532 const SBPrefix kPrefix3 = 0x33333333; | |
| 1533 database_.reset(); | |
| 1534 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | |
| 1535 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | |
| 1536 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | |
| 1537 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | |
| 1538 download_store, | |
| 1539 NULL, NULL)); | |
| 1540 database_->Init(database_filename_); | |
| 1541 | |
| 1542 SBChunkList chunks; | |
| 1543 SBChunk chunk; | |
| 1544 // Insert one host. | |
| 1545 InsertAddChunkHostPrefixValue(&chunk, 1, 0, kPrefix1); | |
| 1546 // Insert a second host, which has the same host prefix as the first one. | |
| 1547 InsertAddChunkHostPrefixValue(&chunk, 1, 0, kPrefix2); | |
| 1548 chunks.push_back(chunk); | |
| 1549 | |
| 1550 // Insert the testing chunks into database. | |
| 1551 std::vector<SBListChunkRanges> lists; | |
| 1552 EXPECT_TRUE(database_->UpdateStarted(&lists)); | |
| 1553 database_->InsertChunks(safe_browsing_util::kBinHashList, chunks); | |
| 1554 database_->UpdateFinished(true); | |
| 1555 | |
| 1556 GetListsInfo(&lists); | |
| 1557 ASSERT_EQ(4U, lists.size()); | |
| 1558 EXPECT_EQ(std::string(safe_browsing_util::kBinHashList), lists[3].name); | |
| 1559 EXPECT_EQ("1", lists[3].adds); | |
| 1560 EXPECT_TRUE(lists[3].subs.empty()); | |
| 1561 | |
| 1562 EXPECT_TRUE(database_->ContainsDownloadHashPrefix(kPrefix1)); | |
| 1563 EXPECT_TRUE(database_->ContainsDownloadHashPrefix(kPrefix2)); | |
| 1564 EXPECT_FALSE(database_->ContainsDownloadHashPrefix(kPrefix3)); | |
| 1565 database_.reset(); | |
| 1566 } | |
| 1567 | |
| 1568 // Test that an empty update doesn't actually update the database. | 1527 // Test that an empty update doesn't actually update the database. |
| 1569 // This isn't a functionality requirement, but it is a useful | 1528 // This isn't a functionality requirement, but it is a useful |
| 1570 // optimization. | 1529 // optimization. |
| 1571 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { | 1530 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { |
| 1572 SBChunkList chunks; | 1531 SBChunkList chunks; |
| 1573 SBChunk chunk; | 1532 SBChunk chunk; |
| 1574 | 1533 |
| 1575 FilePath filename = database_->BrowseDBFilename(database_filename_); | 1534 FilePath filename = database_->BrowseDBFilename(database_filename_); |
| 1576 | 1535 |
| 1577 // Prime the database. | 1536 // Prime the database. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1619 | 1578 |
| 1620 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not | 1579 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not |
| 1621 // update the database file. | 1580 // update the database file. |
| 1622 ASSERT_TRUE(file_util::SetLastModifiedTime(filename, old_last_modified)); | 1581 ASSERT_TRUE(file_util::SetLastModifiedTime(filename, old_last_modified)); |
| 1623 ASSERT_TRUE(file_util::GetFileInfo(filename, &before_info)); | 1582 ASSERT_TRUE(file_util::GetFileInfo(filename, &before_info)); |
| 1624 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1583 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1625 database_->UpdateFinished(true); | 1584 database_->UpdateFinished(true); |
| 1626 ASSERT_TRUE(file_util::GetFileInfo(filename, &after_info)); | 1585 ASSERT_TRUE(file_util::GetFileInfo(filename, &after_info)); |
| 1627 EXPECT_EQ(before_info.last_modified, after_info.last_modified); | 1586 EXPECT_EQ(before_info.last_modified, after_info.last_modified); |
| 1628 } | 1587 } |
| OLD | NEW |