OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include <map> | 5 #include <map> |
6 #include <memory> | 6 #include <memory> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
17 #include "base/strings/stringprintf.h" | |
17 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/metrics/subprocess_metrics_provider.h" | 20 #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
20 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h" | 21 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h" |
21 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" | 22 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
22 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" | 23 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" |
23 #include "chrome/browser/ui/browser.h" | 24 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/browser/ui/browser_commands.h" | 25 #include "chrome/browser/ui/browser_commands.h" |
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 26 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
26 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
40 #include "content/public/browser/notification_service.h" | 41 #include "content/public/browser/notification_service.h" |
41 #include "content/public/browser/notification_types.h" | 42 #include "content/public/browser/notification_types.h" |
42 #include "content/public/browser/render_frame_host.h" | 43 #include "content/public/browser/render_frame_host.h" |
43 #include "content/public/browser/web_contents.h" | 44 #include "content/public/browser/web_contents.h" |
44 #include "content/public/common/content_switches.h" | 45 #include "content/public/common/content_switches.h" |
45 #include "content/public/test/browser_test_utils.h" | 46 #include "content/public/test/browser_test_utils.h" |
46 #include "content/public/test/test_navigation_observer.h" | 47 #include "content/public/test/test_navigation_observer.h" |
47 #include "content/public/test/test_utils.h" | 48 #include "content/public/test/test_utils.h" |
48 #include "net/dns/mock_host_resolver.h" | 49 #include "net/dns/mock_host_resolver.h" |
49 #include "net/test/embedded_test_server/embedded_test_server.h" | 50 #include "net/test/embedded_test_server/embedded_test_server.h" |
51 #include "net/test/spawned_test_server/spawned_test_server.h" | |
52 #include "net/test/test_data_directory.h" | |
50 #include "testing/gmock/include/gmock/gmock.h" | 53 #include "testing/gmock/include/gmock/gmock.h" |
51 #include "testing/gtest/include/gtest/gtest.h" | 54 #include "testing/gtest/include/gtest/gtest.h" |
52 | 55 |
53 namespace { | 56 namespace { |
54 | 57 |
55 // The path to a multi-frame document used for tests. | 58 // The path to a multi-frame document used for tests. |
56 static constexpr const char kTestFrameSetPath[] = | 59 static constexpr const char kTestFrameSetPath[] = |
57 "subresource_filter/frame_set.html"; | 60 "subresource_filter/frame_set.html"; |
58 | 61 |
59 // Names of DocumentLoad histograms. | 62 // Names of DocumentLoad histograms. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 | 251 |
249 base::FilePath test_data_dir; | 252 base::FilePath test_data_dir; |
250 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 253 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
251 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 254 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
252 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); | 255 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); |
253 host_resolver()->AddRule("*", "127.0.0.1"); | 256 host_resolver()->AddRule("*", "127.0.0.1"); |
254 content::SetupCrossSiteRedirector(embedded_test_server()); | 257 content::SetupCrossSiteRedirector(embedded_test_server()); |
255 ASSERT_TRUE(embedded_test_server()->Start()); | 258 ASSERT_TRUE(embedded_test_server()->Start()); |
256 } | 259 } |
257 | 260 |
258 GURL GetTestUrl(const std::string& path) { | 261 GURL GetTestUrl(const std::string& path) { |
engedy
2017/03/06 16:31:01
nit: s/path/relative_url/ now that we are passing
Charlie Harrison
2017/03/06 19:22:11
Done.
| |
259 return embedded_test_server()->base_url().Resolve(path); | 262 return embedded_test_server()->base_url().Resolve(path); |
260 } | 263 } |
261 | 264 |
262 void ConfigureAsPhishingURL(const GURL& url) { | 265 void ConfigureAsPhishingURL(const GURL& url) { |
263 fake_safe_browsing_database_->AddBlacklistedURL( | 266 fake_safe_browsing_database_->AddBlacklistedURL( |
264 url, safe_browsing::SB_THREAT_TYPE_URL_PHISHING); | 267 url, safe_browsing::SB_THREAT_TYPE_URL_PHISHING); |
265 } | 268 } |
266 | 269 |
267 content::WebContents* web_contents() { | 270 content::WebContents* web_contents() { |
268 return browser()->tab_strip_model()->GetActiveWebContents(); | 271 return browser()->tab_strip_model()->GetActiveWebContents(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 : SubresourceFilterBrowserTestImpl(true, false) {} | 369 : SubresourceFilterBrowserTestImpl(true, false) {} |
367 }; | 370 }; |
368 | 371 |
369 class SubresourceFilterWhitelistSiteOnReloadBrowserTest | 372 class SubresourceFilterWhitelistSiteOnReloadBrowserTest |
370 : public SubresourceFilterBrowserTestImpl { | 373 : public SubresourceFilterBrowserTestImpl { |
371 public: | 374 public: |
372 SubresourceFilterWhitelistSiteOnReloadBrowserTest() | 375 SubresourceFilterWhitelistSiteOnReloadBrowserTest() |
373 : SubresourceFilterBrowserTestImpl(false, true) {} | 376 : SubresourceFilterBrowserTestImpl(false, true) {} |
374 }; | 377 }; |
375 | 378 |
379 enum WebSocketCreationPolicy { | |
380 IN_MAIN_FRAME, | |
381 IN_WORKER, | |
382 }; | |
383 class SubresourceFilterWebSocketBrowserTest | |
384 : public SubresourceFilterBrowserTestImpl, | |
385 public ::testing::WithParamInterface<WebSocketCreationPolicy> { | |
386 public: | |
387 SubresourceFilterWebSocketBrowserTest() | |
388 : SubresourceFilterBrowserTestImpl(false, false) {} | |
389 | |
390 void SetUpOnMainThread() override { | |
391 SubresourceFilterBrowserTestImpl::SetUpOnMainThread(); | |
392 websocket_test_server_ = base::MakeUnique<net::SpawnedTestServer>( | |
393 net::SpawnedTestServer::TYPE_WS, net::SpawnedTestServer::kLocalhost, | |
394 net::GetWebSocketTestDataDirectory()); | |
395 ASSERT_TRUE(websocket_test_server_->Start()); | |
396 } | |
397 | |
398 net::SpawnedTestServer* websocket_test_server() { | |
399 return websocket_test_server_.get(); | |
400 } | |
401 | |
402 GURL GetWebSocketUrl(const std::string& path) { | |
403 GURL::Replacements replacements; | |
404 replacements.SetSchemeStr("ws"); | |
405 return websocket_test_server_->GetURL(path).ReplaceComponents(replacements); | |
406 } | |
407 | |
408 private: | |
409 std::unique_ptr<net::SpawnedTestServer> websocket_test_server_; | |
410 }; | |
411 | |
376 // Tests ----------------------------------------------------------------------- | 412 // Tests ----------------------------------------------------------------------- |
377 | 413 |
378 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, MainFrameActivation) { | 414 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, MainFrameActivation) { |
379 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html")); | 415 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html")); |
380 ConfigureAsPhishingURL(url); | 416 ConfigureAsPhishingURL(url); |
381 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix( | 417 ASSERT_NO_FATAL_FAILURE(SetRulesetToDisallowURLsWithPathSuffix( |
382 "suffix-that-does-not-match-anything")); | 418 "suffix-that-does-not-match-anything")); |
383 ui_test_utils::NavigateToURL(browser(), url); | 419 ui_test_utils::NavigateToURL(browser(), url); |
384 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); | 420 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
385 | 421 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
639 "a.com", "/subresource_filter/frame_cross_site_set.html")); | 675 "a.com", "/subresource_filter/frame_cross_site_set.html")); |
640 ConfigureAsPhishingURL(a_url); | 676 ConfigureAsPhishingURL(a_url); |
641 ASSERT_NO_FATAL_FAILURE( | 677 ASSERT_NO_FATAL_FAILURE( |
642 SetRulesetWithRules({testing::CreateSuffixRule("included_script.js"), | 678 SetRulesetWithRules({testing::CreateSuffixRule("included_script.js"), |
643 testing::CreateWhitelistRuleForDocument("c.com")})); | 679 testing::CreateWhitelistRuleForDocument("c.com")})); |
644 ui_test_utils::NavigateToURL(browser(), a_url); | 680 ui_test_utils::NavigateToURL(browser(), a_url); |
645 ExpectParsedScriptElementLoadedStatusInFrames( | 681 ExpectParsedScriptElementLoadedStatusInFrames( |
646 std::vector<const char*>{"b", "d"}, {false, true}); | 682 std::vector<const char*>{"b", "d"}, {false, true}); |
647 } | 683 } |
648 | 684 |
685 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { | |
engedy
2017/03/06 14:12:46
Have you considered implementing some of these (al
Charlie Harrison
2017/03/06 14:48:23
Yeah I have a layout test in the follow up CL.
| |
686 GURL url(GetTestUrl( | |
687 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", | |
688 GetParam() == IN_WORKER ? "inWorker" : ""))); | |
689 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); | |
690 ConfigureAsPhishingURL(url); | |
691 ASSERT_NO_FATAL_FAILURE( | |
692 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); | |
693 ui_test_utils::NavigateToURL(browser(), url); | |
694 | |
695 bool websocket_connection_succeeded = false; | |
696 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
engedy
2017/03/06 16:31:01
nit: To avoid repeating this, let's make this a he
Charlie Harrison
2017/03/06 19:22:11
Done.
| |
697 browser()->tab_strip_model()->GetActiveWebContents(), | |
698 base::StringPrintf("connectWebSocket('%s');", | |
699 websocket_url.spec().c_str()), | |
700 &websocket_connection_succeeded)); | |
701 EXPECT_FALSE(websocket_connection_succeeded); | |
702 } | |
703 | |
704 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, | |
705 DoNotBlockWebSocketNoActivatedFrame) { | |
706 GURL url(GetTestUrl( | |
707 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", | |
708 GetParam() == IN_WORKER ? "inWorker" : ""))); | |
709 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); | |
710 ASSERT_NO_FATAL_FAILURE( | |
711 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); | |
712 ui_test_utils::NavigateToURL(browser(), url); | |
713 | |
714 bool websocket_connection_succeeded = false; | |
715 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
716 browser()->tab_strip_model()->GetActiveWebContents(), | |
717 base::StringPrintf("connectWebSocket('%s');", | |
718 websocket_url.spec().c_str()), | |
719 &websocket_connection_succeeded)); | |
720 EXPECT_TRUE(websocket_connection_succeeded); | |
721 } | |
722 | |
723 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, | |
724 DoNotBlockWebSocketInActivatedFrameWithNoRule) { | |
725 GURL url(GetTestUrl( | |
726 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", | |
727 GetParam() == IN_WORKER ? "inWorker" : ""))); | |
728 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); | |
729 ConfigureAsPhishingURL(url); | |
730 ui_test_utils::NavigateToURL(browser(), url); | |
731 | |
732 bool websocket_connection_succeeded = false; | |
733 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
734 browser()->tab_strip_model()->GetActiveWebContents(), | |
735 base::StringPrintf("connectWebSocket('%s');", | |
736 websocket_url.spec().c_str()), | |
737 &websocket_connection_succeeded)); | |
738 EXPECT_TRUE(websocket_connection_succeeded); | |
739 } | |
740 | |
741 INSTANTIATE_TEST_CASE_P( | |
742 /* no prefix */, | |
743 SubresourceFilterWebSocketBrowserTest, | |
744 ::testing::Values(WebSocketCreationPolicy::IN_WORKER, | |
745 WebSocketCreationPolicy::IN_MAIN_FRAME)); | |
746 | |
649 // Tests checking how histograms are recorded. --------------------------------- | 747 // Tests checking how histograms are recorded. --------------------------------- |
650 | 748 |
651 namespace { | 749 namespace { |
652 | 750 |
653 void ExpectHistogramsAreRecordedForTestFrameSet( | 751 void ExpectHistogramsAreRecordedForTestFrameSet( |
654 const base::HistogramTester& tester, | 752 const base::HistogramTester& tester, |
655 bool expect_performance_measurements) { | 753 bool expect_performance_measurements) { |
656 const bool time_recorded = | 754 const bool time_recorded = |
657 expect_performance_measurements && ScopedThreadTimers::IsSupported(); | 755 expect_performance_measurements && ScopedThreadTimers::IsSupported(); |
658 | 756 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
898 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); | 996 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); |
899 | 997 |
900 tester.ExpectTotalCount( | 998 tester.ExpectTotalCount( |
901 internal::kHistogramSubresourceFilterActivationDecisionReload, 1); | 999 internal::kHistogramSubresourceFilterActivationDecisionReload, 1); |
902 tester.ExpectBucketCount( | 1000 tester.ExpectBucketCount( |
903 internal::kHistogramSubresourceFilterActivationDecisionReload, | 1001 internal::kHistogramSubresourceFilterActivationDecisionReload, |
904 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); | 1002 static_cast<int>(ActivationDecision::URL_WHITELISTED), 1); |
905 } | 1003 } |
906 | 1004 |
907 } // namespace subresource_filter | 1005 } // namespace subresource_filter |
OLD | NEW |