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.
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"
17 using content::TestBrowserThreadBundle
;
21 class FakeSafeBrowsingService
: public SafeBrowsingService
{
23 FakeSafeBrowsingService() {}
25 virtual ~FakeSafeBrowsingService() {}
27 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingService
);
30 // Factory that creates FakeSafeBrowsingService instances.
31 class TestSafeBrowsingServiceFactory
: public SafeBrowsingServiceFactory
{
32 virtual SafeBrowsingService
* CreateSafeBrowsingService() OVERRIDE
{
33 return new FakeSafeBrowsingService();
39 class SafeBrowsingDatabaseManagerTest
: public PlatformTest
{
42 virtual void SetUp() OVERRIDE
{
43 PlatformTest::SetUp();
44 SafeBrowsingService::RegisterFactory(&factory_
);
47 virtual void TearDown() OVERRIDE
{
48 SafeBrowsingService::RegisterFactory(NULL
);
49 PlatformTest::TearDown();
52 bool RunSBHashTest(const safe_browsing_util::ListType list_type
,
53 const std::vector
<SBThreatType
>& expected_threats
,
54 const std::string
& result_list
);
57 TestBrowserThreadBundle thread_bundle_
;
58 TestSafeBrowsingServiceFactory factory_
;
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
= {};
71 SafeBrowsingDatabaseManager::SafeBrowsingCheck
* check
=
72 new SafeBrowsingDatabaseManager::SafeBrowsingCheck(
74 std::vector
<SBFullHash
>(1, same_full_hash
),
78 db_manager_
->checks_
.insert(check
);
80 const SBFullHashResult full_hash_result
= {
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
);
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
,
98 safe_browsing_util::kMalwareList
));
99 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL
,
101 safe_browsing_util::kBinUrlList
));
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
,
109 safe_browsing_util::kBinUrlList
));
110 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE
,
112 safe_browsing_util::kMalwareList
));