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" | 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
8 | 8 |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <utility> | 10 #include <utility> |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 EXPECT_TRUE(lists[7].subs.empty()); | 470 EXPECT_TRUE(lists[7].subs.empty()); |
471 EXPECT_EQ(kUnwantedUrlList, lists[8].name); | 471 EXPECT_EQ(kUnwantedUrlList, lists[8].name); |
472 EXPECT_EQ("11", lists[8].adds); | 472 EXPECT_EQ("11", lists[8].adds); |
473 EXPECT_TRUE(lists[8].subs.empty()); | 473 EXPECT_TRUE(lists[8].subs.empty()); |
474 | 474 |
475 database_.reset(); | 475 database_.reset(); |
476 } | 476 } |
477 | 477 |
478 // Checks database reading and writing for browse and unwanted PrefixSets. | 478 // Checks database reading and writing for browse and unwanted PrefixSets. |
479 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { | 479 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { |
| 480 |
480 struct TestCase { | 481 struct TestCase { |
481 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( | 482 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( |
482 const GURL& url, | 483 const GURL& url, |
483 std::vector<SBPrefix>* prefix_hits, | 484 std::vector<SBPrefix>* prefix_hits, |
484 std::vector<SBFullHashResult>* cache_hits); | 485 std::vector<SBFullHashResult>* cache_hits); |
| 486 using TestListContainsBadHashes = bool (SafeBrowsingDatabase::*)( |
| 487 const std::vector<SBFullHash>& full_hashes, |
| 488 std::vector<SBPrefix>* prefix_hits, |
| 489 std::vector<SBFullHashResult>* cache_hits); |
485 | 490 |
486 const char* test_list_name; | 491 const char* test_list_name; |
487 size_t expected_list_index; | 492 size_t expected_list_index; |
488 TestListContainsBadUrl test_list_contains_bad_url; | 493 TestListContainsBadUrl test_list_contains_bad_url; |
489 } const kTestCases[]{ | 494 TestListContainsBadHashes test_list_contains_bad_hashes; |
490 {kMalwareList, 0U, &SafeBrowsingDatabase::ContainsBrowseUrl}, | 495 |
491 {kPhishingList, 1U, &SafeBrowsingDatabase::ContainsBrowseUrl}, | 496 void TestContainsFunctions(SafeBrowsingDatabaseNew& database, |
492 {kUnwantedUrlList, 8U, | 497 bool expected_outcome, |
493 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl}, | 498 const GURL& url, |
| 499 std::vector<SBPrefix>* prefix_hits, |
| 500 std::vector<SBFullHashResult>* cache_hits) const { |
| 501 EXPECT_EQ(expected_outcome, |
| 502 (database.*test_list_contains_bad_url)(url, prefix_hits, cache_hits)) |
| 503 << test_list_name << url; |
| 504 |
| 505 // Contains*Hashes should always return the same result as Contains*Url. |
| 506 std::vector<SBFullHash> full_hashes; |
| 507 UrlToFullHashes(url, false, &full_hashes); |
| 508 ASSERT_FALSE(full_hashes.empty()) << test_list_name << url; |
| 509 |
| 510 std::vector<SBPrefix> hash_prefix_hits; |
| 511 std::vector<SBFullHashResult> hash_cache_hits; |
| 512 EXPECT_EQ(expected_outcome, (database.*test_list_contains_bad_hashes)( |
| 513 full_hashes, &hash_prefix_hits, &hash_cache_hits)) << test_list_name |
| 514 << url; |
| 515 |
| 516 EXPECT_EQ(prefix_hits->size(), hash_prefix_hits.size()) << test_list_name |
| 517 << url; |
| 518 EXPECT_EQ(cache_hits->size(), hash_cache_hits.size()) << test_list_name |
| 519 << url; |
| 520 } |
| 521 }; |
| 522 |
| 523 TestCase const kTestCases[] { |
| 524 { |
| 525 kMalwareList, |
| 526 0U, |
| 527 &SafeBrowsingDatabase::ContainsBrowseUrl, |
| 528 &SafeBrowsingDatabase::ContainsBrowseHashes |
| 529 }, |
| 530 { |
| 531 kPhishingList, |
| 532 1U, |
| 533 &SafeBrowsingDatabase::ContainsBrowseUrl, |
| 534 &SafeBrowsingDatabase::ContainsBrowseHashes |
| 535 }, |
| 536 { |
| 537 kUnwantedUrlList, |
| 538 8U, |
| 539 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl, |
| 540 &SafeBrowsingDatabase::ContainsUnwantedSoftwareHashes |
| 541 }, |
494 }; | 542 }; |
495 | 543 |
496 for (const auto& test_case : kTestCases) { | 544 for (const auto& test_case : kTestCases) { |
497 SCOPED_TRACE(std::string("Tested list at fault => ") + | 545 SCOPED_TRACE(std::string("Tested list at fault => ") + |
498 test_case.test_list_name); | 546 test_case.test_list_name); |
499 | 547 |
500 std::vector<SBListChunkRanges> lists; | 548 std::vector<SBListChunkRanges> lists; |
501 std::vector<scoped_ptr<SBChunkData>> chunks; | 549 std::vector<scoped_ptr<SBChunkData>> chunks; |
502 | 550 |
503 chunks.push_back(AddChunkPrefix2Value(1, | 551 chunks.push_back(AddChunkPrefix2Value(1, |
(...skipping 15 matching lines...) Expand all Loading... |
519 GetListsInfo(&lists); | 567 GetListsInfo(&lists); |
520 | 568 |
521 ASSERT_LE(1U, lists.size()); | 569 ASSERT_LE(1U, lists.size()); |
522 EXPECT_EQ(test_case.test_list_name, | 570 EXPECT_EQ(test_case.test_list_name, |
523 lists[test_case.expected_list_index].name); | 571 lists[test_case.expected_list_index].name); |
524 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); | 572 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
525 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); | 573 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); |
526 | 574 |
527 std::vector<SBPrefix> prefix_hits; | 575 std::vector<SBPrefix> prefix_hits; |
528 std::vector<SBFullHashResult> cache_hits; | 576 std::vector<SBFullHashResult> cache_hits; |
529 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 577 test_case.TestContainsFunctions(*database_, true, |
530 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 578 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits); |
| 579 |
531 ASSERT_EQ(1U, prefix_hits.size()); | 580 ASSERT_EQ(1U, prefix_hits.size()); |
532 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 581 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); |
533 EXPECT_TRUE(cache_hits.empty()); | 582 EXPECT_TRUE(cache_hits.empty()); |
534 | 583 |
535 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 584 test_case.TestContainsFunctions(*database_, true, |
536 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 585 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits); |
537 | 586 |
538 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 587 test_case.TestContainsFunctions(*database_, true, |
539 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); | 588 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits); |
540 | 589 |
541 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 590 test_case.TestContainsFunctions(*database_, true, |
542 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 591 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits); |
543 | 592 |
544 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 593 test_case.TestContainsFunctions(*database_, true, |
545 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 594 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits); |
546 | 595 |
547 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 596 test_case.TestContainsFunctions(*database_, true, |
548 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 597 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits); |
549 | 598 |
550 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 599 test_case.TestContainsFunctions(*database_, true, |
551 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits)); | 600 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits); |
552 | 601 |
553 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 602 test_case.TestContainsFunctions(*database_, false, |
554 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); | 603 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits); |
555 EXPECT_TRUE(prefix_hits.empty()); | 604 EXPECT_TRUE(prefix_hits.empty()); |
556 EXPECT_TRUE(cache_hits.empty()); | 605 EXPECT_TRUE(cache_hits.empty()); |
557 | 606 |
558 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 607 test_case.TestContainsFunctions(*database_, false, |
559 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits)); | 608 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits); |
560 | 609 |
561 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 610 test_case.TestContainsFunctions(*database_, true, |
562 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); | 611 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits); |
563 ASSERT_EQ(1U, prefix_hits.size()); | 612 ASSERT_EQ(1U, prefix_hits.size()); |
564 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); | 613 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); |
565 | 614 |
566 // Attempt to re-add the first chunk (should be a no-op). | 615 // Attempt to re-add the first chunk (should be a no-op). |
567 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 616 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
568 chunks.clear(); | 617 chunks.clear(); |
569 chunks.push_back(AddChunkPrefix2Value(1, | 618 chunks.push_back(AddChunkPrefix2Value(1, |
570 "www.evil.com/phishing.html", | 619 "www.evil.com/phishing.html", |
571 "www.evil.com/malware.html")); | 620 "www.evil.com/malware.html")); |
572 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 621 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
573 database_->InsertChunks(test_case.test_list_name, chunks); | 622 database_->InsertChunks(test_case.test_list_name, chunks); |
574 database_->UpdateFinished(true); | 623 database_->UpdateFinished(true); |
575 | 624 |
576 GetListsInfo(&lists); | 625 GetListsInfo(&lists); |
577 ASSERT_LE(1U, lists.size()); | 626 ASSERT_LE(1U, lists.size()); |
578 EXPECT_EQ(test_case.test_list_name, | 627 EXPECT_EQ(test_case.test_list_name, |
579 lists[test_case.expected_list_index].name); | 628 lists[test_case.expected_list_index].name); |
580 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); | 629 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
581 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); | 630 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); |
582 | 631 |
583 // Test removing a single prefix from the add chunk. | 632 // Test removing a single prefix from the add chunk. |
584 chunks.clear(); | 633 chunks.clear(); |
585 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); | 634 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); |
586 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 635 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
587 database_->InsertChunks(test_case.test_list_name, chunks); | 636 database_->InsertChunks(test_case.test_list_name, chunks); |
588 database_->UpdateFinished(true); | 637 database_->UpdateFinished(true); |
589 | 638 |
590 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 639 test_case.TestContainsFunctions(*database_, true, |
591 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 640 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits); |
592 ASSERT_EQ(1U, prefix_hits.size()); | 641 ASSERT_EQ(1U, prefix_hits.size()); |
593 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 642 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); |
594 EXPECT_TRUE(cache_hits.empty()); | 643 EXPECT_TRUE(cache_hits.empty()); |
595 | 644 |
596 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 645 test_case.TestContainsFunctions(*database_, false, |
597 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); | 646 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits); |
598 EXPECT_TRUE(prefix_hits.empty()); | 647 EXPECT_TRUE(prefix_hits.empty()); |
599 EXPECT_TRUE(cache_hits.empty()); | 648 EXPECT_TRUE(cache_hits.empty()); |
600 | 649 |
601 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 650 test_case.TestContainsFunctions(*database_, true, |
602 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 651 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits); |
603 | 652 |
604 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 653 test_case.TestContainsFunctions(*database_, true, |
605 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 654 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits); |
606 | 655 |
607 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 656 test_case.TestContainsFunctions(*database_, true, |
608 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 657 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits); |
609 | 658 |
610 GetListsInfo(&lists); | 659 GetListsInfo(&lists); |
611 ASSERT_LE(1U, lists.size()); | 660 ASSERT_LE(1U, lists.size()); |
612 EXPECT_EQ(test_case.test_list_name, | 661 EXPECT_EQ(test_case.test_list_name, |
613 lists[test_case.expected_list_index].name); | 662 lists[test_case.expected_list_index].name); |
614 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); | 663 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
615 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); | 664 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
616 | 665 |
617 // Test the same sub chunk again. This should be a no-op. | 666 // Test the same sub chunk again. This should be a no-op. |
618 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 667 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
619 chunks.clear(); | 668 chunks.clear(); |
620 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); | 669 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); |
621 | 670 |
622 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 671 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
623 database_->InsertChunks(test_case.test_list_name, chunks); | 672 database_->InsertChunks(test_case.test_list_name, chunks); |
624 database_->UpdateFinished(true); | 673 database_->UpdateFinished(true); |
625 | 674 |
626 GetListsInfo(&lists); | 675 GetListsInfo(&lists); |
627 ASSERT_LE(1U, lists.size()); | 676 ASSERT_LE(1U, lists.size()); |
628 EXPECT_EQ(test_case.test_list_name, | 677 EXPECT_EQ(test_case.test_list_name, |
629 lists[test_case.expected_list_index].name); | 678 lists[test_case.expected_list_index].name); |
630 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); | 679 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
631 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); | 680 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
632 | 681 |
633 // Test removing all the prefixes from an add chunk. | 682 // Test removing all the prefixes from an add chunk. |
634 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 683 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
635 AddDelChunk(test_case.test_list_name, 2); | 684 AddDelChunk(test_case.test_list_name, 2); |
636 database_->UpdateFinished(true); | 685 database_->UpdateFinished(true); |
637 | 686 |
638 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 687 test_case.TestContainsFunctions(*database_, false, |
639 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 688 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits); |
640 | 689 |
641 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 690 test_case.TestContainsFunctions(*database_, false, |
642 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 691 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits); |
643 | 692 |
644 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 693 test_case.TestContainsFunctions(*database_, false, |
645 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 694 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits); |
646 | 695 |
647 GetListsInfo(&lists); | 696 GetListsInfo(&lists); |
648 ASSERT_LE(1U, lists.size()); | 697 ASSERT_LE(1U, lists.size()); |
649 EXPECT_EQ(test_case.test_list_name, | 698 EXPECT_EQ(test_case.test_list_name, |
650 lists[test_case.expected_list_index].name); | 699 lists[test_case.expected_list_index].name); |
651 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds); | 700 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds); |
652 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); | 701 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
653 | 702 |
654 // The adddel command exposed a bug in the transaction code where any | 703 // The adddel command exposed a bug in the transaction code where any |
655 // transaction after it would fail. Add a dummy entry and remove it to | 704 // transaction after it would fail. Add a dummy entry and remove it to |
(...skipping 22 matching lines...) Expand all Loading... |
678 chunks.clear(); | 727 chunks.clear(); |
679 chunks.push_back(SubChunkPrefix2Value(5, | 728 chunks.push_back(SubChunkPrefix2Value(5, |
680 "www.notevilanymore.com/index.html", | 729 "www.notevilanymore.com/index.html", |
681 10, | 730 10, |
682 "www.notevilanymore.com/good.html", | 731 "www.notevilanymore.com/good.html", |
683 10)); | 732 10)); |
684 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 733 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
685 database_->InsertChunks(test_case.test_list_name, chunks); | 734 database_->InsertChunks(test_case.test_list_name, chunks); |
686 database_->UpdateFinished(true); | 735 database_->UpdateFinished(true); |
687 | 736 |
688 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 737 test_case.TestContainsFunctions(*database_, false, |
689 GURL("http://www.notevilanymore.com/index.html"), | 738 GURL("http://www.notevilanymore.com/index.html"), |
690 &prefix_hits, | 739 &prefix_hits, |
691 &cache_hits)); | 740 &cache_hits); |
692 | 741 |
693 // Now insert the tardy add chunk and we don't expect them to appear | 742 // Now insert the tardy add chunk and we don't expect them to appear |
694 // in database because of the previous sub chunk. | 743 // in database because of the previous sub chunk. |
695 chunks.clear(); | 744 chunks.clear(); |
696 chunks.push_back(AddChunkPrefix2Value(10, | 745 chunks.push_back(AddChunkPrefix2Value(10, |
697 "www.notevilanymore.com/index.html", | 746 "www.notevilanymore.com/index.html", |
698 "www.notevilanymore.com/good.html")); | 747 "www.notevilanymore.com/good.html")); |
699 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 748 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
700 database_->InsertChunks(test_case.test_list_name, chunks); | 749 database_->InsertChunks(test_case.test_list_name, chunks); |
701 database_->UpdateFinished(true); | 750 database_->UpdateFinished(true); |
702 | 751 |
703 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 752 test_case.TestContainsFunctions(*database_, false, |
704 GURL("http://www.notevilanymore.com/index.html"), | 753 GURL("http://www.notevilanymore.com/index.html"), |
705 &prefix_hits, | 754 &prefix_hits, |
706 &cache_hits)); | 755 &cache_hits); |
707 | 756 |
708 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 757 test_case.TestContainsFunctions(*database_, false, |
709 GURL("http://www.notevilanymore.com/good.html"), | 758 GURL("http://www.notevilanymore.com/good.html"), |
710 &prefix_hits, | 759 &prefix_hits, |
711 &cache_hits)); | 760 &cache_hits); |
712 | 761 |
713 // Reset and reload the database. The database will rely on the prefix set. | 762 // Reset and reload the database. The database will rely on the prefix set. |
714 ResetAndReloadFullDatabase(); | 763 ResetAndReloadFullDatabase(); |
715 | 764 |
716 // Check that a prefix still hits. | 765 // Check that a prefix still hits. |
717 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 766 test_case.TestContainsFunctions(*database_, true, |
718 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 767 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits); |
719 ASSERT_EQ(1U, prefix_hits.size()); | 768 ASSERT_EQ(1U, prefix_hits.size()); |
720 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 769 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); |
721 | 770 |
722 // Also check that it's not just always returning true in this case. | 771 // Also check that it's not just always returning true in this case. |
723 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | 772 test_case.TestContainsFunctions(*database_, false, |
724 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); | 773 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits); |
725 | 774 |
726 // Check that the full hash is still present. | 775 // Check that the full hash is still present. |
727 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | 776 test_case.TestContainsFunctions(*database_, true, |
728 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); | 777 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits); |
729 ASSERT_EQ(1U, prefix_hits.size()); | 778 ASSERT_EQ(1U, prefix_hits.size()); |
730 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); | 779 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); |
731 } | 780 } |
732 } | 781 } |
733 | 782 |
734 // Test adding zero length chunks to the database. | 783 // Test adding zero length chunks to the database. |
735 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { | 784 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { |
736 std::vector<SBListChunkRanges> lists; | 785 std::vector<SBListChunkRanges> lists; |
737 std::vector<scoped_ptr<SBChunkData>> chunks; | 786 std::vector<scoped_ptr<SBChunkData>> chunks; |
738 | 787 |
(...skipping 967 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1706 std::vector<SBFullHashResult> cache_results; | 1755 std::vector<SBFullHashResult> cache_results; |
1707 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1756 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
1708 } | 1757 } |
1709 | 1758 |
1710 { | 1759 { |
1711 // kFullHash1_1 gets no prefix hit because of the cached item, and also does | 1760 // kFullHash1_1 gets no prefix hit because of the cached item, and also does |
1712 // not have a cache hit. | 1761 // not have a cache hit. |
1713 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1762 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
1714 std::vector<SBPrefix> prefix_hits; | 1763 std::vector<SBPrefix> prefix_hits; |
1715 std::vector<SBFullHashResult> cache_hits; | 1764 std::vector<SBFullHashResult> cache_hits; |
1716 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1765 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1717 full_hashes, &prefix_hits, &cache_hits)); | 1766 full_hashes, &prefix_hits, &cache_hits)); |
1718 | 1767 |
1719 // kFullHash2_1 gets a hit from the prefix in the database. | 1768 // kFullHash2_1 gets a hit from the prefix in the database. |
1720 full_hashes.push_back(kFullHash2_1); | 1769 full_hashes.push_back(kFullHash2_1); |
1721 prefix_hits.clear(); | 1770 prefix_hits.clear(); |
1722 cache_hits.clear(); | 1771 cache_hits.clear(); |
1723 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1772 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1724 full_hashes, &prefix_hits, &cache_hits)); | 1773 full_hashes, &prefix_hits, &cache_hits)); |
1725 ASSERT_EQ(1U, prefix_hits.size()); | 1774 ASSERT_EQ(1U, prefix_hits.size()); |
1726 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1775 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
1727 EXPECT_TRUE(cache_hits.empty()); | 1776 EXPECT_TRUE(cache_hits.empty()); |
1728 } | 1777 } |
1729 } | 1778 } |
1730 | 1779 |
1731 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) { | 1780 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) { |
1732 const SBPrefix kPrefix1 = 1001U; | 1781 const SBPrefix kPrefix1 = 1001U; |
1733 const SBFullHash kFullHash1_1 = | 1782 const SBFullHash kFullHash1_1 = |
(...skipping 20 matching lines...) Expand all Loading... |
1754 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1803 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
1755 database_->InsertChunks(kMalwareList, chunks); | 1804 database_->InsertChunks(kMalwareList, chunks); |
1756 database_->UpdateFinished(true); | 1805 database_->UpdateFinished(true); |
1757 | 1806 |
1758 { | 1807 { |
1759 // kFullHash1_1 has a prefix hit of kPrefix1. | 1808 // kFullHash1_1 has a prefix hit of kPrefix1. |
1760 std::vector<SBFullHash> full_hashes; | 1809 std::vector<SBFullHash> full_hashes; |
1761 full_hashes.push_back(kFullHash1_1); | 1810 full_hashes.push_back(kFullHash1_1); |
1762 std::vector<SBPrefix> prefix_hits; | 1811 std::vector<SBPrefix> prefix_hits; |
1763 std::vector<SBFullHashResult> cache_hits; | 1812 std::vector<SBFullHashResult> cache_hits; |
1764 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1813 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1765 full_hashes, &prefix_hits, &cache_hits)); | 1814 full_hashes, &prefix_hits, &cache_hits)); |
1766 ASSERT_EQ(1U, prefix_hits.size()); | 1815 ASSERT_EQ(1U, prefix_hits.size()); |
1767 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1816 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
1768 EXPECT_TRUE(cache_hits.empty()); | 1817 EXPECT_TRUE(cache_hits.empty()); |
1769 | 1818 |
1770 // kFullHash2_1 has a prefix hit of kPrefix2. | 1819 // kFullHash2_1 has a prefix hit of kPrefix2. |
1771 full_hashes.push_back(kFullHash2_1); | 1820 full_hashes.push_back(kFullHash2_1); |
1772 prefix_hits.clear(); | 1821 prefix_hits.clear(); |
1773 cache_hits.clear(); | 1822 cache_hits.clear(); |
1774 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1823 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1775 full_hashes, &prefix_hits, &cache_hits)); | 1824 full_hashes, &prefix_hits, &cache_hits)); |
1776 ASSERT_EQ(2U, prefix_hits.size()); | 1825 ASSERT_EQ(2U, prefix_hits.size()); |
1777 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1826 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
1778 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1827 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
1779 EXPECT_TRUE(cache_hits.empty()); | 1828 EXPECT_TRUE(cache_hits.empty()); |
1780 | 1829 |
1781 // kFullHash3_1 has no hits. | 1830 // kFullHash3_1 has no hits. |
1782 full_hashes.push_back(kFullHash3_1); | 1831 full_hashes.push_back(kFullHash3_1); |
1783 prefix_hits.clear(); | 1832 prefix_hits.clear(); |
1784 cache_hits.clear(); | 1833 cache_hits.clear(); |
1785 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1834 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1786 full_hashes, &prefix_hits, &cache_hits)); | 1835 full_hashes, &prefix_hits, &cache_hits)); |
1787 ASSERT_EQ(2U, prefix_hits.size()); | 1836 ASSERT_EQ(2U, prefix_hits.size()); |
1788 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1837 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
1789 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1838 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
1790 EXPECT_TRUE(cache_hits.empty()); | 1839 EXPECT_TRUE(cache_hits.empty()); |
1791 } | 1840 } |
1792 | 1841 |
1793 { | 1842 { |
1794 // Cache a fullhash result for two kPrefix1 full hashes. | 1843 // Cache a fullhash result for two kPrefix1 full hashes. |
1795 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1844 std::vector<SBPrefix> prefixes(1, kPrefix1); |
1796 std::vector<SBFullHashResult> cache_results; | 1845 std::vector<SBFullHashResult> cache_results; |
1797 | 1846 |
1798 SBFullHashResult full_hash_result; | 1847 SBFullHashResult full_hash_result; |
1799 full_hash_result.list_id = MALWARE; | 1848 full_hash_result.list_id = MALWARE; |
1800 | 1849 |
1801 full_hash_result.hash = kFullHash1_1; | 1850 full_hash_result.hash = kFullHash1_1; |
1802 cache_results.push_back(full_hash_result); | 1851 cache_results.push_back(full_hash_result); |
1803 | 1852 |
1804 full_hash_result.hash = kFullHash1_3; | 1853 full_hash_result.hash = kFullHash1_3; |
1805 cache_results.push_back(full_hash_result); | 1854 cache_results.push_back(full_hash_result); |
1806 | 1855 |
1807 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1856 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
1808 } | 1857 } |
1809 | 1858 |
1810 { | 1859 { |
1811 // kFullHash1_1 should now see a cache hit. | 1860 // kFullHash1_1 should now see a cache hit. |
1812 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1861 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
1813 std::vector<SBPrefix> prefix_hits; | 1862 std::vector<SBPrefix> prefix_hits; |
1814 std::vector<SBFullHashResult> cache_hits; | 1863 std::vector<SBFullHashResult> cache_hits; |
1815 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1864 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1816 full_hashes, &prefix_hits, &cache_hits)); | 1865 full_hashes, &prefix_hits, &cache_hits)); |
1817 EXPECT_TRUE(prefix_hits.empty()); | 1866 EXPECT_TRUE(prefix_hits.empty()); |
1818 ASSERT_EQ(1U, cache_hits.size()); | 1867 ASSERT_EQ(1U, cache_hits.size()); |
1819 EXPECT_TRUE(SBFullHashEqual( | 1868 EXPECT_TRUE(SBFullHashEqual( |
1820 kFullHash1_1, cache_hits[0].hash)); | 1869 kFullHash1_1, cache_hits[0].hash)); |
1821 | 1870 |
1822 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit | 1871 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit |
1823 // for kPrefix2. | 1872 // for kPrefix2. |
1824 full_hashes.push_back(kFullHash2_1); | 1873 full_hashes.push_back(kFullHash2_1); |
1825 prefix_hits.clear(); | 1874 prefix_hits.clear(); |
1826 cache_hits.clear(); | 1875 cache_hits.clear(); |
1827 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1876 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1828 full_hashes, &prefix_hits, &cache_hits)); | 1877 full_hashes, &prefix_hits, &cache_hits)); |
1829 ASSERT_EQ(1U, prefix_hits.size()); | 1878 ASSERT_EQ(1U, prefix_hits.size()); |
1830 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1879 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
1831 ASSERT_EQ(1U, cache_hits.size()); | 1880 ASSERT_EQ(1U, cache_hits.size()); |
1832 EXPECT_TRUE(SBFullHashEqual( | 1881 EXPECT_TRUE(SBFullHashEqual( |
1833 kFullHash1_1, cache_hits[0].hash)); | 1882 kFullHash1_1, cache_hits[0].hash)); |
1834 | 1883 |
1835 // kFullHash1_3 also gets a cache hit. | 1884 // kFullHash1_3 also gets a cache hit. |
1836 full_hashes.push_back(kFullHash1_3); | 1885 full_hashes.push_back(kFullHash1_3); |
1837 prefix_hits.clear(); | 1886 prefix_hits.clear(); |
1838 cache_hits.clear(); | 1887 cache_hits.clear(); |
1839 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1888 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1840 full_hashes, &prefix_hits, &cache_hits)); | 1889 full_hashes, &prefix_hits, &cache_hits)); |
1841 ASSERT_EQ(1U, prefix_hits.size()); | 1890 ASSERT_EQ(1U, prefix_hits.size()); |
1842 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1891 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
1843 ASSERT_EQ(2U, cache_hits.size()); | 1892 ASSERT_EQ(2U, cache_hits.size()); |
1844 EXPECT_TRUE(SBFullHashEqual( | 1893 EXPECT_TRUE(SBFullHashEqual( |
1845 kFullHash1_1, cache_hits[0].hash)); | 1894 kFullHash1_1, cache_hits[0].hash)); |
1846 EXPECT_TRUE(SBFullHashEqual( | 1895 EXPECT_TRUE(SBFullHashEqual( |
1847 kFullHash1_3, cache_hits[1].hash)); | 1896 kFullHash1_3, cache_hits[1].hash)); |
1848 } | 1897 } |
1849 | 1898 |
1850 { | 1899 { |
1851 // Check if DB contains only kFullHash1_3. Should return a cache hit. | 1900 // Check if DB contains only kFullHash1_3. Should return a cache hit. |
1852 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1901 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
1853 std::vector<SBPrefix> prefix_hits; | 1902 std::vector<SBPrefix> prefix_hits; |
1854 std::vector<SBFullHashResult> cache_hits; | 1903 std::vector<SBFullHashResult> cache_hits; |
1855 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1904 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1856 full_hashes, &prefix_hits, &cache_hits)); | 1905 full_hashes, &prefix_hits, &cache_hits)); |
1857 EXPECT_TRUE(prefix_hits.empty()); | 1906 EXPECT_TRUE(prefix_hits.empty()); |
1858 ASSERT_EQ(1U, cache_hits.size()); | 1907 ASSERT_EQ(1U, cache_hits.size()); |
1859 EXPECT_TRUE(SBFullHashEqual( | 1908 EXPECT_TRUE(SBFullHashEqual( |
1860 kFullHash1_3, cache_hits[0].hash)); | 1909 kFullHash1_3, cache_hits[0].hash)); |
1861 } | 1910 } |
1862 | 1911 |
1863 { | 1912 { |
1864 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for | 1913 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for |
1865 // kPrefix1. | 1914 // kPrefix1. |
1866 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1915 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
1867 std::vector<SBPrefix> prefix_hits; | 1916 std::vector<SBPrefix> prefix_hits; |
1868 std::vector<SBFullHashResult> cache_hits; | 1917 std::vector<SBFullHashResult> cache_hits; |
1869 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1918 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1870 full_hashes, &prefix_hits, &cache_hits)); | 1919 full_hashes, &prefix_hits, &cache_hits)); |
1871 | 1920 |
1872 // Other prefix hits possible when kFullHash1_2 hits nothing. | 1921 // Other prefix hits possible when kFullHash1_2 hits nothing. |
1873 full_hashes.push_back(kFullHash2_1); | 1922 full_hashes.push_back(kFullHash2_1); |
1874 prefix_hits.clear(); | 1923 prefix_hits.clear(); |
1875 cache_hits.clear(); | 1924 cache_hits.clear(); |
1876 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1925 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1877 full_hashes, &prefix_hits, &cache_hits)); | 1926 full_hashes, &prefix_hits, &cache_hits)); |
1878 ASSERT_EQ(1U, prefix_hits.size()); | 1927 ASSERT_EQ(1U, prefix_hits.size()); |
1879 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1928 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
1880 EXPECT_TRUE(cache_hits.empty()); | 1929 EXPECT_TRUE(cache_hits.empty()); |
1881 } | 1930 } |
1882 } | 1931 } |
1883 | 1932 |
1884 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { | 1933 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { |
1885 const SBPrefix kPrefix1 = 1001U; | 1934 const SBPrefix kPrefix1 = 1001U; |
1886 const SBFullHash kFullHash1_1 = | 1935 const SBFullHash kFullHash1_1 = |
(...skipping 11 matching lines...) Expand all Loading... |
1898 std::vector<SBListChunkRanges> lists; | 1947 std::vector<SBListChunkRanges> lists; |
1899 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1948 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
1900 database_->InsertChunks(kMalwareList, chunks); | 1949 database_->InsertChunks(kMalwareList, chunks); |
1901 database_->UpdateFinished(true); | 1950 database_->UpdateFinished(true); |
1902 | 1951 |
1903 { | 1952 { |
1904 // Check a full hash which isn't present. | 1953 // Check a full hash which isn't present. |
1905 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1954 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
1906 std::vector<SBPrefix> prefix_hits; | 1955 std::vector<SBPrefix> prefix_hits; |
1907 std::vector<SBFullHashResult> cache_hits; | 1956 std::vector<SBFullHashResult> cache_hits; |
1908 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1957 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1909 full_hashes, &prefix_hits, &cache_hits)); | 1958 full_hashes, &prefix_hits, &cache_hits)); |
1910 | 1959 |
1911 // Also one which is present, should have a prefix hit. | 1960 // Also one which is present, should have a prefix hit. |
1912 full_hashes.push_back(kFullHash1_1); | 1961 full_hashes.push_back(kFullHash1_1); |
1913 prefix_hits.clear(); | 1962 prefix_hits.clear(); |
1914 cache_hits.clear(); | 1963 cache_hits.clear(); |
1915 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1964 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1916 full_hashes, &prefix_hits, &cache_hits)); | 1965 full_hashes, &prefix_hits, &cache_hits)); |
1917 ASSERT_EQ(1U, prefix_hits.size()); | 1966 ASSERT_EQ(1U, prefix_hits.size()); |
1918 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1967 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
1919 EXPECT_TRUE(cache_hits.empty()); | 1968 EXPECT_TRUE(cache_hits.empty()); |
1920 | 1969 |
1921 // Two full hash matches with the same prefix should return one prefix hit. | 1970 // Two full hash matches with the same prefix should return one prefix hit. |
1922 full_hashes.push_back(kFullHash1_2); | 1971 full_hashes.push_back(kFullHash1_2); |
1923 prefix_hits.clear(); | 1972 prefix_hits.clear(); |
1924 cache_hits.clear(); | 1973 cache_hits.clear(); |
1925 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1974 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1926 full_hashes, &prefix_hits, &cache_hits)); | 1975 full_hashes, &prefix_hits, &cache_hits)); |
1927 ASSERT_EQ(1U, prefix_hits.size()); | 1976 ASSERT_EQ(1U, prefix_hits.size()); |
1928 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1977 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
1929 EXPECT_TRUE(cache_hits.empty()); | 1978 EXPECT_TRUE(cache_hits.empty()); |
1930 } | 1979 } |
1931 | 1980 |
1932 { | 1981 { |
1933 // Cache a gethash result for kFullHash1_2. | 1982 // Cache a gethash result for kFullHash1_2. |
1934 SBFullHashResult full_hash_result; | 1983 SBFullHashResult full_hash_result; |
1935 full_hash_result.list_id = MALWARE; | 1984 full_hash_result.list_id = MALWARE; |
1936 full_hash_result.hash = kFullHash1_2; | 1985 full_hash_result.hash = kFullHash1_2; |
1937 | 1986 |
1938 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1987 std::vector<SBPrefix> prefixes(1, kPrefix1); |
1939 std::vector<SBFullHashResult> cache_results(1, full_hash_result); | 1988 std::vector<SBFullHashResult> cache_results(1, full_hash_result); |
1940 | 1989 |
1941 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1990 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
1942 } | 1991 } |
1943 | 1992 |
1944 { | 1993 { |
1945 // kFullHash1_3 should still return false, because the cached | 1994 // kFullHash1_3 should still return false, because the cached |
1946 // result for kPrefix1 doesn't contain kFullHash1_3. | 1995 // result for kPrefix1 doesn't contain kFullHash1_3. |
1947 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1996 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
1948 std::vector<SBPrefix> prefix_hits; | 1997 std::vector<SBPrefix> prefix_hits; |
1949 std::vector<SBFullHashResult> cache_hits; | 1998 std::vector<SBFullHashResult> cache_hits; |
1950 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1999 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1951 full_hashes, &prefix_hits, &cache_hits)); | 2000 full_hashes, &prefix_hits, &cache_hits)); |
1952 | 2001 |
1953 // kFullHash1_1 is also not in the cached result, which takes | 2002 // kFullHash1_1 is also not in the cached result, which takes |
1954 // priority over the database. | 2003 // priority over the database. |
1955 prefix_hits.clear(); | 2004 prefix_hits.clear(); |
1956 full_hashes.push_back(kFullHash1_1); | 2005 full_hashes.push_back(kFullHash1_1); |
1957 cache_hits.clear(); | 2006 cache_hits.clear(); |
1958 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2007 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1959 full_hashes, &prefix_hits, &cache_hits)); | 2008 full_hashes, &prefix_hits, &cache_hits)); |
1960 | 2009 |
1961 // kFullHash1_2 is in the cached result. | 2010 // kFullHash1_2 is in the cached result. |
1962 full_hashes.push_back(kFullHash1_2); | 2011 full_hashes.push_back(kFullHash1_2); |
1963 prefix_hits.clear(); | 2012 prefix_hits.clear(); |
1964 cache_hits.clear(); | 2013 cache_hits.clear(); |
1965 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2014 EXPECT_TRUE(database_->ContainsBrowseHashes( |
1966 full_hashes, &prefix_hits, &cache_hits)); | 2015 full_hashes, &prefix_hits, &cache_hits)); |
1967 EXPECT_TRUE(prefix_hits.empty()); | 2016 EXPECT_TRUE(prefix_hits.empty()); |
1968 ASSERT_EQ(1U, cache_hits.size()); | 2017 ASSERT_EQ(1U, cache_hits.size()); |
1969 EXPECT_TRUE(SBFullHashEqual( | 2018 EXPECT_TRUE(SBFullHashEqual( |
1970 kFullHash1_2, cache_hits[0].hash)); | 2019 kFullHash1_2, cache_hits[0].hash)); |
1971 } | 2020 } |
1972 | 2021 |
1973 // Remove kFullHash1_1 from the database. | 2022 // Remove kFullHash1_1 from the database. |
1974 chunks.clear(); | 2023 chunks.clear(); |
1975 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 2024 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
1976 | 2025 |
1977 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2026 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
1978 database_->InsertChunks(kMalwareList, chunks); | 2027 database_->InsertChunks(kMalwareList, chunks); |
1979 database_->UpdateFinished(true); | 2028 database_->UpdateFinished(true); |
1980 | 2029 |
1981 // Cache should be cleared after updating. | 2030 // Cache should be cleared after updating. |
1982 EXPECT_TRUE( | 2031 EXPECT_TRUE( |
1983 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 2032 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
1984 | 2033 |
1985 { | 2034 { |
1986 // Now the database doesn't contain kFullHash1_1. | 2035 // Now the database doesn't contain kFullHash1_1. |
1987 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 2036 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
1988 std::vector<SBPrefix> prefix_hits; | 2037 std::vector<SBPrefix> prefix_hits; |
1989 std::vector<SBFullHashResult> cache_hits; | 2038 std::vector<SBFullHashResult> cache_hits; |
1990 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2039 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1991 full_hashes, &prefix_hits, &cache_hits)); | 2040 full_hashes, &prefix_hits, &cache_hits)); |
1992 | 2041 |
1993 // Nor kFullHash1_3. | 2042 // Nor kFullHash1_3. |
1994 full_hashes.push_back(kFullHash1_3); | 2043 full_hashes.push_back(kFullHash1_3); |
1995 prefix_hits.clear(); | 2044 prefix_hits.clear(); |
1996 cache_hits.clear(); | 2045 cache_hits.clear(); |
1997 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2046 EXPECT_FALSE(database_->ContainsBrowseHashes( |
1998 full_hashes, &prefix_hits, &cache_hits)); | 2047 full_hashes, &prefix_hits, &cache_hits)); |
1999 | 2048 |
2000 // Still has kFullHash1_2. | 2049 // Still has kFullHash1_2. |
2001 full_hashes.push_back(kFullHash1_2); | 2050 full_hashes.push_back(kFullHash1_2); |
2002 prefix_hits.clear(); | 2051 prefix_hits.clear(); |
2003 cache_hits.clear(); | 2052 cache_hits.clear(); |
2004 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2053 EXPECT_TRUE(database_->ContainsBrowseHashes( |
2005 full_hashes, &prefix_hits, &cache_hits)); | 2054 full_hashes, &prefix_hits, &cache_hits)); |
2006 ASSERT_EQ(1U, prefix_hits.size()); | 2055 ASSERT_EQ(1U, prefix_hits.size()); |
2007 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2056 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
2008 EXPECT_TRUE(cache_hits.empty()); | 2057 EXPECT_TRUE(cache_hits.empty()); |
2009 } | 2058 } |
2010 | 2059 |
2011 // Remove kFullHash1_2 from the database. | 2060 // Remove kFullHash1_2 from the database. |
2012 chunks.clear(); | 2061 chunks.clear(); |
2013 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); | 2062 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); |
2014 | 2063 |
2015 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2064 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
2016 database_->InsertChunks(kMalwareList, chunks); | 2065 database_->InsertChunks(kMalwareList, chunks); |
2017 database_->UpdateFinished(true); | 2066 database_->UpdateFinished(true); |
2018 | 2067 |
2019 // Cache should be cleared after updating. | 2068 // Cache should be cleared after updating. |
2020 EXPECT_TRUE( | 2069 EXPECT_TRUE( |
2021 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 2070 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
2022 | 2071 |
2023 { | 2072 { |
2024 // None are present. | 2073 // None are present. |
2025 std::vector<SBFullHash> full_hashes; | 2074 std::vector<SBFullHash> full_hashes; |
2026 std::vector<SBPrefix> prefix_hits; | 2075 std::vector<SBPrefix> prefix_hits; |
2027 std::vector<SBFullHashResult> cache_hits; | 2076 std::vector<SBFullHashResult> cache_hits; |
2028 full_hashes.push_back(kFullHash1_1); | 2077 full_hashes.push_back(kFullHash1_1); |
2029 full_hashes.push_back(kFullHash1_2); | 2078 full_hashes.push_back(kFullHash1_2); |
2030 full_hashes.push_back(kFullHash1_3); | 2079 full_hashes.push_back(kFullHash1_3); |
2031 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2080 EXPECT_FALSE(database_->ContainsBrowseHashes( |
2032 full_hashes, &prefix_hits, &cache_hits)); | 2081 full_hashes, &prefix_hits, &cache_hits)); |
2033 } | 2082 } |
2034 } | 2083 } |
2035 | 2084 |
2036 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { | 2085 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { |
2037 const SBPrefix kPrefix1 = 1001U; | 2086 const SBPrefix kPrefix1 = 1001U; |
2038 const SBFullHash kFullHash1_1 = | 2087 const SBFullHash kFullHash1_1 = |
2039 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); | 2088 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); |
2040 const SBFullHash kFullHash1_2 = | 2089 const SBFullHash kFullHash1_2 = |
2041 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); | 2090 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); |
2042 | 2091 |
2043 std::vector<scoped_ptr<SBChunkData>> chunks; | 2092 std::vector<scoped_ptr<SBChunkData>> chunks; |
2044 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 2093 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
2045 | 2094 |
2046 std::vector<SBListChunkRanges> lists; | 2095 std::vector<SBListChunkRanges> lists; |
2047 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2096 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
2048 database_->InsertChunks(kMalwareList, chunks); | 2097 database_->InsertChunks(kMalwareList, chunks); |
2049 database_->UpdateFinished(true); | 2098 database_->UpdateFinished(true); |
2050 | 2099 |
2051 { | 2100 { |
2052 // kFullHash1_2 does not match kFullHash1_1. | 2101 // kFullHash1_2 does not match kFullHash1_1. |
2053 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2102 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
2054 std::vector<SBPrefix> prefix_hits; | 2103 std::vector<SBPrefix> prefix_hits; |
2055 std::vector<SBFullHashResult> cache_hits; | 2104 std::vector<SBFullHashResult> cache_hits; |
2056 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2105 EXPECT_FALSE(database_->ContainsBrowseHashes( |
2057 full_hashes, &prefix_hits, &cache_hits)); | 2106 full_hashes, &prefix_hits, &cache_hits)); |
2058 } | 2107 } |
2059 | 2108 |
2060 // Add a prefix match. | 2109 // Add a prefix match. |
2061 chunks.clear(); | 2110 chunks.clear(); |
2062 chunks.push_back(AddChunkPrefix(2, kPrefix1)); | 2111 chunks.push_back(AddChunkPrefix(2, kPrefix1)); |
2063 | 2112 |
2064 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2113 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
2065 database_->InsertChunks(kMalwareList, chunks); | 2114 database_->InsertChunks(kMalwareList, chunks); |
2066 database_->UpdateFinished(true); | 2115 database_->UpdateFinished(true); |
2067 | 2116 |
2068 { | 2117 { |
2069 // kFullHash1_2 does match kPrefix1. | 2118 // kFullHash1_2 does match kPrefix1. |
2070 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2119 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
2071 std::vector<SBPrefix> prefix_hits; | 2120 std::vector<SBPrefix> prefix_hits; |
2072 std::vector<SBFullHashResult> cache_hits; | 2121 std::vector<SBFullHashResult> cache_hits; |
2073 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2122 EXPECT_TRUE(database_->ContainsBrowseHashes( |
2074 full_hashes, &prefix_hits, &cache_hits)); | 2123 full_hashes, &prefix_hits, &cache_hits)); |
2075 ASSERT_EQ(1U, prefix_hits.size()); | 2124 ASSERT_EQ(1U, prefix_hits.size()); |
2076 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2125 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
2077 EXPECT_TRUE(cache_hits.empty()); | 2126 EXPECT_TRUE(cache_hits.empty()); |
2078 } | 2127 } |
2079 | 2128 |
2080 // Remove the full hash. | 2129 // Remove the full hash. |
2081 chunks.clear(); | 2130 chunks.clear(); |
2082 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 2131 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
2083 | 2132 |
2084 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2133 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
2085 database_->InsertChunks(kMalwareList, chunks); | 2134 database_->InsertChunks(kMalwareList, chunks); |
2086 database_->UpdateFinished(true); | 2135 database_->UpdateFinished(true); |
2087 | 2136 |
2088 { | 2137 { |
2089 // kFullHash1_2 still returns true due to the prefix hit. | 2138 // kFullHash1_2 still returns true due to the prefix hit. |
2090 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2139 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
2091 std::vector<SBPrefix> prefix_hits; | 2140 std::vector<SBPrefix> prefix_hits; |
2092 std::vector<SBFullHashResult> cache_hits; | 2141 std::vector<SBFullHashResult> cache_hits; |
2093 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2142 EXPECT_TRUE(database_->ContainsBrowseHashes( |
2094 full_hashes, &prefix_hits, &cache_hits)); | 2143 full_hashes, &prefix_hits, &cache_hits)); |
2095 ASSERT_EQ(1U, prefix_hits.size()); | 2144 ASSERT_EQ(1U, prefix_hits.size()); |
2096 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2145 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
2097 EXPECT_TRUE(cache_hits.empty()); | 2146 EXPECT_TRUE(cache_hits.empty()); |
2098 } | 2147 } |
2099 } | 2148 } |
2100 | 2149 |
2101 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { | 2150 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { |
2102 std::vector<SBListChunkRanges> lists; | 2151 std::vector<SBListChunkRanges> lists; |
2103 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2152 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2228 ASSERT_EQ(1U, prefix_hits.size()); | 2277 ASSERT_EQ(1U, prefix_hits.size()); |
2229 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2278 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
2230 EXPECT_TRUE(cache_hits.empty()); | 2279 EXPECT_TRUE(cache_hits.empty()); |
2231 | 2280 |
2232 // This prefix collides, but no full hash match. | 2281 // This prefix collides, but no full hash match. |
2233 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2282 EXPECT_FALSE(database_->ContainsBrowseUrl( |
2234 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2283 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
2235 } | 2284 } |
2236 | 2285 |
2237 } // namespace safe_browsing | 2286 } // namespace safe_browsing |
OLD | NEW |