| 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" |
| 8 |
| 7 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 11 #include "base/sha1.h" | 13 #include "base/sha1.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | |
| 14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 15 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
| 17 #include "net/base/net_util.h" | 18 #include "net/base/net_util.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" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 using base::Time; | 25 using base::Time; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 SBPrefix SBPrefixForString(const std::string& str) { | 29 SBPrefix SBPrefixForString(const std::string& str) { |
| 29 return SBFullHashForString(str).prefix; | 30 return SBFullHashForString(str).prefix; |
| 30 } | 31 } |
| 31 | 32 |
| 33 SBFullHash SBFullHashForPrefixAndSuffix(SBPrefix prefix, std::string suffix) { |
| 34 SBFullHash full_hash; |
| 35 memset(&full_hash, 0, sizeof(SBFullHash)); |
| 36 full_hash.prefix = prefix; |
| 37 CHECK_LE(suffix.size() + sizeof(SBPrefix), sizeof(SBFullHash)); |
| 38 memcpy(full_hash.full_hash + sizeof(SBPrefix), suffix.data(), suffix.size()); |
| 39 return full_hash; |
| 40 } |
| 41 |
| 32 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { | 42 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { |
| 33 net::IPAddressNumber ip_number; | 43 net::IPAddressNumber ip_number; |
| 34 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); | 44 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); |
| 35 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); | 45 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); |
| 36 const std::string hashed_ip_prefix = base::SHA1HashString( | 46 const std::string hashed_ip_prefix = base::SHA1HashString( |
| 37 net::IPAddressToPackedString(ip_number)); | 47 net::IPAddressToPackedString(ip_number)); |
| 38 std::string hash(crypto::kSHA256Length, '\0'); | 48 std::string hash(crypto::kSHA256Length, '\0'); |
| 39 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); | 49 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); |
| 40 hash[base::kSHA1Length] = static_cast<char>(prefix_size); | 50 hash[base::kSHA1Length] = static_cast<char>(prefix_size); |
| 41 return hash; | 51 return hash; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 61 // one url for prefix. | 71 // one url for prefix. |
| 62 void InsertAddChunkHostPrefixUrl(SBChunk* chunk, | 72 void InsertAddChunkHostPrefixUrl(SBChunk* chunk, |
| 63 int chunk_number, | 73 int chunk_number, |
| 64 const std::string& host_name, | 74 const std::string& host_name, |
| 65 const std::string& url) { | 75 const std::string& url) { |
| 66 InsertAddChunkHostPrefixValue(chunk, chunk_number, | 76 InsertAddChunkHostPrefixValue(chunk, chunk_number, |
| 67 SBPrefixForString(host_name), | 77 SBPrefixForString(host_name), |
| 68 SBPrefixForString(url)); | 78 SBPrefixForString(url)); |
| 69 } | 79 } |
| 70 | 80 |
| 81 // Same as InsertAddChunkHostFullHashes, but with pre-computed |
| 82 // prefix and fullhash values. |
| 83 void InsertAddChunkHostFullHashValue(SBChunk* chunk, |
| 84 int chunk_number, |
| 85 const SBPrefix& host_prefix, |
| 86 const SBFullHash& url_fullhash) { |
| 87 chunk->chunk_number = chunk_number; |
| 88 chunk->is_add = true; |
| 89 SBChunkHost host; |
| 90 host.host = host_prefix; |
| 91 host.entry = SBEntry::Create(SBEntry::ADD_FULL_HASH, 1); |
| 92 host.entry->set_chunk_id(chunk->chunk_number); |
| 93 host.entry->SetFullHashAt(0, url_fullhash); |
| 94 chunk->hosts.push_back(host); |
| 95 } |
| 96 |
| 71 // Same as InsertAddChunkHostPrefixUrl, but with full hashes. | 97 // Same as InsertAddChunkHostPrefixUrl, but with full hashes. |
| 72 void InsertAddChunkHostFullHashes(SBChunk* chunk, | 98 void InsertAddChunkHostFullHashes(SBChunk* chunk, |
| 73 int chunk_number, | 99 int chunk_number, |
| 74 const std::string& host_name, | 100 const std::string& host_name, |
| 75 const std::string& url) { | 101 const std::string& url) { |
| 76 chunk->chunk_number = chunk_number; | 102 InsertAddChunkHostFullHashValue(chunk, |
| 77 chunk->is_add = true; | 103 chunk_number, |
| 78 SBChunkHost host; | 104 SBPrefixForString(host_name), |
| 79 host.host = SBPrefixForString(host_name); | 105 SBFullHashForString(url)); |
| 80 host.entry = SBEntry::Create(SBEntry::ADD_FULL_HASH, 1); | |
| 81 host.entry->set_chunk_id(chunk->chunk_number); | |
| 82 host.entry->SetFullHashAt(0, SBFullHashForString(url)); | |
| 83 chunk->hosts.push_back(host); | |
| 84 } | 106 } |
| 85 | 107 |
| 86 void InsertAddChunkFullHash(SBChunk* chunk, | 108 void InsertAddChunkFullHash(SBChunk* chunk, |
| 87 int chunk_number, | 109 int chunk_number, |
| 88 const std::string& ip_str, | 110 const std::string& ip_str, |
| 89 size_t prefix_size) { | 111 size_t prefix_size) { |
| 90 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); | 112 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); |
| 91 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); | 113 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); |
| 92 SBFullHash full_hash; | 114 SBFullHash full_hash; |
| 93 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); | 115 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 300 } |
| 279 | 301 |
| 280 void AddDelChunk(const std::string& list, int chunk_id) { | 302 void AddDelChunk(const std::string& list, int chunk_id) { |
| 281 DelChunk(list, chunk_id, false); | 303 DelChunk(list, chunk_id, false); |
| 282 } | 304 } |
| 283 | 305 |
| 284 void SubDelChunk(const std::string& list, int chunk_id) { | 306 void SubDelChunk(const std::string& list, int chunk_id) { |
| 285 DelChunk(list, chunk_id, true); | 307 DelChunk(list, chunk_id, true); |
| 286 } | 308 } |
| 287 | 309 |
| 310 bool ContainsBrowseUrlHashes(std::vector<SBFullHash>* full_hashes, |
| 311 std::vector<SBPrefix>* prefix_hits, |
| 312 std::vector<SBFullHashResult>* cache_hits) { |
| 313 std::sort(full_hashes->begin(), full_hashes->end(), SBFullHashLess); |
| 314 return database_->ContainsBrowseUrlHashes( |
| 315 *full_hashes, prefix_hits, cache_hits); |
| 316 } |
| 317 |
| 288 // Utility function for setting up the database for the caching test. | 318 // Utility function for setting up the database for the caching test. |
| 289 void PopulateDatabaseForCacheTest(); | 319 void PopulateDatabaseForCacheTest(); |
| 290 | 320 |
| 291 scoped_ptr<SafeBrowsingDatabaseNew> database_; | 321 scoped_ptr<SafeBrowsingDatabaseNew> database_; |
| 292 base::FilePath database_filename_; | 322 base::FilePath database_filename_; |
| 293 base::ScopedTempDir temp_dir_; | 323 base::ScopedTempDir temp_dir_; |
| 294 }; | 324 }; |
| 295 | 325 |
| 296 // Tests retrieving list name information. | 326 // Tests retrieving list name information. |
| 297 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowse) { | 327 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowse) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 chunks.push_back(chunk); | 551 chunks.push_back(chunk); |
| 522 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 552 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 523 database_->UpdateFinished(true); | 553 database_->UpdateFinished(true); |
| 524 | 554 |
| 525 // Make sure they were added correctly. | 555 // Make sure they were added correctly. |
| 526 GetListsInfo(&lists); | 556 GetListsInfo(&lists); |
| 527 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 557 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 528 EXPECT_EQ(lists[0].adds, "1-3"); | 558 EXPECT_EQ(lists[0].adds, "1-3"); |
| 529 EXPECT_TRUE(lists[0].subs.empty()); | 559 EXPECT_TRUE(lists[0].subs.empty()); |
| 530 | 560 |
| 531 const Time now = Time::Now(); | 561 std::vector<SBFullHashResult> cache_hits; |
| 532 std::vector<SBFullHashResult> full_hashes; | |
| 533 std::vector<SBPrefix> prefix_hits; | 562 std::vector<SBPrefix> prefix_hits; |
| 534 std::string matching_list; | |
| 535 EXPECT_TRUE(database_->ContainsBrowseUrl( | 563 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 536 GURL("http://www.evil.com/phishing.html"), | 564 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 537 &matching_list, &prefix_hits, | |
| 538 &full_hashes, now)); | |
| 539 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); | 565 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); |
| 540 EXPECT_EQ(prefix_hits.size(), 1U); | 566 EXPECT_EQ(prefix_hits.size(), 1U); |
| 567 EXPECT_TRUE(cache_hits.empty()); |
| 541 | 568 |
| 542 EXPECT_TRUE(database_->ContainsBrowseUrl( | 569 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 543 GURL("http://www.evil.com/malware.html"), | 570 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 544 &matching_list, &prefix_hits, | |
| 545 &full_hashes, now)); | |
| 546 | 571 |
| 547 EXPECT_TRUE(database_->ContainsBrowseUrl( | 572 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 548 GURL("http://www.evil.com/notevil1.html"), | 573 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); |
| 549 &matching_list, &prefix_hits, | |
| 550 &full_hashes, now)); | |
| 551 | 574 |
| 552 EXPECT_TRUE(database_->ContainsBrowseUrl( | 575 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 553 GURL("http://www.evil.com/notevil2.html"), | 576 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 554 &matching_list, &prefix_hits, | |
| 555 &full_hashes, now)); | |
| 556 | 577 |
| 557 EXPECT_TRUE(database_->ContainsBrowseUrl( | 578 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 558 GURL("http://www.good.com/good1.html"), | 579 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 559 &matching_list, &prefix_hits, | |
| 560 &full_hashes, now)); | |
| 561 | 580 |
| 562 EXPECT_TRUE(database_->ContainsBrowseUrl( | 581 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 563 GURL("http://www.good.com/good2.html"), | 582 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 564 &matching_list, &prefix_hits, | |
| 565 &full_hashes, now)); | |
| 566 | 583 |
| 567 EXPECT_TRUE(database_->ContainsBrowseUrl( | 584 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 568 GURL("http://192.168.0.1/malware.html"), | 585 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits)); |
| 569 &matching_list, &prefix_hits, | |
| 570 &full_hashes, now)); | |
| 571 | 586 |
| 572 EXPECT_FALSE(database_->ContainsBrowseUrl( | 587 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 573 GURL("http://www.evil.com/"), | 588 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); |
| 574 &matching_list, &prefix_hits, | |
| 575 &full_hashes, now)); | |
| 576 EXPECT_TRUE(prefix_hits.empty()); | 589 EXPECT_TRUE(prefix_hits.empty()); |
| 590 EXPECT_TRUE(cache_hits.empty()); |
| 577 | 591 |
| 578 EXPECT_FALSE(database_->ContainsBrowseUrl( | 592 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 579 GURL("http://www.evil.com/robots.txt"), | 593 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits)); |
| 580 &matching_list, &prefix_hits, | |
| 581 &full_hashes, now)); | |
| 582 | 594 |
| 583 // Attempt to re-add the first chunk (should be a no-op). | 595 // Attempt to re-add the first chunk (should be a no-op). |
| 584 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 596 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 585 chunk.hosts.clear(); | 597 chunk.hosts.clear(); |
| 586 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", | 598 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", |
| 587 "www.evil.com/phishing.html", | 599 "www.evil.com/phishing.html", |
| 588 "www.evil.com/malware.html"); | 600 "www.evil.com/malware.html"); |
| 589 chunks.clear(); | 601 chunks.clear(); |
| 590 chunks.push_back(chunk); | 602 chunks.push_back(chunk); |
| 591 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 603 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 602 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", | 614 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", |
| 603 "www.evil.com/notevil1.html"); | 615 "www.evil.com/notevil1.html"); |
| 604 chunks.clear(); | 616 chunks.clear(); |
| 605 chunks.push_back(chunk); | 617 chunks.push_back(chunk); |
| 606 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 618 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 607 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 619 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 608 | 620 |
| 609 database_->UpdateFinished(true); | 621 database_->UpdateFinished(true); |
| 610 | 622 |
| 611 EXPECT_TRUE(database_->ContainsBrowseUrl( | 623 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 612 GURL("http://www.evil.com/phishing.html"), | 624 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 613 &matching_list, &prefix_hits, | |
| 614 &full_hashes, now)); | |
| 615 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); | 625 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); |
| 616 EXPECT_EQ(prefix_hits.size(), 1U); | 626 EXPECT_EQ(prefix_hits.size(), 1U); |
| 627 EXPECT_TRUE(cache_hits.empty()); |
| 617 | 628 |
| 618 EXPECT_FALSE(database_->ContainsBrowseUrl( | 629 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 619 GURL("http://www.evil.com/notevil1.html"), | 630 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); |
| 620 &matching_list, &prefix_hits, | |
| 621 &full_hashes, now)); | |
| 622 EXPECT_TRUE(prefix_hits.empty()); | 631 EXPECT_TRUE(prefix_hits.empty()); |
| 632 EXPECT_TRUE(cache_hits.empty()); |
| 623 | 633 |
| 624 EXPECT_TRUE(database_->ContainsBrowseUrl( | 634 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 625 GURL("http://www.evil.com/notevil2.html"), | 635 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 626 &matching_list, &prefix_hits, | |
| 627 &full_hashes, now)); | |
| 628 | 636 |
| 629 EXPECT_TRUE(database_->ContainsBrowseUrl( | 637 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 630 GURL("http://www.good.com/good1.html"), | 638 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 631 &matching_list, &prefix_hits, | |
| 632 &full_hashes, now)); | |
| 633 | 639 |
| 634 EXPECT_TRUE(database_->ContainsBrowseUrl( | 640 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 635 GURL("http://www.good.com/good2.html"), | 641 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 636 &matching_list, &prefix_hits, | |
| 637 &full_hashes, now)); | |
| 638 | 642 |
| 639 GetListsInfo(&lists); | 643 GetListsInfo(&lists); |
| 640 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 644 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 641 EXPECT_EQ(lists[0].subs, "4"); | 645 EXPECT_EQ(lists[0].subs, "4"); |
| 642 | 646 |
| 643 // Test the same sub chunk again. This should be a no-op. | 647 // Test the same sub chunk again. This should be a no-op. |
| 644 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 648 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 645 chunk.hosts.clear(); | 649 chunk.hosts.clear(); |
| 646 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", | 650 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", |
| 647 "www.evil.com/notevil1.html"); | 651 "www.evil.com/notevil1.html"); |
| 648 chunks.clear(); | 652 chunks.clear(); |
| 649 chunks.push_back(chunk); | 653 chunks.push_back(chunk); |
| 650 | 654 |
| 651 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 655 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 652 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 656 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 653 database_->UpdateFinished(true); | 657 database_->UpdateFinished(true); |
| 654 | 658 |
| 655 GetListsInfo(&lists); | 659 GetListsInfo(&lists); |
| 656 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 660 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 657 EXPECT_EQ(lists[0].subs, "4"); | 661 EXPECT_EQ(lists[0].subs, "4"); |
| 658 | 662 |
| 659 // Test removing all the prefixes from an add chunk. | 663 // Test removing all the prefixes from an add chunk. |
| 660 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 664 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 661 AddDelChunk(safe_browsing_util::kMalwareList, 2); | 665 AddDelChunk(safe_browsing_util::kMalwareList, 2); |
| 662 database_->UpdateFinished(true); | 666 database_->UpdateFinished(true); |
| 663 | 667 |
| 664 EXPECT_FALSE(database_->ContainsBrowseUrl( | 668 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 665 GURL("http://www.evil.com/notevil2.html"), | 669 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 666 &matching_list, &prefix_hits, | |
| 667 &full_hashes, now)); | |
| 668 | 670 |
| 669 EXPECT_FALSE(database_->ContainsBrowseUrl( | 671 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 670 GURL("http://www.good.com/good1.html"), | 672 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 671 &matching_list, &prefix_hits, | |
| 672 &full_hashes, now)); | |
| 673 | 673 |
| 674 EXPECT_FALSE(database_->ContainsBrowseUrl( | 674 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 675 GURL("http://www.good.com/good2.html"), | 675 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 676 &matching_list, &prefix_hits, | |
| 677 &full_hashes, now)); | |
| 678 | 676 |
| 679 GetListsInfo(&lists); | 677 GetListsInfo(&lists); |
| 680 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 678 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 681 EXPECT_EQ(lists[0].adds, "1,3"); | 679 EXPECT_EQ(lists[0].adds, "1,3"); |
| 682 EXPECT_EQ(lists[0].subs, "4"); | 680 EXPECT_EQ(lists[0].subs, "4"); |
| 683 | 681 |
| 684 // The adddel command exposed a bug in the transaction code where any | 682 // The adddel command exposed a bug in the transaction code where any |
| 685 // transaction after it would fail. Add a dummy entry and remove it to | 683 // transaction after it would fail. Add a dummy entry and remove it to |
| 686 // make sure the transcation works fine. | 684 // make sure the transcation works fine. |
| 687 chunk.hosts.clear(); | 685 chunk.hosts.clear(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 712 "www.notevilanymore.com/index.html", | 710 "www.notevilanymore.com/index.html", |
| 713 "www.notevilanymore.com/good.html"); | 711 "www.notevilanymore.com/good.html"); |
| 714 chunks.clear(); | 712 chunks.clear(); |
| 715 chunks.push_back(chunk); | 713 chunks.push_back(chunk); |
| 716 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 714 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 717 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 715 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 718 database_->UpdateFinished(true); | 716 database_->UpdateFinished(true); |
| 719 | 717 |
| 720 EXPECT_FALSE(database_->ContainsBrowseUrl( | 718 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 721 GURL("http://www.notevilanymore.com/index.html"), | 719 GURL("http://www.notevilanymore.com/index.html"), |
| 722 &matching_list, &prefix_hits, &full_hashes, now)); | 720 &prefix_hits, |
| 721 &cache_hits)); |
| 723 | 722 |
| 724 // Now insert the tardy add chunk and we don't expect them to appear | 723 // Now insert the tardy add chunk and we don't expect them to appear |
| 725 // in database because of the previous sub chunk. | 724 // in database because of the previous sub chunk. |
| 726 chunk.hosts.clear(); | 725 chunk.hosts.clear(); |
| 727 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", | 726 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", |
| 728 "www.notevilanymore.com/index.html", | 727 "www.notevilanymore.com/index.html", |
| 729 "www.notevilanymore.com/good.html"); | 728 "www.notevilanymore.com/good.html"); |
| 730 chunks.clear(); | 729 chunks.clear(); |
| 731 chunks.push_back(chunk); | 730 chunks.push_back(chunk); |
| 732 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 731 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 733 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 732 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 734 database_->UpdateFinished(true); | 733 database_->UpdateFinished(true); |
| 735 | 734 |
| 736 EXPECT_FALSE(database_->ContainsBrowseUrl( | 735 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 737 GURL("http://www.notevilanymore.com/index.html"), | 736 GURL("http://www.notevilanymore.com/index.html"), |
| 738 &matching_list, &prefix_hits, &full_hashes, now)); | 737 &prefix_hits, |
| 738 &cache_hits)); |
| 739 | 739 |
| 740 EXPECT_FALSE(database_->ContainsBrowseUrl( | 740 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 741 GURL("http://www.notevilanymore.com/good.html"), | 741 GURL("http://www.notevilanymore.com/good.html"), |
| 742 &matching_list, &prefix_hits, &full_hashes, now)); | 742 &prefix_hits, |
| 743 &cache_hits)); |
| 743 } | 744 } |
| 744 | 745 |
| 745 | 746 |
| 746 // Test adding zero length chunks to the database. | 747 // Test adding zero length chunks to the database. |
| 747 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { | 748 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { |
| 748 SBChunkList chunks; | 749 SBChunkList chunks; |
| 749 SBChunk chunk; | 750 SBChunk chunk; |
| 750 | 751 |
| 751 // Populate with a couple of normal chunks. | 752 // Populate with a couple of normal chunks. |
| 752 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/", | 753 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/", |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 | 804 |
| 804 empty_chunk.hosts.clear(); | 805 empty_chunk.hosts.clear(); |
| 805 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/", | 806 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/", |
| 806 "www.notempty.com/full2.html"); | 807 "www.notempty.com/full2.html"); |
| 807 chunks.push_back(empty_chunk); | 808 chunks.push_back(empty_chunk); |
| 808 | 809 |
| 809 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 810 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 810 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 811 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 811 database_->UpdateFinished(true); | 812 database_->UpdateFinished(true); |
| 812 | 813 |
| 813 const Time now = Time::Now(); | 814 std::vector<SBFullHashResult> cache_hits; |
| 814 std::vector<SBFullHashResult> full_hashes; | |
| 815 std::vector<SBPrefix> prefix_hits; | 815 std::vector<SBPrefix> prefix_hits; |
| 816 std::string matching_list; | |
| 817 EXPECT_TRUE(database_->ContainsBrowseUrl( | 816 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 818 GURL("http://www.notempty.com/full1.html"), | 817 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits)); |
| 819 &matching_list, &prefix_hits, | |
| 820 &full_hashes, now)); | |
| 821 EXPECT_TRUE(database_->ContainsBrowseUrl( | 818 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 822 GURL("http://www.notempty.com/full2.html"), | 819 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits)); |
| 823 &matching_list, &prefix_hits, | |
| 824 &full_hashes, now)); | |
| 825 | 820 |
| 826 GetListsInfo(&lists); | 821 GetListsInfo(&lists); |
| 827 EXPECT_EQ(lists[0].adds, "1,10,19-22"); | 822 EXPECT_EQ(lists[0].adds, "1,10,19-22"); |
| 828 EXPECT_EQ(lists[0].subs, "7"); | 823 EXPECT_EQ(lists[0].subs, "7"); |
| 829 | 824 |
| 830 // Handle AddDel and SubDel commands for empty chunks. | 825 // Handle AddDel and SubDel commands for empty chunks. |
| 831 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 826 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 832 AddDelChunk(safe_browsing_util::kMalwareList, 21); | 827 AddDelChunk(safe_browsing_util::kMalwareList, 21); |
| 833 database_->UpdateFinished(true); | 828 database_->UpdateFinished(true); |
| 834 | 829 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 857 | 852 |
| 858 std::vector<SBListChunkRanges> lists; | 853 std::vector<SBListChunkRanges> lists; |
| 859 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 854 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 860 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 855 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 861 database_->UpdateFinished(true); | 856 database_->UpdateFinished(true); |
| 862 | 857 |
| 863 // Add the GetHash results to the cache. | 858 // Add the GetHash results to the cache. |
| 864 SBFullHashResult full_hash; | 859 SBFullHashResult full_hash; |
| 865 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); | 860 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); |
| 866 full_hash.list_name = safe_browsing_util::kMalwareList; | 861 full_hash.list_name = safe_browsing_util::kMalwareList; |
| 867 full_hash.add_chunk_id = 1; | |
| 868 | 862 |
| 869 std::vector<SBFullHashResult> results; | 863 std::vector<SBFullHashResult> results; |
| 864 std::vector<SBPrefix> prefixes; |
| 870 results.push_back(full_hash); | 865 results.push_back(full_hash); |
| 866 prefixes.push_back(full_hash.hash.prefix); |
| 867 database_->CacheHashResults( |
| 868 prefixes, results, base::TimeDelta::FromMinutes(45)); |
| 871 | 869 |
| 872 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); | 870 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); |
| 871 results.clear(); |
| 872 prefixes.clear(); |
| 873 results.push_back(full_hash); | 873 results.push_back(full_hash); |
| 874 | 874 prefixes.push_back(full_hash.hash.prefix); |
| 875 std::vector<SBPrefix> prefixes; | 875 database_->CacheHashResults( |
| 876 database_->CacheHashResults(prefixes, results); | 876 prefixes, results, base::TimeDelta::FromMinutes(45)); |
| 877 } | 877 } |
| 878 | 878 |
| 879 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 879 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 880 PopulateDatabaseForCacheTest(); | 880 PopulateDatabaseForCacheTest(); |
| 881 | 881 |
| 882 // We should have both full hashes in the cache. | 882 // We should have both full hashes in the cache. |
| 883 EXPECT_EQ(database_->pending_browse_hashes_.size(), 2U); | 883 EXPECT_EQ(database_->browse_gethash_cache_.size(), 2U); |
| 884 | 884 |
| 885 // Test the cache lookup for the first prefix. | 885 // Test the cache lookup for the first prefix. |
| 886 std::string listname; | 886 std::string listname; |
| 887 std::vector<SBPrefix> prefixes; | 887 std::vector<SBPrefix> prefixes; |
| 888 std::vector<SBFullHashResult> full_hashes; | 888 std::vector<SBFullHashResult> cache_hits; |
| 889 database_->ContainsBrowseUrl( | 889 database_->ContainsBrowseUrl( |
| 890 GURL("http://www.evil.com/phishing.html"), | 890 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits); |
| 891 &listname, &prefixes, &full_hashes, Time::Now()); | 891 EXPECT_TRUE(prefixes.empty()); |
| 892 EXPECT_EQ(full_hashes.size(), 1U); | 892 EXPECT_EQ(cache_hits.size(), 1U); |
| 893 EXPECT_TRUE( | 893 EXPECT_TRUE(SBFullHashEqual( |
| 894 SBFullHashEqual(full_hashes[0].hash, | 894 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); |
| 895 SBFullHashForString("www.evil.com/phishing.html"))); | |
| 896 | 895 |
| 897 prefixes.clear(); | 896 prefixes.clear(); |
| 898 full_hashes.clear(); | 897 cache_hits.clear(); |
| 899 | 898 |
| 900 // Test the cache lookup for the second prefix. | 899 // Test the cache lookup for the second prefix. |
| 901 database_->ContainsBrowseUrl( | 900 database_->ContainsBrowseUrl( |
| 902 GURL("http://www.evil.com/malware.html"), | 901 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits); |
| 903 &listname, &prefixes, &full_hashes, Time::Now()); | 902 EXPECT_TRUE(prefixes.empty()); |
| 904 EXPECT_EQ(full_hashes.size(), 1U); | 903 EXPECT_EQ(cache_hits.size(), 1U); |
| 905 EXPECT_TRUE( | 904 EXPECT_TRUE(SBFullHashEqual( |
| 906 SBFullHashEqual(full_hashes[0].hash, | 905 cache_hits[0].hash, SBFullHashForString("www.evil.com/malware.html"))); |
| 907 SBFullHashForString("www.evil.com/malware.html"))); | |
| 908 | 906 |
| 909 prefixes.clear(); | 907 prefixes.clear(); |
| 910 full_hashes.clear(); | 908 cache_hits.clear(); |
| 911 | 909 |
| 912 // Test removing a prefix via a sub chunk. | 910 // Test removing a prefix via a sub chunk. |
| 913 SBChunk chunk; | 911 SBChunk chunk; |
| 914 SBChunkList chunks; | 912 SBChunkList chunks; |
| 915 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", | 913 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", |
| 916 "www.evil.com/phishing.html"); | 914 "www.evil.com/phishing.html"); |
| 917 chunks.push_back(chunk); | 915 chunks.push_back(chunk); |
| 918 | 916 |
| 919 std::vector<SBListChunkRanges> lists; | 917 std::vector<SBListChunkRanges> lists; |
| 920 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 918 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 921 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 919 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 922 database_->UpdateFinished(true); | 920 database_->UpdateFinished(true); |
| 923 | 921 |
| 924 // This prefix should still be there. | 922 // This prefix should still be there, but cached fullhash should be gone. |
| 925 database_->ContainsBrowseUrl( | 923 database_->ContainsBrowseUrl( |
| 926 GURL("http://www.evil.com/malware.html"), | 924 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits); |
| 927 &listname, &prefixes, &full_hashes, Time::Now()); | 925 EXPECT_TRUE(cache_hits.empty()); |
| 928 EXPECT_EQ(full_hashes.size(), 1U); | 926 EXPECT_EQ(prefixes.size(), 1U); |
| 929 EXPECT_TRUE( | 927 EXPECT_EQ(prefixes[0], SBPrefixForString("www.evil.com/malware.html")); |
| 930 SBFullHashEqual(full_hashes[0].hash, | |
| 931 SBFullHashForString("www.evil.com/malware.html"))); | |
| 932 prefixes.clear(); | 928 prefixes.clear(); |
| 933 full_hashes.clear(); | 929 cache_hits.clear(); |
| 934 | 930 |
| 935 // This prefix should be gone. | 931 // This prefix should be gone. |
| 936 database_->ContainsBrowseUrl( | 932 database_->ContainsBrowseUrl( |
| 937 GURL("http://www.evil.com/phishing.html"), | 933 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits); |
| 938 &listname, &prefixes, &full_hashes, Time::Now()); | 934 EXPECT_TRUE(prefixes.empty()); |
| 939 EXPECT_TRUE(full_hashes.empty()); | 935 EXPECT_TRUE(cache_hits.empty()); |
| 940 | 936 |
| 941 prefixes.clear(); | 937 prefixes.clear(); |
| 942 full_hashes.clear(); | 938 cache_hits.clear(); |
| 943 | 939 |
| 944 // Test that an AddDel for the original chunk removes the last cached entry. | 940 // Test that an AddDel for the original chunk removes the last entry. |
| 945 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 941 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 946 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 942 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 947 database_->UpdateFinished(true); | 943 database_->UpdateFinished(true); |
| 948 database_->ContainsBrowseUrl( | 944 database_->ContainsBrowseUrl( |
| 949 GURL("http://www.evil.com/malware.html"), | 945 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits); |
| 950 &listname, &prefixes, &full_hashes, Time::Now()); | 946 EXPECT_TRUE(cache_hits.empty()); |
| 951 EXPECT_TRUE(full_hashes.empty()); | |
| 952 EXPECT_TRUE(database_->full_browse_hashes_.empty()); | 947 EXPECT_TRUE(database_->full_browse_hashes_.empty()); |
| 953 EXPECT_TRUE(database_->pending_browse_hashes_.empty()); | 948 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); |
| 954 | 949 |
| 955 prefixes.clear(); | 950 prefixes.clear(); |
| 956 full_hashes.clear(); | 951 cache_hits.clear(); |
| 957 | 952 |
| 958 // Test that the cache won't return expired values. First we have to adjust | 953 // Test that the cache won't return expired values. First we have to adjust |
| 959 // the cached entries' received time to make them older, since the database | 954 // the cached entries' received time to make them older, since the database |
| 960 // cache insert uses Time::Now(). First, store some entries. | 955 // cache insert uses Time::Now(). First, store some entries. |
| 961 PopulateDatabaseForCacheTest(); | 956 PopulateDatabaseForCacheTest(); |
| 962 | 957 |
| 963 std::vector<SBAddFullHash>* hash_cache = &database_->pending_browse_hashes_; | 958 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache = |
| 959 &database_->browse_gethash_cache_; |
| 964 EXPECT_EQ(hash_cache->size(), 2U); | 960 EXPECT_EQ(hash_cache->size(), 2U); |
| 965 | 961 |
| 966 // Now adjust one of the entries times to be in the past. | 962 // Now adjust one of the entries times to be in the past. |
| 967 base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60); | |
| 968 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); | 963 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); |
| 969 std::vector<SBAddFullHash>::iterator iter; | 964 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter = |
| 970 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) { | 965 hash_cache->find(key); |
| 971 if (iter->full_hash.prefix == key) { | 966 ASSERT_TRUE(iter != hash_cache->end()); |
| 972 iter->received = static_cast<int32>(expired.ToTimeT()); | 967 iter->second.expire_after = |
| 973 break; | 968 base::Time::Now() - base::TimeDelta::FromMinutes(1); |
| 974 } | |
| 975 } | |
| 976 EXPECT_TRUE(iter != hash_cache->end()); | |
| 977 | 969 |
| 978 database_->ContainsBrowseUrl( | 970 database_->ContainsBrowseUrl( |
| 979 GURL("http://www.evil.com/malware.html"), | 971 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits); |
| 980 &listname, &prefixes, &full_hashes, expired); | 972 EXPECT_EQ(prefixes.size(), 1U); |
| 981 EXPECT_TRUE(full_hashes.empty()); | 973 EXPECT_TRUE(cache_hits.empty()); |
| 974 // Expired entry should have been removed from cache. |
| 975 EXPECT_EQ(hash_cache->size(), 1U); |
| 982 | 976 |
| 983 // This entry should still exist. | 977 // This entry should still exist. |
| 984 database_->ContainsBrowseUrl( | 978 database_->ContainsBrowseUrl( |
| 985 GURL("http://www.evil.com/phishing.html"), | 979 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits); |
| 986 &listname, &prefixes, &full_hashes, expired); | 980 EXPECT_TRUE(prefixes.empty()); |
| 987 EXPECT_EQ(full_hashes.size(), 1U); | 981 EXPECT_EQ(cache_hits.size(), 1U); |
| 988 | |
| 989 | 982 |
| 990 // Testing prefix miss caching. First, we clear out the existing database, | 983 // Testing prefix miss caching. First, we clear out the existing database, |
| 991 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate | 984 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate |
| 992 // chunks. | 985 // chunks. |
| 993 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 986 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 994 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 987 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 995 database_->UpdateFinished(true); | 988 database_->UpdateFinished(true); |
| 996 | 989 |
| 997 std::vector<SBPrefix> prefix_misses; | 990 std::vector<SBPrefix> prefix_misses; |
| 998 std::vector<SBFullHashResult> empty_full_hash; | 991 std::vector<SBFullHashResult> empty_full_hash; |
| 999 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); | 992 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); |
| 1000 prefix_misses.push_back( | 993 prefix_misses.push_back( |
| 1001 SBPrefixForString("http://www.bad.com/phishing.html")); | 994 SBPrefixForString("http://www.bad.com/phishing.html")); |
| 1002 database_->CacheHashResults(prefix_misses, empty_full_hash); | 995 database_->CacheHashResults( |
| 996 prefix_misses, empty_full_hash, base::TimeDelta::FromMinutes(45)); |
| 1003 | 997 |
| 1004 // Prefixes with no full results are misses. | 998 // Prefixes with no full results are misses. |
| 1005 EXPECT_EQ(database_->prefix_miss_cache_.size(), 2U); | 999 EXPECT_EQ(hash_cache->size(), 2U); |
| 1000 EXPECT_NE( |
| 1001 hash_cache->find(SBPrefixForString("http://www.bad.com/malware.html")), |
| 1002 hash_cache->end()); |
| 1003 EXPECT_TRUE( |
| 1004 hash_cache->find(SBPrefixForString("http://www.bad.com/malware.html")) |
| 1005 ->second.full_hashes.empty()); |
| 1006 EXPECT_NE( |
| 1007 hash_cache->find(SBPrefixForString("http://www.bad.com/phishing.html")), |
| 1008 hash_cache->end()); |
| 1009 EXPECT_TRUE( |
| 1010 hash_cache->find(SBPrefixForString("http://www.bad.com/phishing.html")) |
| 1011 ->second.full_hashes.empty()); |
| 1006 | 1012 |
| 1007 // Update the database. | 1013 // Update the database. |
| 1008 PopulateDatabaseForCacheTest(); | 1014 PopulateDatabaseForCacheTest(); |
| 1009 | 1015 |
| 1010 // Prefix miss cache should be cleared. | |
| 1011 EXPECT_TRUE(database_->prefix_miss_cache_.empty()); | |
| 1012 | |
| 1013 // Cache a GetHash miss for a particular prefix, and even though the prefix is | 1016 // Cache a GetHash miss for a particular prefix, and even though the prefix is |
| 1014 // in the database, it is flagged as a miss so looking up the associated URL | 1017 // in the database, it is flagged as a miss so looking up the associated URL |
| 1015 // will not succeed. | 1018 // will not succeed. |
| 1016 prefixes.clear(); | 1019 prefixes.clear(); |
| 1017 full_hashes.clear(); | 1020 cache_hits.clear(); |
| 1018 prefix_misses.clear(); | 1021 prefix_misses.clear(); |
| 1019 empty_full_hash.clear(); | 1022 empty_full_hash.clear(); |
| 1020 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); | 1023 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); |
| 1021 database_->CacheHashResults(prefix_misses, empty_full_hash); | 1024 database_->CacheHashResults( |
| 1025 prefix_misses, empty_full_hash, base::TimeDelta::FromMinutes(45)); |
| 1022 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1026 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1023 GURL("http://www.evil.com/phishing.html"), | 1027 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits)); |
| 1024 &listname, &prefixes, | 1028 EXPECT_TRUE(prefixes.empty()); |
| 1025 &full_hashes, Time::Now())); | 1029 EXPECT_TRUE(cache_hits.empty()); |
| 1026 | 1030 |
| 1027 prefixes.clear(); | 1031 prefixes.clear(); |
| 1028 full_hashes.clear(); | 1032 cache_hits.clear(); |
| 1029 | 1033 |
| 1030 // Test receiving a full add chunk. | 1034 // Test receiving a full add chunk. |
| 1031 chunk.hosts.clear(); | 1035 chunk.hosts.clear(); |
| 1032 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", | 1036 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", |
| 1033 "www.fullevil.com/bad1.html", | 1037 "www.fullevil.com/bad1.html", |
| 1034 "www.fullevil.com/bad2.html"); | 1038 "www.fullevil.com/bad2.html"); |
| 1035 chunks.clear(); | 1039 chunks.clear(); |
| 1036 chunks.push_back(chunk); | 1040 chunks.push_back(chunk); |
| 1037 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1041 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1038 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1042 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1039 database_->UpdateFinished(true); | 1043 database_->UpdateFinished(true); |
| 1040 | 1044 |
| 1041 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1045 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1042 GURL("http://www.fullevil.com/bad1.html"), | 1046 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits)); |
| 1043 &listname, &prefixes, &full_hashes, | 1047 EXPECT_EQ(prefixes.size(), 1U); |
| 1044 Time::Now())); | 1048 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad1.html")); |
| 1045 EXPECT_EQ(full_hashes.size(), 1U); | 1049 EXPECT_TRUE(cache_hits.empty()); |
| 1046 EXPECT_TRUE( | |
| 1047 SBFullHashEqual(full_hashes[0].hash, | |
| 1048 SBFullHashForString("www.fullevil.com/bad1.html"))); | |
| 1049 prefixes.clear(); | 1050 prefixes.clear(); |
| 1050 full_hashes.clear(); | 1051 cache_hits.clear(); |
| 1051 | 1052 |
| 1052 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1053 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1053 GURL("http://www.fullevil.com/bad2.html"), | 1054 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits)); |
| 1054 &listname, &prefixes, &full_hashes, | 1055 EXPECT_EQ(prefixes.size(), 1U); |
| 1055 Time::Now())); | 1056 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad2.html")); |
| 1056 EXPECT_EQ(full_hashes.size(), 1U); | 1057 EXPECT_TRUE(cache_hits.empty()); |
| 1057 EXPECT_TRUE( | |
| 1058 SBFullHashEqual(full_hashes[0].hash, | |
| 1059 SBFullHashForString("www.fullevil.com/bad2.html"))); | |
| 1060 prefixes.clear(); | 1058 prefixes.clear(); |
| 1061 full_hashes.clear(); | 1059 cache_hits.clear(); |
| 1062 | 1060 |
| 1063 // Test receiving a full sub chunk, which will remove one of the full adds. | 1061 // Test receiving a full sub chunk, which will remove one of the full adds. |
| 1064 chunk.hosts.clear(); | 1062 chunk.hosts.clear(); |
| 1065 InsertSubChunkHostFullHash(&chunk, 200, 20, | 1063 InsertSubChunkHostFullHash(&chunk, 200, 20, |
| 1066 "www.fullevil.com/", | 1064 "www.fullevil.com/", |
| 1067 "www.fullevil.com/bad1.html"); | 1065 "www.fullevil.com/bad1.html"); |
| 1068 chunks.clear(); | 1066 chunks.clear(); |
| 1069 chunks.push_back(chunk); | 1067 chunks.push_back(chunk); |
| 1070 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1068 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1071 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1069 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1072 database_->UpdateFinished(true); | 1070 database_->UpdateFinished(true); |
| 1073 | 1071 |
| 1074 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1072 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1075 GURL("http://www.fullevil.com/bad1.html"), | 1073 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits)); |
| 1076 &listname, &prefixes, &full_hashes, | 1074 EXPECT_TRUE(prefixes.empty()); |
| 1077 Time::Now())); | 1075 EXPECT_TRUE(cache_hits.empty()); |
| 1078 EXPECT_TRUE(full_hashes.empty()); | |
| 1079 | 1076 |
| 1080 // There should be one remaining full add. | 1077 // There should be one remaining full add. |
| 1081 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1078 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1082 GURL("http://www.fullevil.com/bad2.html"), | 1079 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits)); |
| 1083 &listname, &prefixes, &full_hashes, | 1080 EXPECT_EQ(prefixes.size(), 1U); |
| 1084 Time::Now())); | 1081 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad2.html")); |
| 1085 EXPECT_EQ(full_hashes.size(), 1U); | 1082 EXPECT_TRUE(cache_hits.empty()); |
| 1086 EXPECT_TRUE( | |
| 1087 SBFullHashEqual(full_hashes[0].hash, | |
| 1088 SBFullHashForString("www.fullevil.com/bad2.html"))); | |
| 1089 prefixes.clear(); | 1083 prefixes.clear(); |
| 1090 full_hashes.clear(); | 1084 cache_hits.clear(); |
| 1091 | 1085 |
| 1092 // Now test an AddDel for the remaining full add. | 1086 // Now test an AddDel for the remaining full add. |
| 1093 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1087 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1094 AddDelChunk(safe_browsing_util::kMalwareList, 20); | 1088 AddDelChunk(safe_browsing_util::kMalwareList, 20); |
| 1095 database_->UpdateFinished(true); | 1089 database_->UpdateFinished(true); |
| 1096 | 1090 |
| 1097 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1091 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1098 GURL("http://www.fullevil.com/bad1.html"), | 1092 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits)); |
| 1099 &listname, &prefixes, &full_hashes, | |
| 1100 Time::Now())); | |
| 1101 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1093 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1102 GURL("http://www.fullevil.com/bad2.html"), | 1094 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits)); |
| 1103 &listname, &prefixes, &full_hashes, | |
| 1104 Time::Now())); | |
| 1105 } | 1095 } |
| 1106 | 1096 |
| 1107 // Test that corrupt databases are appropriately handled, even if the | 1097 // Test that corrupt databases are appropriately handled, even if the |
| 1108 // corruption is detected in the midst of the update. | 1098 // corruption is detected in the midst of the update. |
| 1109 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. | 1099 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. |
| 1110 // http://crbug.com/56448 | 1100 // http://crbug.com/56448 |
| 1111 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { | 1101 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { |
| 1112 // Re-create the database in a captive message loop so that we can | 1102 // Re-create the database in a captive message loop so that we can |
| 1113 // influence task-posting. Database specifically needs to the | 1103 // influence task-posting. Database specifically needs to the |
| 1114 // file-backed. | 1104 // file-backed. |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1540 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1530 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1541 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); | 1531 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); |
| 1542 database_->UpdateFinished(true); | 1532 database_->UpdateFinished(true); |
| 1543 | 1533 |
| 1544 GetListsInfo(&lists); | 1534 GetListsInfo(&lists); |
| 1545 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); | 1535 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); |
| 1546 EXPECT_EQ("1", lists[0].adds); | 1536 EXPECT_EQ("1", lists[0].adds); |
| 1547 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); | 1537 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); |
| 1548 EXPECT_EQ("47", lists[1].adds); | 1538 EXPECT_EQ("47", lists[1].adds); |
| 1549 | 1539 |
| 1550 const Time now = Time::Now(); | |
| 1551 std::vector<SBPrefix> prefixes; | 1540 std::vector<SBPrefix> prefixes; |
| 1552 std::vector<SBFullHashResult> full_hashes; | 1541 std::vector<SBFullHashResult> cache_hits; |
| 1553 std::vector<SBPrefix> prefix_hits; | |
| 1554 std::string matching_list; | |
| 1555 std::string listname; | 1542 std::string listname; |
| 1556 | 1543 |
| 1557 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1544 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1558 GURL("http://www.evil.com/malware1.html"), | 1545 GURL("http://www.evil.com/malware1.html"), &prefixes, &cache_hits)); |
| 1559 &listname, &prefixes, &full_hashes, now)); | |
| 1560 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1546 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1561 GURL("http://www.evil.com/malware2.html"), | 1547 GURL("http://www.evil.com/malware2.html"), &prefixes, &cache_hits)); |
| 1562 &listname, &prefixes, &full_hashes, now)); | |
| 1563 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1548 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1564 GURL("http://www.evil.com/phishing1.html"), | 1549 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cache_hits)); |
| 1565 &listname, &prefixes, &full_hashes, now)); | |
| 1566 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1550 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1567 GURL("http://www.evil.com/phishing2.html"), | 1551 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cache_hits)); |
| 1568 &listname, &prefixes, &full_hashes, now)); | |
| 1569 | 1552 |
| 1570 // Test removing a single prefix from the add chunk. | 1553 // Test removing a single prefix from the add chunk. |
| 1571 // Remove the prefix that added first. | 1554 // Remove the prefix that added first. |
| 1572 chunk.hosts.clear(); | 1555 chunk.hosts.clear(); |
| 1573 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", | 1556 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", |
| 1574 "www.evil.com/malware1.html"); | 1557 "www.evil.com/malware1.html"); |
| 1575 chunks.clear(); | 1558 chunks.clear(); |
| 1576 chunks.push_back(chunk); | 1559 chunks.push_back(chunk); |
| 1577 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1560 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1578 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1561 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1579 database_->UpdateFinished(true); | 1562 database_->UpdateFinished(true); |
| 1580 | 1563 |
| 1581 // Remove the prefix that added last. | 1564 // Remove the prefix that added last. |
| 1582 chunk.hosts.clear(); | 1565 chunk.hosts.clear(); |
| 1583 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", | 1566 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", |
| 1584 "www.evil.com/phishing2.html"); | 1567 "www.evil.com/phishing2.html"); |
| 1585 chunks.clear(); | 1568 chunks.clear(); |
| 1586 chunks.push_back(chunk); | 1569 chunks.push_back(chunk); |
| 1587 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1570 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1588 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); | 1571 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); |
| 1589 database_->UpdateFinished(true); | 1572 database_->UpdateFinished(true); |
| 1590 | 1573 |
| 1591 // Verify that the database contains urls expected. | 1574 // Verify that the database contains urls expected. |
| 1592 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1575 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1593 GURL("http://www.evil.com/malware1.html"), | 1576 GURL("http://www.evil.com/malware1.html"), &prefixes, &cache_hits)); |
| 1594 &listname, &prefixes, &full_hashes, now)); | |
| 1595 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1577 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1596 GURL("http://www.evil.com/malware2.html"), | 1578 GURL("http://www.evil.com/malware2.html"), &prefixes, &cache_hits)); |
| 1597 &listname, &prefixes, &full_hashes, now)); | |
| 1598 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1579 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1599 GURL("http://www.evil.com/phishing1.html"), | 1580 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cache_hits)); |
| 1600 &listname, &prefixes, &full_hashes, now)); | |
| 1601 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1581 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1602 GURL("http://www.evil.com/phishing2.html"), | 1582 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cache_hits)); |
| 1603 &listname, &prefixes, &full_hashes, now)); | |
| 1604 } | 1583 } |
| 1605 | 1584 |
| 1606 // Test that an empty update doesn't actually update the database. | 1585 // Test that an empty update doesn't actually update the database. |
| 1607 // This isn't a functionality requirement, but it is a useful | 1586 // This isn't a functionality requirement, but it is a useful |
| 1608 // optimization. | 1587 // optimization. |
| 1609 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { | 1588 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { |
| 1610 SBChunkList chunks; | 1589 SBChunkList chunks; |
| 1611 SBChunk chunk; | 1590 SBChunk chunk; |
| 1612 | 1591 |
| 1613 base::FilePath filename = database_->BrowseDBFilename(database_filename_); | 1592 base::FilePath filename = database_->BrowseDBFilename(database_filename_); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1679 | 1658 |
| 1680 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", | 1659 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", |
| 1681 "www.evil.com/malware.html"); | 1660 "www.evil.com/malware.html"); |
| 1682 chunks.clear(); | 1661 chunks.clear(); |
| 1683 chunks.push_back(chunk); | 1662 chunks.push_back(chunk); |
| 1684 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1663 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1685 database_->UpdateFinished(true); | 1664 database_->UpdateFinished(true); |
| 1686 } | 1665 } |
| 1687 | 1666 |
| 1688 // Find the malware url in the database, don't find a good url. | 1667 // Find the malware url in the database, don't find a good url. |
| 1689 const Time now = Time::Now(); | 1668 std::vector<SBFullHashResult> cache_hits; |
| 1690 std::vector<SBFullHashResult> full_hashes; | |
| 1691 std::vector<SBPrefix> prefix_hits; | 1669 std::vector<SBPrefix> prefix_hits; |
| 1692 std::string matching_list; | |
| 1693 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1670 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1694 GURL("http://www.evil.com/malware.html"), | 1671 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1695 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1696 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1672 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1697 GURL("http://www.good.com/goodware.html"), | 1673 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1698 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1699 | 1674 |
| 1700 base::FilePath filter_file = database_->PrefixSetForFilename( | 1675 base::FilePath filter_file = database_->PrefixSetForFilename( |
| 1701 database_->BrowseDBFilename(database_filename_)); | 1676 database_->BrowseDBFilename(database_filename_)); |
| 1702 | 1677 |
| 1703 // After re-creating the database, it should have a filter read from | 1678 // After re-creating the database, it should have a filter read from |
| 1704 // a file, so it should find the same results. | 1679 // a file, so it should find the same results. |
| 1705 ASSERT_TRUE(base::PathExists(filter_file)); | 1680 ASSERT_TRUE(base::PathExists(filter_file)); |
| 1706 database_.reset(new SafeBrowsingDatabaseNew); | 1681 database_.reset(new SafeBrowsingDatabaseNew); |
| 1707 database_->Init(database_filename_); | 1682 database_->Init(database_filename_); |
| 1708 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1683 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1709 GURL("http://www.evil.com/malware.html"), | 1684 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1710 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1711 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1685 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1712 GURL("http://www.good.com/goodware.html"), | 1686 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1713 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1714 | 1687 |
| 1715 // If there is no filter file, the database cannot find malware urls. | 1688 // If there is no filter file, the database cannot find malware urls. |
| 1716 base::DeleteFile(filter_file, false); | 1689 base::DeleteFile(filter_file, false); |
| 1717 ASSERT_FALSE(base::PathExists(filter_file)); | 1690 ASSERT_FALSE(base::PathExists(filter_file)); |
| 1718 database_.reset(new SafeBrowsingDatabaseNew); | 1691 database_.reset(new SafeBrowsingDatabaseNew); |
| 1719 database_->Init(database_filename_); | 1692 database_->Init(database_filename_); |
| 1720 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1693 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1721 GURL("http://www.evil.com/malware.html"), | 1694 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1722 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1723 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1695 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1724 GURL("http://www.good.com/goodware.html"), | 1696 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1725 &matching_list, &prefix_hits, &full_hashes, now)); | |
| 1726 } | 1697 } |
| 1727 | 1698 |
| 1699 TEST_F(SafeBrowsingDatabaseTest, TestCachedFullMiss) { |
| 1700 std::vector<SBListChunkRanges> lists; |
| 1701 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1702 |
| 1703 SBChunkList chunks; |
| 1704 { |
| 1705 // Insert prefix 1001 into database. |
| 1706 SBChunk chunk; |
| 1707 InsertAddChunkHostPrefixValue(&chunk, 1, SBPrefix(100), SBPrefix(1001)); |
| 1708 chunks.push_back(chunk); |
| 1709 } |
| 1710 { |
| 1711 // Insert prefix 1002 into database. |
| 1712 SBChunk chunk; |
| 1713 InsertAddChunkHostPrefixValue(&chunk, 2, SBPrefix(101), SBPrefix(1002)); |
| 1714 chunks.push_back(chunk); |
| 1715 } |
| 1716 |
| 1717 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1718 database_->UpdateFinished(true); |
| 1719 |
| 1720 { |
| 1721 // Cache a full miss result for 1001. |
| 1722 std::vector<SBPrefix> prefixes; |
| 1723 prefixes.push_back(SBPrefix(1001)); |
| 1724 std::vector<SBFullHashResult> cache_results; |
| 1725 database_->CacheHashResults( |
| 1726 prefixes, cache_results, base::TimeDelta::FromMinutes(60)); |
| 1727 } |
| 1728 |
| 1729 { |
| 1730 // Check if DB contains (prefix 1001, suffix 01). Should return false, since |
| 1731 // we have a cached miss for that prefix. |
| 1732 std::vector<SBFullHash> full_hashes; |
| 1733 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 1734 std::vector<SBPrefix> prefix_hits; |
| 1735 std::vector<SBFullHashResult> cache_hits; |
| 1736 EXPECT_FALSE( |
| 1737 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1738 EXPECT_TRUE(prefix_hits.empty()); |
| 1739 EXPECT_TRUE(cache_hits.empty()); |
| 1740 |
| 1741 // Add (1002,01) to search. Should return a prefix hit for 1002 only. |
| 1742 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01")); |
| 1743 prefix_hits.clear(); |
| 1744 cache_hits.clear(); |
| 1745 EXPECT_TRUE( |
| 1746 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1747 ASSERT_EQ(1U, prefix_hits.size()); |
| 1748 EXPECT_EQ(1002U, prefix_hits[0]); |
| 1749 EXPECT_TRUE(cache_hits.empty()); |
| 1750 } |
| 1751 } |
| 1752 |
| 1753 TEST_F(SafeBrowsingDatabaseTest, TestCachedPrefixHitFullMiss) { |
| 1754 std::vector<SBListChunkRanges> lists; |
| 1755 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1756 |
| 1757 SBChunkList chunks; |
| 1758 { |
| 1759 // Insert prefix 1001 into database. |
| 1760 SBChunk chunk; |
| 1761 InsertAddChunkHostPrefixValue(&chunk, 1, SBPrefix(100), SBPrefix(1001)); |
| 1762 chunks.push_back(chunk); |
| 1763 } |
| 1764 { |
| 1765 // Insert prefix 1002 into database. |
| 1766 SBChunk chunk; |
| 1767 InsertAddChunkHostPrefixValue(&chunk, 2, SBPrefix(101), SBPrefix(1002)); |
| 1768 chunks.push_back(chunk); |
| 1769 } |
| 1770 |
| 1771 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1772 database_->UpdateFinished(true); |
| 1773 |
| 1774 { |
| 1775 // Check if DB contains (prefix 1001, suffix 01). Should return a prefix |
| 1776 // hit. |
| 1777 std::vector<SBFullHash> full_hashes; |
| 1778 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 1779 std::vector<SBPrefix> prefix_hits; |
| 1780 std::vector<SBFullHashResult> cache_hits; |
| 1781 EXPECT_TRUE( |
| 1782 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1783 ASSERT_EQ(1U, prefix_hits.size()); |
| 1784 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1785 EXPECT_TRUE(cache_hits.empty()); |
| 1786 |
| 1787 // Add (1002,01) to search. Should return a prefix hit for both 1001 and |
| 1788 // 1002. |
| 1789 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01")); |
| 1790 prefix_hits.clear(); |
| 1791 cache_hits.clear(); |
| 1792 EXPECT_TRUE( |
| 1793 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1794 ASSERT_EQ(2U, prefix_hits.size()); |
| 1795 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1796 EXPECT_EQ(1002U, prefix_hits[1]); |
| 1797 EXPECT_TRUE(cache_hits.empty()); |
| 1798 |
| 1799 // Add (1003,01) to search. Should still return a prefix hit for both 1001 |
| 1800 // and 1002. |
| 1801 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1003, "\x01")); |
| 1802 prefix_hits.clear(); |
| 1803 cache_hits.clear(); |
| 1804 EXPECT_TRUE( |
| 1805 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1806 ASSERT_EQ(2U, prefix_hits.size()); |
| 1807 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1808 EXPECT_EQ(1002U, prefix_hits[1]); |
| 1809 EXPECT_TRUE(cache_hits.empty()); |
| 1810 } |
| 1811 |
| 1812 { |
| 1813 // Cache a fullhash result for (1001,01) and (1001,03). |
| 1814 std::vector<SBPrefix> prefixes; |
| 1815 prefixes.push_back(SBPrefix(1001)); |
| 1816 std::vector<SBFullHashResult> cache_results; |
| 1817 |
| 1818 SBFullHashResult full_hash_result; |
| 1819 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x01"); |
| 1820 full_hash_result.list_name = safe_browsing_util::kMalwareList; |
| 1821 cache_results.push_back(full_hash_result); |
| 1822 |
| 1823 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x03"); |
| 1824 cache_results.push_back(full_hash_result); |
| 1825 |
| 1826 database_->CacheHashResults( |
| 1827 prefixes, cache_results, base::TimeDelta::FromMinutes(60)); |
| 1828 } |
| 1829 |
| 1830 { |
| 1831 // Check again if DB contains (prefix 1001, suffix 01). Should return a |
| 1832 // cache hit now. |
| 1833 std::vector<SBFullHash> full_hashes; |
| 1834 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 1835 std::vector<SBPrefix> prefix_hits; |
| 1836 std::vector<SBFullHashResult> cache_hits; |
| 1837 EXPECT_TRUE( |
| 1838 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1839 EXPECT_TRUE(prefix_hits.empty()); |
| 1840 ASSERT_EQ(1U, cache_hits.size()); |
| 1841 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"), |
| 1842 cache_hits[0].hash)); |
| 1843 |
| 1844 // Add (1002,01) to search. Should return a cache hit for 1001 and prefix |
| 1845 // hit for 1002. |
| 1846 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01")); |
| 1847 prefix_hits.clear(); |
| 1848 cache_hits.clear(); |
| 1849 EXPECT_TRUE( |
| 1850 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1851 ASSERT_EQ(1U, prefix_hits.size()); |
| 1852 EXPECT_EQ(1002U, prefix_hits[0]); |
| 1853 ASSERT_EQ(1U, cache_hits.size()); |
| 1854 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"), |
| 1855 cache_hits[0].hash)); |
| 1856 |
| 1857 // Add (1001,03) to search. Should return 2 cache hits for 1001 and a prefix |
| 1858 // hit for 1002. |
| 1859 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03")); |
| 1860 prefix_hits.clear(); |
| 1861 cache_hits.clear(); |
| 1862 EXPECT_TRUE( |
| 1863 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1864 ASSERT_EQ(1U, prefix_hits.size()); |
| 1865 EXPECT_EQ(1002U, prefix_hits[0]); |
| 1866 ASSERT_EQ(2U, cache_hits.size()); |
| 1867 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"), |
| 1868 cache_hits[0].hash)); |
| 1869 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x03"), |
| 1870 cache_hits[1].hash)); |
| 1871 } |
| 1872 |
| 1873 { |
| 1874 // Check if DB contains only (prefix 1001, suffix 03). Should return a cache |
| 1875 // hit. |
| 1876 std::vector<SBFullHash> full_hashes; |
| 1877 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03")); |
| 1878 std::vector<SBPrefix> prefix_hits; |
| 1879 std::vector<SBFullHashResult> cache_hits; |
| 1880 EXPECT_TRUE( |
| 1881 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1882 EXPECT_TRUE(prefix_hits.empty()); |
| 1883 ASSERT_EQ(1U, cache_hits.size()); |
| 1884 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x03"), |
| 1885 cache_hits[0].hash)); |
| 1886 } |
| 1887 |
| 1888 { |
| 1889 // Check if DB contains (prefix 1001, suffix 02). Should return false, since |
| 1890 // we have a cached fullhash for that prefix but which does not match the |
| 1891 // fullhash. |
| 1892 std::vector<SBFullHash> full_hashes; |
| 1893 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02")); |
| 1894 std::vector<SBPrefix> prefix_hits; |
| 1895 std::vector<SBFullHashResult> cache_hits; |
| 1896 EXPECT_FALSE( |
| 1897 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1898 EXPECT_TRUE(prefix_hits.empty()); |
| 1899 EXPECT_TRUE(cache_hits.empty()); |
| 1900 |
| 1901 // Add (1002,01) to search. Should return a prefix hit for 1002. |
| 1902 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01")); |
| 1903 prefix_hits.clear(); |
| 1904 cache_hits.clear(); |
| 1905 EXPECT_TRUE( |
| 1906 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1907 ASSERT_EQ(1U, prefix_hits.size()); |
| 1908 EXPECT_EQ(1002U, prefix_hits[0]); |
| 1909 EXPECT_TRUE(cache_hits.empty()); |
| 1910 } |
| 1911 } |
| 1912 |
| 1913 TEST_F(SafeBrowsingDatabaseTest, TestBrowseFullHashMatching) { |
| 1914 std::vector<SBListChunkRanges> lists; |
| 1915 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1916 |
| 1917 SBChunkList chunks; |
| 1918 { |
| 1919 // Insert fullhash (1001,1) into database. |
| 1920 SBChunk chunk; |
| 1921 InsertAddChunkHostFullHashValue( |
| 1922 &chunk, 1, SBPrefix(100), SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 1923 chunks.push_back(chunk); |
| 1924 } |
| 1925 { |
| 1926 // Insert fullhash (1001,2) into database. |
| 1927 SBChunk chunk; |
| 1928 InsertAddChunkHostFullHashValue( |
| 1929 &chunk, 2, SBPrefix(100), SBFullHashForPrefixAndSuffix(1001, "\x02")); |
| 1930 chunks.push_back(chunk); |
| 1931 } |
| 1932 |
| 1933 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1934 database_->UpdateFinished(true); |
| 1935 |
| 1936 { |
| 1937 // Check if DB contains (prefix 1001, suffix 03). Should return false, |
| 1938 // since the DB contains a matching prefix but the full hashes do not match. |
| 1939 std::vector<SBFullHash> full_hashes; |
| 1940 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03")); |
| 1941 std::vector<SBPrefix> prefix_hits; |
| 1942 std::vector<SBFullHashResult> cache_hits; |
| 1943 EXPECT_FALSE( |
| 1944 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1945 EXPECT_TRUE(prefix_hits.empty()); |
| 1946 EXPECT_TRUE(cache_hits.empty()); |
| 1947 |
| 1948 // Add (1001,1). Should now return a prefix hit. |
| 1949 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 1950 prefix_hits.clear(); |
| 1951 cache_hits.clear(); |
| 1952 EXPECT_TRUE( |
| 1953 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1954 ASSERT_EQ(1U, prefix_hits.size()); |
| 1955 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1956 EXPECT_TRUE(cache_hits.empty()); |
| 1957 |
| 1958 // Add (1001,2). Should still return a single prefix hit. |
| 1959 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02")); |
| 1960 prefix_hits.clear(); |
| 1961 cache_hits.clear(); |
| 1962 EXPECT_TRUE( |
| 1963 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1964 ASSERT_EQ(1U, prefix_hits.size()); |
| 1965 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1966 EXPECT_TRUE(cache_hits.empty()); |
| 1967 } |
| 1968 |
| 1969 { |
| 1970 // Check if DB contains (prefix 1001, suffix 02) alone. Should return a |
| 1971 // prefix hit. |
| 1972 std::vector<SBFullHash> full_hashes; |
| 1973 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02")); |
| 1974 std::vector<SBPrefix> prefix_hits; |
| 1975 std::vector<SBFullHashResult> cache_hits; |
| 1976 EXPECT_TRUE( |
| 1977 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 1978 ASSERT_EQ(1U, prefix_hits.size()); |
| 1979 EXPECT_EQ(1001U, prefix_hits[0]); |
| 1980 EXPECT_TRUE(cache_hits.empty()); |
| 1981 } |
| 1982 |
| 1983 { |
| 1984 // Cache a fullhash result for (prefix 1001, suffix 02). |
| 1985 std::vector<SBPrefix> prefixes; |
| 1986 prefixes.push_back(SBPrefix(1001)); |
| 1987 std::vector<SBFullHashResult> cache_results; |
| 1988 |
| 1989 SBFullHashResult full_hash_result; |
| 1990 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x02"); |
| 1991 full_hash_result.list_name = safe_browsing_util::kMalwareList; |
| 1992 |
| 1993 cache_results.push_back(full_hash_result); |
| 1994 |
| 1995 database_->CacheHashResults( |
| 1996 prefixes, cache_results, base::TimeDelta::FromMinutes(60)); |
| 1997 } |
| 1998 |
| 1999 { |
| 2000 // Check if DB contains (prefix 1001, suffix 03). Should return false, |
| 2001 // since the DB containts a cached hit with matching prefix but not |
| 2002 // matching the fullhash. |
| 2003 std::vector<SBFullHash> full_hashes; |
| 2004 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03")); |
| 2005 std::vector<SBPrefix> prefix_hits; |
| 2006 std::vector<SBFullHashResult> cache_hits; |
| 2007 EXPECT_FALSE( |
| 2008 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 2009 EXPECT_TRUE(prefix_hits.empty()); |
| 2010 EXPECT_TRUE(cache_hits.empty()); |
| 2011 |
| 2012 // Add (1001,1). Should still return false. |
| 2013 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01")); |
| 2014 prefix_hits.clear(); |
| 2015 cache_hits.clear(); |
| 2016 EXPECT_FALSE( |
| 2017 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 2018 EXPECT_TRUE(prefix_hits.empty()); |
| 2019 EXPECT_TRUE(cache_hits.empty()); |
| 2020 |
| 2021 // Add (1001,2). Should now return a cache hit. |
| 2022 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02")); |
| 2023 prefix_hits.clear(); |
| 2024 cache_hits.clear(); |
| 2025 EXPECT_TRUE( |
| 2026 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits)); |
| 2027 EXPECT_TRUE(prefix_hits.empty()); |
| 2028 ASSERT_EQ(1U, cache_hits.size()); |
| 2029 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x02"), |
| 2030 cache_hits[0].hash)); |
| 2031 } |
| 2032 } |
| 2033 |
| 1728 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { | 2034 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { |
| 1729 database_.reset(); | 2035 database_.reset(); |
| 1730 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 2036 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); |
| 1731 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | 2037 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); |
| 1732 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | 2038 database_.reset(new SafeBrowsingDatabaseNew(browse_store, |
| 1733 NULL, | 2039 NULL, |
| 1734 NULL, | 2040 NULL, |
| 1735 NULL, | 2041 NULL, |
| 1736 NULL, | 2042 NULL, |
| 1737 NULL, | 2043 NULL, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1815 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); | 2121 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); |
| 1816 | 2122 |
| 1817 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); | 2123 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); |
| 1818 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); | 2124 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); |
| 1819 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); | 2125 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); |
| 1820 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); | 2126 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); |
| 1821 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); | 2127 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); |
| 1822 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); | 2128 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); |
| 1823 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); | 2129 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); |
| 1824 } | 2130 } |
| OLD | NEW |