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
;
19 class SafeBrowsingDatabaseManagerTest
: public PlatformTest
{
21 bool RunSBHashTest(const safe_browsing_util::ListType list_type
,
22 const std::vector
<SBThreatType
>& expected_threats
,
23 const std::string
& result_list
);
26 TestBrowserThreadBundle thread_bundle_
;
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
= {};
39 SafeBrowsingDatabaseManager::SafeBrowsingCheck
* check
=
40 new SafeBrowsingDatabaseManager::SafeBrowsingCheck(
42 std::vector
<SBFullHash
>(1, same_full_hash
),
46 db_manager_
->checks_
.insert(check
);
48 const SBFullHashResult full_hash_result
= {
50 safe_browsing_util::GetListId(result_list
)
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
);
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
,
65 safe_browsing_util::kMalwareList
));
66 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL
,
68 safe_browsing_util::kBinUrlList
));
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
,
76 safe_browsing_util::kBinUrlList
));
77 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE
,
79 safe_browsing_util::kMalwareList
));
82 TEST_F(SafeBrowsingDatabaseManagerTest
, GetUrlThreatType
) {
83 std::vector
<SBFullHashResult
> full_hashes
;
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");
89 const SBFullHash kMalwareHostHash
= SBFullHashForString("malware.com/");
90 const SBFullHash kPhishingHostHash
= SBFullHashForString("phishing.com/");
91 const SBFullHash kSafeHostHash
= SBFullHashForString("www.safe.com/");
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
);
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
);
107 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
108 SafeBrowsingDatabaseManager::GetHashThreatType(
109 kMalwareHostHash
, full_hashes
));
111 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING
,
112 SafeBrowsingDatabaseManager::GetHashThreatType(
113 kPhishingHostHash
, full_hashes
));
115 EXPECT_EQ(SB_THREAT_TYPE_SAFE
,
116 SafeBrowsingDatabaseManager::GetHashThreatType(
117 kSafeHostHash
, full_hashes
));
119 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
120 SafeBrowsingDatabaseManager::GetUrlThreatType(
121 kMalwareUrl
, full_hashes
));
123 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING
,
124 SafeBrowsingDatabaseManager::GetUrlThreatType(
125 kPhishingUrl
, full_hashes
));
127 EXPECT_EQ(SB_THREAT_TYPE_SAFE
,
128 SafeBrowsingDatabaseManager::GetUrlThreatType(
129 kSafeUrl
, full_hashes
));