Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_database_unittest.cc

Issue 280013002: [safe browsing] Switch to independent cache lifetimes for gethash items. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Bah, just use base::Time and be done. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/sha1.h" 11 #include "base/sha1.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 13 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" 14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "crypto/sha2.h" 16 #include "crypto/sha2.h"
17 #include "net/base/net_util.h" 17 #include "net/base/net_util.h"
18 #include "sql/connection.h" 18 #include "sql/connection.h"
19 #include "sql/statement.h" 19 #include "sql/statement.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h" 21 #include "testing/platform_test.h"
22 #include "url/gurl.h" 22 #include "url/gurl.h"
23 23
24 using base::Time; 24 using base::Time;
25 using base::TimeDelta;
25 26
26 namespace { 27 namespace {
27 28
29 TimeDelta kCacheLifetime = TimeDelta::FromMinutes(45);
mattm 2014/05/12 21:56:27 const?
Scott Hess - ex-Googler 2014/05/12 23:10:48 Sensible!
30
28 SBPrefix SBPrefixForString(const std::string& str) { 31 SBPrefix SBPrefixForString(const std::string& str) {
29 return SBFullHashForString(str).prefix; 32 return SBFullHashForString(str).prefix;
30 } 33 }
31 34
32 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { 35 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) {
33 net::IPAddressNumber ip_number; 36 net::IPAddressNumber ip_number;
34 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); 37 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number));
35 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); 38 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size());
36 const std::string hashed_ip_prefix = base::SHA1HashString( 39 const std::string hashed_ip_prefix = base::SHA1HashString(
37 net::IPAddressToPackedString(ip_number)); 40 net::IPAddressToPackedString(ip_number));
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 548 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
546 549
547 database_->UpdateFinished(true); 550 database_->UpdateFinished(true);
548 551
549 // Make sure they were added correctly. 552 // Make sure they were added correctly.
550 GetListsInfo(&lists); 553 GetListsInfo(&lists);
551 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 554 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
552 EXPECT_EQ(lists[0].adds, "1-3,7"); 555 EXPECT_EQ(lists[0].adds, "1-3,7");
553 EXPECT_TRUE(lists[0].subs.empty()); 556 EXPECT_TRUE(lists[0].subs.empty());
554 557
555 const Time now = Time::Now();
556 std::vector<SBFullHashResult> cached_hashes; 558 std::vector<SBFullHashResult> cached_hashes;
557 std::vector<SBPrefix> prefix_hits; 559 std::vector<SBPrefix> prefix_hits;
558 EXPECT_TRUE(database_->ContainsBrowseUrl( 560 EXPECT_TRUE(database_->ContainsBrowseUrl(
559 GURL("http://www.evil.com/phishing.html"), 561 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cached_hashes));
560 &prefix_hits, &cached_hashes, now));
561 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); 562 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html"));
562 EXPECT_EQ(prefix_hits.size(), 1U); 563 EXPECT_EQ(prefix_hits.size(), 1U);
563 564
564 EXPECT_TRUE(database_->ContainsBrowseUrl( 565 EXPECT_TRUE(database_->ContainsBrowseUrl(
565 GURL("http://www.evil.com/malware.html"), 566 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cached_hashes));
566 &prefix_hits, &cached_hashes, now));
567 567
568 EXPECT_TRUE(database_->ContainsBrowseUrl( 568 EXPECT_TRUE(database_->ContainsBrowseUrl(
569 GURL("http://www.evil.com/notevil1.html"), 569 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cached_hashes));
570 &prefix_hits, &cached_hashes, now));
571 570
572 EXPECT_TRUE(database_->ContainsBrowseUrl( 571 EXPECT_TRUE(database_->ContainsBrowseUrl(
573 GURL("http://www.evil.com/notevil2.html"), 572 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cached_hashes));
574 &prefix_hits, &cached_hashes, now));
575 573
576 EXPECT_TRUE(database_->ContainsBrowseUrl( 574 EXPECT_TRUE(database_->ContainsBrowseUrl(
577 GURL("http://www.good.com/good1.html"), 575 GURL("http://www.good.com/good1.html"), &prefix_hits, &cached_hashes));
578 &prefix_hits, &cached_hashes, now));
579 576
580 EXPECT_TRUE(database_->ContainsBrowseUrl( 577 EXPECT_TRUE(database_->ContainsBrowseUrl(
581 GURL("http://www.good.com/good2.html"), 578 GURL("http://www.good.com/good2.html"), &prefix_hits, &cached_hashes));
582 &prefix_hits, &cached_hashes, now));
583 579
584 EXPECT_TRUE(database_->ContainsBrowseUrl( 580 EXPECT_TRUE(database_->ContainsBrowseUrl(
585 GURL("http://192.168.0.1/malware.html"), 581 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cached_hashes));
586 &prefix_hits, &cached_hashes, now));
587 582
588 EXPECT_FALSE(database_->ContainsBrowseUrl( 583 EXPECT_FALSE(database_->ContainsBrowseUrl(
589 GURL("http://www.evil.com/"), 584 GURL("http://www.evil.com/"), &prefix_hits, &cached_hashes));
590 &prefix_hits, &cached_hashes, now));
591 EXPECT_TRUE(prefix_hits.empty()); 585 EXPECT_TRUE(prefix_hits.empty());
592 586
593 EXPECT_FALSE(database_->ContainsBrowseUrl( 587 EXPECT_FALSE(database_->ContainsBrowseUrl(
594 GURL("http://www.evil.com/robots.txt"), 588 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cached_hashes));
595 &prefix_hits, &cached_hashes, now));
596 589
597 EXPECT_TRUE(database_->ContainsBrowseUrl( 590 EXPECT_TRUE(database_->ContainsBrowseUrl(
598 GURL("http://www.evil.com/evil.html"), 591 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cached_hashes));
599 &prefix_hits, &cached_hashes, now));
600 ASSERT_EQ(1U, prefix_hits.size()); 592 ASSERT_EQ(1U, prefix_hits.size());
601 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/evil.html")); 593 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/evil.html"));
602 594
603 // Attempt to re-add the first chunk (should be a no-op). 595 // Attempt to re-add the first chunk (should be a no-op).
604 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 596 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
605 chunk.hosts.clear(); 597 chunk.hosts.clear();
606 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", 598 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/",
607 "www.evil.com/phishing.html", 599 "www.evil.com/phishing.html",
608 "www.evil.com/malware.html"); 600 "www.evil.com/malware.html");
609 chunks.clear(); 601 chunks.clear();
(...skipping 12 matching lines...) Expand all
622 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", 614 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/",
623 "www.evil.com/notevil1.html"); 615 "www.evil.com/notevil1.html");
624 chunks.clear(); 616 chunks.clear();
625 chunks.push_back(chunk); 617 chunks.push_back(chunk);
626 EXPECT_TRUE(database_->UpdateStarted(&lists)); 618 EXPECT_TRUE(database_->UpdateStarted(&lists));
627 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 619 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
628 620
629 database_->UpdateFinished(true); 621 database_->UpdateFinished(true);
630 622
631 EXPECT_TRUE(database_->ContainsBrowseUrl( 623 EXPECT_TRUE(database_->ContainsBrowseUrl(
632 GURL("http://www.evil.com/phishing.html"), 624 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cached_hashes));
633 &prefix_hits, &cached_hashes, now));
634 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); 625 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html"));
635 EXPECT_EQ(prefix_hits.size(), 1U); 626 EXPECT_EQ(prefix_hits.size(), 1U);
636 627
637 EXPECT_FALSE(database_->ContainsBrowseUrl( 628 EXPECT_FALSE(database_->ContainsBrowseUrl(
638 GURL("http://www.evil.com/notevil1.html"), 629 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cached_hashes));
639 &prefix_hits, &cached_hashes, now));
640 EXPECT_TRUE(prefix_hits.empty()); 630 EXPECT_TRUE(prefix_hits.empty());
641 631
642 EXPECT_TRUE(database_->ContainsBrowseUrl( 632 EXPECT_TRUE(database_->ContainsBrowseUrl(
643 GURL("http://www.evil.com/notevil2.html"), 633 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cached_hashes));
644 &prefix_hits, &cached_hashes, now));
645 634
646 EXPECT_TRUE(database_->ContainsBrowseUrl( 635 EXPECT_TRUE(database_->ContainsBrowseUrl(
647 GURL("http://www.good.com/good1.html"), 636 GURL("http://www.good.com/good1.html"), &prefix_hits, &cached_hashes));
648 &prefix_hits, &cached_hashes, now));
649 637
650 EXPECT_TRUE(database_->ContainsBrowseUrl( 638 EXPECT_TRUE(database_->ContainsBrowseUrl(
651 GURL("http://www.good.com/good2.html"), 639 GURL("http://www.good.com/good2.html"), &prefix_hits, &cached_hashes));
652 &prefix_hits, &cached_hashes, now));
653 640
654 GetListsInfo(&lists); 641 GetListsInfo(&lists);
655 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 642 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
656 EXPECT_EQ(lists[0].subs, "4"); 643 EXPECT_EQ(lists[0].subs, "4");
657 644
658 // Test the same sub chunk again. This should be a no-op. 645 // Test the same sub chunk again. This should be a no-op.
659 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 646 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
660 chunk.hosts.clear(); 647 chunk.hosts.clear();
661 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", 648 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/",
662 "www.evil.com/notevil1.html"); 649 "www.evil.com/notevil1.html");
663 chunks.clear(); 650 chunks.clear();
664 chunks.push_back(chunk); 651 chunks.push_back(chunk);
665 652
666 EXPECT_TRUE(database_->UpdateStarted(&lists)); 653 EXPECT_TRUE(database_->UpdateStarted(&lists));
667 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 654 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
668 database_->UpdateFinished(true); 655 database_->UpdateFinished(true);
669 656
670 GetListsInfo(&lists); 657 GetListsInfo(&lists);
671 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 658 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
672 EXPECT_EQ(lists[0].subs, "4"); 659 EXPECT_EQ(lists[0].subs, "4");
673 660
674 // Test removing all the prefixes from an add chunk. 661 // Test removing all the prefixes from an add chunk.
675 EXPECT_TRUE(database_->UpdateStarted(&lists)); 662 EXPECT_TRUE(database_->UpdateStarted(&lists));
676 AddDelChunk(safe_browsing_util::kMalwareList, 2); 663 AddDelChunk(safe_browsing_util::kMalwareList, 2);
677 database_->UpdateFinished(true); 664 database_->UpdateFinished(true);
678 665
679 EXPECT_FALSE(database_->ContainsBrowseUrl( 666 EXPECT_FALSE(database_->ContainsBrowseUrl(
680 GURL("http://www.evil.com/notevil2.html"), 667 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cached_hashes));
681 &prefix_hits, &cached_hashes, now));
682 668
683 EXPECT_FALSE(database_->ContainsBrowseUrl( 669 EXPECT_FALSE(database_->ContainsBrowseUrl(
684 GURL("http://www.good.com/good1.html"), 670 GURL("http://www.good.com/good1.html"), &prefix_hits, &cached_hashes));
685 &prefix_hits, &cached_hashes, now));
686 671
687 EXPECT_FALSE(database_->ContainsBrowseUrl( 672 EXPECT_FALSE(database_->ContainsBrowseUrl(
688 GURL("http://www.good.com/good2.html"), 673 GURL("http://www.good.com/good2.html"), &prefix_hits, &cached_hashes));
689 &prefix_hits, &cached_hashes, now));
690 674
691 GetListsInfo(&lists); 675 GetListsInfo(&lists);
692 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 676 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
693 EXPECT_EQ(lists[0].adds, "1,3,7"); 677 EXPECT_EQ(lists[0].adds, "1,3,7");
694 EXPECT_EQ(lists[0].subs, "4"); 678 EXPECT_EQ(lists[0].subs, "4");
695 679
696 // The adddel command exposed a bug in the transaction code where any 680 // The adddel command exposed a bug in the transaction code where any
697 // transaction after it would fail. Add a dummy entry and remove it to 681 // transaction after it would fail. Add a dummy entry and remove it to
698 // make sure the transcation works fine. 682 // make sure the transcation works fine.
699 chunk.hosts.clear(); 683 chunk.hosts.clear();
(...skipping 24 matching lines...) Expand all
724 "www.notevilanymore.com/index.html", 708 "www.notevilanymore.com/index.html",
725 "www.notevilanymore.com/good.html"); 709 "www.notevilanymore.com/good.html");
726 chunks.clear(); 710 chunks.clear();
727 chunks.push_back(chunk); 711 chunks.push_back(chunk);
728 EXPECT_TRUE(database_->UpdateStarted(&lists)); 712 EXPECT_TRUE(database_->UpdateStarted(&lists));
729 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 713 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
730 database_->UpdateFinished(true); 714 database_->UpdateFinished(true);
731 715
732 EXPECT_FALSE(database_->ContainsBrowseUrl( 716 EXPECT_FALSE(database_->ContainsBrowseUrl(
733 GURL("http://www.notevilanymore.com/index.html"), 717 GURL("http://www.notevilanymore.com/index.html"),
734 &prefix_hits, &cached_hashes, now)); 718 &prefix_hits, &cached_hashes));
735 719
736 // Now insert the tardy add chunk and we don't expect them to appear 720 // Now insert the tardy add chunk and we don't expect them to appear
737 // in database because of the previous sub chunk. 721 // in database because of the previous sub chunk.
738 chunk.hosts.clear(); 722 chunk.hosts.clear();
739 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", 723 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/",
740 "www.notevilanymore.com/index.html", 724 "www.notevilanymore.com/index.html",
741 "www.notevilanymore.com/good.html"); 725 "www.notevilanymore.com/good.html");
742 chunks.clear(); 726 chunks.clear();
743 chunks.push_back(chunk); 727 chunks.push_back(chunk);
744 EXPECT_TRUE(database_->UpdateStarted(&lists)); 728 EXPECT_TRUE(database_->UpdateStarted(&lists));
745 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 729 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
746 database_->UpdateFinished(true); 730 database_->UpdateFinished(true);
747 731
748 EXPECT_FALSE(database_->ContainsBrowseUrl( 732 EXPECT_FALSE(database_->ContainsBrowseUrl(
749 GURL("http://www.notevilanymore.com/index.html"), 733 GURL("http://www.notevilanymore.com/index.html"),
750 &prefix_hits, &cached_hashes, now)); 734 &prefix_hits, &cached_hashes));
751 735
752 EXPECT_FALSE(database_->ContainsBrowseUrl( 736 EXPECT_FALSE(database_->ContainsBrowseUrl(
753 GURL("http://www.notevilanymore.com/good.html"), 737 GURL("http://www.notevilanymore.com/good.html"),
754 &prefix_hits, &cached_hashes, now)); 738 &prefix_hits, &cached_hashes));
755 739
756 // Reset and reload the database. The database will rely on the prefix set. 740 // Reset and reload the database. The database will rely on the prefix set.
757 database_.reset(new SafeBrowsingDatabaseNew); 741 database_.reset(new SafeBrowsingDatabaseNew);
758 database_->Init(database_filename_); 742 database_->Init(database_filename_);
759 743
760 // Check that a prefix still hits. 744 // Check that a prefix still hits.
761 EXPECT_TRUE(database_->ContainsBrowseUrl( 745 EXPECT_TRUE(database_->ContainsBrowseUrl(
762 GURL("http://www.evil.com/phishing.html"), 746 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cached_hashes));
763 &prefix_hits, &cached_hashes, now));
764 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); 747 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html"));
765 EXPECT_EQ(prefix_hits.size(), 1U); 748 EXPECT_EQ(prefix_hits.size(), 1U);
766 749
767 // Also check that it's not just always returning true in this case. 750 // Also check that it's not just always returning true in this case.
768 EXPECT_FALSE(database_->ContainsBrowseUrl( 751 EXPECT_FALSE(database_->ContainsBrowseUrl(
769 GURL("http://www.evil.com/"), 752 GURL("http://www.evil.com/"), &prefix_hits, &cached_hashes));
770 &prefix_hits, &cached_hashes, now));
771 753
772 // Check that the full hash is still present. 754 // Check that the full hash is still present.
773 EXPECT_TRUE(database_->ContainsBrowseUrl( 755 EXPECT_TRUE(database_->ContainsBrowseUrl(
774 GURL("http://www.evil.com/evil.html"), 756 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cached_hashes));
775 &prefix_hits, &cached_hashes, now));
776 ASSERT_EQ(1U, prefix_hits.size()); 757 ASSERT_EQ(1U, prefix_hits.size());
777 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/evil.html")); 758 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/evil.html"));
778 } 759 }
779 760
780 761
781 // Test adding zero length chunks to the database. 762 // Test adding zero length chunks to the database.
782 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { 763 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) {
783 SBChunkList chunks; 764 SBChunkList chunks;
784 SBChunk chunk; 765 SBChunk chunk;
785 766
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 819
839 empty_chunk.hosts.clear(); 820 empty_chunk.hosts.clear();
840 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/", 821 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/",
841 "www.notempty.com/full2.html"); 822 "www.notempty.com/full2.html");
842 chunks.push_back(empty_chunk); 823 chunks.push_back(empty_chunk);
843 824
844 EXPECT_TRUE(database_->UpdateStarted(&lists)); 825 EXPECT_TRUE(database_->UpdateStarted(&lists));
845 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 826 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
846 database_->UpdateFinished(true); 827 database_->UpdateFinished(true);
847 828
848 const Time now = Time::Now();
849 std::vector<SBFullHashResult> cached_hashes; 829 std::vector<SBFullHashResult> cached_hashes;
850 std::vector<SBPrefix> prefix_hits; 830 std::vector<SBPrefix> prefix_hits;
851 EXPECT_TRUE(database_->ContainsBrowseUrl( 831 EXPECT_TRUE(database_->ContainsBrowseUrl(
852 GURL("http://www.notempty.com/full1.html"), 832 GURL("http://www.notempty.com/full1.html"),
853 &prefix_hits, &cached_hashes, now)); 833 &prefix_hits, &cached_hashes));
854 EXPECT_TRUE(database_->ContainsBrowseUrl( 834 EXPECT_TRUE(database_->ContainsBrowseUrl(
855 GURL("http://www.notempty.com/full2.html"), 835 GURL("http://www.notempty.com/full2.html"),
856 &prefix_hits, &cached_hashes, now)); 836 &prefix_hits, &cached_hashes));
857 837
858 GetListsInfo(&lists); 838 GetListsInfo(&lists);
859 EXPECT_EQ(lists[0].adds, "1,10,19-22"); 839 EXPECT_EQ(lists[0].adds, "1,10,19-22");
860 EXPECT_EQ(lists[0].subs, "7"); 840 EXPECT_EQ(lists[0].subs, "7");
861 841
862 // Handle AddDel and SubDel commands for empty chunks. 842 // Handle AddDel and SubDel commands for empty chunks.
863 EXPECT_TRUE(database_->UpdateStarted(&lists)); 843 EXPECT_TRUE(database_->UpdateStarted(&lists));
864 AddDelChunk(safe_browsing_util::kMalwareList, 21); 844 AddDelChunk(safe_browsing_util::kMalwareList, 21);
865 database_->UpdateFinished(true); 845 database_->UpdateFinished(true);
866 846
(...skipping 30 matching lines...) Expand all
897 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); 877 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html");
898 full_hash.list_id = safe_browsing_util::MALWARE; 878 full_hash.list_id = safe_browsing_util::MALWARE;
899 879
900 std::vector<SBFullHashResult> results; 880 std::vector<SBFullHashResult> results;
901 results.push_back(full_hash); 881 results.push_back(full_hash);
902 882
903 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); 883 full_hash.hash = SBFullHashForString("www.evil.com/malware.html");
904 results.push_back(full_hash); 884 results.push_back(full_hash);
905 885
906 std::vector<SBPrefix> prefixes; 886 std::vector<SBPrefix> prefixes;
907 database_->CacheHashResults(prefixes, results); 887 database_->CacheHashResults(prefixes, results, kCacheLifetime);
908 } 888 }
909 889
910 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { 890 TEST_F(SafeBrowsingDatabaseTest, HashCaching) {
911 PopulateDatabaseForCacheTest(); 891 PopulateDatabaseForCacheTest();
912 892
913 // We should have both full hashes in the cache. 893 // We should have both full hashes in the cache.
914 EXPECT_EQ(2U, database_->cached_browse_hashes_.size()); 894 EXPECT_EQ(2U, database_->cached_browse_hashes_.size());
915 895
916 // Test the cache lookup for the first prefix. 896 // Test the cache lookup for the first prefix.
917 std::vector<SBPrefix> prefixes; 897 std::vector<SBPrefix> prefixes;
918 std::vector<SBFullHashResult> cached_hashes; 898 std::vector<SBFullHashResult> cached_hashes;
919 database_->ContainsBrowseUrl( 899 database_->ContainsBrowseUrl(
920 GURL("http://www.evil.com/phishing.html"), 900 GURL("http://www.evil.com/phishing.html"), &prefixes, &cached_hashes);
921 &prefixes, &cached_hashes, Time::Now());
922 ASSERT_EQ(1U, cached_hashes.size()); 901 ASSERT_EQ(1U, cached_hashes.size());
923 EXPECT_TRUE( 902 EXPECT_TRUE(
924 SBFullHashEqual(cached_hashes[0].hash, 903 SBFullHashEqual(cached_hashes[0].hash,
925 SBFullHashForString("www.evil.com/phishing.html"))); 904 SBFullHashForString("www.evil.com/phishing.html")));
926 905
927 prefixes.clear(); 906 prefixes.clear();
928 cached_hashes.clear(); 907 cached_hashes.clear();
929 908
930 // Test the cache lookup for the second prefix. 909 // Test the cache lookup for the second prefix.
931 database_->ContainsBrowseUrl( 910 database_->ContainsBrowseUrl(
932 GURL("http://www.evil.com/malware.html"), 911 GURL("http://www.evil.com/malware.html"), &prefixes, &cached_hashes);
933 &prefixes, &cached_hashes, Time::Now());
934 ASSERT_EQ(1U, cached_hashes.size()); 912 ASSERT_EQ(1U, cached_hashes.size());
935 EXPECT_TRUE( 913 EXPECT_TRUE(
936 SBFullHashEqual(cached_hashes[0].hash, 914 SBFullHashEqual(cached_hashes[0].hash,
937 SBFullHashForString("www.evil.com/malware.html"))); 915 SBFullHashForString("www.evil.com/malware.html")));
938 916
939 prefixes.clear(); 917 prefixes.clear();
940 cached_hashes.clear(); 918 cached_hashes.clear();
941 919
942 // Test removing a prefix via a sub chunk. 920 // Test removing a prefix via a sub chunk.
943 SBChunk chunk; 921 SBChunk chunk;
944 SBChunkList chunks; 922 SBChunkList chunks;
945 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", 923 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/",
946 "www.evil.com/phishing.html"); 924 "www.evil.com/phishing.html");
947 chunks.push_back(chunk); 925 chunks.push_back(chunk);
948 926
949 std::vector<SBListChunkRanges> lists; 927 std::vector<SBListChunkRanges> lists;
950 EXPECT_TRUE(database_->UpdateStarted(&lists)); 928 EXPECT_TRUE(database_->UpdateStarted(&lists));
951 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 929 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
952 database_->UpdateFinished(true); 930 database_->UpdateFinished(true);
953 931
954 // This prefix should still be there, but the fullhash is gone. 932 // This prefix should still be there, but the fullhash is gone.
955 EXPECT_TRUE(database_->ContainsBrowseUrl( 933 EXPECT_TRUE(database_->ContainsBrowseUrl(
956 GURL("http://www.evil.com/malware.html"), 934 GURL("http://www.evil.com/malware.html"), &prefixes, &cached_hashes));
957 &prefixes, &cached_hashes, Time::Now()));
958 ASSERT_EQ(1U, prefixes.size()); 935 ASSERT_EQ(1U, prefixes.size());
959 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefixes[0]); 936 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefixes[0]);
960 EXPECT_TRUE(cached_hashes.empty()); 937 EXPECT_TRUE(cached_hashes.empty());
961 prefixes.clear(); 938 prefixes.clear();
962 cached_hashes.clear(); 939 cached_hashes.clear();
963 940
964 // This prefix should be gone. 941 // This prefix should be gone.
965 database_->ContainsBrowseUrl( 942 database_->ContainsBrowseUrl(
966 GURL("http://www.evil.com/phishing.html"), 943 GURL("http://www.evil.com/phishing.html"), &prefixes, &cached_hashes);
967 &prefixes, &cached_hashes, Time::Now());
968 EXPECT_TRUE(cached_hashes.empty()); 944 EXPECT_TRUE(cached_hashes.empty());
969 945
970 prefixes.clear(); 946 prefixes.clear();
971 cached_hashes.clear(); 947 cached_hashes.clear();
972 948
973 // Test that an AddDel for the original chunk removes the last cached entry. 949 // Test that an AddDel for the original chunk removes the last cached entry.
974 EXPECT_TRUE(database_->UpdateStarted(&lists)); 950 EXPECT_TRUE(database_->UpdateStarted(&lists));
975 AddDelChunk(safe_browsing_util::kMalwareList, 1); 951 AddDelChunk(safe_browsing_util::kMalwareList, 1);
976 database_->UpdateFinished(true); 952 database_->UpdateFinished(true);
977 database_->ContainsBrowseUrl( 953 database_->ContainsBrowseUrl(
978 GURL("http://www.evil.com/malware.html"), 954 GURL("http://www.evil.com/malware.html"), &prefixes, &cached_hashes);
979 &prefixes, &cached_hashes, Time::Now());
980 EXPECT_TRUE(cached_hashes.empty()); 955 EXPECT_TRUE(cached_hashes.empty());
981 EXPECT_TRUE(database_->cached_browse_hashes_.empty()); 956 EXPECT_TRUE(database_->cached_browse_hashes_.empty());
982 957
983 prefixes.clear(); 958 prefixes.clear();
984 cached_hashes.clear(); 959 cached_hashes.clear();
985 960
986 // Test that the cache won't return expired values. First we have to adjust 961 // Test that the cache won't return expired values. First we have to adjust
987 // the cached entries' received time to make them older, since the database 962 // the cached entries' received time to make them older, since the database
988 // cache insert uses Time::Now(). First, store some entries. 963 // cache insert uses Time::Now(). First, store some entries.
989 PopulateDatabaseForCacheTest(); 964 PopulateDatabaseForCacheTest();
990 965
991 std::vector<SBFullHashCached>* hash_cache = &database_->cached_browse_hashes_; 966 std::vector<SBFullHashCached>* hash_cache = &database_->cached_browse_hashes_;
992 EXPECT_EQ(2U, hash_cache->size()); 967 EXPECT_EQ(2U, hash_cache->size());
993 968
994 // Now adjust one of the entries times to be in the past. 969 // Now adjust one of the entries times to be expired.
995 base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60); 970 const Time expired = Time::Now() - TimeDelta::FromMinutes(1);
996 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); 971 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html");
997 std::vector<SBFullHashCached>::iterator iter; 972 std::vector<SBFullHashCached>::iterator iter;
998 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) { 973 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) {
999 if (iter->hash.prefix == key) { 974 if (iter->hash.prefix == key) {
1000 iter->received = static_cast<int32>(expired.ToTimeT()); 975 iter->expire_after = expired;
1001 break; 976 break;
1002 } 977 }
1003 } 978 }
1004 EXPECT_TRUE(iter != hash_cache->end()); 979 EXPECT_TRUE(iter != hash_cache->end());
1005 980
1006 database_->ContainsBrowseUrl( 981 database_->ContainsBrowseUrl(
1007 GURL("http://www.evil.com/malware.html"), 982 GURL("http://www.evil.com/malware.html"), &prefixes, &cached_hashes);
1008 &prefixes, &cached_hashes, expired);
1009 EXPECT_TRUE(cached_hashes.empty()); 983 EXPECT_TRUE(cached_hashes.empty());
1010 984
1011 // This entry should still exist. 985 // This entry should still exist.
1012 database_->ContainsBrowseUrl( 986 database_->ContainsBrowseUrl(
1013 GURL("http://www.evil.com/phishing.html"), 987 GURL("http://www.evil.com/phishing.html"), &prefixes, &cached_hashes);
1014 &prefixes, &cached_hashes, expired);
1015 EXPECT_EQ(1U, cached_hashes.size()); 988 EXPECT_EQ(1U, cached_hashes.size());
1016 989
1017 // Testing prefix miss caching. First, we clear out the existing database, 990 // Testing prefix miss caching. First, we clear out the existing database,
1018 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate 991 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate
1019 // chunks. 992 // chunks.
1020 EXPECT_TRUE(database_->UpdateStarted(&lists)); 993 EXPECT_TRUE(database_->UpdateStarted(&lists));
1021 AddDelChunk(safe_browsing_util::kMalwareList, 1); 994 AddDelChunk(safe_browsing_util::kMalwareList, 1);
1022 database_->UpdateFinished(true); 995 database_->UpdateFinished(true);
1023 996
1024 std::vector<SBPrefix> prefix_misses; 997 std::vector<SBPrefix> prefix_misses;
1025 std::vector<SBFullHashResult> empty_full_hash; 998 std::vector<SBFullHashResult> empty_full_hash;
1026 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); 999 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html"));
1027 prefix_misses.push_back( 1000 prefix_misses.push_back(
1028 SBPrefixForString("http://www.bad.com/phishing.html")); 1001 SBPrefixForString("http://www.bad.com/phishing.html"));
1029 database_->CacheHashResults(prefix_misses, empty_full_hash); 1002 database_->CacheHashResults(prefix_misses, empty_full_hash, kCacheLifetime);
1030 1003
1031 // Prefixes with no full results are misses. 1004 // Prefixes with no full results are misses.
1032 EXPECT_EQ(2U, database_->prefix_miss_cache_.size()); 1005 EXPECT_EQ(2U, database_->prefix_miss_cache_.size());
1033 1006
1034 // Update the database. 1007 // Update the database.
1035 PopulateDatabaseForCacheTest(); 1008 PopulateDatabaseForCacheTest();
1036 1009
1037 // Prefix miss cache should be cleared. 1010 // Prefix miss cache should be cleared.
1038 EXPECT_TRUE(database_->prefix_miss_cache_.empty()); 1011 EXPECT_TRUE(database_->prefix_miss_cache_.empty());
1039 1012
1040 // Cache a GetHash miss for a particular prefix, and even though the prefix is 1013 // Cache a GetHash miss for a particular prefix, and even though the prefix is
1041 // in the database, it is flagged as a miss so looking up the associated URL 1014 // in the database, it is flagged as a miss so looking up the associated URL
1042 // will not succeed. 1015 // will not succeed.
1043 prefixes.clear(); 1016 prefixes.clear();
1044 cached_hashes.clear(); 1017 cached_hashes.clear();
1045 prefix_misses.clear(); 1018 prefix_misses.clear();
1046 empty_full_hash.clear(); 1019 empty_full_hash.clear();
1047 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); 1020 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html"));
1048 database_->CacheHashResults(prefix_misses, empty_full_hash); 1021 database_->CacheHashResults(prefix_misses, empty_full_hash, kCacheLifetime);
1049 EXPECT_FALSE(database_->ContainsBrowseUrl( 1022 EXPECT_FALSE(database_->ContainsBrowseUrl(
1050 GURL("http://www.evil.com/phishing.html"), 1023 GURL("http://www.evil.com/phishing.html"), &prefixes, &cached_hashes));
1051 &prefixes, &cached_hashes, Time::Now()));
1052 1024
1053 prefixes.clear(); 1025 prefixes.clear();
1054 cached_hashes.clear(); 1026 cached_hashes.clear();
1055 1027
1056 // Test receiving a full add chunk. 1028 // Test receiving a full add chunk.
1057 chunk.hosts.clear(); 1029 chunk.hosts.clear();
1058 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", 1030 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/",
1059 "www.fullevil.com/bad1.html", 1031 "www.fullevil.com/bad1.html",
1060 "www.fullevil.com/bad2.html"); 1032 "www.fullevil.com/bad2.html");
1061 chunks.clear(); 1033 chunks.clear();
1062 chunks.push_back(chunk); 1034 chunks.push_back(chunk);
1063 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1035 EXPECT_TRUE(database_->UpdateStarted(&lists));
1064 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1036 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1065 database_->UpdateFinished(true); 1037 database_->UpdateFinished(true);
1066 1038
1067 EXPECT_TRUE(database_->ContainsBrowseUrl( 1039 EXPECT_TRUE(database_->ContainsBrowseUrl(
1068 GURL("http://www.fullevil.com/bad1.html"), 1040 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cached_hashes));
1069 &prefixes, &cached_hashes, Time::Now()));
1070 EXPECT_TRUE(cached_hashes.empty()); 1041 EXPECT_TRUE(cached_hashes.empty());
1071 ASSERT_EQ(1U, prefixes.size()); 1042 ASSERT_EQ(1U, prefixes.size());
1072 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefixes[0]); 1043 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefixes[0]);
1073 prefixes.clear(); 1044 prefixes.clear();
1074 cached_hashes.clear(); 1045 cached_hashes.clear();
1075 1046
1076 EXPECT_TRUE(database_->ContainsBrowseUrl( 1047 EXPECT_TRUE(database_->ContainsBrowseUrl(
1077 GURL("http://www.fullevil.com/bad2.html"), 1048 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cached_hashes));
1078 &prefixes, &cached_hashes, Time::Now()));
1079 EXPECT_TRUE(cached_hashes.empty()); 1049 EXPECT_TRUE(cached_hashes.empty());
1080 ASSERT_EQ(1U, prefixes.size()); 1050 ASSERT_EQ(1U, prefixes.size());
1081 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefixes[0]); 1051 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefixes[0]);
1082 prefixes.clear(); 1052 prefixes.clear();
1083 cached_hashes.clear(); 1053 cached_hashes.clear();
1084 1054
1085 // Test receiving a full sub chunk, which will remove one of the full adds. 1055 // Test receiving a full sub chunk, which will remove one of the full adds.
1086 chunk.hosts.clear(); 1056 chunk.hosts.clear();
1087 InsertSubChunkHostFullHash(&chunk, 200, 20, 1057 InsertSubChunkHostFullHash(&chunk, 200, 20,
1088 "www.fullevil.com/", 1058 "www.fullevil.com/",
1089 "www.fullevil.com/bad1.html"); 1059 "www.fullevil.com/bad1.html");
1090 chunks.clear(); 1060 chunks.clear();
1091 chunks.push_back(chunk); 1061 chunks.push_back(chunk);
1092 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1062 EXPECT_TRUE(database_->UpdateStarted(&lists));
1093 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1063 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1094 database_->UpdateFinished(true); 1064 database_->UpdateFinished(true);
1095 1065
1096 EXPECT_FALSE(database_->ContainsBrowseUrl( 1066 EXPECT_FALSE(database_->ContainsBrowseUrl(
1097 GURL("http://www.fullevil.com/bad1.html"), 1067 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cached_hashes));
1098 &prefixes, &cached_hashes, Time::Now()));
1099 EXPECT_TRUE(cached_hashes.empty()); 1068 EXPECT_TRUE(cached_hashes.empty());
1100 1069
1101 // There should be one remaining full add. 1070 // There should be one remaining full add.
1102 EXPECT_TRUE(database_->ContainsBrowseUrl( 1071 EXPECT_TRUE(database_->ContainsBrowseUrl(
1103 GURL("http://www.fullevil.com/bad2.html"), 1072 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cached_hashes));
1104 &prefixes, &cached_hashes, Time::Now()));
1105 EXPECT_TRUE(cached_hashes.empty()); 1073 EXPECT_TRUE(cached_hashes.empty());
1106 ASSERT_EQ(1U, prefixes.size()); 1074 ASSERT_EQ(1U, prefixes.size());
1107 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefixes[0]); 1075 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefixes[0]);
1108 prefixes.clear(); 1076 prefixes.clear();
1109 cached_hashes.clear(); 1077 cached_hashes.clear();
1110 1078
1111 // Now test an AddDel for the remaining full add. 1079 // Now test an AddDel for the remaining full add.
1112 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1080 EXPECT_TRUE(database_->UpdateStarted(&lists));
1113 AddDelChunk(safe_browsing_util::kMalwareList, 20); 1081 AddDelChunk(safe_browsing_util::kMalwareList, 20);
1114 database_->UpdateFinished(true); 1082 database_->UpdateFinished(true);
1115 1083
1116 EXPECT_FALSE(database_->ContainsBrowseUrl( 1084 EXPECT_FALSE(database_->ContainsBrowseUrl(
1117 GURL("http://www.fullevil.com/bad1.html"), 1085 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cached_hashes));
1118 &prefixes, &cached_hashes, Time::Now()));
1119 EXPECT_FALSE(database_->ContainsBrowseUrl( 1086 EXPECT_FALSE(database_->ContainsBrowseUrl(
1120 GURL("http://www.fullevil.com/bad2.html"), 1087 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cached_hashes));
1121 &prefixes, &cached_hashes, Time::Now()));
1122 1088
1123 // Add a fullhash which has a prefix collision for a known url. 1089 // Add a fullhash which has a prefix collision for a known url.
1124 static const char kExampleFine[] = "www.example.com/fine.html"; 1090 static const char kExampleFine[] = "www.example.com/fine.html";
1125 static const char kExampleCollision[] = 1091 static const char kExampleCollision[] =
1126 "www.example.com/3123364814/malware.htm"; 1092 "www.example.com/3123364814/malware.htm";
1127 ASSERT_EQ(SBPrefixForString(kExampleFine), 1093 ASSERT_EQ(SBPrefixForString(kExampleFine),
1128 SBPrefixForString(kExampleCollision)); 1094 SBPrefixForString(kExampleCollision));
1129 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1095 EXPECT_TRUE(database_->UpdateStarted(&lists));
1130 { 1096 {
1131 SBChunkList chunks; 1097 SBChunkList chunks;
1132 SBChunk chunk; 1098 SBChunk chunk;
1133 InsertAddChunkHostPrefixUrl(&chunk, 21, "www.example.com/", 1099 InsertAddChunkHostPrefixUrl(&chunk, 21, "www.example.com/",
1134 kExampleCollision); 1100 kExampleCollision);
1135 chunks.push_back(chunk); 1101 chunks.push_back(chunk);
1136 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1102 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1137 } 1103 }
1138 database_->UpdateFinished(true); 1104 database_->UpdateFinished(true);
1139 1105
1140 // Cache gethash response for |kExampleCollision|. 1106 // Cache gethash response for |kExampleCollision|.
1141 { 1107 {
1142 SBFullHashResult result; 1108 SBFullHashResult result;
1143 result.hash = SBFullHashForString(kExampleCollision); 1109 result.hash = SBFullHashForString(kExampleCollision);
1144 result.list_id = safe_browsing_util::MALWARE; 1110 result.list_id = safe_browsing_util::MALWARE;
1145 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix), 1111 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix),
1146 std::vector<SBFullHashResult>(1, result)); 1112 std::vector<SBFullHashResult>(1, result),
1113 kCacheLifetime);
1147 } 1114 }
1148 1115
1149 // Expect a prefix hit due to the collision between |kExampleFine| and 1116 // Expect a prefix hit due to the collision between |kExampleFine| and
1150 // |kExampleCollision|, with the gethash showing only |kExampleCollision|. 1117 // |kExampleCollision|, with the gethash showing only |kExampleCollision|.
1151 EXPECT_TRUE(database_->ContainsBrowseUrl( 1118 EXPECT_TRUE(database_->ContainsBrowseUrl(
1152 GURL(std::string("http://") + kExampleFine), 1119 GURL(std::string("http://") + kExampleFine), &prefixes, &cached_hashes));
1153 &prefixes, &cached_hashes, Time::Now()));
1154 ASSERT_EQ(1U, prefixes.size()); 1120 ASSERT_EQ(1U, prefixes.size());
1155 EXPECT_EQ(SBPrefixForString(kExampleFine), prefixes[0]); 1121 EXPECT_EQ(SBPrefixForString(kExampleFine), prefixes[0]);
1156 ASSERT_EQ(1U, cached_hashes.size()); 1122 ASSERT_EQ(1U, cached_hashes.size());
1157 EXPECT_TRUE(SBFullHashEqual(cached_hashes[0].hash, 1123 EXPECT_TRUE(SBFullHashEqual(cached_hashes[0].hash,
1158 SBFullHashForString(kExampleCollision))); 1124 SBFullHashForString(kExampleCollision)));
1159 } 1125 }
1160 1126
1161 // Test that corrupt databases are appropriately handled, even if the 1127 // Test that corrupt databases are appropriately handled, even if the
1162 // corruption is detected in the midst of the update. 1128 // corruption is detected in the midst of the update.
1163 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. 1129 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved.
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
1594 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1560 EXPECT_TRUE(database_->UpdateStarted(&lists));
1595 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 1561 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
1596 database_->UpdateFinished(true); 1562 database_->UpdateFinished(true);
1597 1563
1598 GetListsInfo(&lists); 1564 GetListsInfo(&lists);
1599 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); 1565 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name);
1600 EXPECT_EQ("1", lists[0].adds); 1566 EXPECT_EQ("1", lists[0].adds);
1601 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); 1567 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name);
1602 EXPECT_EQ("47", lists[1].adds); 1568 EXPECT_EQ("47", lists[1].adds);
1603 1569
1604 const Time now = Time::Now();
1605 std::vector<SBPrefix> prefixes; 1570 std::vector<SBPrefix> prefixes;
1606 std::vector<SBFullHashResult> cached_hashes; 1571 std::vector<SBFullHashResult> cached_hashes;
1607 std::vector<SBPrefix> prefix_hits; 1572 std::vector<SBPrefix> prefix_hits;
1608 1573
1609 EXPECT_TRUE(database_->ContainsBrowseUrl( 1574 EXPECT_TRUE(database_->ContainsBrowseUrl(
1610 GURL("http://www.evil.com/malware1.html"), 1575 GURL("http://www.evil.com/malware1.html"), &prefixes, &cached_hashes));
1611 &prefixes, &cached_hashes, now));
1612 EXPECT_TRUE(database_->ContainsBrowseUrl( 1576 EXPECT_TRUE(database_->ContainsBrowseUrl(
1613 GURL("http://www.evil.com/malware2.html"), 1577 GURL("http://www.evil.com/malware2.html"), &prefixes, &cached_hashes));
1614 &prefixes, &cached_hashes, now));
1615 EXPECT_TRUE(database_->ContainsBrowseUrl( 1578 EXPECT_TRUE(database_->ContainsBrowseUrl(
1616 GURL("http://www.evil.com/phishing1.html"), 1579 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cached_hashes));
1617 &prefixes, &cached_hashes, now));
1618 EXPECT_TRUE(database_->ContainsBrowseUrl( 1580 EXPECT_TRUE(database_->ContainsBrowseUrl(
1619 GURL("http://www.evil.com/phishing2.html"), 1581 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cached_hashes));
1620 &prefixes, &cached_hashes, now));
1621 1582
1622 // Test removing a single prefix from the add chunk. 1583 // Test removing a single prefix from the add chunk.
1623 // Remove the prefix that added first. 1584 // Remove the prefix that added first.
1624 chunk.hosts.clear(); 1585 chunk.hosts.clear();
1625 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", 1586 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/",
1626 "www.evil.com/malware1.html"); 1587 "www.evil.com/malware1.html");
1627 chunks.clear(); 1588 chunks.clear();
1628 chunks.push_back(chunk); 1589 chunks.push_back(chunk);
1629 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1590 EXPECT_TRUE(database_->UpdateStarted(&lists));
1630 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1591 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1631 database_->UpdateFinished(true); 1592 database_->UpdateFinished(true);
1632 1593
1633 // Remove the prefix that added last. 1594 // Remove the prefix that added last.
1634 chunk.hosts.clear(); 1595 chunk.hosts.clear();
1635 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", 1596 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/",
1636 "www.evil.com/phishing2.html"); 1597 "www.evil.com/phishing2.html");
1637 chunks.clear(); 1598 chunks.clear();
1638 chunks.push_back(chunk); 1599 chunks.push_back(chunk);
1639 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1600 EXPECT_TRUE(database_->UpdateStarted(&lists));
1640 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 1601 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
1641 database_->UpdateFinished(true); 1602 database_->UpdateFinished(true);
1642 1603
1643 // Verify that the database contains urls expected. 1604 // Verify that the database contains urls expected.
1644 EXPECT_FALSE(database_->ContainsBrowseUrl( 1605 EXPECT_FALSE(database_->ContainsBrowseUrl(
1645 GURL("http://www.evil.com/malware1.html"), 1606 GURL("http://www.evil.com/malware1.html"), &prefixes, &cached_hashes));
1646 &prefixes, &cached_hashes, now));
1647 EXPECT_TRUE(database_->ContainsBrowseUrl( 1607 EXPECT_TRUE(database_->ContainsBrowseUrl(
1648 GURL("http://www.evil.com/malware2.html"), 1608 GURL("http://www.evil.com/malware2.html"), &prefixes, &cached_hashes));
1649 &prefixes, &cached_hashes, now));
1650 EXPECT_TRUE(database_->ContainsBrowseUrl( 1609 EXPECT_TRUE(database_->ContainsBrowseUrl(
1651 GURL("http://www.evil.com/phishing1.html"), 1610 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cached_hashes));
1652 &prefixes, &cached_hashes, now));
1653 EXPECT_FALSE(database_->ContainsBrowseUrl( 1611 EXPECT_FALSE(database_->ContainsBrowseUrl(
1654 GURL("http://www.evil.com/phishing2.html"), 1612 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cached_hashes));
1655 &prefixes, &cached_hashes, now));
1656 } 1613 }
1657 1614
1658 // Test that an empty update doesn't actually update the database. 1615 // Test that an empty update doesn't actually update the database.
1659 // This isn't a functionality requirement, but it is a useful 1616 // This isn't a functionality requirement, but it is a useful
1660 // optimization. 1617 // optimization.
1661 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { 1618 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) {
1662 SBChunkList chunks; 1619 SBChunkList chunks;
1663 SBChunk chunk; 1620 SBChunk chunk;
1664 1621
1665 base::FilePath filename = database_->BrowseDBFilename(database_filename_); 1622 base::FilePath filename = database_->BrowseDBFilename(database_filename_);
1666 1623
1667 // Prime the database. 1624 // Prime the database.
1668 std::vector<SBListChunkRanges> lists; 1625 std::vector<SBListChunkRanges> lists;
1669 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1626 EXPECT_TRUE(database_->UpdateStarted(&lists));
1670 1627
1671 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1628 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1672 "www.evil.com/malware.html"); 1629 "www.evil.com/malware.html");
1673 chunks.clear(); 1630 chunks.clear();
1674 chunks.push_back(chunk); 1631 chunks.push_back(chunk);
1675 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1632 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1676 database_->UpdateFinished(true); 1633 database_->UpdateFinished(true);
1677 1634
1678 // Get an older time to reset the lastmod time for detecting whether 1635 // Get an older time to reset the lastmod time for detecting whether
1679 // the file has been updated. 1636 // the file has been updated.
1680 base::File::Info before_info, after_info; 1637 base::File::Info before_info, after_info;
1681 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1638 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1682 const base::Time old_last_modified = 1639 const Time old_last_modified =
1683 before_info.last_modified - base::TimeDelta::FromSeconds(10); 1640 before_info.last_modified - TimeDelta::FromSeconds(10);
1684 1641
1685 // Inserting another chunk updates the database file. The sleep is 1642 // Inserting another chunk updates the database file. The sleep is
1686 // needed because otherwise the entire test can finish w/in the 1643 // needed because otherwise the entire test can finish w/in the
1687 // resolution of the lastmod time. 1644 // resolution of the lastmod time.
1688 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); 1645 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified));
1689 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1646 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1690 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1647 EXPECT_TRUE(database_->UpdateStarted(&lists));
1691 chunk.hosts.clear(); 1648 chunk.hosts.clear();
1692 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/", 1649 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/",
1693 "www.foo.com/malware.html"); 1650 "www.foo.com/malware.html");
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1731 1688
1732 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1689 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1733 "www.evil.com/malware.html"); 1690 "www.evil.com/malware.html");
1734 chunks.clear(); 1691 chunks.clear();
1735 chunks.push_back(chunk); 1692 chunks.push_back(chunk);
1736 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1693 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1737 database_->UpdateFinished(true); 1694 database_->UpdateFinished(true);
1738 } 1695 }
1739 1696
1740 // Find the malware url in the database, don't find a good url. 1697 // Find the malware url in the database, don't find a good url.
1741 const Time now = Time::Now();
1742 std::vector<SBFullHashResult> cached_hashes; 1698 std::vector<SBFullHashResult> cached_hashes;
1743 std::vector<SBPrefix> prefix_hits; 1699 std::vector<SBPrefix> prefix_hits;
1744 EXPECT_TRUE(database_->ContainsBrowseUrl( 1700 EXPECT_TRUE(database_->ContainsBrowseUrl(
1745 GURL("http://www.evil.com/malware.html"), 1701 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cached_hashes));
1746 &prefix_hits, &cached_hashes, now));
1747 EXPECT_FALSE(database_->ContainsBrowseUrl( 1702 EXPECT_FALSE(database_->ContainsBrowseUrl(
1748 GURL("http://www.good.com/goodware.html"), 1703 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cached_hashes));
1749 &prefix_hits, &cached_hashes, now));
1750 1704
1751 base::FilePath filter_file = database_->PrefixSetForFilename( 1705 base::FilePath filter_file = database_->PrefixSetForFilename(
1752 database_->BrowseDBFilename(database_filename_)); 1706 database_->BrowseDBFilename(database_filename_));
1753 1707
1754 // After re-creating the database, it should have a filter read from 1708 // After re-creating the database, it should have a filter read from
1755 // a file, so it should find the same results. 1709 // a file, so it should find the same results.
1756 ASSERT_TRUE(base::PathExists(filter_file)); 1710 ASSERT_TRUE(base::PathExists(filter_file));
1757 database_.reset(new SafeBrowsingDatabaseNew); 1711 database_.reset(new SafeBrowsingDatabaseNew);
1758 database_->Init(database_filename_); 1712 database_->Init(database_filename_);
1759 EXPECT_TRUE(database_->ContainsBrowseUrl( 1713 EXPECT_TRUE(database_->ContainsBrowseUrl(
1760 GURL("http://www.evil.com/malware.html"), 1714 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cached_hashes));
1761 &prefix_hits, &cached_hashes, now));
1762 EXPECT_FALSE(database_->ContainsBrowseUrl( 1715 EXPECT_FALSE(database_->ContainsBrowseUrl(
1763 GURL("http://www.good.com/goodware.html"), 1716 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cached_hashes));
1764 &prefix_hits, &cached_hashes, now));
1765 1717
1766 // If there is no filter file, the database cannot find malware urls. 1718 // If there is no filter file, the database cannot find malware urls.
1767 base::DeleteFile(filter_file, false); 1719 base::DeleteFile(filter_file, false);
1768 ASSERT_FALSE(base::PathExists(filter_file)); 1720 ASSERT_FALSE(base::PathExists(filter_file));
1769 database_.reset(new SafeBrowsingDatabaseNew); 1721 database_.reset(new SafeBrowsingDatabaseNew);
1770 database_->Init(database_filename_); 1722 database_->Init(database_filename_);
1771 EXPECT_FALSE(database_->ContainsBrowseUrl( 1723 EXPECT_FALSE(database_->ContainsBrowseUrl(
1772 GURL("http://www.evil.com/malware.html"), 1724 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cached_hashes));
1773 &prefix_hits, &cached_hashes, now));
1774 EXPECT_FALSE(database_->ContainsBrowseUrl( 1725 EXPECT_FALSE(database_->ContainsBrowseUrl(
1775 GURL("http://www.good.com/goodware.html"), 1726 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cached_hashes));
1776 &prefix_hits, &cached_hashes, now));
1777 } 1727 }
1778 1728
1779 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { 1729 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) {
1780 database_.reset(); 1730 database_.reset();
1781 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); 1731 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile();
1782 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); 1732 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile();
1783 database_.reset(new SafeBrowsingDatabaseNew(browse_store, 1733 database_.reset(new SafeBrowsingDatabaseNew(browse_store,
1784 NULL, 1734 NULL,
1785 NULL, 1735 NULL,
1786 NULL, 1736 NULL,
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1908 SBChunkList chunks; 1858 SBChunkList chunks;
1909 SBChunk chunk; 1859 SBChunk chunk;
1910 InsertAddChunkHostFullHashes(&chunk, 3, "www.example.com/", 1860 InsertAddChunkHostFullHashes(&chunk, 3, "www.example.com/",
1911 kExampleCollision); 1861 kExampleCollision);
1912 chunks.push_back(chunk); 1862 chunks.push_back(chunk);
1913 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1863 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1914 } 1864 }
1915 1865
1916 database_->UpdateFinished(true); 1866 database_->UpdateFinished(true);
1917 1867
1918 const Time now = Time::Now();
1919 std::vector<SBFullHashResult> cached_hashes; 1868 std::vector<SBFullHashResult> cached_hashes;
1920 std::vector<SBPrefix> prefix_hits; 1869 std::vector<SBPrefix> prefix_hits;
1921 1870
1922 // Anything will hit the host prefix. 1871 // Anything will hit the host prefix.
1923 EXPECT_TRUE(database_->ContainsBrowseUrl( 1872 EXPECT_TRUE(database_->ContainsBrowseUrl(
1924 GURL("http://www.evil.com/malware.html"), 1873 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cached_hashes));
1925 &prefix_hits, &cached_hashes, now));
1926 ASSERT_EQ(1U, prefix_hits.size()); 1874 ASSERT_EQ(1U, prefix_hits.size());
1927 EXPECT_EQ(SBPrefixForString("www.evil.com/"), prefix_hits[0]); 1875 EXPECT_EQ(SBPrefixForString("www.evil.com/"), prefix_hits[0]);
1928 EXPECT_TRUE(cached_hashes.empty()); 1876 EXPECT_TRUE(cached_hashes.empty());
1929 1877
1930 // Hit the specific URL prefix. 1878 // Hit the specific URL prefix.
1931 EXPECT_TRUE(database_->ContainsBrowseUrl( 1879 EXPECT_TRUE(database_->ContainsBrowseUrl(
1932 GURL(std::string("http://") + kWhateverMalware), 1880 GURL(std::string("http://") + kWhateverMalware),
1933 &prefix_hits, &cached_hashes, now)); 1881 &prefix_hits, &cached_hashes));
1934 ASSERT_EQ(1U, prefix_hits.size()); 1882 ASSERT_EQ(1U, prefix_hits.size());
1935 EXPECT_EQ(SBPrefixForString(kWhateverMalware), prefix_hits[0]); 1883 EXPECT_EQ(SBPrefixForString(kWhateverMalware), prefix_hits[0]);
1936 EXPECT_TRUE(cached_hashes.empty()); 1884 EXPECT_TRUE(cached_hashes.empty());
1937 1885
1938 // Other URLs at that host are fine. 1886 // Other URLs at that host are fine.
1939 EXPECT_FALSE(database_->ContainsBrowseUrl( 1887 EXPECT_FALSE(database_->ContainsBrowseUrl(
1940 GURL("http://www.whatever.com/fine.html"), 1888 GURL("http://www.whatever.com/fine.html"), &prefix_hits, &cached_hashes));
1941 &prefix_hits, &cached_hashes, now));
1942 EXPECT_TRUE(prefix_hits.empty()); 1889 EXPECT_TRUE(prefix_hits.empty());
1943 EXPECT_TRUE(cached_hashes.empty()); 1890 EXPECT_TRUE(cached_hashes.empty());
1944 1891
1945 // Hit the specific URL full hash. 1892 // Hit the specific URL full hash.
1946 EXPECT_TRUE(database_->ContainsBrowseUrl( 1893 EXPECT_TRUE(database_->ContainsBrowseUrl(
1947 GURL(std::string("http://") + kExampleCollision), 1894 GURL(std::string("http://") + kExampleCollision),
1948 &prefix_hits, &cached_hashes, now)); 1895 &prefix_hits, &cached_hashes));
1949 ASSERT_EQ(1U, prefix_hits.size()); 1896 ASSERT_EQ(1U, prefix_hits.size());
1950 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); 1897 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]);
1951 EXPECT_TRUE(cached_hashes.empty()); 1898 EXPECT_TRUE(cached_hashes.empty());
1952 1899
1953 // This prefix collides, but no full hash match. 1900 // This prefix collides, but no full hash match.
1954 EXPECT_FALSE(database_->ContainsBrowseUrl( 1901 EXPECT_FALSE(database_->ContainsBrowseUrl(
1955 GURL(std::string("http://") + kExampleFine), 1902 GURL(std::string("http://") + kExampleFine),
1956 &prefix_hits, &cached_hashes, now)); 1903 &prefix_hits, &cached_hashes));
1957 } 1904 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698