Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: chrome/browser/subresource_filter/subresource_filter_browsertest.cc

Issue 2795053002: [subresource_filter] Implement the "Smart" UI on Android (Closed)
Patch Set: rebase on #463637 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/strings/string_piece.h" 17 #include "base/strings/string_piece.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #include "base/test/histogram_tester.h" 20 #include "base/test/histogram_tester.h"
21 #include "base/test/simple_test_clock.h"
21 #include "chrome/browser/browser_process.h" 22 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 23 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
23 #include "chrome/browser/metrics/subprocess_metrics_provider.h" 24 #include "chrome/browser/metrics/subprocess_metrics_provider.h"
24 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h" 25 #include "chrome/browser/page_load_metrics/observers/subresource_filter_metrics_ observer.h"
25 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" 26 #include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
26 #include "chrome/browser/safe_browsing/v4_test_utils.h" 27 #include "chrome/browser/safe_browsing/v4_test_utils.h"
27 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" 28 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
29 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ manager.h"
30 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ manager_factory.h"
28 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" 31 #include "chrome/browser/subresource_filter/test_ruleset_publisher.h"
29 #include "chrome/browser/ui/browser.h" 32 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_commands.h" 33 #include "chrome/browser/ui/browser_commands.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" 34 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_paths.h" 35 #include "chrome/common/chrome_paths.h"
33 #include "chrome/common/url_constants.h" 36 #include "chrome/common/url_constants.h"
34 #include "chrome/test/base/in_process_browser_test.h" 37 #include "chrome/test/base/in_process_browser_test.h"
35 #include "chrome/test/base/ui_test_utils.h" 38 #include "chrome/test/base/ui_test_utils.h"
36 #include "components/content_settings/core/browser/host_content_settings_map.h" 39 #include "components/content_settings/core/browser/host_content_settings_map.h"
37 #include "components/content_settings/core/common/content_settings.h" 40 #include "components/content_settings/core/common/content_settings.h"
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 269
267 void SetUpOnMainThread() override { 270 void SetUpOnMainThread() override {
268 SetUpActivationFeature(); 271 SetUpActivationFeature();
269 base::FilePath test_data_dir; 272 base::FilePath test_data_dir;
270 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); 273 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
271 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); 274 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
272 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure"); 275 host_resolver()->AddSimulatedFailure("host-with-dns-lookup-failure");
273 host_resolver()->AddRule("*", "127.0.0.1"); 276 host_resolver()->AddRule("*", "127.0.0.1");
274 content::SetupCrossSiteRedirector(embedded_test_server()); 277 content::SetupCrossSiteRedirector(embedded_test_server());
275 ASSERT_TRUE(embedded_test_server()->Start()); 278 ASSERT_TRUE(embedded_test_server()->Start());
279
280 settings_manager_ =
281 SubresourceFilterContentSettingsManagerFactory::EnsureForProfile(
282 browser()->profile());
283 #if defined(OS_ANDROID)
284 EXPECT_TRUE(settings_manager->should_use_smart_ui());
285 #endif
276 } 286 }
277 287
278 virtual void SetUpActivationFeature() { 288 virtual void SetUpActivationFeature() {
279 ToggleFeatures(base::MakeUnique<ScopedSubresourceFilterFeatureToggle>( 289 ToggleFeatures(base::MakeUnique<ScopedSubresourceFilterFeatureToggle>(
280 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 290 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
281 kActivationScopeActivationList, kActivationListPhishingInterstitial, 291 kActivationScopeActivationList, kActivationListPhishingInterstitial,
282 measure_performance_ ? "1" : "0", "" /* suppress_notifications */, 292 measure_performance_ ? "1" : "0", "" /* suppress_notifications */,
283 whitelist_site_on_reload_ ? "true" : "false")); 293 whitelist_site_on_reload_ ? "true" : "false"));
284 } 294 }
285 295
(...skipping 18 matching lines...) Expand all
304 314
305 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { 315 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) {
306 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSubresourceFilterId(), 316 MarkUrlAsMatchingListWithId(url, safe_browsing::GetUrlSubresourceFilterId(),
307 safe_browsing::ThreatPatternType::NONE); 317 safe_browsing::ThreatPatternType::NONE);
308 } 318 }
309 319
310 content::WebContents* web_contents() { 320 content::WebContents* web_contents() {
311 return browser()->tab_strip_model()->GetActiveWebContents(); 321 return browser()->tab_strip_model()->GetActiveWebContents();
312 } 322 }
313 323
324 SubresourceFilterContentSettingsManager* settings_manager() {
325 return settings_manager_;
326 }
327
314 content::RenderFrameHost* FindFrameByName(const std::string& name) { 328 content::RenderFrameHost* FindFrameByName(const std::string& name) {
315 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) { 329 for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) {
316 if (frame->GetFrameName() == name) 330 if (frame->GetFrameName() == name)
317 return frame; 331 return frame;
318 } 332 }
319 return nullptr; 333 return nullptr;
320 } 334 }
321 335
322 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) { 336 bool WasParsedScriptElementLoaded(content::RenderFrameHost* rfh) {
323 DCHECK(rfh); 337 DCHECK(rfh);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 TestRulesetPublisher test_ruleset_publisher_; 407 TestRulesetPublisher test_ruleset_publisher_;
394 const bool measure_performance_; 408 const bool measure_performance_;
395 const bool whitelist_site_on_reload_; 409 const bool whitelist_site_on_reload_;
396 410
397 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_; 411 std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory> sb_factory_;
398 // Owned by the V4Database. 412 // Owned by the V4Database.
399 safe_browsing::TestV4DatabaseFactory* v4_db_factory_; 413 safe_browsing::TestV4DatabaseFactory* v4_db_factory_;
400 // Owned by the V4GetHashProtocolManager. 414 // Owned by the V4GetHashProtocolManager.
401 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; 415 safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_;
402 416
417 // Owned by the profile.
418 SubresourceFilterContentSettingsManager* settings_manager_;
419
403 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTestImpl); 420 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTestImpl);
404 }; 421 };
405 422
406 class SubresourceFilterBrowserTest : public SubresourceFilterBrowserTestImpl { 423 class SubresourceFilterBrowserTest : public SubresourceFilterBrowserTestImpl {
407 public: 424 public:
408 SubresourceFilterBrowserTest() 425 SubresourceFilterBrowserTest()
409 : SubresourceFilterBrowserTestImpl(false, false) {} 426 : SubresourceFilterBrowserTestImpl(false, false) {}
410 }; 427 };
411 428
412 // TODO(pkalinnikov): It should be possible to have only one fixture, i.e., 429 // TODO(pkalinnikov): It should be possible to have only one fixture, i.e.,
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 // page load right after start-up. 851 // page load right after start-up.
835 ui_test_utils::NavigateToURL(browser(), url); 852 ui_test_utils::NavigateToURL(browser(), url);
836 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 853 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
837 } 854 }
838 855
839 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, 856 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
840 PromptShownAgainOnNextNavigation) { 857 PromptShownAgainOnNextNavigation) {
841 ASSERT_NO_FATAL_FAILURE( 858 ASSERT_NO_FATAL_FAILURE(
842 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); 859 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
843 GURL url(GetTestUrl(kTestFrameSetPath)); 860 GURL url(GetTestUrl(kTestFrameSetPath));
861 GURL a_url(embedded_test_server()->GetURL(
862 "a.com", "/subresource_filter/frame_with_included_script.html"));
844 ConfigureAsPhishingURL(url); 863 ConfigureAsPhishingURL(url);
845 base::HistogramTester tester; 864 base::HistogramTester tester;
846 ui_test_utils::NavigateToURL(browser(), url); 865 ui_test_utils::NavigateToURL(browser(), url);
847 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, 866 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown,
848 1); 867 1);
849 // Check that the bubble is not shown again for this navigation. 868 // Check that the bubble is not shown again for this navigation.
850 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); 869 EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five")));
851 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, 870 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown,
852 1); 871 1);
853 // Check that bubble is shown for new navigation. 872 // Check that bubble is shown for new navigation. Must be cross site to avoid
854 ui_test_utils::NavigateToURL(browser(), url); 873 // triggering smart UI on Android.
874 ConfigureAsPhishingURL(a_url);
875 ui_test_utils::NavigateToURL(browser(), a_url);
855 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, 876 tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown,
856 2); 877 settings_manager()->should_use_smart_ui() ? 1 : 2);
engedy 2017/04/12 14:02:50 I'm not sure I understand. If it's cross site, sho
Charlie Harrison 2017/04/12 17:53:44 Yeah, this is an error.
857 } 878 }
858 879
859 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, 880 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
860 CrossSiteSubFrameActivationWithoutWhitelist) { 881 CrossSiteSubFrameActivationWithoutWhitelist) {
861 GURL a_url(embedded_test_server()->GetURL( 882 GURL a_url(embedded_test_server()->GetURL(
862 "a.com", "/subresource_filter/frame_cross_site_set.html")); 883 "a.com", "/subresource_filter/frame_cross_site_set.html"));
863 ConfigureAsPhishingURL(a_url); 884 ConfigureAsPhishingURL(a_url);
864 ASSERT_NO_FATAL_FAILURE( 885 ASSERT_NO_FATAL_FAILURE(
865 SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); 886 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
866 ui_test_utils::NavigateToURL(browser(), a_url); 887 ui_test_utils::NavigateToURL(browser(), a_url);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 996 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
976 997
977 // A cross site blacklisted navigation should stay activated, however. 998 // A cross site blacklisted navigation should stay activated, however.
978 GURL a_url(embedded_test_server()->GetURL( 999 GURL a_url(embedded_test_server()->GetURL(
979 "a.com", "/subresource_filter/frame_with_included_script.html")); 1000 "a.com", "/subresource_filter/frame_with_included_script.html"));
980 ConfigureAsPhishingURL(a_url); 1001 ConfigureAsPhishingURL(a_url);
981 ui_test_utils::NavigateToURL(browser(), a_url); 1002 ui_test_utils::NavigateToURL(browser(), a_url);
982 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); 1003 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
983 } 1004 }
984 1005
1006 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
1007 SiteDeactivated_ContentSettingCleared) {
1008 HostContentSettingsMap* settings_map =
1009 HostContentSettingsMapFactory::GetForProfile(browser()->profile());
1010 ContentSettingsForOneType host_settings;
1011 settings_map->GetSettingsForOneType(
1012 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
1013 std::string(), &host_settings);
1014
1015 // Originally should just have a single default rule: * -> Allowed.
1016 EXPECT_EQ(1u, host_settings.size());
1017
1018 ASSERT_NO_FATAL_FAILURE(
1019 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
1020 GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html"));
1021 ConfigureAsPhishingURL(url);
1022
1023 ui_test_utils::NavigateToURL(browser(), url);
1024 EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetContentSetting(url));
engedy 2017/04/12 14:02:50 nit: Can you please add an expectation that this i
Charlie Harrison 2017/04/12 17:53:44 Done, it's still just CONTENT_SETTING_ALLOW.
1025
1026 // Should have two rules now, one explicitly setting the origin of |url| to
1027 // Allow.
1028 settings_map->GetSettingsForOneType(
1029 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
1030 std::string(), &host_settings);
1031 EXPECT_EQ(2u, host_settings.size());
1032
1033 // Configure a different URL as the phishing URL, which resets |url|.
1034 ConfigureAsPhishingURL(GURL("https://example.other/"));
engedy 2017/04/12 14:02:50 Does this really work? I thought we would just hav
Charlie Harrison 2017/04/12 17:53:44 Yes this is how the test utils are currently desig
1035
1036 // Re-navigating to |url| should reset the content setting.
1037 ui_test_utils::NavigateToURL(browser(), url);
1038 settings_map->GetSettingsForOneType(
1039 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
1040 std::string(), &host_settings);
1041 EXPECT_EQ(1u, host_settings.size());
1042 EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
1043 }
1044
1045 // Test the "smart" UI, aka the logic to hide the UI on subsequent same-domain
1046 // navigations, until a certain time threshold has been reached. This is an
1047 // android-only feature.
1048 IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest,
1049 DoNotShowUIUntilThresholdReached) {
1050 ASSERT_NO_FATAL_FAILURE(
1051 SetRulesetToDisallowURLsWithPathSuffix("included_script.js"));
1052 GURL a_url(embedded_test_server()->GetURL(
1053 "a.com", "/subresource_filter/frame_with_included_script.html"));
1054 GURL b_url(embedded_test_server()->GetURL(
1055 "b.com", "/subresource_filter/frame_with_included_script.html"));
1056 // Test utils only support one blacklisted site at a time.
1057 // TODO(csharrison): Add support for more than one URL.
1058 ConfigureAsPhishingURL(a_url);
1059
1060 // Cast is safe because this is the only type of client in non-unittest code.
1061 ChromeSubresourceFilterClient* client =
1062 static_cast<ChromeSubresourceFilterClient*>(
1063 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents())
1064 ->client());
1065 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
1066 base::SimpleTestClock* raw_clock = test_clock.get();
1067 settings_manager()->set_clock_for_testing(std::move(test_clock));
1068
1069 base::HistogramTester histogram_tester;
1070
1071 // First load should trigger the UI.
1072 ui_test_utils::NavigateToURL(browser(), a_url);
1073 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
1074 EXPECT_TRUE(client->shown_for_navigation());
1075
1076 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram,
1077 kActionUISuppressed, 0);
1078
1079 // Second load should not trigger the UI, but should still filter content.
1080 ui_test_utils::NavigateToURL(browser(), a_url);
1081 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
1082
1083 bool use_smart_ui = settings_manager()->should_use_smart_ui();
1084 EXPECT_EQ(client->shown_for_navigation(), !use_smart_ui);
1085
1086 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram,
1087 kActionUISuppressed, use_smart_ui ? 1 : 0);
1088
1089 ConfigureAsPhishingURL(b_url);
1090
1091 // Load to another domain should trigger the UI.
1092 ui_test_utils::NavigateToURL(browser(), b_url);
1093 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
1094 EXPECT_TRUE(client->shown_for_navigation());
1095
1096 ConfigureAsPhishingURL(a_url);
1097
1098 // Fast forward the clock, and a_url should trigger the UI again.
1099 raw_clock->Advance(
1100 SubresourceFilterContentSettingsManager::kUIShowThresholdTime);
1101 ui_test_utils::NavigateToURL(browser(), a_url);
1102 EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame()));
1103 EXPECT_TRUE(client->shown_for_navigation());
1104
1105 histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram,
1106 kActionUISuppressed, 1);
engedy 2017/04/12 14:02:50 Don't we need to make this conditional on |use_sma
Charlie Harrison 2017/04/12 17:53:44 Yes, done. Sorry there were a bunch of mistakes, i
1107 }
1108
985 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { 1109 IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) {
986 GURL url(GetTestUrl( 1110 GURL url(GetTestUrl(
987 base::StringPrintf("subresource_filter/page_with_websocket.html?%s", 1111 base::StringPrintf("subresource_filter/page_with_websocket.html?%s",
988 GetParam() == IN_WORKER ? "inWorker" : ""))); 1112 GetParam() == IN_WORKER ? "inWorker" : "")));
989 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension")); 1113 GURL websocket_url(GetWebSocketUrl("echo-with-no-extension"));
990 ConfigureAsPhishingURL(url); 1114 ConfigureAsPhishingURL(url);
991 ASSERT_NO_FATAL_FAILURE( 1115 ASSERT_NO_FATAL_FAILURE(
992 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension")); 1116 SetRulesetToDisallowURLsWithPathSuffix("echo-with-no-extension"));
993 ui_test_utils::NavigateToURL(browser(), url); 1117 ui_test_utils::NavigateToURL(browser(), url);
994 CreateWebSocketAndExpectResult(websocket_url, 1118 CreateWebSocketAndExpectResult(websocket_url,
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 std::string(kSubresourceFilterOnlySuffix)), 1468 std::string(kSubresourceFilterOnlySuffix)),
1345 ::testing::IsEmpty()); 1469 ::testing::IsEmpty());
1346 1470
1347 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) + 1471 EXPECT_THAT(tester.GetAllSamples(std::string(kNavigationChainSize) +
1348 std::string(kSubresourceFilterOnlySuffix)), 1472 std::string(kSubresourceFilterOnlySuffix)),
1349 ::testing::IsEmpty()); 1473 ::testing::IsEmpty());
1350 } 1474 }
1351 #endif 1475 #endif
1352 1476
1353 } // namespace subresource_filter 1477 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698