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

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

Powered by Google App Engine
This is Rietveld 408576698