1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <string> 6 #include <vector> 7 8 #include "base/memory/ref_counted.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/safe_browsing/database_manager.h" 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 12 #include "content/public/test/test_browser_thread_bundle.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/platform_test.h" 15 #include "url/gurl.h" 16 17 using content::TestBrowserThreadBundle; 18 19 class SafeBrowsingDatabaseManagerTest : public PlatformTest { 20 public: 21 bool RunSBHashTest(const safe_browsing_util::ListType list_type, 22 const std::vector<SBThreatType>& expected_threats, 23 const std::string& result_list); 24 25 private: 26 TestBrowserThreadBundle thread_bundle_; 27 }; 28 29 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest( 30 const safe_browsing_util::ListType list_type, 31 const std::vector<SBThreatType>& expected_threats, 32 const std::string& result_list) { 33 scoped_refptr<SafeBrowsingService> sb_service_( 34 SafeBrowsingService::CreateSafeBrowsingService()); 35 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_( 36 new SafeBrowsingDatabaseManager(sb_service_)); 37 const SBFullHash same_full_hash = {}; 38 39 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check = 40 new SafeBrowsingDatabaseManager::SafeBrowsingCheck( 41 std::vector<GURL>(), 42 std::vector<SBFullHash>(1, same_full_hash), 43 NULL, 44 list_type, 45 expected_threats); 46 db_manager_->checks_.insert(check); 47 48 const SBFullHashResult full_hash_result = { 49 same_full_hash, 50 safe_browsing_util::GetListId(result_list) 51 }; 52 53 std::vector<SBFullHashResult> fake_results(1, full_hash_result); 54 bool result = db_manager_->HandleOneCheck(check, fake_results); 55 db_manager_->checks_.erase(check); 56 delete check; 57 return result; 58 } 59 60 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) { 61 std::vector<SBThreatType> malware_threat(1, 62 SB_THREAT_TYPE_BINARY_MALWARE_URL); 63 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL, 64 malware_threat, 65 safe_browsing_util::kMalwareList)); 66 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL, 67 malware_threat, 68 safe_browsing_util::kBinUrlList)); 69 70 // Check for multiple threats 71 std::vector<SBThreatType> multiple_threats; 72 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); 73 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); 74 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE, 75 multiple_threats, 76 safe_browsing_util::kBinUrlList)); 77 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE, 78 multiple_threats, 79 safe_browsing_util::kMalwareList)); 80 } 81 82 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlThreatType) { 83 std::vector<SBFullHashResult> full_hashes; 84 85 const GURL kMalwareUrl("http://www.malware.com/page.html"); 86 const GURL kPhishingUrl("http://www.phishing.com/page.html"); 87 const GURL kSafeUrl("http://www.safe.com/page.html"); 88 89 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/"); 90 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/"); 91 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/"); 92 93 { 94 SBFullHashResult full_hash; 95 full_hash.hash = kMalwareHostHash; 96 full_hash.list_id = static_cast<int>(safe_browsing_util::MALWARE); 97 full_hashes.push_back(full_hash); 98 } 99 100 { 101 SBFullHashResult full_hash; 102 full_hash.hash = kPhishingHostHash; 103 full_hash.list_id = static_cast<int>(safe_browsing_util::PHISH); 104 full_hashes.push_back(full_hash); 105 } 106 107 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, 108 SafeBrowsingDatabaseManager::GetHashThreatType( 109 kMalwareHostHash, full_hashes)); 110 111 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, 112 SafeBrowsingDatabaseManager::GetHashThreatType( 113 kPhishingHostHash, full_hashes)); 114 115 EXPECT_EQ(SB_THREAT_TYPE_SAFE, 116 SafeBrowsingDatabaseManager::GetHashThreatType( 117 kSafeHostHash, full_hashes)); 118 119 size_t index = 100; 120 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, 121 SafeBrowsingDatabaseManager::GetUrlThreatType( 122 kMalwareUrl, full_hashes, &index)); 123 EXPECT_EQ(0U, index); 124 125 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, 126 SafeBrowsingDatabaseManager::GetUrlThreatType( 127 kPhishingUrl, full_hashes, &index)); 128 EXPECT_EQ(1U, index); 129 130 index = 100; 131 EXPECT_EQ(SB_THREAT_TYPE_SAFE, 132 SafeBrowsingDatabaseManager::GetUrlThreatType( 133 kSafeUrl, full_hashes, &index)); 134 EXPECT_EQ(100U, index); 135 } 136