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

Side by Side Diff: components/subresource_filter/content/browser/content_subresource_filter_driver_factory_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698