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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/test/histogram_tester.h" | 8 #include "base/test/histogram_tester.h" |
9 #include "components/safe_browsing_db/util.h" | 9 #include "components/safe_browsing_db/util.h" |
10 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver.h" | 10 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 namespace subresource_filter { | 24 namespace subresource_filter { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kExampleUrlWithParams[] = "https://example.com/soceng?q=engsoc"; | 28 const char kExampleUrlWithParams[] = "https://example.com/soceng?q=engsoc"; |
29 const char kExampleUrl[] = "https://example.com"; | 29 const char kExampleUrl[] = "https://example.com"; |
30 const char kExampleLoginUrl[] = "https://example.com/login"; | 30 const char kExampleLoginUrl[] = "https://example.com/login"; |
31 const char kMatchesPatternHistogramName[] = | |
32 "SubresourceFilter.PageLoad.RedirectChainMatchPattern"; | |
33 const char kNavigationChainSize[] = | |
34 "SubresourceFilter.PageLoad.RedirectChainLength"; | |
35 const char kUrlA[] = "https://example_a.com"; | 31 const char kUrlA[] = "https://example_a.com"; |
36 const char kUrlB[] = "https://example_b.com"; | 32 const char kUrlB[] = "https://example_b.com"; |
37 const char kUrlC[] = "https://example_c.com"; | 33 const char kUrlC[] = "https://example_c.com"; |
38 const char kUrlD[] = "https://example_d.com"; | 34 const char kUrlD[] = "https://example_d.com"; |
39 | 35 |
| 36 const char kMatchesPatternHistogramName[] = |
| 37 "SubresourceFilter.PageLoad.RedirectChainMatchPattern"; |
| 38 const char kNavigationChainSize[] = |
| 39 "SubresourceFilter.PageLoad.RedirectChainLength"; |
| 40 const char kSubresourceFilterOnlySuffix[] = ".SubresourceFilterOnly"; |
| 41 |
40 // Human readable representation of expected redirect chain match patterns. | 42 // Human readable representation of expected redirect chain match patterns. |
41 // The explanations for the buckets given for the following redirect chain: | 43 // The explanations for the buckets given for the following redirect chain: |
42 // A->B->C->D, where A is initial URL and D is a final URL. | 44 // A->B->C->D, where A is initial URL and D is a final URL. |
43 enum RedirectChainMatchPattern { | 45 enum RedirectChainMatchPattern { |
44 EMPTY, // No histograms were recorded. | 46 EMPTY, // No histograms were recorded. |
45 F0M0L1, // D is a Safe Browsing match. | 47 F0M0L1, // D is a Safe Browsing match. |
46 F0M1L0, // B or C, or both are Safe Browsing matches. | 48 F0M1L0, // B or C, or both are Safe Browsing matches. |
47 F0M1L1, // B or C, or both and D are Safe Browsing matches. | 49 F0M1L1, // B or C, or both and D are Safe Browsing matches. |
48 F1M0L0, // A is Safe Browsing match | 50 F1M0L0, // A is Safe Browsing match |
49 F1M0L1, // A and D are Safe Browsing matches. | 51 F1M0L1, // A and D are Safe Browsing matches. |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 207 |
206 MockSubresourceFilterDriver* subframe_driver() { return subframe_driver_; } | 208 MockSubresourceFilterDriver* subframe_driver() { return subframe_driver_; } |
207 content::RenderFrameHost* subframe_rfh() { return subframe_rfh_; } | 209 content::RenderFrameHost* subframe_rfh() { return subframe_rfh_; } |
208 | 210 |
209 void BlacklistURLWithRedirectsNavigateAndCommit( | 211 void BlacklistURLWithRedirectsNavigateAndCommit( |
210 const std::vector<bool>& blacklisted_urls, | 212 const std::vector<bool>& blacklisted_urls, |
211 const std::vector<GURL>& navigation_chain, | 213 const std::vector<GURL>& navigation_chain, |
212 safe_browsing::SBThreatType threat_type, | 214 safe_browsing::SBThreatType threat_type, |
213 safe_browsing::ThreatPatternType threat_type_metadata, | 215 safe_browsing::ThreatPatternType threat_type_metadata, |
214 RedirectChainMatchPattern expected_pattern, | 216 RedirectChainMatchPattern expected_pattern, |
| 217 const std::string histogram_suffix, |
215 bool expected_activation) { | 218 bool expected_activation) { |
216 base::HistogramTester tester; | 219 base::HistogramTester tester; |
217 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1); | 220 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1); |
218 content::RenderFrameHostTester* rfh_tester = | 221 content::RenderFrameHostTester* rfh_tester = |
219 content::RenderFrameHostTester::For(main_rfh()); | 222 content::RenderFrameHostTester::For(main_rfh()); |
220 | 223 |
221 rfh_tester->SimulateNavigationStart(navigation_chain.front()); | 224 rfh_tester->SimulateNavigationStart(navigation_chain.front()); |
222 if (blacklisted_urls.front()) { | 225 if (blacklisted_urls.front()) { |
223 factory()->OnMainResourceMatchedSafeBrowsingBlacklist( | 226 factory()->OnMainResourceMatchedSafeBrowsingBlacklist( |
224 navigation_chain.front(), navigation_chain, threat_type, | 227 navigation_chain.front(), navigation_chain, threat_type, |
(...skipping 15 matching lines...) Expand all Loading... |
240 .Times(expected_activation); | 243 .Times(expected_activation); |
241 if (!content::IsBrowserSideNavigationEnabled()) { | 244 if (!content::IsBrowserSideNavigationEnabled()) { |
242 factory()->ReadyToCommitNavigationInternal(main_rfh(), | 245 factory()->ReadyToCommitNavigationInternal(main_rfh(), |
243 navigation_chain.back()); | 246 navigation_chain.back()); |
244 } | 247 } |
245 | 248 |
246 rfh_tester->SimulateNavigationCommit(navigation_chain.back()); | 249 rfh_tester->SimulateNavigationCommit(navigation_chain.back()); |
247 ::testing::Mock::VerifyAndClearExpectations(driver()); | 250 ::testing::Mock::VerifyAndClearExpectations(driver()); |
248 | 251 |
249 if (expected_pattern != EMPTY) { | 252 if (expected_pattern != EMPTY) { |
250 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), | |
251 ::testing::ElementsAre(base::Bucket(expected_pattern, 1))); | |
252 EXPECT_THAT( | 253 EXPECT_THAT( |
253 tester.GetAllSamples(kNavigationChainSize), | 254 tester.GetAllSamples(kMatchesPatternHistogramName + histogram_suffix), |
| 255 ::testing::ElementsAre(base::Bucket(expected_pattern, 1))); |
| 256 EXPECT_THAT( |
| 257 tester.GetAllSamples(kNavigationChainSize + histogram_suffix), |
254 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); | 258 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); |
255 | 259 |
256 } else { | 260 } else { |
257 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), | 261 EXPECT_THAT( |
258 ::testing::IsEmpty()); | 262 tester.GetAllSamples(kMatchesPatternHistogramName + histogram_suffix), |
259 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize), | 263 ::testing::IsEmpty()); |
| 264 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize + histogram_suffix), |
260 ::testing::IsEmpty()); | 265 ::testing::IsEmpty()); |
261 } | 266 } |
262 } | 267 } |
263 | 268 |
264 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) { | 269 void NavigateAndCommitSubframe(const GURL& url, bool expected_activation) { |
265 EXPECT_CALL(*subframe_driver(), | 270 EXPECT_CALL(*subframe_driver(), |
266 ActivateForProvisionalLoad(::testing::_, ::testing::_, | 271 ActivateForProvisionalLoad(::testing::_, ::testing::_, |
267 expected_measure_performance())) | 272 expected_measure_performance())) |
268 .Times(expected_activation); | 273 .Times(expected_activation); |
269 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); | 274 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); |
270 | 275 |
271 factory()->ReadyToCommitNavigationInternal(subframe_rfh(), url); | 276 factory()->ReadyToCommitNavigationInternal(subframe_rfh(), url); |
272 ::testing::Mock::VerifyAndClearExpectations(subframe_driver()); | 277 ::testing::Mock::VerifyAndClearExpectations(subframe_driver()); |
273 ::testing::Mock::VerifyAndClearExpectations(client()); | 278 ::testing::Mock::VerifyAndClearExpectations(client()); |
274 } | 279 } |
275 | 280 |
276 void NavigateAndExpectActivation( | 281 void NavigateAndExpectActivation( |
277 const std::vector<bool>& blacklisted_urls, | 282 const std::vector<bool>& blacklisted_urls, |
278 const std::vector<GURL>& navigation_chain, | 283 const std::vector<GURL>& navigation_chain, |
279 safe_browsing::SBThreatType threat_type, | 284 safe_browsing::SBThreatType threat_type, |
280 safe_browsing::ThreatPatternType threat_type_metadata, | 285 safe_browsing::ThreatPatternType threat_type_metadata, |
281 RedirectChainMatchPattern expected_pattern, | 286 RedirectChainMatchPattern expected_pattern, |
| 287 const std::string& histogram_suffix, |
282 bool expected_activation) { | 288 bool expected_activation) { |
283 BlacklistURLWithRedirectsNavigateAndCommit( | 289 BlacklistURLWithRedirectsNavigateAndCommit( |
284 blacklisted_urls, navigation_chain, threat_type, threat_type_metadata, | 290 blacklisted_urls, navigation_chain, threat_type, threat_type_metadata, |
285 expected_pattern, expected_activation); | 291 expected_pattern, histogram_suffix, expected_activation); |
286 | |
287 NavigateAndCommitSubframe(GURL(kExampleLoginUrl), expected_activation); | 292 NavigateAndCommitSubframe(GURL(kExampleLoginUrl), expected_activation); |
288 } | 293 } |
289 | 294 |
290 void NavigateAndExpectActivation(const std::vector<bool>& blacklisted_urls, | 295 void NavigateAndExpectActivation(const std::vector<bool>& blacklisted_urls, |
291 const std::vector<GURL>& navigation_chain, | 296 const std::vector<GURL>& navigation_chain, |
292 RedirectChainMatchPattern expected_pattern, | 297 RedirectChainMatchPattern expected_pattern, |
293 bool expected_activation) { | 298 bool expected_activation) { |
294 NavigateAndExpectActivation( | 299 NavigateAndExpectActivation( |
295 blacklisted_urls, navigation_chain, | 300 blacklisted_urls, navigation_chain, |
296 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, | 301 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
297 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, | 302 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, |
298 expected_pattern, expected_activation); | 303 expected_pattern, std::string(), expected_activation); |
299 } | 304 } |
300 | 305 |
301 void EmulateDidDisallowFirstSubresourceMessage() { | 306 void EmulateDidDisallowFirstSubresourceMessage() { |
302 factory()->OnMessageReceived( | 307 factory()->OnMessageReceived( |
303 SubresourceFilterHostMsg_DidDisallowFirstSubresource( | 308 SubresourceFilterHostMsg_DidDisallowFirstSubresource( |
304 main_rfh()->GetRoutingID()), | 309 main_rfh()->GetRoutingID()), |
305 main_rfh()); | 310 main_rfh()); |
306 } | 311 } |
307 | 312 |
308 void EmulateInPageNavigation(const std::vector<bool>& blacklisted_urls, | 313 void EmulateInPageNavigation(const std::vector<bool>& blacklisted_urls, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 std::vector<GURL> navigation_chain; | 458 std::vector<GURL> navigation_chain; |
454 RedirectChainMatchPattern hit_expected_pattern; | 459 RedirectChainMatchPattern hit_expected_pattern; |
455 bool expected_activation; | 460 bool expected_activation; |
456 } kRedirectRedirectChainMatchPatternTestData[] = { | 461 } kRedirectRedirectChainMatchPatternTestData[] = { |
457 {{false}, {GURL(kUrlA)}, EMPTY, false}, | 462 {{false}, {GURL(kUrlA)}, EMPTY, false}, |
458 {{true}, {GURL(kUrlA)}, NO_REDIRECTS_HIT, true}, | 463 {{true}, {GURL(kUrlA)}, NO_REDIRECTS_HIT, true}, |
459 {{false, false}, {GURL(kUrlA), GURL(kUrlB)}, EMPTY, false}, | 464 {{false, false}, {GURL(kUrlA), GURL(kUrlB)}, EMPTY, false}, |
460 {{false, true}, {GURL(kUrlA), GURL(kUrlB)}, F0M0L1, true}, | 465 {{false, true}, {GURL(kUrlA), GURL(kUrlB)}, F0M0L1, true}, |
461 {{true, false}, {GURL(kUrlA), GURL(kUrlB)}, F1M0L0, false}, | 466 {{true, false}, {GURL(kUrlA), GURL(kUrlB)}, F1M0L0, false}, |
462 {{true, true}, {GURL(kUrlA), GURL(kUrlB)}, F1M0L1, true}, | 467 {{true, true}, {GURL(kUrlA), GURL(kUrlB)}, F1M0L1, true}, |
463 | |
464 {{false, false, false}, | 468 {{false, false, false}, |
465 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, | 469 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, |
466 EMPTY, | 470 EMPTY, |
467 false}, | 471 false}, |
468 {{false, false, true}, | 472 {{false, false, true}, |
469 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, | 473 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, |
470 F0M0L1, | 474 F0M0L1, |
471 true}, | 475 true}, |
472 {{false, true, false}, | 476 {{false, true, false}, |
473 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, | 477 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC)}, |
(...skipping 25 matching lines...) Expand all Loading... |
499 false}, | 503 false}, |
500 }; | 504 }; |
501 | 505 |
502 for (size_t i = 0U; i < arraysize(kRedirectRedirectChainMatchPatternTestData); | 506 for (size_t i = 0U; i < arraysize(kRedirectRedirectChainMatchPatternTestData); |
503 ++i) { | 507 ++i) { |
504 auto test_data = kRedirectRedirectChainMatchPatternTestData[i]; | 508 auto test_data = kRedirectRedirectChainMatchPatternTestData[i]; |
505 NavigateAndExpectActivation( | 509 NavigateAndExpectActivation( |
506 test_data.blacklisted_urls, test_data.navigation_chain, | 510 test_data.blacklisted_urls, test_data.navigation_chain, |
507 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, | 511 safe_browsing::SB_THREAT_TYPE_URL_PHISHING, |
508 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, | 512 safe_browsing::ThreatPatternType::SOCIAL_ENGINEERING_ADS, |
509 test_data.hit_expected_pattern, test_data.expected_activation); | 513 test_data.hit_expected_pattern, std::string(), |
| 514 test_data.expected_activation); |
510 NavigateAndExpectActivation({false}, {GURL("https://dummy.com")}, EMPTY, | 515 NavigateAndExpectActivation({false}, {GURL("https://dummy.com")}, EMPTY, |
511 false); | 516 false); |
| 517 NavigateAndExpectActivation( |
| 518 test_data.blacklisted_urls, test_data.navigation_chain, |
| 519 safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER, |
| 520 safe_browsing::ThreatPatternType::NONE, test_data.hit_expected_pattern, |
| 521 kSubresourceFilterOnlySuffix, false); |
512 } | 522 } |
513 } | 523 } |
514 | 524 |
515 TEST_F(ContentSubresourceFilterDriverFactoryTest, NotificationVisibility) { | 525 TEST_F(ContentSubresourceFilterDriverFactoryTest, NotificationVisibility) { |
516 base::FieldTrialList field_trial_list(nullptr); | 526 base::FieldTrialList field_trial_list(nullptr); |
517 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 527 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
518 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 528 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
519 kActivationScopeAllSites); | 529 kActivationScopeAllSites); |
520 | 530 |
521 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY, | 531 NavigateAndExpectActivation({false}, {GURL(kExampleUrl)}, EMPTY, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 kActivationScopeActivationList, test_data.activation_list); | 578 kActivationScopeActivationList, test_data.activation_list); |
569 | 579 |
570 const GURL test_url("https://example.com/nonsoceng?q=engsocnon"); | 580 const GURL test_url("https://example.com/nonsoceng?q=engsocnon"); |
571 std::vector<GURL> navigation_chain; | 581 std::vector<GURL> navigation_chain; |
572 | 582 |
573 NavigateAndExpectActivation({false, false, false, true}, | 583 NavigateAndExpectActivation({false, false, false, true}, |
574 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, | 584 {GURL(kUrlA), GURL(kUrlB), GURL(kUrlC), test_url}, |
575 test_data.threat_type, | 585 test_data.threat_type, |
576 test_data.threat_type_metadata, | 586 test_data.threat_type_metadata, |
577 test_data.expected_activation ? F0M0L1 : EMPTY, | 587 test_data.expected_activation ? F0M0L1 : EMPTY, |
578 test_data.expected_activation); | 588 std::string(), test_data.expected_activation); |
579 }; | 589 }; |
580 | 590 |
581 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest, | 591 TEST_P(ContentSubresourceFilterDriverFactoryActivationScopeTest, |
582 ActivateForScopeType) { | 592 ActivateForScopeType) { |
583 const ActivationScopeTestData& test_data = GetParam(); | 593 const ActivationScopeTestData& test_data = GetParam(); |
584 base::FieldTrialList field_trial_list(nullptr); | 594 base::FieldTrialList field_trial_list(nullptr); |
585 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( | 595 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( |
586 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 596 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
587 test_data.activation_scope, | 597 test_data.activation_scope, |
588 kActivationListSocialEngineeringAdsInterstitial); | 598 kActivationListSocialEngineeringAdsInterstitial); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 ActivationScopeTest, | 653 ActivationScopeTest, |
644 ContentSubresourceFilterDriverFactoryActivationScopeTest, | 654 ContentSubresourceFilterDriverFactoryActivationScopeTest, |
645 ::testing::ValuesIn(kActivationScopeTestData)); | 655 ::testing::ValuesIn(kActivationScopeTestData)); |
646 | 656 |
647 INSTANTIATE_TEST_CASE_P( | 657 INSTANTIATE_TEST_CASE_P( |
648 ActivationLevelTest, | 658 ActivationLevelTest, |
649 ContentSubresourceFilterDriverFactoryActivationLevelTest, | 659 ContentSubresourceFilterDriverFactoryActivationLevelTest, |
650 ::testing::ValuesIn(kActivationLevelTestData)); | 660 ::testing::ValuesIn(kActivationLevelTestData)); |
651 | 661 |
652 } // namespace subresource_filter | 662 } // namespace subresource_filter |
OLD | NEW |