| 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 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> |
| 13 #include <utility> | 14 #include <utility> |
| 14 | 15 |
| 15 #include "base/bind.h" | 16 #include "base/bind.h" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 18 #include "base/files/scoped_temp_dir.h" | 19 #include "base/files/scoped_temp_dir.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 21 #include "base/memory/ptr_util.h" |
| 21 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 22 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
| (...skipping 1451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1474 EXPECT_TRUE(csd_service->enabled()); | 1475 EXPECT_TRUE(csd_service->enabled()); |
| 1475 | 1476 |
| 1476 // Delete the Profile. SBS stops again. | 1477 // Delete the Profile. SBS stops again. |
| 1477 pref_service2 = nullptr; | 1478 pref_service2 = nullptr; |
| 1478 profile2.reset(); | 1479 profile2.reset(); |
| 1479 EXPECT_FALSE(sb_service->enabled_by_prefs()); | 1480 EXPECT_FALSE(sb_service->enabled_by_prefs()); |
| 1480 WaitForIOAndCheckEnabled(sb_service, false); | 1481 WaitForIOAndCheckEnabled(sb_service, false); |
| 1481 EXPECT_FALSE(csd_service->enabled()); | 1482 EXPECT_FALSE(csd_service->enabled()); |
| 1482 } | 1483 } |
| 1483 | 1484 |
| 1484 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, MalwareWebSocketBlocked) { | 1485 // Parameterised fixture to permit running the same test for Window and Worker |
| 1486 // scopes. |
| 1487 class SafeBrowsingServiceWebSocketTest |
| 1488 : public ::testing::WithParamInterface<std::string>, |
| 1489 public SafeBrowsingServiceTest {}; |
| 1490 |
| 1491 IN_PROC_BROWSER_TEST_P(SafeBrowsingServiceWebSocketTest, |
| 1492 MalwareWebSocketBlocked) { |
| 1485 // This test currently only passes when the network service is enabled. | 1493 // This test currently only passes when the network service is enabled. |
| 1486 if (!base::FeatureList::IsEnabled(features::kNetworkService)) | 1494 if (!base::FeatureList::IsEnabled(features::kNetworkService)) |
| 1487 return; | 1495 return; |
| 1488 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); | 1496 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); |
| 1489 // This constructs the URL with the same logic as malware_websocket.html. | 1497 // This constructs the URL with the same logic as malware_websocket.html. |
| 1490 GURL resolved = main_url.Resolve("/safe_browsing/malware-ws"); | 1498 GURL resolved = main_url.Resolve("/safe_browsing/malware-ws"); |
| 1491 GURL::Replacements replace_scheme; | 1499 GURL::Replacements replace_scheme; |
| 1492 replace_scheme.SetScheme("ws", url::Component(0, strlen("ws"))); | 1500 replace_scheme.SetScheme("ws", url::Component(0, strlen("ws"))); |
| 1493 GURL websocket_url = resolved.ReplaceComponents(replace_scheme); | 1501 GURL websocket_url = resolved.ReplaceComponents(replace_scheme); |
| 1494 | 1502 |
| 1503 GURL::Replacements add_query; |
| 1504 std::string query = "type=" + GetParam(); |
| 1505 add_query.SetQueryStr(query); |
| 1506 GURL main_url_with_query = main_url.ReplaceComponents(add_query); |
| 1507 |
| 1495 // Add the WebSocket url as malware. | 1508 // Add the WebSocket url as malware. |
| 1496 SBFullHashResult uws_full_hash; | 1509 SBFullHashResult uws_full_hash; |
| 1497 GenUrlFullHashResult(websocket_url, MALWARE, &uws_full_hash); | 1510 GenUrlFullHashResult(websocket_url, MALWARE, &uws_full_hash); |
| 1498 SetupResponseForUrl(websocket_url, uws_full_hash); | 1511 SetupResponseForUrl(websocket_url, uws_full_hash); |
| 1499 | 1512 |
| 1500 // Brute force method for waiting for the interstitial to be displayed. | 1513 // Brute force method for waiting for the interstitial to be displayed. |
| 1501 content::WindowedNotificationObserver load_stop_observer( | 1514 content::WindowedNotificationObserver load_stop_observer( |
| 1502 content::NOTIFICATION_ALL, | 1515 content::NOTIFICATION_ALL, |
| 1503 base::Bind( | 1516 base::Bind( |
| 1504 [](SafeBrowsingServiceTest* self, | 1517 [](SafeBrowsingServiceTest* self, |
| 1505 const content::NotificationSource& source, | 1518 const content::NotificationSource& source, |
| 1506 const content::NotificationDetails& details) { | 1519 const content::NotificationDetails& details) { |
| 1507 return self->ShowingInterstitialPage(); | 1520 return self->ShowingInterstitialPage(); |
| 1508 }, | 1521 }, |
| 1509 base::Unretained(this))); | 1522 base::Unretained(this))); |
| 1510 | 1523 |
| 1511 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(websocket_url))); | 1524 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(websocket_url))); |
| 1512 ui_test_utils::NavigateToURL(browser(), main_url); | 1525 ui_test_utils::NavigateToURL(browser(), main_url_with_query); |
| 1513 | 1526 |
| 1514 // If the interstitial fails to be displayed, the test will hang here. | 1527 // If the interstitial fails to be displayed, the test will hang here. |
| 1515 load_stop_observer.Wait(); | 1528 load_stop_observer.Wait(); |
| 1516 | 1529 |
| 1517 EXPECT_TRUE(ShowingInterstitialPage()); | 1530 EXPECT_TRUE(ShowingInterstitialPage()); |
| 1518 EXPECT_TRUE(got_hit_report()); | 1531 EXPECT_TRUE(got_hit_report()); |
| 1519 } | 1532 } |
| 1520 | 1533 |
| 1534 INSTANTIATE_TEST_CASE_P( |
| 1535 /* no prefix */, |
| 1536 SafeBrowsingServiceWebSocketTest, |
| 1537 ::testing::Values("window", "worker")); |
| 1538 |
| 1521 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, UnknownWebSocketNotBlocked) { | 1539 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, UnknownWebSocketNotBlocked) { |
| 1522 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); | 1540 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); |
| 1523 | 1541 |
| 1524 auto expected_title = base::ASCIIToUTF16("COMPLETED"); | 1542 auto expected_title = base::ASCIIToUTF16("COMPLETED"); |
| 1525 content::TitleWatcher title_watcher( | 1543 content::TitleWatcher title_watcher( |
| 1526 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); | 1544 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); |
| 1527 | 1545 |
| 1528 // Load the parent page without marking the WebSocket as malware. | 1546 // Load the parent page without marking the WebSocket as malware. |
| 1529 ui_test_utils::NavigateToURL(browser(), main_url); | 1547 ui_test_utils::NavigateToURL(browser(), main_url); |
| 1530 | 1548 |
| (...skipping 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2240 { | 2258 { |
| 2241 scoped_refptr<TestSBClient> client(new TestSBClient); | 2259 scoped_refptr<TestSBClient> client(new TestSBClient); |
| 2242 | 2260 |
| 2243 MarkUrlForUwsUnexpired(bad_url); | 2261 MarkUrlForUwsUnexpired(bad_url); |
| 2244 | 2262 |
| 2245 client->CheckBrowseUrl(bad_url); | 2263 client->CheckBrowseUrl(bad_url); |
| 2246 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, client->GetThreatType()); | 2264 EXPECT_EQ(SB_THREAT_TYPE_URL_MALWARE, client->GetThreatType()); |
| 2247 } | 2265 } |
| 2248 } | 2266 } |
| 2249 | 2267 |
| 2268 // Parameterised fixture to permit running the same test for Window and Worker |
| 2269 // scopes. |
| 2270 class V4SafeBrowsingServiceWebSocketTest |
| 2271 : public ::testing::WithParamInterface<std::string>, |
| 2272 public V4SafeBrowsingServiceTest {}; |
| 2273 |
| 2250 // This is almost identical to | 2274 // This is almost identical to |
| 2251 // SafeBrowsingServiceTest.MalwareWebSocketBlocked. That test will be deleted | 2275 // SafeBrowsingServiceWebSocketTest.MalwareWebSocketBlocked. That test will be |
| 2252 // when the old database backend stops being used. | 2276 // deleted when the old database backend is removed. |
| 2253 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, MalwareWebSocketBlocked) { | 2277 IN_PROC_BROWSER_TEST_P(V4SafeBrowsingServiceWebSocketTest, |
| 2278 MalwareWebSocketBlocked) { |
| 2254 // This test currently only passes when the network service is enabled. | 2279 // This test currently only passes when the network service is enabled. |
| 2255 if (!base::FeatureList::IsEnabled(features::kNetworkService)) | 2280 if (!base::FeatureList::IsEnabled(features::kNetworkService)) |
| 2256 return; | 2281 return; |
| 2257 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); | 2282 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); |
| 2258 // This constructs the URL with the same logic as malware_websocket.html. | 2283 // This constructs the URL with the same logic as malware_websocket.html. |
| 2259 GURL resolved = main_url.Resolve("/safe_browsing/malware-ws"); | 2284 GURL resolved = main_url.Resolve("/safe_browsing/malware-ws"); |
| 2260 GURL::Replacements replace_scheme; | 2285 GURL::Replacements replace_scheme; |
| 2261 replace_scheme.SetScheme("ws", url::Component(0, strlen("ws"))); | 2286 replace_scheme.SetScheme("ws", url::Component(0, strlen("ws"))); |
| 2262 GURL websocket_url = resolved.ReplaceComponents(replace_scheme); | 2287 GURL websocket_url = resolved.ReplaceComponents(replace_scheme); |
| 2263 | 2288 |
| 2289 GURL::Replacements add_query; |
| 2290 std::string query = "type=" + GetParam(); |
| 2291 add_query.SetQueryStr(query); |
| 2292 GURL main_url_with_query = main_url.ReplaceComponents(add_query); |
| 2293 |
| 2264 MarkUrlForMalwareUnexpired(websocket_url); | 2294 MarkUrlForMalwareUnexpired(websocket_url); |
| 2265 | 2295 |
| 2266 // Brute force method for waiting for the interstitial to be displayed. | 2296 // Brute force method for waiting for the interstitial to be displayed. |
| 2267 content::WindowedNotificationObserver load_stop_observer( | 2297 content::WindowedNotificationObserver load_stop_observer( |
| 2268 content::NOTIFICATION_ALL, | 2298 content::NOTIFICATION_ALL, |
| 2269 base::Bind( | 2299 base::Bind( |
| 2270 [](SafeBrowsingServiceTest* self, | 2300 [](SafeBrowsingServiceTest* self, |
| 2271 const content::NotificationSource& source, | 2301 const content::NotificationSource& source, |
| 2272 const content::NotificationDetails& details) { | 2302 const content::NotificationDetails& details) { |
| 2273 return self->ShowingInterstitialPage(); | 2303 return self->ShowingInterstitialPage(); |
| 2274 }, | 2304 }, |
| 2275 base::Unretained(this))); | 2305 base::Unretained(this))); |
| 2276 | 2306 |
| 2277 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(websocket_url))); | 2307 EXPECT_CALL(observer_, OnSafeBrowsingHit(IsUnsafeResourceFor(websocket_url))); |
| 2278 ui_test_utils::NavigateToURL(browser(), main_url); | 2308 ui_test_utils::NavigateToURL(browser(), main_url_with_query); |
| 2279 | 2309 |
| 2280 // If the interstitial fails to be displayed, the test will hang here. | 2310 // If the interstitial fails to be displayed, the test will hang here. |
| 2281 load_stop_observer.Wait(); | 2311 load_stop_observer.Wait(); |
| 2282 | 2312 |
| 2283 EXPECT_TRUE(ShowingInterstitialPage()); | 2313 EXPECT_TRUE(ShowingInterstitialPage()); |
| 2284 EXPECT_TRUE(got_hit_report()); | 2314 EXPECT_TRUE(got_hit_report()); |
| 2285 EXPECT_EQ(websocket_url, hit_report().malicious_url); | 2315 EXPECT_EQ(websocket_url, hit_report().malicious_url); |
| 2286 EXPECT_EQ(main_url, hit_report().page_url); | 2316 EXPECT_EQ(main_url_with_query, hit_report().page_url); |
| 2287 EXPECT_TRUE(hit_report().is_subresource); | 2317 EXPECT_TRUE(hit_report().is_subresource); |
| 2288 } | 2318 } |
| 2289 | 2319 |
| 2320 // TODO(ricea): Test SharedWorker and ServiceWorker scopes as well. |
| 2321 INSTANTIATE_TEST_CASE_P(/* no prefix */, |
| 2322 V4SafeBrowsingServiceWebSocketTest, |
| 2323 ::testing::Values("window", "worker")); |
| 2324 |
| 2290 // Identical to SafeBrowsingServiceTest.UnknownWebSocketNotBlocked. Uses the | 2325 // Identical to SafeBrowsingServiceTest.UnknownWebSocketNotBlocked. Uses the |
| 2291 // V4 database backend. | 2326 // V4 database backend. |
| 2292 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnknownWebSocketNotBlocked) { | 2327 IN_PROC_BROWSER_TEST_F(V4SafeBrowsingServiceTest, UnknownWebSocketNotBlocked) { |
| 2293 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); | 2328 GURL main_url = embedded_test_server()->GetURL(kMalwareWebSocketPage); |
| 2294 | 2329 |
| 2295 auto expected_title = base::ASCIIToUTF16("COMPLETED"); | 2330 auto expected_title = base::ASCIIToUTF16("COMPLETED"); |
| 2296 content::TitleWatcher title_watcher( | 2331 content::TitleWatcher title_watcher( |
| 2297 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); | 2332 browser()->tab_strip_model()->GetActiveWebContents(), expected_title); |
| 2298 | 2333 |
| 2299 // Load the parent page without marking the WebSocket as malware. | 2334 // Load the parent page without marking the WebSocket as malware. |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2472 } | 2507 } |
| 2473 | 2508 |
| 2474 INSTANTIATE_TEST_CASE_P( | 2509 INSTANTIATE_TEST_CASE_P( |
| 2475 MaybeSetMetadata, | 2510 MaybeSetMetadata, |
| 2476 V4SafeBrowsingServiceMetadataTest, | 2511 V4SafeBrowsingServiceMetadataTest, |
| 2477 testing::Values(ThreatPatternType::NONE, | 2512 testing::Values(ThreatPatternType::NONE, |
| 2478 ThreatPatternType::MALWARE_LANDING, | 2513 ThreatPatternType::MALWARE_LANDING, |
| 2479 ThreatPatternType::MALWARE_DISTRIBUTION)); | 2514 ThreatPatternType::MALWARE_DISTRIBUTION)); |
| 2480 | 2515 |
| 2481 } // namespace safe_browsing | 2516 } // namespace safe_browsing |
| OLD | NEW |