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 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1130 | 1137 |
1131 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, | 1138 class TestSBClient : public base::RefCountedThreadSafe<TestSBClient>, |
1132 public SafeBrowsingDatabaseManager::Client { | 1139 public SafeBrowsingDatabaseManager::Client { |
1133 public: | 1140 public: |
1134 TestSBClient() | 1141 TestSBClient() |
1135 : threat_type_(SB_THREAT_TYPE_SAFE), | 1142 : threat_type_(SB_THREAT_TYPE_SAFE), |
1136 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} | 1143 safe_browsing_service_(g_browser_process->safe_browsing_service()) {} |
1137 | 1144 |
1138 SBThreatType GetThreatType() const { return threat_type_; } | 1145 SBThreatType GetThreatType() const { return threat_type_; } |
1139 | 1146 |
| 1147 std::string GetThreatHash() const { return threat_hash_; } |
| 1148 |
1140 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { | 1149 void CheckDownloadUrl(const std::vector<GURL>& url_chain) { |
1141 BrowserThread::PostTask( | 1150 BrowserThread::PostTask( |
1142 BrowserThread::IO, FROM_HERE, | 1151 BrowserThread::IO, FROM_HERE, |
1143 base::Bind(&TestSBClient::CheckDownloadUrlOnIOThread, this, url_chain)); | 1152 base::Bind(&TestSBClient::CheckDownloadUrlOnIOThread, this, url_chain)); |
1144 content::RunMessageLoop(); // Will stop in OnCheckDownloadUrlResult. | 1153 content::RunMessageLoop(); // Will stop in OnCheckDownloadUrlResult. |
1145 } | 1154 } |
1146 | 1155 |
1147 void CheckBrowseUrl(const GURL& url) { | 1156 void CheckBrowseUrl(const GURL& url) { |
1148 BrowserThread::PostTask( | 1157 BrowserThread::PostTask( |
1149 BrowserThread::IO, FROM_HERE, | 1158 BrowserThread::IO, FROM_HERE, |
1150 base::Bind(&TestSBClient::CheckBrowseUrlOnIOThread, this, url)); | 1159 base::Bind(&TestSBClient::CheckBrowseUrlOnIOThread, this, url)); |
1151 content::RunMessageLoop(); // Will stop in OnCheckBrowseUrlResult. | 1160 content::RunMessageLoop(); // Will stop in OnCheckBrowseUrlResult. |
1152 } | 1161 } |
1153 | 1162 |
| 1163 void CheckResourceUrl(const GURL& url) { |
| 1164 BrowserThread::PostTask( |
| 1165 BrowserThread::IO, FROM_HERE, |
| 1166 base::Bind(&TestSBClient::CheckResourceUrlOnIOThread, this, url)); |
| 1167 content::RunMessageLoop(); // Will stop in OnCheckResourceUrlResult. |
| 1168 } |
| 1169 |
1154 private: | 1170 private: |
1155 friend class base::RefCountedThreadSafe<TestSBClient>; | 1171 friend class base::RefCountedThreadSafe<TestSBClient>; |
1156 ~TestSBClient() override {} | 1172 ~TestSBClient() override {} |
1157 | 1173 |
1158 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { | 1174 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain) { |
1159 bool synchronous_safe_signal = | 1175 bool synchronous_safe_signal = |
1160 safe_browsing_service_->database_manager()->CheckDownloadUrl(url_chain, | 1176 safe_browsing_service_->database_manager()->CheckDownloadUrl(url_chain, |
1161 this); | 1177 this); |
1162 if (synchronous_safe_signal) { | 1178 if (synchronous_safe_signal) { |
1163 threat_type_ = SB_THREAT_TYPE_SAFE; | 1179 threat_type_ = SB_THREAT_TYPE_SAFE; |
1164 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1180 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1165 base::Bind(&TestSBClient::CheckDone, this)); | 1181 base::Bind(&TestSBClient::CheckDone, this)); |
1166 } | 1182 } |
1167 } | 1183 } |
1168 | 1184 |
1169 void CheckBrowseUrlOnIOThread(const GURL& url) { | 1185 void CheckBrowseUrlOnIOThread(const GURL& url) { |
1170 // The async CheckDone() hook will not be called when we have a synchronous | 1186 // The async CheckDone() hook will not be called when we have a synchronous |
1171 // safe signal, handle it right away. | 1187 // safe signal, handle it right away. |
1172 bool synchronous_safe_signal = | 1188 bool synchronous_safe_signal = |
1173 safe_browsing_service_->database_manager()->CheckBrowseUrl(url, this); | 1189 safe_browsing_service_->database_manager()->CheckBrowseUrl(url, this); |
1174 if (synchronous_safe_signal) { | 1190 if (synchronous_safe_signal) { |
1175 threat_type_ = SB_THREAT_TYPE_SAFE; | 1191 threat_type_ = SB_THREAT_TYPE_SAFE; |
1176 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1192 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1177 base::Bind(&TestSBClient::CheckDone, this)); | 1193 base::Bind(&TestSBClient::CheckDone, this)); |
1178 } | 1194 } |
1179 } | 1195 } |
1180 | 1196 |
| 1197 void CheckResourceUrlOnIOThread(const GURL& url) { |
| 1198 bool synchronous_safe_signal = |
| 1199 safe_browsing_service_->database_manager()->CheckResourceUrl(url, this); |
| 1200 if (synchronous_safe_signal) { |
| 1201 threat_type_ = SB_THREAT_TYPE_SAFE; |
| 1202 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1203 base::Bind(&TestSBClient::CheckDone, this)); |
| 1204 } |
| 1205 } |
| 1206 |
1181 // Called when the result of checking a download URL is known. | 1207 // Called when the result of checking a download URL is known. |
1182 void OnCheckDownloadUrlResult(const std::vector<GURL>& /* url_chain */, | 1208 void OnCheckDownloadUrlResult(const std::vector<GURL>& /* url_chain */, |
1183 SBThreatType threat_type) override { | 1209 SBThreatType threat_type) override { |
1184 threat_type_ = threat_type; | 1210 threat_type_ = threat_type; |
1185 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1211 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1186 base::Bind(&TestSBClient::CheckDone, this)); | 1212 base::Bind(&TestSBClient::CheckDone, this)); |
1187 } | 1213 } |
1188 | 1214 |
1189 // Called when the result of checking a browse URL is known. | 1215 // Called when the result of checking a browse URL is known. |
1190 void OnCheckBrowseUrlResult(const GURL& /* url */, | 1216 void OnCheckBrowseUrlResult(const GURL& /* url */, |
1191 SBThreatType threat_type, | 1217 SBThreatType threat_type, |
1192 const std::string& /* metadata */) override { | 1218 const std::string& /* metadata */) override { |
1193 threat_type_ = threat_type; | 1219 threat_type_ = threat_type; |
1194 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 1220 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
1195 base::Bind(&TestSBClient::CheckDone, this)); | 1221 base::Bind(&TestSBClient::CheckDone, this)); |
1196 } | 1222 } |
1197 | 1223 |
| 1224 // Called when the result of checking a resource URL is known. |
| 1225 void OnCheckResourceUrlResult(const GURL& /* url */, |
| 1226 SBThreatType threat_type, |
| 1227 const std::string& threat_hash) override { |
| 1228 threat_type_ = threat_type; |
| 1229 threat_hash_ = threat_hash; |
| 1230 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 1231 base::Bind(&TestSBClient::CheckDone, this)); |
| 1232 } |
| 1233 |
1198 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } | 1234 void CheckDone() { base::MessageLoopForUI::current()->QuitWhenIdle(); } |
1199 | 1235 |
1200 SBThreatType threat_type_; | 1236 SBThreatType threat_type_; |
| 1237 std::string threat_hash_; |
1201 SafeBrowsingService* safe_browsing_service_; | 1238 SafeBrowsingService* safe_browsing_service_; |
1202 | 1239 |
1203 DISALLOW_COPY_AND_ASSIGN(TestSBClient); | 1240 DISALLOW_COPY_AND_ASSIGN(TestSBClient); |
1204 }; | 1241 }; |
1205 | 1242 |
1206 } // namespace | 1243 } // namespace |
1207 | 1244 |
1208 // These tests use SafeBrowsingService::Client to directly interact with | 1245 // These tests use SafeBrowsingService::Client to directly interact with |
1209 // SafeBrowsingService. | 1246 // SafeBrowsingService. |
1210 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckDownloadUrl) { | 1247 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; | 1363 SBFullHashResult full_hash_result; |
1327 GenUrlFullhashResult(badbin_url, BINURL, &full_hash_result); | 1364 GenUrlFullhashResult(badbin_url, BINURL, &full_hash_result); |
1328 SetupResponseForUrl(badbin_url, full_hash_result); | 1365 SetupResponseForUrl(badbin_url, full_hash_result); |
1329 | 1366 |
1330 client->CheckDownloadUrl(badbin_urls); | 1367 client->CheckDownloadUrl(badbin_urls); |
1331 | 1368 |
1332 // Now, the badbin_url is not safe since it is added to download database. | 1369 // 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()); | 1370 EXPECT_EQ(SB_THREAT_TYPE_BINARY_MALWARE_URL, client->GetThreatType()); |
1334 } | 1371 } |
1335 | 1372 |
| 1373 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, CheckResourceUrl) { |
| 1374 const char* kBlacklistResource = "/blacklisted/script.js"; |
| 1375 GURL blacklist_resource = embedded_test_server()->GetURL(kBlacklistResource); |
| 1376 std::string blacklist_resource_hash; |
| 1377 const char* kMaliciousResource = "/malware/script.js"; |
| 1378 GURL malware_resource = embedded_test_server()->GetURL(kMaliciousResource); |
| 1379 std::string malware_resource_hash; |
| 1380 |
| 1381 { |
| 1382 SBFullHashResult full_hash; |
| 1383 GenUrlFullhashResult(blacklist_resource, RESOURCEBLACKLIST, &full_hash); |
| 1384 SetupResponseForUrl(blacklist_resource, full_hash); |
| 1385 blacklist_resource_hash = std::string(full_hash.hash.full_hash, |
| 1386 full_hash.hash.full_hash + 32); |
| 1387 } |
| 1388 { |
| 1389 SBFullHashResult full_hash; |
| 1390 GenUrlFullhashResult(malware_resource, MALWARE, &full_hash); |
| 1391 SetupResponseForUrl(malware_resource, full_hash); |
| 1392 full_hash.list_id = RESOURCEBLACKLIST; |
| 1393 SetupResponseForUrl(malware_resource, full_hash); |
| 1394 malware_resource_hash = std::string(full_hash.hash.full_hash, |
| 1395 full_hash.hash.full_hash + 32); |
| 1396 } |
| 1397 |
| 1398 scoped_refptr<TestSBClient> client(new TestSBClient); |
| 1399 client->CheckResourceUrl(blacklist_resource); |
| 1400 EXPECT_EQ(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, client->GetThreatType()); |
| 1401 EXPECT_EQ(blacklist_resource_hash, client->GetThreatHash()); |
| 1402 |
| 1403 // Since we're checking a resource url, we should receive result that it's |
| 1404 // a blacklisted resource, not a malware. |
| 1405 client = new TestSBClient; |
| 1406 client->CheckResourceUrl(malware_resource); |
| 1407 EXPECT_EQ(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, client->GetThreatType()); |
| 1408 EXPECT_EQ(malware_resource_hash, client->GetThreatHash()); |
| 1409 |
| 1410 client->CheckResourceUrl(embedded_test_server()->GetURL(kEmptyPage)); |
| 1411 EXPECT_EQ(SB_THREAT_TYPE_SAFE, client->GetThreatType()); |
| 1412 } |
| 1413 |
1336 #if defined(OS_WIN) | 1414 #if defined(OS_WIN) |
1337 // http://crbug.com/396409 | 1415 // http://crbug.com/396409 |
1338 #define MAYBE_CheckDownloadUrlTimedOut DISABLED_CheckDownloadUrlTimedOut | 1416 #define MAYBE_CheckDownloadUrlTimedOut DISABLED_CheckDownloadUrlTimedOut |
1339 #else | 1417 #else |
1340 #define MAYBE_CheckDownloadUrlTimedOut CheckDownloadUrlTimedOut | 1418 #define MAYBE_CheckDownloadUrlTimedOut CheckDownloadUrlTimedOut |
1341 #endif | 1419 #endif |
1342 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, | 1420 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, |
1343 MAYBE_CheckDownloadUrlTimedOut) { | 1421 MAYBE_CheckDownloadUrlTimedOut) { |
1344 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); | 1422 GURL badbin_url = embedded_test_server()->GetURL(kMalwareFile); |
1345 std::vector<GURL> badbin_urls(1, badbin_url); | 1423 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, | 1745 chrome::NOTIFICATION_SAFE_BROWSING_UPDATE_COMPLETE, |
1668 content::Source<SafeBrowsingDatabaseManager>( | 1746 content::Source<SafeBrowsingDatabaseManager>( |
1669 sb_service_->database_manager().get())); | 1747 sb_service_->database_manager().get())); |
1670 BrowserThread::PostTask( | 1748 BrowserThread::PostTask( |
1671 BrowserThread::IO, FROM_HERE, | 1749 BrowserThread::IO, FROM_HERE, |
1672 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); | 1750 base::Bind(&SafeBrowsingDatabaseManagerCookieTest::ForceUpdate, this)); |
1673 observer.Wait(); | 1751 observer.Wait(); |
1674 } | 1752 } |
1675 | 1753 |
1676 } // namespace safe_browsing | 1754 } // namespace safe_browsing |
OLD | NEW |