OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This test creates a safebrowsing service using test safebrowsing database | 5 // This test creates a safebrowsing service using test safebrowsing database |
6 // and a test protocol manager. It is used to test logics in safebrowsing | 6 // and a test protocol manager. It is used to test logics in safebrowsing |
7 // service. | 7 // service. |
8 | 8 |
9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
10 | 10 |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 bool ContainsInclusionWhitelistedUrl(const GURL& url) override { | 253 bool ContainsInclusionWhitelistedUrl(const GURL& url) override { |
254 return true; | 254 return true; |
255 } | 255 } |
256 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, | 256 bool ContainsExtensionPrefixes(const std::vector<SBPrefix>& prefixes, |
257 std::vector<SBPrefix>* prefix_hits) override { | 257 std::vector<SBPrefix>* prefix_hits) override { |
258 return false; | 258 return false; |
259 } | 259 } |
260 bool ContainsMalwareIP(const std::string& ip_address) override { | 260 bool ContainsMalwareIP(const std::string& ip_address) override { |
261 return true; | 261 return true; |
262 } | 262 } |
| 263 bool ContainsResourceUrlPrefixes( |
| 264 const std::vector<SBPrefix>& prefixes, |
| 265 std::vector<SBPrefix>* prefix_hits) override { |
| 266 prefix_hits->clear(); |
| 267 return ContainsUrlPrefixes(RESOURCEBLACKLIST, RESOURCEBLACKLIST, |
| 268 prefixes, prefix_hits); |
| 269 } |
263 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override { | 270 bool UpdateStarted(std::vector<SBListChunkRanges>* lists) override { |
264 ADD_FAILURE() << "Not implemented."; | 271 ADD_FAILURE() << "Not implemented."; |
265 return false; | 272 return false; |
266 } | 273 } |
267 void InsertChunks( | 274 void InsertChunks( |
268 const std::string& list_name, | 275 const std::string& list_name, |
269 const std::vector<scoped_ptr<SBChunkData>>& chunks) override { | 276 const std::vector<scoped_ptr<SBChunkData>>& chunks) override { |
270 ADD_FAILURE() << "Not implemented."; | 277 ADD_FAILURE() << "Not implemented."; |
271 } | 278 } |
272 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override { | 279 void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) override { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} | 377 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} |
371 ~TestSafeBrowsingDatabaseFactory() override {} | 378 ~TestSafeBrowsingDatabaseFactory() override {} |
372 | 379 |
373 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( | 380 SafeBrowsingDatabase* CreateSafeBrowsingDatabase( |
374 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 381 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
375 bool enable_download_protection, | 382 bool enable_download_protection, |
376 bool enable_client_side_whitelist, | 383 bool enable_client_side_whitelist, |
377 bool enable_download_whitelist, | 384 bool enable_download_whitelist, |
378 bool enable_extension_blacklist, | 385 bool enable_extension_blacklist, |
379 bool enable_ip_blacklist, | 386 bool enable_ip_blacklist, |
380 bool enabled_unwanted_software_list) override { | 387 bool enabled_unwanted_software_list, |
| 388 bool enable_resource_blacklist) override { |
381 db_ = new TestSafeBrowsingDatabase(); | 389 db_ = new TestSafeBrowsingDatabase(); |
382 return db_; | 390 return db_; |
383 } | 391 } |
384 TestSafeBrowsingDatabase* GetDb() { return db_; } | 392 TestSafeBrowsingDatabase* GetDb() { return db_; } |
385 | 393 |
386 private: | 394 private: |
387 // Owned by the SafebrowsingService. | 395 // Owned by the SafebrowsingService. |
388 TestSafeBrowsingDatabase* db_; | 396 TestSafeBrowsingDatabase* db_; |
389 }; | 397 }; |
390 | 398 |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 | 1138 |
1131 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, | 1139 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, |
1132 public SafeBrowsingDatabaseManager::Client { | 1140 public SafeBrowsingDatabaseManager::Client { |
1133 public: | 1141 public: |
1134 TestSBClient() | 1142 TestSBClient() |
1135 : threat_type_(SB_THREAT_TYPE_SAFE), | 1143 : threat_type_(SB_THREAT_TYPE_SAFE), |
1136 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} | 1144 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} |
1137 | 1145 |
1138 SBThreatType GetThreatType() const { return threat_type_; } | 1146 SBThreatType GetThreatType() const { return threat_type_; } |
1139 | 1147 |
| 1148 std::string GetThreatHash() const { return threat_hash_; } |
| 1149 |
1140 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { | 1150 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { |
1141 BrowserThread::PostTask( | 1151 BrowserThread::PostTask( |
1142 BrowserThread::IO, FROM_HERE, | 1152 BrowserThread::IO, FROM_HERE, |
1143 base::Bind(&TestSBClient::CheckDownloadUrlOnIOThread, this, url_chain)); | 1153 base::Bind(&TestSBClient::CheckDownloadUrlOnIOThread, this, url_chain)); |
1144 content::RunMessageLoop(); // Will stop in OnCheckDownloadUrlResult. | 1154 content::RunMessageLoop(); // Will stop in OnCheckDownloadUrlResult. |
1145 } | 1155 } |
1146 | 1156 |
1147 void CheckBrowseUrl(const GURL& url) { | 1157 void CheckBrowseUrl(const GURL& url) { |
1148 BrowserThread::PostTask( | 1158 BrowserThread::PostTask( |
1149 BrowserThread::IO, FROM_HERE, | 1159 BrowserThread::IO, FROM_HERE, |
1150 base::Bind(&TestSBClient::CheckBrowseUrlOnIOThread, this, url)); | 1160 base::Bind(&TestSBClient::CheckBrowseUrlOnIOThread, this, url)); |
1151 content::RunMessageLoop(); // Will stop in OnCheckBrowseUrlResult. | 1161 content::RunMessageLoop(); // Will stop in OnCheckBrowseUrlResult. |
1152 } | 1162 } |
1153 | 1163 |
| 1164 void CheckResourceUrl(const GURL& url) { |
| 1165 BrowserThread::PostTask( |
| 1166 BrowserThread::IO, FROM_HERE, |
| 1167 base::Bind(&TestSBClient::CheckResourceUrlOnIOThread, this, url)); |
| 1168 content::RunMessageLoop(); // Will stop in OnCheckResourceUrlResult. |
| 1169 } |
| 1170 |
1154 private: | 1171 private: |
1155 friend class base::RefCountedThreadSafe<TestSBClient>; | 1172 friend class base::RefCountedThreadSafe<TestSBClient>; |
1156 ~TestSBClient() override {} | 1173 ~TestSBClient() override {} |
1157 | 1174 |
1158 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { | 1175 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { |
1159 bool synchronous_safe_signal = | 1176 bool synchronous_safe_signal = |
1160 safe_browsing_service_->database_manager()->CheckDownloadUrl(url_chain, | 1177 safe_browsing_service_->database_manager()->CheckDownloadUrl(url_chain, |
1161 this); | 1178 this); |
1162 if (synchronous_safe_signal) { | 1179 if (synchronous_safe_signal) { |
1163 threat_type_ = SB_THREAT_TYPE_SAFE; | 1180 threat_type_ = SB_THREAT_TYPE_SAFE; |
1164 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1181 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1165 base::Bind(&TestSBClient::CheckDone, this)); | 1182 base::Bind(&TestSBClient::CheckDone, this)); |
1166 } | 1183 } |
1167 } | 1184 } |
1168 | 1185 |
1169 void CheckBrowseUrlOnIOThread(const GURL& url) { | 1186 void CheckBrowseUrlOnIOThread(const GURL& url) { |
1170 // The async CheckDone() hook will not be called when we have a synchronous | 1187 // The async CheckDone() hook will not be called when we have a synchronous |
1171 // safe signal, handle it right away. | 1188 // safe signal, handle it right away. |
1172 bool synchronous_safe_signal = | 1189 bool synchronous_safe_signal = |
1173 safe_browsing_service_->database_manager()->CheckBrowseUrl(url, this); | 1190 safe_browsing_service_->database_manager()->CheckBrowseUrl(url, this); |
1174 if (synchronous_safe_signal) { | 1191 if (synchronous_safe_signal) { |
1175 threat_type_ = SB_THREAT_TYPE_SAFE; | 1192 threat_type_ = SB_THREAT_TYPE_SAFE; |
1176 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1193 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1177 base::Bind(&TestSBClient::CheckDone, this)); | 1194 base::Bind(&TestSBClient::CheckDone, this)); |
1178 } | 1195 } |
1179 } | 1196 } |
1180 | 1197 |
| 1198 void CheckResourceUrlOnIOThread(const GURL& url) { |
| 1199 bool synchronous_safe_signal = |
| 1200 safe_browsing_service_->database_manager()->CheckResourceUrl(url, this); |
| 1201 if (synchronous_safe_signal) { |
| 1202 threat_type_ = SB_THREAT_TYPE_SAFE; |
| 1203 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1204 base::Bind(&TestSBClient::CheckDone, this)); |
| 1205 } |
| 1206 } |
| 1207 |
1181 // Called when the result of checking a download URL is known. | 1208 // Called when the result of checking a download URL is known. |
1182 void OnCheckDownloadUrlResult(const std::vector<GURL>& /* url_chain */, | 1209 void OnCheckDownloadUrlResult(const std::vector<GURL>& /* url_chain */, |
1183 SBThreatType threat_type) override { | 1210 SBThreatType threat_type) override { |
1184 threat_type_ = threat_type; | 1211 threat_type_ = threat_type; |
1185 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1212 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1186 base::Bind(&TestSBClient::CheckDone, this)); | 1213 base::Bind(&TestSBClient::CheckDone, this)); |
1187 } | 1214 } |
1188 | 1215 |
1189 // Called when the result of checking a browse URL is known. | 1216 // Called when the result of checking a browse URL is known. |
1190 void OnCheckBrowseUrlResult(const GURL& /* url */, | 1217 void OnCheckBrowseUrlResult(const GURL& /* url */, |
1191 SBThreatType threat_type, | 1218 SBThreatType threat_type, |
1192 const std::string& /* metadata */) override { | 1219 const std::string& /* metadata */) override { |
1193 threat_type_ = threat_type; | 1220 threat_type_ = threat_type; |
1194 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1221 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1195 base::Bind(&TestSBClient::CheckDone, this)); | 1222 base::Bind(&TestSBClient::CheckDone, this)); |
1196 } | 1223 } |
1197 | 1224 |
| 1225 // Called when the result of checking a resource URL is known. |
| 1226 void OnCheckResourceUrlResult(const GURL& /* url */, |
| 1227 SBThreatType threat_type, |
| 1228 const std::string& threat_hash) override { |
| 1229 threat_type_ = threat_type; |
| 1230 threat_hash_ = threat_hash; |
| 1231 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1232 base::Bind(&TestSBClient::CheckDone, this)); |
| 1233 } |
| 1234 |
1198 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } | 1235 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } |
1199 | 1236 |
1200 SBThreatType threat_type_; | 1237 SBThreatType threat_type_; |
| 1238 std::string threat_hash_; |
1201 SafeBrowsingService* safe_browsing_service_; | 1239 SafeBrowsingService* safe_browsing_service_; |
1202 | 1240 |
1203 DISALLOW_COPY_AND_ASSIGN(TestSBClient); | 1241 DISALLOW_COPY_AND_ASSIGN(TestSBClient); |
1204 }; | 1242 }; |
1205 | 1243 |
1206 } // namespace | 1244 } // namespace |
1207 | 1245 |
1208 // These tests use SafeBrowsingService::Client to directly interact with | 1246 // These tests use SafeBrowsingService::Client to directly interact with |
1209 // SafeBrowsingService. | 1247 // SafeBrowsingService. |
1210 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadUrl) { | 1248 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadUrl) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1326 SBFullHashResult full_hash_result; | 1364 SBFullHashResult full_hash_result; |
1327 GenUrlFullhashResult(badbin_url, BINURL, &full_hash_result); | 1365 GenUrlFullhashResult(badbin_url, BINURL, &full_hash_result); |
1328 SetupResponseForUrl(badbin_url, full_hash_result); | 1366 SetupResponseForUrl(badbin_url, full_hash_result); |
1329 | 1367 |
1330 client->CheckDownloadUrl(badbin_urls); | 1368 client->CheckDownloadUrl(badbin_urls); |
1331 | 1369 |
1332 // Now, the badbin_url is not safe since it is added to download database. | 1370 // Now, the badbin_url is not safe since it is added to download database. |
1333 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType()); | 1371 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType()); |
1334 } | 1372 } |
1335 | 1373 |
| 1374 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckResourceUrl) { |
| 1375 const char* kBlacklistResource = "/blacklisted/script.js"; |
| 1376 GURL blacklist_resource = embedded_test_server()->GetURL(kBlacklistResource); |
| 1377 std::string blacklist_resource_hash; |
| 1378 const char* kMaliciousResource = "/malware/script.js"; |
| 1379 GURL malware_resource = embedded_test_server()->GetURL(kMaliciousResource); |
| 1380 std::string malware_resource_hash; |
| 1381 |
| 1382 { |
| 1383 SBFullHashResult full_hash; |
| 1384 GenUrlFullhashResult(blacklist_resource, RESOURCEBLACKLIST, &full_hash); |
| 1385 SetupResponseForUrl(blacklist_resource, full_hash); |
| 1386 blacklist_resource_hash = std::string(full_hash.hash.full_hash, |
| 1387 full_hash.hash.full_hash + 32); |
| 1388 } |
| 1389 { |
| 1390 SBFullHashResult full_hash; |
| 1391 GenUrlFullhashResult(malware_resource, MALWARE, &full_hash); |
| 1392 SetupResponseForUrl(malware_resource, full_hash); |
| 1393 full_hash.list_id = RESOURCEBLACKLIST; |
| 1394 SetupResponseForUrl(malware_resource, full_hash); |
| 1395 malware_resource_hash = std::string(full_hash.hash.full_hash, |
| 1396 full_hash.hash.full_hash + 32); |
| 1397 } |
| 1398 |
| 1399 scoped_refptr<TestSBClient> client(new TestSBClient); |
| 1400 client->CheckResourceUrl(blacklist_resource); |
| 1401 EXPECT_EQ(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, client->GetThreatType()); |
| 1402 EXPECT_EQ(blacklist_resource_hash, client->GetThreatHash()); |
| 1403 |
| 1404 // Since we're checking a resource url, we should receive result that it's |
| 1405 // a blacklisted resource, not a malware. |
| 1406 client = new TestSBClient; |
| 1407 client->CheckResourceUrl(malware_resource); |
| 1408 EXPECT_EQ(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, client->GetThreatType()); |
| 1409 EXPECT_EQ(malware_resource_hash, client->GetThreatHash()); |
| 1410 |
| 1411 client->CheckResourceUrl(embedded_test_server()->GetURL(kEmptyPage)); |
| 1412 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); |
| 1413 } |
| 1414 |
1336 #if defined(OS_WIN) | 1415 #if defined(OS_WIN) |
1337 // http://crbug.com/396409 | 1416 // http://crbug.com/396409 |
1338 #define MAYBE_CheckDownloadUrlTimedOut DISABLED_CheckDownloadUrlTimedOut | 1417 #define MAYBE_CheckDownloadUrlTimedOut DISABLED_CheckDownloadUrlTimedOut |
1339 #else | 1418 #else |
1340 #define MAYBE_CheckDownloadUrlTimedOut CheckDownloadUrlTimedOut | 1419 #define MAYBE_CheckDownloadUrlTimedOut CheckDownloadUrlTimedOut |
1341 #endif | 1420 #endif |
1342 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, | 1421 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
1343 MAYBE_CheckDownloadUrlTimedOut) { | 1422 MAYBE_CheckDownloadUrlTimedOut) { |
1344 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); | 1423 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); |
1345 std::vector<GURL> badbin_urls(1, badbin_url); | 1424 std::vector<GURL> badbin_urls(1, badbin_url); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1667 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, | 1746 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
1668 content::Source<SafeBrowsingDatabaseManager>( | 1747 content::Source<SafeBrowsingDatabaseManager>( |
1669 sb_service_->database_manager().get())); | 1748 sb_service_->database_manager().get())); |
1670 BrowserThread::PostTask( | 1749 BrowserThread::PostTask( |
1671 BrowserThread::IO, FROM_HERE, | 1750 BrowserThread::IO, FROM_HERE, |
1672 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 1751 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
1673 observer.Wait(); | 1752 observer.Wait(); |
1674 } | 1753 } |
1675 | 1754 |
1676 } // namespace safe_browsing | 1755 } // namespace safe_browsing |
OLD | NEW |