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

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

Issue 2645283007: Add the client for accessing Subresource Filter only list. (Closed)
Patch Set: adressed comments from jwd 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"
(...skipping 16 matching lines...) Expand all
27 namespace subresource_filter { 27 namespace subresource_filter {
28 28
29 using ActivationDecision = 29 using ActivationDecision =
30 ContentSubresourceFilterDriverFactory::ActivationDecision; 30 ContentSubresourceFilterDriverFactory::ActivationDecision;
31 31
32 namespace { 32 namespace {
33 33
34 const char kExampleUrlWithParams[] = "https://example.com/soceng?q=engsoc"; 34 const char kExampleUrlWithParams[] = "https://example.com/soceng?q=engsoc";
35 const char kExampleUrl[] = "https://example.com"; 35 const char kExampleUrl[] = "https://example.com";
36 const char kExampleLoginUrl[] = "https://example.com/login"; 36 const char kExampleLoginUrl[] = "https://example.com/login";
37 const char kMatchesPatternHistogramName[] =
38 "SubresourceFilter.PageLoad.RedirectChainMatchPattern";
39 const char kNavigationChainSize[] =
40 "SubresourceFilter.PageLoad.RedirectChainLength";
41 const char kUrlA[] = "https://example_a.com"; 37 const char kUrlA[] = "https://example_a.com";
42 const char kUrlB[] = "https://example_b.com"; 38 const char kUrlB[] = "https://example_b.com";
43 const char kUrlC[] = "https://example_c.com"; 39 const char kUrlC[] = "https://example_c.com";
44 const char kUrlD[] = "https://example_d.com"; 40 const char kUrlD[] = "https://example_d.com";
45 const char kSubframeName[] = "Child"; 41 const char kSubframeName[] = "Child";
46 42
43 const char kMatchesPatternHistogramName[] =
44 "SubresourceFilter.PageLoad.RedirectChainMatchPattern";
45 const char kNavigationChainSize[] =
46 "SubresourceFilter.PageLoad.RedirectChainLength";
47
48 #if defined(GOOGLE_CHROME_BUILD)
49 const char kSubresourceFilterSuffix[] = ".SubresourceFilter";
50 #endif
51
47 // Human readable representation of expected redirect chain match patterns. 52 // Human readable representation of expected redirect chain match patterns.
48 // The explanations for the buckets given for the following redirect chain: 53 // The explanations for the buckets given for the following redirect chain:
49 // A->B->C->D, where A is initial URL and D is a final URL. 54 // A->B->C->D, where A is initial URL and D is a final URL.
50 enum RedirectChainMatchPattern { 55 enum RedirectChainMatchPattern {
51 EMPTY, // No histograms were recorded. 56 EMPTY, // No histograms were recorded.
52 F0M0L1, // D is a Safe Browsing match. 57 F0M0L1, // D is a Safe Browsing match.
53 F0M1L0, // B or C, or both are Safe Browsing matches. 58 F0M1L0, // B or C, or both are Safe Browsing matches.
54 F0M1L1, // B or C, or both and D are Safe Browsing matches. 59 F0M1L1, // B or C, or both and D are Safe Browsing matches.
55 F1M0L0, // A is Safe Browsing match 60 F1M0L0, // A is Safe Browsing match
56 F1M0L1, // A and D are Safe Browsing matches. 61 F1M0L1, // A and D are Safe Browsing matches.
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 228 }
224 229
225 void BlacklistURLWithRedirectsNavigateAndCommit( 230 void BlacklistURLWithRedirectsNavigateAndCommit(
226 const std::vector<bool>& blacklisted_urls, 231 const std::vector<bool>& blacklisted_urls,
227 const std::vector<GURL>& navigation_chain, 232 const std::vector<GURL>& navigation_chain,
228 safe_browsing::SBThreatType threat_type, 233 safe_browsing::SBThreatType threat_type,
229 safe_browsing::ThreatPatternType threat_type_metadata, 234 safe_browsing::ThreatPatternType threat_type_metadata,
230 const content::Referrer& referrer, 235 const content::Referrer& referrer,
231 ui::PageTransition transition, 236 ui::PageTransition transition,
232 RedirectChainMatchPattern expected_pattern, 237 RedirectChainMatchPattern expected_pattern,
238 const std::string& histogram_suffix,
233 ActivationDecision expected_activation_decision) { 239 ActivationDecision expected_activation_decision) {
234 const bool expected_activation = 240 const bool expected_activation =
235 expected_activation_decision == ActivationDecision::ACTIVATED; 241 expected_activation_decision == ActivationDecision::ACTIVATED;
236 base::HistogramTester tester; 242 base::HistogramTester tester;
237 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1); 243 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1);
238 244
239 std::unique_ptr<content::NavigationSimulator> navigation_simulator = 245 std::unique_ptr<content::NavigationSimulator> navigation_simulator =
240 content::NavigationSimulator::CreateRendererInitiated( 246 content::NavigationSimulator::CreateRendererInitiated(
241 navigation_chain.front(), main_rfh()); 247 navigation_chain.front(), main_rfh());
242 navigation_simulator->SetReferrer(referrer); 248 navigation_simulator->SetReferrer(referrer);
(...skipping 20 matching lines...) Expand all
263 ExpectActivationSignalForFrame(main_rfh(), expected_activation); 269 ExpectActivationSignalForFrame(main_rfh(), expected_activation);
264 EXPECT_EQ(expected_activation_decision, 270 EXPECT_EQ(expected_activation_decision,
265 factory()->GetActivationDecisionForLastCommittedPageLoad()); 271 factory()->GetActivationDecisionForLastCommittedPageLoad());
266 272
267 // Re-create a subframe now that the frame has navigated. 273 // Re-create a subframe now that the frame has navigated.
268 content::RenderFrameHostTester* rfh_tester = 274 content::RenderFrameHostTester* rfh_tester =
269 content::RenderFrameHostTester::For(main_rfh()); 275 content::RenderFrameHostTester::For(main_rfh());
270 rfh_tester->AppendChild(kSubframeName); 276 rfh_tester->AppendChild(kSubframeName);
271 277
272 if (expected_pattern != EMPTY) { 278 if (expected_pattern != EMPTY) {
273 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName),
274 ::testing::ElementsAre(base::Bucket(expected_pattern, 1)));
275 EXPECT_THAT( 279 EXPECT_THAT(
276 tester.GetAllSamples(kNavigationChainSize), 280 tester.GetAllSamples(kMatchesPatternHistogramName + histogram_suffix),
281 ::testing::ElementsAre(base::Bucket(expected_pattern, 1)));
282 EXPECT_THAT(
283 tester.GetAllSamples(kNavigationChainSize + histogram_suffix),
277 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); 284 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1)));
278 285
279 } else { 286 } else {
280 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), 287 EXPECT_THAT(
281 ::testing::IsEmpty()); 288 tester.GetAllSamples(kMatchesPatternHistogramName + histogram_suffix),
282 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize), 289 ::testing::IsEmpty());
290 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize + histogram_suffix),
283 ::testing::IsEmpty()); 291 ::testing::IsEmpty());
284 } 292 }
285 } 293 }
286 294
287 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) { 295 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) {
288 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); 296 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0);
289 297
290 content::NavigationSimulator::NavigateAndCommitFromDocument( 298 content::NavigationSimulator::NavigateAndCommitFromDocument(
291 url, GetSubframeRFH()); 299 url, GetSubframeRFH());
292 ExpectActivationSignalForFrame(GetSubframeRFH(), expected_activation); 300 ExpectActivationSignalForFrame(GetSubframeRFH(), expected_activation);
293 ::testing::Mock::VerifyAndClearExpectations(client()); 301 ::testing::Mock::VerifyAndClearExpectations(client());
294 } 302 }
295 303
296 void NavigateAndExpectActivation( 304 void NavigateAndExpectActivation(
297 const std::vector<bool>& blacklisted_urls, 305 const std::vector<bool>& blacklisted_urls,
298 const std::vector<GURL>& navigation_chain, 306 const std::vector<GURL>& navigation_chain,
299 safe_browsing::SBThreatType threat_type, 307 safe_browsing::SBThreatType threat_type,
300 safe_browsing::ThreatPatternType threat_type_metadata, 308 safe_browsing::ThreatPatternType threat_type_metadata,
301 const content::Referrer& referrer, 309 const content::Referrer& referrer,
302 ui::PageTransition transition, 310 ui::PageTransition transition,
303 RedirectChainMatchPattern expected_pattern, 311 RedirectChainMatchPattern expected_pattern,
312 const std::string& histogram_suffix,
304 ActivationDecision expected_activation_decision) { 313 ActivationDecision expected_activation_decision) {
305 const bool expected_activation = 314 const bool expected_activation =
306 expected_activation_decision == ActivationDecision::ACTIVATED; 315 expected_activation_decision == ActivationDecision::ACTIVATED;
307 BlacklistURLWithRedirectsNavigateAndCommit( 316 BlacklistURLWithRedirectsNavigateAndCommit(
308 blacklisted_urls, navigation_chain, threat_type, threat_type_metadata, 317 blacklisted_urls, navigation_chain, threat_type, threat_type_metadata,
309 referrer, transition, expected_pattern, expected_activation_decision); 318 referrer, transition, expected_pattern, histogram_suffix,
319 expected_activation_decision);
310 320
311 NavigateAndCommitSubframe(GURL(kExampleLoginUrl), expected_activation); 321 NavigateAndCommitSubframe(GURL(kExampleLoginUrl), expected_activation);
312 } 322 }
313 323
314 void NavigateAndExpectActivation( 324 void NavigateAndExpectActivation(
315 const std::vector<bool>& blacklisted_urls, 325 const std::vector<bool>& blacklisted_urls,
316 const std::vector<GURL>& navigation_chain, 326 const std::vector<GURL>& navigation_chain,
317 RedirectChainMatchPattern expected_pattern, 327 RedirectChainMatchPattern expected_pattern,
318 ActivationDecision expected_activation_decision) { 328 ActivationDecision expected_activation_decision) {
319 NavigateAndExpectActivation( 329 NavigateAndExpectActivation(
320 blacklisted_urls, navigation_chain, 330 blacklisted_urls, navigation_chain,
321 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, 331 safe_browsing::SB_THREAT_TYPE_URL_PHISHING,
322 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, 332 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS,
323 content::Referrer(), ui::PAGE_TRANSITION_LINK, expected_pattern, 333 content::Referrer(), ui::PAGE_TRANSITION_LINK, expected_pattern,
324 expected_activation_decision); 334 std::string(), expected_activation_decision);
325 } 335 }
326 336
327 void EmulateDidDisallowFirstSubresourceMessage() { 337 void EmulateDidDisallowFirstSubresourceMessage() {
328 factory()->OnMessageReceived( 338 factory()->OnMessageReceived(
329 SubresourceFilterHostMsg_DidDisallowFirstSubresource( 339 SubresourceFilterHostMsg_DidDisallowFirstSubresource(
330 main_rfh()->GetRoutingID()), 340 main_rfh()->GetRoutingID()),
331 main_rfh()); 341 main_rfh());
332 } 342 }
333 343
334 void EmulateFailedNavigationAndExpectNoActivation(const GURL& url) { 344 void EmulateFailedNavigationAndExpectNoActivation(const GURL& url) {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 F0M0L1, 524 F0M0L1,
515 ActivationDecision::ACTIVATED}, 525 ActivationDecision::ACTIVATED},
516 {{true, false}, 526 {{true, false},
517 {GURL(kUrlA), GURL(kUrlB)}, 527 {GURL(kUrlA), GURL(kUrlB)},
518 F1M0L0, 528 F1M0L0,
519 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED}, 529 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED},
520 {{true, true}, 530 {{true, true},
521 {GURL(kUrlA), GURL(kUrlB)}, 531 {GURL(kUrlA), GURL(kUrlB)},
522 F1M0L1, 532 F1M0L1,
523 ActivationDecision::ACTIVATED}, 533 ActivationDecision::ACTIVATED},
524
525 {{false, false, false}, 534 {{false, false, false},
526 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, 535 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)},
527 EMPTY, 536 EMPTY,
528 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED}, 537 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED},
529 {{false, false, true}, 538 {{false, false, true},
530 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, 539 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)},
531 F0M0L1, 540 F0M0L1,
532 ActivationDecision::ACTIVATED}, 541 ActivationDecision::ACTIVATED},
533 {{false, true, false}, 542 {{false, true, false},
534 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, 543 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)},
(...skipping 26 matching lines...) Expand all
561 }; 570 };
562 571
563 for (size_t i = 0U; i < arraysize(kRedirectRedirectChainMatchPatternTestData); 572 for (size_t i = 0U; i < arraysize(kRedirectRedirectChainMatchPatternTestData);
564 ++i) { 573 ++i) {
565 auto test_data = kRedirectRedirectChainMatchPatternTestData[i]; 574 auto test_data = kRedirectRedirectChainMatchPatternTestData[i];
566 NavigateAndExpectActivation( 575 NavigateAndExpectActivation(
567 test_data.blacklisted_urls, test_data.navigation_chain, 576 test_data.blacklisted_urls, test_data.navigation_chain,
568 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, 577 safe_browsing::SB_THREAT_TYPE_URL_PHISHING,
569 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, 578 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS,
570 content::Referrer(), ui::PAGE_TRANSITION_LINK, 579 content::Referrer(), ui::PAGE_TRANSITION_LINK,
571 test_data.hit_expected_pattern, test_data.expected_activation_decision); 580 test_data.hit_expected_pattern, std::string(),
581 test_data.expected_activation_decision);
572 NavigateAndExpectActivation( 582 NavigateAndExpectActivation(
573 {false}, {GURL("https://dummy.com")}, EMPTY, 583 {false}, {GURL("https://dummy.com")}, EMPTY,
574 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED); 584 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED);
585 #if defined(GOOGLE_CHROME_BUILD)
586 NavigateAndExpectActivation(
587 test_data.blacklisted_urls, test_data.navigation_chain,
588 safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER,
589 safe_browsing::ThreatPatternType::NONE, content::Referrer(),
590 ui::PAGE_TRANSITION_LINK, EMPTY, test_data.hit_expected_pattern,
591 kSubresourceFilterSuffix,
592 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED);
593 #endif
575 } 594 }
576 } 595 }
577 596
578 TEST_F(ContentSubresourceFilterDriverFactoryTest, NotificationVisibility) { 597 TEST_F(ContentSubresourceFilterDriverFactoryTest, NotificationVisibility) {
579 base::FieldTrialList field_trial_list(nullptr); 598 base::FieldTrialList field_trial_list(nullptr);
580 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( 599 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
581 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 600 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
582 kActivationScopeAllSites); 601 kActivationScopeAllSites);
583 602
584 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY, 603 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( 645 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
627 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 646 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
628 kActivationScopeAllSites, "" /* activation_lists */, 647 kActivationScopeAllSites, "" /* activation_lists */,
629 "" /* performance_measurement_rate */, "" /* suppress_notifications */, 648 "" /* performance_measurement_rate */, "" /* suppress_notifications */,
630 "true" /* whitelist_site_on_reload */); 649 "true" /* whitelist_site_on_reload */);
631 650
632 NavigateAndExpectActivation( 651 NavigateAndExpectActivation(
633 {false}, {GURL(kExampleUrl)}, 652 {false}, {GURL(kExampleUrl)},
634 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, 653 safe_browsing::SB_THREAT_TYPE_URL_PHISHING,
635 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, 654 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS,
636 test_case.referrer, test_case.transition, EMPTY, 655 test_case.referrer, test_case.transition, EMPTY, std::string(),
637 test_case.expected_activation_decision); 656 test_case.expected_activation_decision);
638 // Verify that if the first URL failed to activate, subsequent same-origin 657 // Verify that if the first URL failed to activate, subsequent same-origin
639 // navigations also fail to activate. 658 // navigations also fail to activate.
640 NavigateAndExpectActivation({false}, {GURL(kExampleUrlWithParams)}, EMPTY, 659 NavigateAndExpectActivation({false}, {GURL(kExampleUrlWithParams)}, EMPTY,
641 test_case.expected_activation_decision); 660 test_case.expected_activation_decision);
642 } 661 }
643 } 662 }
644 663
645 TEST_P(ContentSubresourceFilterDriverFactoryActivationLevelTest, 664 TEST_P(ContentSubresourceFilterDriverFactoryActivationLevelTest,
646 ActivateForFrameState) { 665 ActivateForFrameState) {
(...skipping 28 matching lines...) Expand all
675 const GURL test_url("https://example.com/nonsoceng?q=engsocnon"); 694 const GURL test_url("https://example.com/nonsoceng?q=engsocnon");
676 std::vector<GURL> navigation_chain; 695 std::vector<GURL> navigation_chain;
677 696
678 const bool expected_activation = 697 const bool expected_activation =
679 test_data.expected_activation_decision == ActivationDecision::ACTIVATED; 698 test_data.expected_activation_decision == ActivationDecision::ACTIVATED;
680 NavigateAndExpectActivation( 699 NavigateAndExpectActivation(
681 {false, false, false, true}, 700 {false, false, false, true},
682 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, test_data.threat_type, 701 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, test_data.threat_type,
683 test_data.threat_type_metadata, content::Referrer(), 702 test_data.threat_type_metadata, content::Referrer(),
684 ui::PAGE_TRANSITION_LINK, expected_activation ? F0M0L1 : EMPTY, 703 ui::PAGE_TRANSITION_LINK, expected_activation ? F0M0L1 : EMPTY,
685 test_data.expected_activation_decision); 704 std::string(), test_data.expected_activation_decision);
686 }; 705 };
687 706
688 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest, 707 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest,
689 ActivateForScopeType) { 708 ActivateForScopeType) {
690 const ActivationScopeTestData& test_data = GetParam(); 709 const ActivationScopeTestData& test_data = GetParam();
691 base::FieldTrialList field_trial_list(nullptr); 710 base::FieldTrialList field_trial_list(nullptr);
692 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( 711 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
693 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 712 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
694 test_data.activation_scope, 713 test_data.activation_scope,
695 kActivationListSocialEngineeringAdsInterstitial); 714 kActivationListSocialEngineeringAdsInterstitial);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 ActivationScopeTest, 774 ActivationScopeTest,
756 ContentSubresourceFilterDriverFactoryActivationScopeTest, 775 ContentSubresourceFilterDriverFactoryActivationScopeTest,
757 ::testing::ValuesIn(kActivationScopeTestData)); 776 ::testing::ValuesIn(kActivationScopeTestData));
758 777
759 INSTANTIATE_TEST_CASE_P( 778 INSTANTIATE_TEST_CASE_P(
760 ActivationLevelTest, 779 ActivationLevelTest,
761 ContentSubresourceFilterDriverFactoryActivationLevelTest, 780 ContentSubresourceFilterDriverFactoryActivationLevelTest,
762 ::testing::ValuesIn(kActivationLevelTestData)); 781 ::testing::ValuesIn(kActivationLevelTestData));
763 782
764 } // namespace subresource_filter 783 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698