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 namespace { 20 21 class FakeSafeBrowsingService : public SafeBrowsingService { 22 public: 23 FakeSafeBrowsingService() {} 24 private: 25 virtual ~FakeSafeBrowsingService() {} 26 27 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService); 28 }; 29 30 // Factory that creates FakeSafeBrowsingService instances. 31 class TestSafeBrowsingServiceFactory : public SafeBrowsingServiceFactory { 32 virtual SafeBrowsingService* CreateSafeBrowsingService() OVERRIDE { 33 return new FakeSafeBrowsingService(); 34 } 35 }; 36 37 } // namespace 38 39 class SafeBrowsingDatabaseManagerTest : public PlatformTest { 40 public: 41 42 virtual void SetUp() OVERRIDE { 43 PlatformTest::SetUp(); 44 SafeBrowsingService::RegisterFactory(&factory_); 45 } 46 47 virtual void TearDown() OVERRIDE { 48 SafeBrowsingService::RegisterFactory(NULL); 49 PlatformTest::TearDown(); 50 } 51 52 bool RunSBHashTest(const safe_browsing_util::ListType list_type, 53 const std::vector<SBThreatType>& expected_threats, 54 const std::string& result_list); 55 56 private: 57 TestBrowserThreadBundle thread_bundle_; 58 TestSafeBrowsingServiceFactory factory_; 59 }; 60 61 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest( 62 const safe_browsing_util::ListType list_type, 63 const std::vector<SBThreatType>& expected_threats, 64 const std::string& result_list) { 65 scoped_refptr<SafeBrowsingService> sb_service_( 66 SafeBrowsingService::CreateSafeBrowsingService()); 67 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_( 68 new SafeBrowsingDatabaseManager(sb_service_)); 69 const SBFullHash same_full_hash = {}; 70 71 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check = 72 new SafeBrowsingDatabaseManager::SafeBrowsingCheck( 73 std::vector<GURL>(), 74 std::vector<SBFullHash>(1, same_full_hash), 75 NULL, 76 list_type, 77 expected_threats); 78 db_manager_->checks_.insert(check); 79 80 const SBFullHashResult full_hash_result = { 81 same_full_hash, 82 result_list, 83 0 84 }; 85 86 std::vector<SBFullHashResult> fake_results(1, full_hash_result); 87 bool result = db_manager_->HandleOneCheck(check, fake_results); 88 db_manager_->checks_.erase(check); 89 delete check; 90 return result; 91 } 92 93 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) { 94 std::vector<SBThreatType> malware_threat(1, 95 SB_THREAT_TYPE_BINARY_MALWARE_URL); 96 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL, 97 malware_threat, 98 safe_browsing_util::kMalwareList)); 99 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL, 100 malware_threat, 101 safe_browsing_util::kBinUrlList)); 102 103 // Check for multiple threats 104 std::vector<SBThreatType> multiple_threats; 105 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); 106 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); 107 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE, 108 multiple_threats, 109 safe_browsing_util::kBinUrlList)); 110 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE, 111 multiple_threats, 112 safe_browsing_util::kMalwareList)); 113 } 114