| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Unit tests for the SafeBrowsing storage system. | 5 // Unit tests for the SafeBrowsing storage system. |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 | 524 |
| 525 // Make sure they were added correctly. | 525 // Make sure they were added correctly. |
| 526 GetListsInfo(&lists); | 526 GetListsInfo(&lists); |
| 527 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 527 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 528 EXPECT_EQ(lists[0].adds, "1-3"); | 528 EXPECT_EQ(lists[0].adds, "1-3"); |
| 529 EXPECT_TRUE(lists[0].subs.empty()); | 529 EXPECT_TRUE(lists[0].subs.empty()); |
| 530 | 530 |
| 531 const Time now = Time::Now(); | 531 const Time now = Time::Now(); |
| 532 std::vector<SBFullHashResult> full_hashes; | 532 std::vector<SBFullHashResult> full_hashes; |
| 533 std::vector<SBPrefix> prefix_hits; | 533 std::vector<SBPrefix> prefix_hits; |
| 534 std::string matching_list; | |
| 535 EXPECT_TRUE(database_->ContainsBrowseUrl( | 534 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 536 GURL("http://www.evil.com/phishing.html"), | 535 GURL("http://www.evil.com/phishing.html"), |
| 537 &matching_list, &prefix_hits, | 536 &prefix_hits, &full_hashes, now)); |
| 538 &full_hashes, now)); | |
| 539 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); | 537 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); |
| 540 EXPECT_EQ(prefix_hits.size(), 1U); | 538 EXPECT_EQ(prefix_hits.size(), 1U); |
| 541 | 539 |
| 542 EXPECT_TRUE(database_->ContainsBrowseUrl( | 540 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 543 GURL("http://www.evil.com/malware.html"), | 541 GURL("http://www.evil.com/malware.html"), |
| 544 &matching_list, &prefix_hits, | 542 &prefix_hits, &full_hashes, now)); |
| 545 &full_hashes, now)); | |
| 546 | 543 |
| 547 EXPECT_TRUE(database_->ContainsBrowseUrl( | 544 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 548 GURL("http://www.evil.com/notevil1.html"), | 545 GURL("http://www.evil.com/notevil1.html"), |
| 549 &matching_list, &prefix_hits, | 546 &prefix_hits, &full_hashes, now)); |
| 550 &full_hashes, now)); | |
| 551 | 547 |
| 552 EXPECT_TRUE(database_->ContainsBrowseUrl( | 548 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 553 GURL("http://www.evil.com/notevil2.html"), | 549 GURL("http://www.evil.com/notevil2.html"), |
| 554 &matching_list, &prefix_hits, | 550 &prefix_hits, &full_hashes, now)); |
| 555 &full_hashes, now)); | |
| 556 | 551 |
| 557 EXPECT_TRUE(database_->ContainsBrowseUrl( | 552 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 558 GURL("http://www.good.com/good1.html"), | 553 GURL("http://www.good.com/good1.html"), |
| 559 &matching_list, &prefix_hits, | 554 &prefix_hits, &full_hashes, now)); |
| 560 &full_hashes, now)); | |
| 561 | 555 |
| 562 EXPECT_TRUE(database_->ContainsBrowseUrl( | 556 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 563 GURL("http://www.good.com/good2.html"), | 557 GURL("http://www.good.com/good2.html"), |
| 564 &matching_list, &prefix_hits, | 558 &prefix_hits, &full_hashes, now)); |
| 565 &full_hashes, now)); | |
| 566 | 559 |
| 567 EXPECT_TRUE(database_->ContainsBrowseUrl( | 560 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 568 GURL("http://192.168.0.1/malware.html"), | 561 GURL("http://192.168.0.1/malware.html"), |
| 569 &matching_list, &prefix_hits, | 562 &prefix_hits, &full_hashes, now)); |
| 570 &full_hashes, now)); | |
| 571 | 563 |
| 572 EXPECT_FALSE(database_->ContainsBrowseUrl( | 564 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 573 GURL("http://www.evil.com/"), | 565 GURL("http://www.evil.com/"), |
| 574 &matching_list, &prefix_hits, | 566 &prefix_hits, &full_hashes, now)); |
| 575 &full_hashes, now)); | |
| 576 EXPECT_TRUE(prefix_hits.empty()); | 567 EXPECT_TRUE(prefix_hits.empty()); |
| 577 | 568 |
| 578 EXPECT_FALSE(database_->ContainsBrowseUrl( | 569 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 579 GURL("http://www.evil.com/robots.txt"), | 570 GURL("http://www.evil.com/robots.txt"), |
| 580 &matching_list, &prefix_hits, | 571 &prefix_hits, &full_hashes, now)); |
| 581 &full_hashes, now)); | |
| 582 | 572 |
| 583 // Attempt to re-add the first chunk (should be a no-op). | 573 // 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 | 574 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 585 chunk.hosts.clear(); | 575 chunk.hosts.clear(); |
| 586 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", | 576 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", |
| 587 "www.evil.com/phishing.html", | 577 "www.evil.com/phishing.html", |
| 588 "www.evil.com/malware.html"); | 578 "www.evil.com/malware.html"); |
| 589 chunks.clear(); | 579 chunks.clear(); |
| 590 chunks.push_back(chunk); | 580 chunks.push_back(chunk); |
| 591 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 581 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 603 "www.evil.com/notevil1.html"); | 593 "www.evil.com/notevil1.html"); |
| 604 chunks.clear(); | 594 chunks.clear(); |
| 605 chunks.push_back(chunk); | 595 chunks.push_back(chunk); |
| 606 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 596 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 607 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 597 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 608 | 598 |
| 609 database_->UpdateFinished(true); | 599 database_->UpdateFinished(true); |
| 610 | 600 |
| 611 EXPECT_TRUE(database_->ContainsBrowseUrl( | 601 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 612 GURL("http://www.evil.com/phishing.html"), | 602 GURL("http://www.evil.com/phishing.html"), |
| 613 &matching_list, &prefix_hits, | 603 &prefix_hits, &full_hashes, now)); |
| 614 &full_hashes, now)); | |
| 615 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); | 604 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); |
| 616 EXPECT_EQ(prefix_hits.size(), 1U); | 605 EXPECT_EQ(prefix_hits.size(), 1U); |
| 617 | 606 |
| 618 EXPECT_FALSE(database_->ContainsBrowseUrl( | 607 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 619 GURL("http://www.evil.com/notevil1.html"), | 608 GURL("http://www.evil.com/notevil1.html"), |
| 620 &matching_list, &prefix_hits, | 609 &prefix_hits, &full_hashes, now)); |
| 621 &full_hashes, now)); | |
| 622 EXPECT_TRUE(prefix_hits.empty()); | 610 EXPECT_TRUE(prefix_hits.empty()); |
| 623 | 611 |
| 624 EXPECT_TRUE(database_->ContainsBrowseUrl( | 612 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 625 GURL("http://www.evil.com/notevil2.html"), | 613 GURL("http://www.evil.com/notevil2.html"), |
| 626 &matching_list, &prefix_hits, | 614 &prefix_hits, &full_hashes, now)); |
| 627 &full_hashes, now)); | |
| 628 | 615 |
| 629 EXPECT_TRUE(database_->ContainsBrowseUrl( | 616 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 630 GURL("http://www.good.com/good1.html"), | 617 GURL("http://www.good.com/good1.html"), |
| 631 &matching_list, &prefix_hits, | 618 &prefix_hits, &full_hashes, now)); |
| 632 &full_hashes, now)); | |
| 633 | 619 |
| 634 EXPECT_TRUE(database_->ContainsBrowseUrl( | 620 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 635 GURL("http://www.good.com/good2.html"), | 621 GURL("http://www.good.com/good2.html"), |
| 636 &matching_list, &prefix_hits, | 622 &prefix_hits, &full_hashes, now)); |
| 637 &full_hashes, now)); | |
| 638 | 623 |
| 639 GetListsInfo(&lists); | 624 GetListsInfo(&lists); |
| 640 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 625 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 641 EXPECT_EQ(lists[0].subs, "4"); | 626 EXPECT_EQ(lists[0].subs, "4"); |
| 642 | 627 |
| 643 // Test the same sub chunk again. This should be a no-op. | 628 // 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 | 629 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 645 chunk.hosts.clear(); | 630 chunk.hosts.clear(); |
| 646 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", | 631 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", |
| 647 "www.evil.com/notevil1.html"); | 632 "www.evil.com/notevil1.html"); |
| 648 chunks.clear(); | 633 chunks.clear(); |
| 649 chunks.push_back(chunk); | 634 chunks.push_back(chunk); |
| 650 | 635 |
| 651 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 636 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 652 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 637 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 653 database_->UpdateFinished(true); | 638 database_->UpdateFinished(true); |
| 654 | 639 |
| 655 GetListsInfo(&lists); | 640 GetListsInfo(&lists); |
| 656 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 641 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 657 EXPECT_EQ(lists[0].subs, "4"); | 642 EXPECT_EQ(lists[0].subs, "4"); |
| 658 | 643 |
| 659 // Test removing all the prefixes from an add chunk. | 644 // Test removing all the prefixes from an add chunk. |
| 660 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 645 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 661 AddDelChunk(safe_browsing_util::kMalwareList, 2); | 646 AddDelChunk(safe_browsing_util::kMalwareList, 2); |
| 662 database_->UpdateFinished(true); | 647 database_->UpdateFinished(true); |
| 663 | 648 |
| 664 EXPECT_FALSE(database_->ContainsBrowseUrl( | 649 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 665 GURL("http://www.evil.com/notevil2.html"), | 650 GURL("http://www.evil.com/notevil2.html"), |
| 666 &matching_list, &prefix_hits, | 651 &prefix_hits, &full_hashes, now)); |
| 667 &full_hashes, now)); | |
| 668 | 652 |
| 669 EXPECT_FALSE(database_->ContainsBrowseUrl( | 653 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 670 GURL("http://www.good.com/good1.html"), | 654 GURL("http://www.good.com/good1.html"), |
| 671 &matching_list, &prefix_hits, | 655 &prefix_hits, &full_hashes, now)); |
| 672 &full_hashes, now)); | |
| 673 | 656 |
| 674 EXPECT_FALSE(database_->ContainsBrowseUrl( | 657 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 675 GURL("http://www.good.com/good2.html"), | 658 GURL("http://www.good.com/good2.html"), |
| 676 &matching_list, &prefix_hits, | 659 &prefix_hits, &full_hashes, now)); |
| 677 &full_hashes, now)); | |
| 678 | 660 |
| 679 GetListsInfo(&lists); | 661 GetListsInfo(&lists); |
| 680 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 662 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 681 EXPECT_EQ(lists[0].adds, "1,3"); | 663 EXPECT_EQ(lists[0].adds, "1,3"); |
| 682 EXPECT_EQ(lists[0].subs, "4"); | 664 EXPECT_EQ(lists[0].subs, "4"); |
| 683 | 665 |
| 684 // The adddel command exposed a bug in the transaction code where any | 666 // 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 | 667 // transaction after it would fail. Add a dummy entry and remove it to |
| 686 // make sure the transcation works fine. | 668 // make sure the transcation works fine. |
| 687 chunk.hosts.clear(); | 669 chunk.hosts.clear(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 712 "www.notevilanymore.com/index.html", | 694 "www.notevilanymore.com/index.html", |
| 713 "www.notevilanymore.com/good.html"); | 695 "www.notevilanymore.com/good.html"); |
| 714 chunks.clear(); | 696 chunks.clear(); |
| 715 chunks.push_back(chunk); | 697 chunks.push_back(chunk); |
| 716 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 698 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 717 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 699 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 718 database_->UpdateFinished(true); | 700 database_->UpdateFinished(true); |
| 719 | 701 |
| 720 EXPECT_FALSE(database_->ContainsBrowseUrl( | 702 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 721 GURL("http://www.notevilanymore.com/index.html"), | 703 GURL("http://www.notevilanymore.com/index.html"), |
| 722 &matching_list, &prefix_hits, &full_hashes, now)); | 704 &prefix_hits, &full_hashes, now)); |
| 723 | 705 |
| 724 // Now insert the tardy add chunk and we don't expect them to appear | 706 // Now insert the tardy add chunk and we don't expect them to appear |
| 725 // in database because of the previous sub chunk. | 707 // in database because of the previous sub chunk. |
| 726 chunk.hosts.clear(); | 708 chunk.hosts.clear(); |
| 727 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", | 709 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", |
| 728 "www.notevilanymore.com/index.html", | 710 "www.notevilanymore.com/index.html", |
| 729 "www.notevilanymore.com/good.html"); | 711 "www.notevilanymore.com/good.html"); |
| 730 chunks.clear(); | 712 chunks.clear(); |
| 731 chunks.push_back(chunk); | 713 chunks.push_back(chunk); |
| 732 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 714 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 733 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 715 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 734 database_->UpdateFinished(true); | 716 database_->UpdateFinished(true); |
| 735 | 717 |
| 736 EXPECT_FALSE(database_->ContainsBrowseUrl( | 718 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 737 GURL("http://www.notevilanymore.com/index.html"), | 719 GURL("http://www.notevilanymore.com/index.html"), |
| 738 &matching_list, &prefix_hits, &full_hashes, now)); | 720 &prefix_hits, &full_hashes, now)); |
| 739 | 721 |
| 740 EXPECT_FALSE(database_->ContainsBrowseUrl( | 722 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 741 GURL("http://www.notevilanymore.com/good.html"), | 723 GURL("http://www.notevilanymore.com/good.html"), |
| 742 &matching_list, &prefix_hits, &full_hashes, now)); | 724 &prefix_hits, &full_hashes, now)); |
| 743 } | 725 } |
| 744 | 726 |
| 745 | 727 |
| 746 // Test adding zero length chunks to the database. | 728 // Test adding zero length chunks to the database. |
| 747 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { | 729 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { |
| 748 SBChunkList chunks; | 730 SBChunkList chunks; |
| 749 SBChunk chunk; | 731 SBChunk chunk; |
| 750 | 732 |
| 751 // Populate with a couple of normal chunks. | 733 // Populate with a couple of normal chunks. |
| 752 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/", | 734 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/", |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 "www.notempty.com/full2.html"); | 788 "www.notempty.com/full2.html"); |
| 807 chunks.push_back(empty_chunk); | 789 chunks.push_back(empty_chunk); |
| 808 | 790 |
| 809 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 791 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 810 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 792 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 811 database_->UpdateFinished(true); | 793 database_->UpdateFinished(true); |
| 812 | 794 |
| 813 const Time now = Time::Now(); | 795 const Time now = Time::Now(); |
| 814 std::vector<SBFullHashResult> full_hashes; | 796 std::vector<SBFullHashResult> full_hashes; |
| 815 std::vector<SBPrefix> prefix_hits; | 797 std::vector<SBPrefix> prefix_hits; |
| 816 std::string matching_list; | |
| 817 EXPECT_TRUE(database_->ContainsBrowseUrl( | 798 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 818 GURL("http://www.notempty.com/full1.html"), | 799 GURL("http://www.notempty.com/full1.html"), |
| 819 &matching_list, &prefix_hits, | 800 &prefix_hits, &full_hashes, now)); |
| 820 &full_hashes, now)); | |
| 821 EXPECT_TRUE(database_->ContainsBrowseUrl( | 801 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 822 GURL("http://www.notempty.com/full2.html"), | 802 GURL("http://www.notempty.com/full2.html"), |
| 823 &matching_list, &prefix_hits, | 803 &prefix_hits, &full_hashes, now)); |
| 824 &full_hashes, now)); | |
| 825 | 804 |
| 826 GetListsInfo(&lists); | 805 GetListsInfo(&lists); |
| 827 EXPECT_EQ(lists[0].adds, "1,10,19-22"); | 806 EXPECT_EQ(lists[0].adds, "1,10,19-22"); |
| 828 EXPECT_EQ(lists[0].subs, "7"); | 807 EXPECT_EQ(lists[0].subs, "7"); |
| 829 | 808 |
| 830 // Handle AddDel and SubDel commands for empty chunks. | 809 // Handle AddDel and SubDel commands for empty chunks. |
| 831 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 810 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 832 AddDelChunk(safe_browsing_util::kMalwareList, 21); | 811 AddDelChunk(safe_browsing_util::kMalwareList, 21); |
| 833 database_->UpdateFinished(true); | 812 database_->UpdateFinished(true); |
| 834 | 813 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 database_->CacheHashResults(prefixes, results); | 854 database_->CacheHashResults(prefixes, results); |
| 876 } | 855 } |
| 877 | 856 |
| 878 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 857 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 879 PopulateDatabaseForCacheTest(); | 858 PopulateDatabaseForCacheTest(); |
| 880 | 859 |
| 881 // We should have both full hashes in the cache. | 860 // We should have both full hashes in the cache. |
| 882 EXPECT_EQ(2U, database_->cached_browse_hashes_.size()); | 861 EXPECT_EQ(2U, database_->cached_browse_hashes_.size()); |
| 883 | 862 |
| 884 // Test the cache lookup for the first prefix. | 863 // Test the cache lookup for the first prefix. |
| 885 std::string listname; | |
| 886 std::vector<SBPrefix> prefixes; | 864 std::vector<SBPrefix> prefixes; |
| 887 std::vector<SBFullHashResult> full_hashes; | 865 std::vector<SBFullHashResult> full_hashes; |
| 888 database_->ContainsBrowseUrl( | 866 database_->ContainsBrowseUrl( |
| 889 GURL("http://www.evil.com/phishing.html"), | 867 GURL("http://www.evil.com/phishing.html"), |
| 890 &listname, &prefixes, &full_hashes, Time::Now()); | 868 &prefixes, &full_hashes, Time::Now()); |
| 891 ASSERT_EQ(1U, full_hashes.size()); | 869 ASSERT_EQ(1U, full_hashes.size()); |
| 892 EXPECT_TRUE( | 870 EXPECT_TRUE( |
| 893 SBFullHashEqual(full_hashes[0].hash, | 871 SBFullHashEqual(full_hashes[0].hash, |
| 894 SBFullHashForString("www.evil.com/phishing.html"))); | 872 SBFullHashForString("www.evil.com/phishing.html"))); |
| 895 | 873 |
| 896 prefixes.clear(); | 874 prefixes.clear(); |
| 897 full_hashes.clear(); | 875 full_hashes.clear(); |
| 898 | 876 |
| 899 // Test the cache lookup for the second prefix. | 877 // Test the cache lookup for the second prefix. |
| 900 database_->ContainsBrowseUrl( | 878 database_->ContainsBrowseUrl( |
| 901 GURL("http://www.evil.com/malware.html"), | 879 GURL("http://www.evil.com/malware.html"), |
| 902 &listname, &prefixes, &full_hashes, Time::Now()); | 880 &prefixes, &full_hashes, Time::Now()); |
| 903 ASSERT_EQ(1U, full_hashes.size()); | 881 ASSERT_EQ(1U, full_hashes.size()); |
| 904 EXPECT_TRUE( | 882 EXPECT_TRUE( |
| 905 SBFullHashEqual(full_hashes[0].hash, | 883 SBFullHashEqual(full_hashes[0].hash, |
| 906 SBFullHashForString("www.evil.com/malware.html"))); | 884 SBFullHashForString("www.evil.com/malware.html"))); |
| 907 | 885 |
| 908 prefixes.clear(); | 886 prefixes.clear(); |
| 909 full_hashes.clear(); | 887 full_hashes.clear(); |
| 910 | 888 |
| 911 // Test removing a prefix via a sub chunk. | 889 // Test removing a prefix via a sub chunk. |
| 912 SBChunk chunk; | 890 SBChunk chunk; |
| 913 SBChunkList chunks; | 891 SBChunkList chunks; |
| 914 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", | 892 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", |
| 915 "www.evil.com/phishing.html"); | 893 "www.evil.com/phishing.html"); |
| 916 chunks.push_back(chunk); | 894 chunks.push_back(chunk); |
| 917 | 895 |
| 918 std::vector<SBListChunkRanges> lists; | 896 std::vector<SBListChunkRanges> lists; |
| 919 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 897 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 920 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 898 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 921 database_->UpdateFinished(true); | 899 database_->UpdateFinished(true); |
| 922 | 900 |
| 923 // This prefix should still be there, but the fullhash is gone. | 901 // This prefix should still be there, but the fullhash is gone. |
| 924 EXPECT_TRUE(database_->ContainsBrowseUrl( | 902 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 925 GURL("http://www.evil.com/malware.html"), | 903 GURL("http://www.evil.com/malware.html"), |
| 926 &listname, &prefixes, &full_hashes, Time::Now())); | 904 &prefixes, &full_hashes, Time::Now())); |
| 927 ASSERT_EQ(1U, prefixes.size()); | 905 ASSERT_EQ(1U, prefixes.size()); |
| 928 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefixes[0]); | 906 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefixes[0]); |
| 929 EXPECT_TRUE(full_hashes.empty()); | 907 EXPECT_TRUE(full_hashes.empty()); |
| 930 prefixes.clear(); | 908 prefixes.clear(); |
| 931 full_hashes.clear(); | 909 full_hashes.clear(); |
| 932 | 910 |
| 933 // This prefix should be gone. | 911 // This prefix should be gone. |
| 934 database_->ContainsBrowseUrl( | 912 database_->ContainsBrowseUrl( |
| 935 GURL("http://www.evil.com/phishing.html"), | 913 GURL("http://www.evil.com/phishing.html"), |
| 936 &listname, &prefixes, &full_hashes, Time::Now()); | 914 &prefixes, &full_hashes, Time::Now()); |
| 937 EXPECT_TRUE(full_hashes.empty()); | 915 EXPECT_TRUE(full_hashes.empty()); |
| 938 | 916 |
| 939 prefixes.clear(); | 917 prefixes.clear(); |
| 940 full_hashes.clear(); | 918 full_hashes.clear(); |
| 941 | 919 |
| 942 // Test that an AddDel for the original chunk removes the last cached entry. | 920 // Test that an AddDel for the original chunk removes the last cached entry. |
| 943 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 921 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 944 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 922 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 945 database_->UpdateFinished(true); | 923 database_->UpdateFinished(true); |
| 946 database_->ContainsBrowseUrl( | 924 database_->ContainsBrowseUrl( |
| 947 GURL("http://www.evil.com/malware.html"), | 925 GURL("http://www.evil.com/malware.html"), |
| 948 &listname, &prefixes, &full_hashes, Time::Now()); | 926 &prefixes, &full_hashes, Time::Now()); |
| 949 EXPECT_TRUE(full_hashes.empty()); | 927 EXPECT_TRUE(full_hashes.empty()); |
| 950 EXPECT_TRUE(database_->full_browse_hashes_.empty()); | 928 EXPECT_TRUE(database_->full_browse_hashes_.empty()); |
| 951 EXPECT_TRUE(database_->cached_browse_hashes_.empty()); | 929 EXPECT_TRUE(database_->cached_browse_hashes_.empty()); |
| 952 | 930 |
| 953 prefixes.clear(); | 931 prefixes.clear(); |
| 954 full_hashes.clear(); | 932 full_hashes.clear(); |
| 955 | 933 |
| 956 // Test that the cache won't return expired values. First we have to adjust | 934 // Test that the cache won't return expired values. First we have to adjust |
| 957 // the cached entries' received time to make them older, since the database | 935 // the cached entries' received time to make them older, since the database |
| 958 // cache insert uses Time::Now(). First, store some entries. | 936 // cache insert uses Time::Now(). First, store some entries. |
| 959 PopulateDatabaseForCacheTest(); | 937 PopulateDatabaseForCacheTest(); |
| 960 | 938 |
| 961 std::vector<SBFullHashCached>* hash_cache = &database_->cached_browse_hashes_; | 939 std::vector<SBFullHashCached>* hash_cache = &database_->cached_browse_hashes_; |
| 962 EXPECT_EQ(2U, hash_cache->size()); | 940 EXPECT_EQ(2U, hash_cache->size()); |
| 963 | 941 |
| 964 // Now adjust one of the entries times to be in the past. | 942 // Now adjust one of the entries times to be in the past. |
| 965 base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60); | 943 base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60); |
| 966 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); | 944 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); |
| 967 std::vector<SBFullHashCached>::iterator iter; | 945 std::vector<SBFullHashCached>::iterator iter; |
| 968 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) { | 946 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) { |
| 969 if (iter->hash.prefix == key) { | 947 if (iter->hash.prefix == key) { |
| 970 iter->received = static_cast<int32>(expired.ToTimeT()); | 948 iter->received = static_cast<int32>(expired.ToTimeT()); |
| 971 break; | 949 break; |
| 972 } | 950 } |
| 973 } | 951 } |
| 974 EXPECT_TRUE(iter != hash_cache->end()); | 952 EXPECT_TRUE(iter != hash_cache->end()); |
| 975 | 953 |
| 976 database_->ContainsBrowseUrl( | 954 database_->ContainsBrowseUrl( |
| 977 GURL("http://www.evil.com/malware.html"), | 955 GURL("http://www.evil.com/malware.html"), |
| 978 &listname, &prefixes, &full_hashes, expired); | 956 &prefixes, &full_hashes, expired); |
| 979 EXPECT_TRUE(full_hashes.empty()); | 957 EXPECT_TRUE(full_hashes.empty()); |
| 980 | 958 |
| 981 // This entry should still exist. | 959 // This entry should still exist. |
| 982 database_->ContainsBrowseUrl( | 960 database_->ContainsBrowseUrl( |
| 983 GURL("http://www.evil.com/phishing.html"), | 961 GURL("http://www.evil.com/phishing.html"), |
| 984 &listname, &prefixes, &full_hashes, expired); | 962 &prefixes, &full_hashes, expired); |
| 985 EXPECT_EQ(1U, full_hashes.size()); | 963 EXPECT_EQ(1U, full_hashes.size()); |
| 986 | 964 |
| 987 // Testing prefix miss caching. First, we clear out the existing database, | 965 // Testing prefix miss caching. First, we clear out the existing database, |
| 988 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate | 966 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate |
| 989 // chunks. | 967 // chunks. |
| 990 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 968 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 991 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 969 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 992 database_->UpdateFinished(true); | 970 database_->UpdateFinished(true); |
| 993 | 971 |
| 994 std::vector<SBPrefix> prefix_misses; | 972 std::vector<SBPrefix> prefix_misses; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1011 // in the database, it is flagged as a miss so looking up the associated URL | 989 // in the database, it is flagged as a miss so looking up the associated URL |
| 1012 // will not succeed. | 990 // will not succeed. |
| 1013 prefixes.clear(); | 991 prefixes.clear(); |
| 1014 full_hashes.clear(); | 992 full_hashes.clear(); |
| 1015 prefix_misses.clear(); | 993 prefix_misses.clear(); |
| 1016 empty_full_hash.clear(); | 994 empty_full_hash.clear(); |
| 1017 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); | 995 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); |
| 1018 database_->CacheHashResults(prefix_misses, empty_full_hash); | 996 database_->CacheHashResults(prefix_misses, empty_full_hash); |
| 1019 EXPECT_FALSE(database_->ContainsBrowseUrl( | 997 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1020 GURL("http://www.evil.com/phishing.html"), | 998 GURL("http://www.evil.com/phishing.html"), |
| 1021 &listname, &prefixes, | 999 &prefixes, &full_hashes, Time::Now())); |
| 1022 &full_hashes, Time::Now())); | |
| 1023 | 1000 |
| 1024 prefixes.clear(); | 1001 prefixes.clear(); |
| 1025 full_hashes.clear(); | 1002 full_hashes.clear(); |
| 1026 | 1003 |
| 1027 // Test receiving a full add chunk. | 1004 // Test receiving a full add chunk. |
| 1028 chunk.hosts.clear(); | 1005 chunk.hosts.clear(); |
| 1029 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", | 1006 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", |
| 1030 "www.fullevil.com/bad1.html", | 1007 "www.fullevil.com/bad1.html", |
| 1031 "www.fullevil.com/bad2.html"); | 1008 "www.fullevil.com/bad2.html"); |
| 1032 chunks.clear(); | 1009 chunks.clear(); |
| 1033 chunks.push_back(chunk); | 1010 chunks.push_back(chunk); |
| 1034 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1011 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1035 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1012 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1036 database_->UpdateFinished(true); | 1013 database_->UpdateFinished(true); |
| 1037 | 1014 |
| 1038 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1015 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1039 GURL("http://www.fullevil.com/bad1.html"), | 1016 GURL("http://www.fullevil.com/bad1.html"), |
| 1040 &listname, &prefixes, &full_hashes, | 1017 &prefixes, &full_hashes, Time::Now())); |
| 1041 Time::Now())); | |
| 1042 ASSERT_EQ(1U, full_hashes.size()); | 1018 ASSERT_EQ(1U, full_hashes.size()); |
| 1043 EXPECT_TRUE( | 1019 EXPECT_TRUE( |
| 1044 SBFullHashEqual(full_hashes[0].hash, | 1020 SBFullHashEqual(full_hashes[0].hash, |
| 1045 SBFullHashForString("www.fullevil.com/bad1.html"))); | 1021 SBFullHashForString("www.fullevil.com/bad1.html"))); |
| 1046 prefixes.clear(); | 1022 prefixes.clear(); |
| 1047 full_hashes.clear(); | 1023 full_hashes.clear(); |
| 1048 | 1024 |
| 1049 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1025 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1050 GURL("http://www.fullevil.com/bad2.html"), | 1026 GURL("http://www.fullevil.com/bad2.html"), |
| 1051 &listname, &prefixes, &full_hashes, | 1027 &prefixes, &full_hashes, Time::Now())); |
| 1052 Time::Now())); | |
| 1053 ASSERT_EQ(1U, full_hashes.size()); | 1028 ASSERT_EQ(1U, full_hashes.size()); |
| 1054 EXPECT_TRUE( | 1029 EXPECT_TRUE( |
| 1055 SBFullHashEqual(full_hashes[0].hash, | 1030 SBFullHashEqual(full_hashes[0].hash, |
| 1056 SBFullHashForString("www.fullevil.com/bad2.html"))); | 1031 SBFullHashForString("www.fullevil.com/bad2.html"))); |
| 1057 prefixes.clear(); | 1032 prefixes.clear(); |
| 1058 full_hashes.clear(); | 1033 full_hashes.clear(); |
| 1059 | 1034 |
| 1060 // Test receiving a full sub chunk, which will remove one of the full adds. | 1035 // Test receiving a full sub chunk, which will remove one of the full adds. |
| 1061 chunk.hosts.clear(); | 1036 chunk.hosts.clear(); |
| 1062 InsertSubChunkHostFullHash(&chunk, 200, 20, | 1037 InsertSubChunkHostFullHash(&chunk, 200, 20, |
| 1063 "www.fullevil.com/", | 1038 "www.fullevil.com/", |
| 1064 "www.fullevil.com/bad1.html"); | 1039 "www.fullevil.com/bad1.html"); |
| 1065 chunks.clear(); | 1040 chunks.clear(); |
| 1066 chunks.push_back(chunk); | 1041 chunks.push_back(chunk); |
| 1067 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1042 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1068 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1043 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1069 database_->UpdateFinished(true); | 1044 database_->UpdateFinished(true); |
| 1070 | 1045 |
| 1071 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1046 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1072 GURL("http://www.fullevil.com/bad1.html"), | 1047 GURL("http://www.fullevil.com/bad1.html"), |
| 1073 &listname, &prefixes, &full_hashes, | 1048 &prefixes, &full_hashes, Time::Now())); |
| 1074 Time::Now())); | |
| 1075 EXPECT_TRUE(full_hashes.empty()); | 1049 EXPECT_TRUE(full_hashes.empty()); |
| 1076 | 1050 |
| 1077 // There should be one remaining full add. | 1051 // There should be one remaining full add. |
| 1078 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1052 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1079 GURL("http://www.fullevil.com/bad2.html"), | 1053 GURL("http://www.fullevil.com/bad2.html"), |
| 1080 &listname, &prefixes, &full_hashes, | 1054 &prefixes, &full_hashes, Time::Now())); |
| 1081 Time::Now())); | |
| 1082 ASSERT_EQ(1U, full_hashes.size()); | 1055 ASSERT_EQ(1U, full_hashes.size()); |
| 1083 EXPECT_TRUE( | 1056 EXPECT_TRUE( |
| 1084 SBFullHashEqual(full_hashes[0].hash, | 1057 SBFullHashEqual(full_hashes[0].hash, |
| 1085 SBFullHashForString("www.fullevil.com/bad2.html"))); | 1058 SBFullHashForString("www.fullevil.com/bad2.html"))); |
| 1086 prefixes.clear(); | 1059 prefixes.clear(); |
| 1087 full_hashes.clear(); | 1060 full_hashes.clear(); |
| 1088 | 1061 |
| 1089 // Now test an AddDel for the remaining full add. | 1062 // Now test an AddDel for the remaining full add. |
| 1090 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1063 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1091 AddDelChunk(safe_browsing_util::kMalwareList, 20); | 1064 AddDelChunk(safe_browsing_util::kMalwareList, 20); |
| 1092 database_->UpdateFinished(true); | 1065 database_->UpdateFinished(true); |
| 1093 | 1066 |
| 1094 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1067 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1095 GURL("http://www.fullevil.com/bad1.html"), | 1068 GURL("http://www.fullevil.com/bad1.html"), |
| 1096 &listname, &prefixes, &full_hashes, | 1069 &prefixes, &full_hashes, Time::Now())); |
| 1097 Time::Now())); | |
| 1098 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1070 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1099 GURL("http://www.fullevil.com/bad2.html"), | 1071 GURL("http://www.fullevil.com/bad2.html"), |
| 1100 &listname, &prefixes, &full_hashes, | 1072 &prefixes, &full_hashes, Time::Now())); |
| 1101 Time::Now())); | |
| 1102 } | 1073 } |
| 1103 | 1074 |
| 1104 // Test that corrupt databases are appropriately handled, even if the | 1075 // Test that corrupt databases are appropriately handled, even if the |
| 1105 // corruption is detected in the midst of the update. | 1076 // corruption is detected in the midst of the update. |
| 1106 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. | 1077 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. |
| 1107 // http://crbug.com/56448 | 1078 // http://crbug.com/56448 |
| 1108 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { | 1079 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { |
| 1109 // Re-create the database in a captive message loop so that we can | 1080 // Re-create the database in a captive message loop so that we can |
| 1110 // influence task-posting. Database specifically needs to the | 1081 // influence task-posting. Database specifically needs to the |
| 1111 // file-backed. | 1082 // file-backed. |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1541 GetListsInfo(&lists); | 1512 GetListsInfo(&lists); |
| 1542 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); | 1513 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); |
| 1543 EXPECT_EQ("1", lists[0].adds); | 1514 EXPECT_EQ("1", lists[0].adds); |
| 1544 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); | 1515 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); |
| 1545 EXPECT_EQ("47", lists[1].adds); | 1516 EXPECT_EQ("47", lists[1].adds); |
| 1546 | 1517 |
| 1547 const Time now = Time::Now(); | 1518 const Time now = Time::Now(); |
| 1548 std::vector<SBPrefix> prefixes; | 1519 std::vector<SBPrefix> prefixes; |
| 1549 std::vector<SBFullHashResult> full_hashes; | 1520 std::vector<SBFullHashResult> full_hashes; |
| 1550 std::vector<SBPrefix> prefix_hits; | 1521 std::vector<SBPrefix> prefix_hits; |
| 1551 std::string matching_list; | |
| 1552 std::string listname; | |
| 1553 | 1522 |
| 1554 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1523 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1555 GURL("http://www.evil.com/malware1.html"), | 1524 GURL("http://www.evil.com/malware1.html"), |
| 1556 &listname, &prefixes, &full_hashes, now)); | 1525 &prefixes, &full_hashes, now)); |
| 1557 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1526 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1558 GURL("http://www.evil.com/malware2.html"), | 1527 GURL("http://www.evil.com/malware2.html"), |
| 1559 &listname, &prefixes, &full_hashes, now)); | 1528 &prefixes, &full_hashes, now)); |
| 1560 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1529 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1561 GURL("http://www.evil.com/phishing1.html"), | 1530 GURL("http://www.evil.com/phishing1.html"), |
| 1562 &listname, &prefixes, &full_hashes, now)); | 1531 &prefixes, &full_hashes, now)); |
| 1563 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1532 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1564 GURL("http://www.evil.com/phishing2.html"), | 1533 GURL("http://www.evil.com/phishing2.html"), |
| 1565 &listname, &prefixes, &full_hashes, now)); | 1534 &prefixes, &full_hashes, now)); |
| 1566 | 1535 |
| 1567 // Test removing a single prefix from the add chunk. | 1536 // Test removing a single prefix from the add chunk. |
| 1568 // Remove the prefix that added first. | 1537 // Remove the prefix that added first. |
| 1569 chunk.hosts.clear(); | 1538 chunk.hosts.clear(); |
| 1570 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", | 1539 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", |
| 1571 "www.evil.com/malware1.html"); | 1540 "www.evil.com/malware1.html"); |
| 1572 chunks.clear(); | 1541 chunks.clear(); |
| 1573 chunks.push_back(chunk); | 1542 chunks.push_back(chunk); |
| 1574 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1543 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1575 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1544 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1576 database_->UpdateFinished(true); | 1545 database_->UpdateFinished(true); |
| 1577 | 1546 |
| 1578 // Remove the prefix that added last. | 1547 // Remove the prefix that added last. |
| 1579 chunk.hosts.clear(); | 1548 chunk.hosts.clear(); |
| 1580 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", | 1549 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", |
| 1581 "www.evil.com/phishing2.html"); | 1550 "www.evil.com/phishing2.html"); |
| 1582 chunks.clear(); | 1551 chunks.clear(); |
| 1583 chunks.push_back(chunk); | 1552 chunks.push_back(chunk); |
| 1584 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1553 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1585 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); | 1554 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); |
| 1586 database_->UpdateFinished(true); | 1555 database_->UpdateFinished(true); |
| 1587 | 1556 |
| 1588 // Verify that the database contains urls expected. | 1557 // Verify that the database contains urls expected. |
| 1589 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1558 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1590 GURL("http://www.evil.com/malware1.html"), | 1559 GURL("http://www.evil.com/malware1.html"), |
| 1591 &listname, &prefixes, &full_hashes, now)); | 1560 &prefixes, &full_hashes, now)); |
| 1592 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1561 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1593 GURL("http://www.evil.com/malware2.html"), | 1562 GURL("http://www.evil.com/malware2.html"), |
| 1594 &listname, &prefixes, &full_hashes, now)); | 1563 &prefixes, &full_hashes, now)); |
| 1595 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1564 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1596 GURL("http://www.evil.com/phishing1.html"), | 1565 GURL("http://www.evil.com/phishing1.html"), |
| 1597 &listname, &prefixes, &full_hashes, now)); | 1566 &prefixes, &full_hashes, now)); |
| 1598 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1567 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1599 GURL("http://www.evil.com/phishing2.html"), | 1568 GURL("http://www.evil.com/phishing2.html"), |
| 1600 &listname, &prefixes, &full_hashes, now)); | 1569 &prefixes, &full_hashes, now)); |
| 1601 } | 1570 } |
| 1602 | 1571 |
| 1603 // Test that an empty update doesn't actually update the database. | 1572 // Test that an empty update doesn't actually update the database. |
| 1604 // This isn't a functionality requirement, but it is a useful | 1573 // This isn't a functionality requirement, but it is a useful |
| 1605 // optimization. | 1574 // optimization. |
| 1606 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { | 1575 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { |
| 1607 SBChunkList chunks; | 1576 SBChunkList chunks; |
| 1608 SBChunk chunk; | 1577 SBChunk chunk; |
| 1609 | 1578 |
| 1610 base::FilePath filename = database_->BrowseDBFilename(database_filename_); | 1579 base::FilePath filename = database_->BrowseDBFilename(database_filename_); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1679 chunks.clear(); | 1648 chunks.clear(); |
| 1680 chunks.push_back(chunk); | 1649 chunks.push_back(chunk); |
| 1681 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1650 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1682 database_->UpdateFinished(true); | 1651 database_->UpdateFinished(true); |
| 1683 } | 1652 } |
| 1684 | 1653 |
| 1685 // Find the malware url in the database, don't find a good url. | 1654 // Find the malware url in the database, don't find a good url. |
| 1686 const Time now = Time::Now(); | 1655 const Time now = Time::Now(); |
| 1687 std::vector<SBFullHashResult> full_hashes; | 1656 std::vector<SBFullHashResult> full_hashes; |
| 1688 std::vector<SBPrefix> prefix_hits; | 1657 std::vector<SBPrefix> prefix_hits; |
| 1689 std::string matching_list; | |
| 1690 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1658 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1691 GURL("http://www.evil.com/malware.html"), | 1659 GURL("http://www.evil.com/malware.html"), |
| 1692 &matching_list, &prefix_hits, &full_hashes, now)); | 1660 &prefix_hits, &full_hashes, now)); |
| 1693 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1661 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1694 GURL("http://www.good.com/goodware.html"), | 1662 GURL("http://www.good.com/goodware.html"), |
| 1695 &matching_list, &prefix_hits, &full_hashes, now)); | 1663 &prefix_hits, &full_hashes, now)); |
| 1696 | 1664 |
| 1697 base::FilePath filter_file = database_->PrefixSetForFilename( | 1665 base::FilePath filter_file = database_->PrefixSetForFilename( |
| 1698 database_->BrowseDBFilename(database_filename_)); | 1666 database_->BrowseDBFilename(database_filename_)); |
| 1699 | 1667 |
| 1700 // After re-creating the database, it should have a filter read from | 1668 // After re-creating the database, it should have a filter read from |
| 1701 // a file, so it should find the same results. | 1669 // a file, so it should find the same results. |
| 1702 ASSERT_TRUE(base::PathExists(filter_file)); | 1670 ASSERT_TRUE(base::PathExists(filter_file)); |
| 1703 database_.reset(new SafeBrowsingDatabaseNew); | 1671 database_.reset(new SafeBrowsingDatabaseNew); |
| 1704 database_->Init(database_filename_); | 1672 database_->Init(database_filename_); |
| 1705 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1673 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1706 GURL("http://www.evil.com/malware.html"), | 1674 GURL("http://www.evil.com/malware.html"), |
| 1707 &matching_list, &prefix_hits, &full_hashes, now)); | 1675 &prefix_hits, &full_hashes, now)); |
| 1708 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1676 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1709 GURL("http://www.good.com/goodware.html"), | 1677 GURL("http://www.good.com/goodware.html"), |
| 1710 &matching_list, &prefix_hits, &full_hashes, now)); | 1678 &prefix_hits, &full_hashes, now)); |
| 1711 | 1679 |
| 1712 // If there is no filter file, the database cannot find malware urls. | 1680 // If there is no filter file, the database cannot find malware urls. |
| 1713 base::DeleteFile(filter_file, false); | 1681 base::DeleteFile(filter_file, false); |
| 1714 ASSERT_FALSE(base::PathExists(filter_file)); | 1682 ASSERT_FALSE(base::PathExists(filter_file)); |
| 1715 database_.reset(new SafeBrowsingDatabaseNew); | 1683 database_.reset(new SafeBrowsingDatabaseNew); |
| 1716 database_->Init(database_filename_); | 1684 database_->Init(database_filename_); |
| 1717 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1685 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1718 GURL("http://www.evil.com/malware.html"), | 1686 GURL("http://www.evil.com/malware.html"), |
| 1719 &matching_list, &prefix_hits, &full_hashes, now)); | 1687 &prefix_hits, &full_hashes, now)); |
| 1720 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1688 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1721 GURL("http://www.good.com/goodware.html"), | 1689 GURL("http://www.good.com/goodware.html"), |
| 1722 &matching_list, &prefix_hits, &full_hashes, now)); | 1690 &prefix_hits, &full_hashes, now)); |
| 1723 } | 1691 } |
| 1724 | 1692 |
| 1725 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { | 1693 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { |
| 1726 database_.reset(); | 1694 database_.reset(); |
| 1727 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 1695 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); |
| 1728 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | 1696 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); |
| 1729 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | 1697 database_.reset(new SafeBrowsingDatabaseNew(browse_store, |
| 1730 NULL, | 1698 NULL, |
| 1731 NULL, | 1699 NULL, |
| 1732 NULL, | 1700 NULL, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1812 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); | 1780 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); |
| 1813 | 1781 |
| 1814 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); | 1782 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); |
| 1815 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); | 1783 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); |
| 1816 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); | 1784 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); |
| 1817 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); | 1785 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); |
| 1818 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); | 1786 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); |
| 1819 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); | 1787 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); |
| 1820 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); | 1788 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); |
| 1821 } | 1789 } |
| OLD | NEW |