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
, GetUrlSeverestThreatType
) {
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
kUnwantedUrl("http://www.unwanted.com/page.html");
88 const GURL
kUnwantedAndMalwareUrl(
89 "http://www.unwantedandmalware.com/page.html");
90 const GURL
kSafeUrl("http://www.safe.com/page.html");
92 const SBFullHash kMalwareHostHash
= SBFullHashForString("malware.com/");
93 const SBFullHash kPhishingHostHash
= SBFullHashForString("phishing.com/");
94 const SBFullHash kUnwantedHostHash
= SBFullHashForString("unwanted.com/");
95 const SBFullHash kUnwantedAndMalwareHostHash
=
96 SBFullHashForString("unwantedandmalware.com/");
97 const SBFullHash kSafeHostHash
= SBFullHashForString("www.safe.com/");
100 SBFullHashResult full_hash
;
101 full_hash
.hash
= kMalwareHostHash
;
102 full_hash
.list_id
= static_cast<int>(safe_browsing_util::MALWARE
);
103 full_hashes
.push_back(full_hash
);
107 SBFullHashResult full_hash
;
108 full_hash
.hash
= kPhishingHostHash
;
109 full_hash
.list_id
= static_cast<int>(safe_browsing_util::PHISH
);
110 full_hashes
.push_back(full_hash
);
114 SBFullHashResult full_hash
;
115 full_hash
.hash
= kUnwantedHostHash
;
116 full_hash
.list_id
= static_cast<int>(safe_browsing_util::UNWANTEDURL
);
117 full_hashes
.push_back(full_hash
);
121 // Add both MALWARE and UNWANTEDURL list IDs for
122 // kUnwantedAndMalwareHostHash.
123 SBFullHashResult full_hash_malware
;
124 full_hash_malware
.hash
= kUnwantedAndMalwareHostHash
;
125 full_hash_malware
.list_id
= static_cast<int>(safe_browsing_util::MALWARE
);
126 full_hashes
.push_back(full_hash_malware
);
128 SBFullHashResult full_hash_unwanted
;
129 full_hash_unwanted
.hash
= kUnwantedAndMalwareHostHash
;
130 full_hash_unwanted
.list_id
=
131 static_cast<int>(safe_browsing_util::UNWANTEDURL
);
132 full_hashes
.push_back(full_hash_unwanted
);
135 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
136 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
137 kMalwareHostHash
, full_hashes
));
139 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING
,
140 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
141 kPhishingHostHash
, full_hashes
));
143 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED
,
144 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
145 kUnwantedHostHash
, full_hashes
));
147 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
148 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
149 kUnwantedAndMalwareHostHash
, full_hashes
));
151 EXPECT_EQ(SB_THREAT_TYPE_SAFE
,
152 SafeBrowsingDatabaseManager::GetHashSeverestThreatType(
153 kSafeHostHash
, full_hashes
));
155 const size_t kArbitraryValue
= 123456U;
156 size_t index
= kArbitraryValue
;
157 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
158 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
159 kMalwareUrl
, full_hashes
, &index
));
160 EXPECT_EQ(0U, index
);
162 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING
,
163 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
164 kPhishingUrl
, full_hashes
, &index
));
165 EXPECT_EQ(1U, index
);
167 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED
,
168 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
169 kUnwantedUrl
, full_hashes
, &index
));
170 EXPECT_EQ(2U, index
);
172 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE
,
173 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
174 kUnwantedAndMalwareUrl
, full_hashes
, &index
));
175 EXPECT_EQ(3U, index
);
177 index
= kArbitraryValue
;
178 EXPECT_EQ(SB_THREAT_TYPE_SAFE
,
179 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType(
180 kSafeUrl
, full_hashes
, &index
));
181 EXPECT_EQ(kArbitraryValue
, index
);