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 "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" | 5 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
11 #include "components/safe_browsing_db/util.h" | 11 #include "components/safe_browsing_db/util.h" |
| 12 #include "components/subresource_filter/content/browser/content_activation_list_
utils.h" |
12 #include "components/subresource_filter/content/browser/subresource_filter_clien
t.h" | 13 #include "components/subresource_filter/content/browser/subresource_filter_clien
t.h" |
13 #include "components/subresource_filter/content/common/subresource_filter_messag
es.h" | 14 #include "components/subresource_filter/content/common/subresource_filter_messag
es.h" |
14 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 15 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
15 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" | 16 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
| 17 #include "components/subresource_filter/core/common/activation_list.h" |
16 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
18 #include "content/public/test/mock_render_process_host.h" | 20 #include "content/public/test/mock_render_process_host.h" |
19 #include "content/public/test/navigation_simulator.h" | 21 #include "content/public/test/navigation_simulator.h" |
20 #include "content/public/test/test_renderer_host.h" | 22 #include "content/public/test/test_renderer_host.h" |
21 #include "content/public/test/web_contents_tester.h" | 23 #include "content/public/test/web_contents_tester.h" |
22 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
(...skipping 28 matching lines...) Expand all Loading... |
54 F0M1L1, // B or C, or both and D are Safe Browsing matches. | 56 F0M1L1, // B or C, or both and D are Safe Browsing matches. |
55 F1M0L0, // A is Safe Browsing match | 57 F1M0L0, // A is Safe Browsing match |
56 F1M0L1, // A and D are Safe Browsing matches. | 58 F1M0L1, // A and D are Safe Browsing matches. |
57 F1M1L0, // B and/or C and A are Safe Browsing matches. | 59 F1M1L0, // B and/or C and A are Safe Browsing matches. |
58 F1M1L1, // B and/or C and A and D are Safe Browsing matches. | 60 F1M1L1, // B and/or C and A and D are Safe Browsing matches. |
59 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects | 61 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects |
60 // has happened, and this URL was a Safe Browsing hit. | 62 // has happened, and this URL was a Safe Browsing hit. |
61 NUM_HIT_PATTERNS, | 63 NUM_HIT_PATTERNS, |
62 }; | 64 }; |
63 | 65 |
| 66 std::string GetSuffixForList(const ActivationList& type) { |
| 67 switch (type) { |
| 68 case ActivationList::SOCIAL_ENG_ADS_INTERSTITIAL: |
| 69 return ".SocialEngineeringAdsInterstitial"; |
| 70 case ActivationList::PHISHING_INTERSTITIAL: |
| 71 return ".PhishingInterstital"; |
| 72 case ActivationList::NONE: |
| 73 return std::string(); |
| 74 } |
| 75 return std::string(); |
| 76 } |
| 77 |
64 struct ActivationListTestData { | 78 struct ActivationListTestData { |
65 ActivationDecision expected_activation_decision; | 79 ActivationDecision expected_activation_decision; |
66 const char* const activation_list; | 80 const char* const activation_list; |
67 safe_browsing::SBThreatType threat_type; | 81 safe_browsing::SBThreatType threat_type; |
68 safe_browsing::ThreatPatternType threat_type_metadata; | 82 safe_browsing::ThreatPatternType threat_type_metadata; |
69 }; | 83 }; |
70 | 84 |
71 const ActivationListTestData kActivationListTestData[] = { | 85 const ActivationListTestData kActivationListTestData[] = { |
72 {ActivationDecision::ACTIVATION_LIST_NOT_MATCHED, "", | 86 {ActivationDecision::ACTIVATION_LIST_NOT_MATCHED, "", |
73 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, | 87 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 safe_browsing::SB_THREAT_TYPE_SAFE, | 131 safe_browsing::SB_THREAT_TYPE_SAFE, |
118 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS}, | 132 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS}, |
119 {ActivationDecision::ACTIVATED, | 133 {ActivationDecision::ACTIVATED, |
120 subresource_filter::kActivationListPhishingInterstitial, | 134 subresource_filter::kActivationListPhishingInterstitial, |
121 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, | 135 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
122 safe_browsing::ThreatPatternType::NONE}, | 136 safe_browsing::ThreatPatternType::NONE}, |
123 {ActivationDecision::ACTIVATED, | 137 {ActivationDecision::ACTIVATED, |
124 subresource_filter::kActivationListSocialEngineeringAdsInterstitial, | 138 subresource_filter::kActivationListSocialEngineeringAdsInterstitial, |
125 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, | 139 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
126 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS}, | 140 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS}, |
| 141 {ActivationDecision::ACTIVATED, |
| 142 subresource_filter::kActivationListPhishingInterstitial, |
| 143 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
| 144 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS}, |
127 }; | 145 }; |
128 | 146 |
129 struct ActivationScopeTestData { | 147 struct ActivationScopeTestData { |
130 ActivationDecision expected_activation_decision; | 148 ActivationDecision expected_activation_decision; |
131 bool url_matches_activation_list; | 149 bool url_matches_activation_list; |
132 const char* const activation_scope; | 150 const char* const activation_scope; |
133 }; | 151 }; |
134 | 152 |
135 const ActivationScopeTestData kActivationScopeTestData[] = { | 153 const ActivationScopeTestData kActivationScopeTestData[] = { |
136 {ActivationDecision::ACTIVATED, false /* url_matches_activation_list */, | 154 {ActivationDecision::ACTIVATED, false /* url_matches_activation_list */, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 279 |
262 navigation_simulator->Commit(); | 280 navigation_simulator->Commit(); |
263 ExpectActivationSignalForFrame(main_rfh(), expected_activation); | 281 ExpectActivationSignalForFrame(main_rfh(), expected_activation); |
264 EXPECT_EQ(expected_activation_decision, | 282 EXPECT_EQ(expected_activation_decision, |
265 factory()->GetActivationDecisionForLastCommittedPageLoad()); | 283 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
266 | 284 |
267 // Re-create a subframe now that the frame has navigated. | 285 // Re-create a subframe now that the frame has navigated. |
268 content::RenderFrameHostTester* rfh_tester = | 286 content::RenderFrameHostTester* rfh_tester = |
269 content::RenderFrameHostTester::For(main_rfh()); | 287 content::RenderFrameHostTester::For(main_rfh()); |
270 rfh_tester->AppendChild(kSubframeName); | 288 rfh_tester->AppendChild(kSubframeName); |
| 289 ActivationList activation_list = |
| 290 GetListForThreatTypeAndMetadata(threat_type, threat_type_metadata); |
271 | 291 |
| 292 const std::string suffix(GetSuffixForList(activation_list)); |
272 if (expected_pattern != EMPTY) { | 293 if (expected_pattern != EMPTY) { |
273 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), | 294 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName + suffix), |
274 ::testing::ElementsAre(base::Bucket(expected_pattern, 1))); | 295 ::testing::ElementsAre(base::Bucket(expected_pattern, 1))); |
275 EXPECT_THAT( | 296 EXPECT_THAT( |
276 tester.GetAllSamples(kNavigationChainSize), | 297 tester.GetAllSamples(kNavigationChainSize + suffix), |
277 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); | 298 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); |
278 | |
279 } else { | 299 } else { |
280 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), | 300 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName + suffix), |
281 ::testing::IsEmpty()); | 301 ::testing::IsEmpty()); |
282 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize), | 302 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize + suffix), |
283 ::testing::IsEmpty()); | 303 ::testing::IsEmpty()); |
284 } | 304 } |
285 } | 305 } |
286 | 306 |
287 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) { | 307 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) { |
288 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); | 308 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); |
289 | 309 |
290 content::NavigationSimulator::NavigateAndCommitFromDocument( | 310 content::NavigationSimulator::NavigateAndCommitFromDocument( |
291 url, GetSubframeRFH()); | 311 url, GetSubframeRFH()); |
292 ExpectActivationSignalForFrame(GetSubframeRFH(), expected_activation); | 312 ExpectActivationSignalForFrame(GetSubframeRFH(), expected_activation); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 TEST_F(ContentSubresourceFilterDriverFactoryTest, | 439 TEST_F(ContentSubresourceFilterDriverFactoryTest, |
420 ActivateForFrameHostDisabledFeature) { | 440 ActivateForFrameHostDisabledFeature) { |
421 // Activation scope is set to NONE => no activation should happen even if URL | 441 // Activation scope is set to NONE => no activation should happen even if URL |
422 // which is visited was a SB hit. | 442 // which is visited was a SB hit. |
423 base::FieldTrialList field_trial_list(nullptr); | 443 base::FieldTrialList field_trial_list(nullptr); |
424 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 444 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
425 base::FeatureList::OVERRIDE_DISABLE_FEATURE, kActivationLevelEnabled, | 445 base::FeatureList::OVERRIDE_DISABLE_FEATURE, kActivationLevelEnabled, |
426 kActivationScopeAllSites, | 446 kActivationScopeAllSites, |
427 kActivationListSocialEngineeringAdsInterstitial); | 447 kActivationListSocialEngineeringAdsInterstitial); |
428 const GURL url(kExampleUrlWithParams); | 448 const GURL url(kExampleUrlWithParams); |
429 NavigateAndExpectActivation({true}, {url}, EMPTY, | 449 NavigateAndExpectActivation({true}, {url}, NO_REDIRECTS_HIT, |
430 ActivationDecision::ACTIVATION_DISABLED); | 450 ActivationDecision::ACTIVATION_DISABLED); |
431 factory()->AddHostOfURLToWhitelistSet(url); | 451 factory()->AddHostOfURLToWhitelistSet(url); |
432 NavigateAndExpectActivation({true}, {url}, EMPTY, | 452 NavigateAndExpectActivation({true}, {url}, NO_REDIRECTS_HIT, |
433 ActivationDecision::ACTIVATION_DISABLED); | 453 ActivationDecision::ACTIVATION_DISABLED); |
434 } | 454 } |
435 | 455 |
436 TEST_F(ContentSubresourceFilterDriverFactoryTest, NoActivationWhenNoMatch) { | 456 TEST_F(ContentSubresourceFilterDriverFactoryTest, NoActivationWhenNoMatch) { |
437 base::FieldTrialList field_trial_list(nullptr); | 457 base::FieldTrialList field_trial_list(nullptr); |
438 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 458 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
439 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 459 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
440 kActivationScopeActivationList, | 460 kActivationScopeActivationList, |
441 kActivationListSocialEngineeringAdsInterstitial); | 461 kActivationListSocialEngineeringAdsInterstitial); |
442 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY, | 462 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 base::FieldTrialList field_trial_list(nullptr); | 501 base::FieldTrialList field_trial_list(nullptr); |
482 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 502 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
483 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 503 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
484 kActivationScopeAllSites); | 504 kActivationScopeAllSites); |
485 const GURL url(kExampleUrl); | 505 const GURL url(kExampleUrl); |
486 NavigateAndExpectActivation({false}, {url}, EMPTY, | 506 NavigateAndExpectActivation({false}, {url}, EMPTY, |
487 ActivationDecision::ACTIVATED); | 507 ActivationDecision::ACTIVATED); |
488 EmulateFailedNavigationAndExpectNoActivation(url); | 508 EmulateFailedNavigationAndExpectNoActivation(url); |
489 } | 509 } |
490 | 510 |
| 511 // TODO(melandory): refactor the test so it no longer require the current |
| 512 // activation list to be matching. |
491 TEST_F(ContentSubresourceFilterDriverFactoryTest, RedirectPatternTest) { | 513 TEST_F(ContentSubresourceFilterDriverFactoryTest, RedirectPatternTest) { |
492 base::FieldTrialList field_trial_list(nullptr); | 514 base::FieldTrialList field_trial_list(nullptr); |
493 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 515 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
494 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 516 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
495 kActivationScopeActivationList, | 517 kActivationScopeActivationList, |
496 kActivationListSocialEngineeringAdsInterstitial); | 518 kActivationListSocialEngineeringAdsInterstitial); |
497 struct RedirectRedirectChainMatchPatternTestData { | 519 struct RedirectRedirectChainMatchPatternTestData { |
498 std::vector<bool> blacklisted_urls; | 520 std::vector<bool> blacklisted_urls; |
499 std::vector<GURL> navigation_chain; | 521 std::vector<GURL> navigation_chain; |
500 RedirectChainMatchPattern hit_expected_pattern; | 522 RedirectChainMatchPattern hit_expected_pattern; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 // list for which the Safe Browsing hit has happened. | 690 // list for which the Safe Browsing hit has happened. |
669 const ActivationListTestData& test_data = GetParam(); | 691 const ActivationListTestData& test_data = GetParam(); |
670 base::FieldTrialList field_trial_list(nullptr); | 692 base::FieldTrialList field_trial_list(nullptr); |
671 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 693 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
672 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 694 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
673 kActivationScopeActivationList, test_data.activation_list); | 695 kActivationScopeActivationList, test_data.activation_list); |
674 | 696 |
675 const GURL test_url("https://example.com/nonsoceng?q=engsocnon"); | 697 const GURL test_url("https://example.com/nonsoceng?q=engsocnon"); |
676 std::vector<GURL> navigation_chain; | 698 std::vector<GURL> navigation_chain; |
677 | 699 |
678 const bool expected_activation = | 700 ActivationList effective_list = GetListForThreatTypeAndMetadata( |
679 test_data.expected_activation_decision == ActivationDecision::ACTIVATED; | 701 test_data.threat_type, test_data.threat_type_metadata); |
680 NavigateAndExpectActivation( | 702 NavigateAndExpectActivation( |
681 {false, false, false, true}, | 703 {false, false, false, true}, |
682 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, test_data.threat_type, | 704 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, test_data.threat_type, |
683 test_data.threat_type_metadata, content::Referrer(), | 705 test_data.threat_type_metadata, content::Referrer(), |
684 ui::PAGE_TRANSITION_LINK, expected_activation ? F0M0L1 : EMPTY, | 706 ui::PAGE_TRANSITION_LINK, |
| 707 effective_list != ActivationList::NONE ? F0M0L1 : EMPTY, |
685 test_data.expected_activation_decision); | 708 test_data.expected_activation_decision); |
686 }; | 709 }; |
687 | 710 |
688 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest, | 711 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest, |
689 ActivateForScopeType) { | 712 ActivateForScopeType) { |
690 const ActivationScopeTestData& test_data = GetParam(); | 713 const ActivationScopeTestData& test_data = GetParam(); |
691 base::FieldTrialList field_trial_list(nullptr); | 714 base::FieldTrialList field_trial_list(nullptr); |
692 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 715 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
693 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 716 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
694 test_data.activation_scope, | 717 test_data.activation_scope, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
755 ActivationScopeTest, | 778 ActivationScopeTest, |
756 ContentSubresourceFilterDriverFactoryActivationScopeTest, | 779 ContentSubresourceFilterDriverFactoryActivationScopeTest, |
757 ::testing::ValuesIn(kActivationScopeTestData)); | 780 ::testing::ValuesIn(kActivationScopeTestData)); |
758 | 781 |
759 INSTANTIATE_TEST_CASE_P( | 782 INSTANTIATE_TEST_CASE_P( |
760 ActivationLevelTest, | 783 ActivationLevelTest, |
761 ContentSubresourceFilterDriverFactoryActivationLevelTest, | 784 ContentSubresourceFilterDriverFactoryActivationLevelTest, |
762 ::testing::ValuesIn(kActivationLevelTestData)); | 785 ::testing::ValuesIn(kActivationLevelTestData)); |
763 | 786 |
764 } // namespace subresource_filter | 787 } // namespace subresource_filter |
OLD | NEW |