OLD | NEW |
| (Empty) |
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 "base/run_loop.h" | |
11 #include "chrome/browser/safe_browsing/database_manager.h" | |
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "content/public/test/test_utils.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "testing/platform_test.h" | |
17 #include "url/gurl.h" | |
18 | |
19 using content::TestBrowserThreadBundle; | |
20 | |
21 namespace { | |
22 | |
23 class TestClient : public SafeBrowsingDatabaseManager::Client { | |
24 public: | |
25 TestClient() {} | |
26 ~TestClient() override {} | |
27 | |
28 void OnCheckBrowseUrlResult(const GURL& url, | |
29 SBThreatType threat_type, | |
30 const std::string& metadata) override {} | |
31 | |
32 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain, | |
33 SBThreatType threat_type) override {} | |
34 | |
35 private: | |
36 DISALLOW_COPY_AND_ASSIGN(TestClient); | |
37 }; | |
38 | |
39 } // namespace | |
40 | |
41 class SafeBrowsingDatabaseManagerTest : public PlatformTest { | |
42 public: | |
43 bool RunSBHashTest(const safe_browsing_util::ListType list_type, | |
44 const std::vector<SBThreatType>& expected_threats, | |
45 const std::string& result_list); | |
46 | |
47 private: | |
48 TestBrowserThreadBundle thread_bundle_; | |
49 }; | |
50 | |
51 bool SafeBrowsingDatabaseManagerTest::RunSBHashTest( | |
52 const safe_browsing_util::ListType list_type, | |
53 const std::vector<SBThreatType>& expected_threats, | |
54 const std::string& result_list) { | |
55 scoped_refptr<SafeBrowsingService> sb_service_( | |
56 SafeBrowsingService::CreateSafeBrowsingService()); | |
57 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_( | |
58 new SafeBrowsingDatabaseManager(sb_service_)); | |
59 const SBFullHash same_full_hash = {}; | |
60 | |
61 SafeBrowsingDatabaseManager::SafeBrowsingCheck* check = | |
62 new SafeBrowsingDatabaseManager::SafeBrowsingCheck( | |
63 std::vector<GURL>(), | |
64 std::vector<SBFullHash>(1, same_full_hash), | |
65 NULL, | |
66 list_type, | |
67 expected_threats); | |
68 db_manager_->checks_.insert(check); | |
69 | |
70 const SBFullHashResult full_hash_result = { | |
71 same_full_hash, | |
72 safe_browsing_util::GetListId(result_list) | |
73 }; | |
74 | |
75 std::vector<SBFullHashResult> fake_results(1, full_hash_result); | |
76 bool result = db_manager_->HandleOneCheck(check, fake_results); | |
77 db_manager_->checks_.erase(check); | |
78 delete check; | |
79 return result; | |
80 } | |
81 | |
82 TEST_F(SafeBrowsingDatabaseManagerTest, CheckCorrespondsListType) { | |
83 std::vector<SBThreatType> malware_threat(1, | |
84 SB_THREAT_TYPE_BINARY_MALWARE_URL); | |
85 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::BINURL, | |
86 malware_threat, | |
87 safe_browsing_util::kMalwareList)); | |
88 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::BINURL, | |
89 malware_threat, | |
90 safe_browsing_util::kBinUrlList)); | |
91 | |
92 // Check for multiple threats | |
93 std::vector<SBThreatType> multiple_threats; | |
94 multiple_threats.push_back(SB_THREAT_TYPE_URL_MALWARE); | |
95 multiple_threats.push_back(SB_THREAT_TYPE_URL_PHISHING); | |
96 EXPECT_FALSE(RunSBHashTest(safe_browsing_util::MALWARE, | |
97 multiple_threats, | |
98 safe_browsing_util::kBinUrlList)); | |
99 EXPECT_TRUE(RunSBHashTest(safe_browsing_util::MALWARE, | |
100 multiple_threats, | |
101 safe_browsing_util::kMalwareList)); | |
102 } | |
103 | |
104 TEST_F(SafeBrowsingDatabaseManagerTest, GetUrlSeverestThreatType) { | |
105 std::vector<SBFullHashResult> full_hashes; | |
106 | |
107 const GURL kMalwareUrl("http://www.malware.com/page.html"); | |
108 const GURL kPhishingUrl("http://www.phishing.com/page.html"); | |
109 const GURL kUnwantedUrl("http://www.unwanted.com/page.html"); | |
110 const GURL kUnwantedAndMalwareUrl( | |
111 "http://www.unwantedandmalware.com/page.html"); | |
112 const GURL kSafeUrl("http://www.safe.com/page.html"); | |
113 | |
114 const SBFullHash kMalwareHostHash = SBFullHashForString("malware.com/"); | |
115 const SBFullHash kPhishingHostHash = SBFullHashForString("phishing.com/"); | |
116 const SBFullHash kUnwantedHostHash = SBFullHashForString("unwanted.com/"); | |
117 const SBFullHash kUnwantedAndMalwareHostHash = | |
118 SBFullHashForString("unwantedandmalware.com/"); | |
119 const SBFullHash kSafeHostHash = SBFullHashForString("www.safe.com/"); | |
120 | |
121 { | |
122 SBFullHashResult full_hash; | |
123 full_hash.hash = kMalwareHostHash; | |
124 full_hash.list_id = static_cast<int>(safe_browsing_util::MALWARE); | |
125 full_hashes.push_back(full_hash); | |
126 } | |
127 | |
128 { | |
129 SBFullHashResult full_hash; | |
130 full_hash.hash = kPhishingHostHash; | |
131 full_hash.list_id = static_cast<int>(safe_browsing_util::PHISH); | |
132 full_hashes.push_back(full_hash); | |
133 } | |
134 | |
135 { | |
136 SBFullHashResult full_hash; | |
137 full_hash.hash = kUnwantedHostHash; | |
138 full_hash.list_id = static_cast<int>(safe_browsing_util::UNWANTEDURL); | |
139 full_hashes.push_back(full_hash); | |
140 } | |
141 | |
142 { | |
143 // Add both MALWARE and UNWANTEDURL list IDs for | |
144 // kUnwantedAndMalwareHostHash. | |
145 SBFullHashResult full_hash_malware; | |
146 full_hash_malware.hash = kUnwantedAndMalwareHostHash; | |
147 full_hash_malware.list_id = static_cast<int>(safe_browsing_util::MALWARE); | |
148 full_hashes.push_back(full_hash_malware); | |
149 | |
150 SBFullHashResult full_hash_unwanted; | |
151 full_hash_unwanted.hash = kUnwantedAndMalwareHostHash; | |
152 full_hash_unwanted.list_id = | |
153 static_cast<int>(safe_browsing_util::UNWANTEDURL); | |
154 full_hashes.push_back(full_hash_unwanted); | |
155 } | |
156 | |
157 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, | |
158 SafeBrowsingDatabaseManager::GetHashSeverestThreatType( | |
159 kMalwareHostHash, full_hashes)); | |
160 | |
161 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, | |
162 SafeBrowsingDatabaseManager::GetHashSeverestThreatType( | |
163 kPhishingHostHash, full_hashes)); | |
164 | |
165 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED, | |
166 SafeBrowsingDatabaseManager::GetHashSeverestThreatType( | |
167 kUnwantedHostHash, full_hashes)); | |
168 | |
169 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, | |
170 SafeBrowsingDatabaseManager::GetHashSeverestThreatType( | |
171 kUnwantedAndMalwareHostHash, full_hashes)); | |
172 | |
173 EXPECT_EQ(SB_THREAT_TYPE_SAFE, | |
174 SafeBrowsingDatabaseManager::GetHashSeverestThreatType( | |
175 kSafeHostHash, full_hashes)); | |
176 | |
177 const size_t kArbitraryValue = 123456U; | |
178 size_t index = kArbitraryValue; | |
179 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, | |
180 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( | |
181 kMalwareUrl, full_hashes, &index)); | |
182 EXPECT_EQ(0U, index); | |
183 | |
184 EXPECT_EQ(SB_THREAT_TYPE_URL_PHISHING, | |
185 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( | |
186 kPhishingUrl, full_hashes, &index)); | |
187 EXPECT_EQ(1U, index); | |
188 | |
189 EXPECT_EQ(SB_THREAT_TYPE_URL_UNWANTED, | |
190 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( | |
191 kUnwantedUrl, full_hashes, &index)); | |
192 EXPECT_EQ(2U, index); | |
193 | |
194 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, | |
195 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( | |
196 kUnwantedAndMalwareUrl, full_hashes, &index)); | |
197 EXPECT_EQ(3U, index); | |
198 | |
199 index = kArbitraryValue; | |
200 EXPECT_EQ(SB_THREAT_TYPE_SAFE, | |
201 SafeBrowsingDatabaseManager::GetUrlSeverestThreatType( | |
202 kSafeUrl, full_hashes, &index)); | |
203 EXPECT_EQ(kArbitraryValue, index); | |
204 } | |
205 | |
206 TEST_F(SafeBrowsingDatabaseManagerTest, ServiceStopWithPendingChecks) { | |
207 scoped_refptr<SafeBrowsingService> sb_service( | |
208 SafeBrowsingService::CreateSafeBrowsingService()); | |
209 scoped_refptr<SafeBrowsingDatabaseManager> db_manager( | |
210 new SafeBrowsingDatabaseManager(sb_service)); | |
211 TestClient client; | |
212 | |
213 // Start the service and flush tasks to ensure database is made available. | |
214 db_manager->StartOnIOThread(); | |
215 content::RunAllBlockingPoolTasksUntilIdle(); | |
216 base::RunLoop().RunUntilIdle(); | |
217 EXPECT_TRUE(db_manager->DatabaseAvailable()); | |
218 | |
219 // Start an extension check operation, which is done asynchronously. | |
220 std::set<std::string> extension_ids; | |
221 extension_ids.insert("testtesttesttesttesttesttesttest"); | |
222 db_manager->CheckExtensionIDs(extension_ids, &client); | |
223 | |
224 // Stop the service without first flushing above tasks. | |
225 db_manager->StopOnIOThread(false); | |
226 | |
227 // Now run posted tasks, whish should include the extension check which has | |
228 // been posted to the safe browsing task runner. This should not crash. | |
229 content::RunAllBlockingPoolTasksUntilIdle(); | |
230 base::RunLoop().RunUntilIdle(); | |
231 } | |
OLD | NEW |