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

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

Issue 2669833004: Refactor subresource filter activation to support PlzNavigate cleanly. (Closed)
Patch Set: Rebase. Created 3 years, 10 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 "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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 }; 136 };
137 137
138 class MockSubresourceFilterDriver : public ContentSubresourceFilterDriver { 138 class MockSubresourceFilterDriver : public ContentSubresourceFilterDriver {
139 public: 139 public:
140 explicit MockSubresourceFilterDriver( 140 explicit MockSubresourceFilterDriver(
141 content::RenderFrameHost* render_frame_host) 141 content::RenderFrameHost* render_frame_host)
142 : ContentSubresourceFilterDriver(render_frame_host) {} 142 : ContentSubresourceFilterDriver(render_frame_host) {}
143 143
144 ~MockSubresourceFilterDriver() override = default; 144 ~MockSubresourceFilterDriver() override = default;
145 145
146 MOCK_METHOD3(ActivateForProvisionalLoad, 146 MOCK_METHOD2(ActivateForNextCommittedLoad, void(ActivationLevel, bool));
147 void(ActivationLevel, const GURL&, bool));
148 147
149 private: 148 private:
150 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterDriver); 149 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterDriver);
151 }; 150 };
152 151
153 class MockSubresourceFilterClient : public SubresourceFilterClient { 152 class MockSubresourceFilterClient : public SubresourceFilterClient {
154 public: 153 public:
155 MockSubresourceFilterClient() {} 154 MockSubresourceFilterClient() {}
156 155
157 ~MockSubresourceFilterClient() override = default; 156 ~MockSubresourceFilterClient() override = default;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 return ContentSubresourceFilterDriverFactory::FromWebContents( 198 return ContentSubresourceFilterDriverFactory::FromWebContents(
200 web_contents()); 199 web_contents());
201 } 200 }
202 201
203 MockSubresourceFilterClient* client() { return client_; } 202 MockSubresourceFilterClient* client() { return client_; }
204 MockSubresourceFilterDriver* driver() { return driver_; } 203 MockSubresourceFilterDriver* driver() { return driver_; }
205 204
206 MockSubresourceFilterDriver* subframe_driver() { return subframe_driver_; } 205 MockSubresourceFilterDriver* subframe_driver() { return subframe_driver_; }
207 content::RenderFrameHost* subframe_rfh() { return subframe_rfh_; } 206 content::RenderFrameHost* subframe_rfh() { return subframe_rfh_; }
208 207
208 void SimulateNavigationCommit(content::RenderFrameHost* rfh,
209 const GURL& url) {
210 // RenderFrameHostTester currently only simulates a call to
211 // ReadyToCommitNavigation when browser-side-navigation is enabled.
clamy 2017/02/09 10:32:06 Can you add a TODO to remove this function when cr
engedy 2017/02/09 13:42:01 Done.
212 if (!content::IsBrowserSideNavigationEnabled()) {
213 factory()->ReadyToCommitNavigationInternal(rfh, url,
214 false /* failed_navigation */);
215 }
216 content::RenderFrameHostTester::For(rfh)->SimulateNavigationCommit(url);
217 }
218
209 void BlacklistURLWithRedirectsNavigateAndCommit( 219 void BlacklistURLWithRedirectsNavigateAndCommit(
210 const std::vector<bool>& blacklisted_urls, 220 const std::vector<bool>& blacklisted_urls,
211 const std::vector<GURL>& navigation_chain, 221 const std::vector<GURL>& navigation_chain,
212 safe_browsing::SBThreatType threat_type, 222 safe_browsing::SBThreatType threat_type,
213 safe_browsing::ThreatPatternType threat_type_metadata, 223 safe_browsing::ThreatPatternType threat_type_metadata,
214 RedirectChainMatchPattern expected_pattern, 224 RedirectChainMatchPattern expected_pattern,
215 bool expected_activation) { 225 bool expected_activation) {
216 base::HistogramTester tester; 226 base::HistogramTester tester;
217 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1); 227 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1);
218 content::RenderFrameHostTester* rfh_tester = 228 content::RenderFrameHostTester* rfh_tester =
219 content::RenderFrameHostTester::For(main_rfh()); 229 content::RenderFrameHostTester::For(main_rfh());
220 230
221 rfh_tester->SimulateNavigationStart(navigation_chain.front()); 231 rfh_tester->SimulateNavigationStart(navigation_chain.front());
222 if (blacklisted_urls.front()) { 232 if (blacklisted_urls.front()) {
223 factory()->OnMainResourceMatchedSafeBrowsingBlacklist( 233 factory()->OnMainResourceMatchedSafeBrowsingBlacklist(
224 navigation_chain.front(), navigation_chain, threat_type, 234 navigation_chain.front(), navigation_chain, threat_type,
225 threat_type_metadata); 235 threat_type_metadata);
226 } 236 }
227 ::testing::Mock::VerifyAndClearExpectations(client()); 237 ::testing::Mock::VerifyAndClearExpectations(client());
228 238
229 for (size_t i = 1; i < navigation_chain.size(); ++i) { 239 for (size_t i = 1; i < navigation_chain.size(); ++i) {
230 const GURL url = navigation_chain[i]; 240 const GURL url = navigation_chain[i];
231 if (i < blacklisted_urls.size() && blacklisted_urls[i]) { 241 if (i < blacklisted_urls.size() && blacklisted_urls[i]) {
232 factory()->OnMainResourceMatchedSafeBrowsingBlacklist( 242 factory()->OnMainResourceMatchedSafeBrowsingBlacklist(
233 url, navigation_chain, threat_type, threat_type_metadata); 243 url, navigation_chain, threat_type, threat_type_metadata);
234 } 244 }
235 rfh_tester->SimulateRedirect(url); 245 rfh_tester->SimulateRedirect(url);
236 } 246 }
237 EXPECT_CALL(*driver(), 247
238 ActivateForProvisionalLoad(::testing::_, ::testing::_, 248 EXPECT_CALL(*driver(), ActivateForNextCommittedLoad(
239 expected_measure_performance())) 249 ::testing::_, expected_measure_performance()))
240 .Times(expected_activation); 250 .Times(expected_activation);
241 if (!content::IsBrowserSideNavigationEnabled()) { 251 SimulateNavigationCommit(main_rfh(), navigation_chain.back());
242 factory()->ReadyToCommitNavigationInternal(main_rfh(),
243 navigation_chain.back());
244 }
245
246 rfh_tester->SimulateNavigationCommit(navigation_chain.back());
247 ::testing::Mock::VerifyAndClearExpectations(driver()); 252 ::testing::Mock::VerifyAndClearExpectations(driver());
248 253
249 if (expected_pattern != EMPTY) { 254 if (expected_pattern != EMPTY) {
250 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), 255 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName),
251 ::testing::ElementsAre(base::Bucket(expected_pattern, 1))); 256 ::testing::ElementsAre(base::Bucket(expected_pattern, 1)));
252 EXPECT_THAT( 257 EXPECT_THAT(
253 tester.GetAllSamples(kNavigationChainSize), 258 tester.GetAllSamples(kNavigationChainSize),
254 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1))); 259 ::testing::ElementsAre(base::Bucket(navigation_chain.size(), 1)));
255 260
256 } else { 261 } else {
257 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName), 262 EXPECT_THAT(tester.GetAllSamples(kMatchesPatternHistogramName),
258 ::testing::IsEmpty()); 263 ::testing::IsEmpty());
259 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize), 264 EXPECT_THAT(tester.GetAllSamples(kNavigationChainSize),
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 ActivateForNextCommittedLoad(::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 content::RenderFrameHostTester::For(subframe_rfh())
277 ->SimulateNavigationStart(url);
278 SimulateNavigationCommit(subframe_rfh(), url);
272 ::testing::Mock::VerifyAndClearExpectations(subframe_driver()); 279 ::testing::Mock::VerifyAndClearExpectations(subframe_driver());
273 ::testing::Mock::VerifyAndClearExpectations(client()); 280 ::testing::Mock::VerifyAndClearExpectations(client());
274 } 281 }
275 282
276 void NavigateAndExpectActivation( 283 void NavigateAndExpectActivation(
277 const std::vector<bool>& blacklisted_urls, 284 const std::vector<bool>& blacklisted_urls,
278 const std::vector<GURL>& navigation_chain, 285 const std::vector<GURL>& navigation_chain,
279 safe_browsing::SBThreatType threat_type, 286 safe_browsing::SBThreatType threat_type,
280 safe_browsing::ThreatPatternType threat_type_metadata, 287 safe_browsing::ThreatPatternType threat_type_metadata,
281 RedirectChainMatchPattern expected_pattern, 288 RedirectChainMatchPattern expected_pattern,
(...skipping 16 matching lines...) Expand all
298 expected_pattern, expected_activation); 305 expected_pattern, expected_activation);
299 } 306 }
300 307
301 void EmulateDidDisallowFirstSubresourceMessage() { 308 void EmulateDidDisallowFirstSubresourceMessage() {
302 factory()->OnMessageReceived( 309 factory()->OnMessageReceived(
303 SubresourceFilterHostMsg_DidDisallowFirstSubresource( 310 SubresourceFilterHostMsg_DidDisallowFirstSubresource(
304 main_rfh()->GetRoutingID()), 311 main_rfh()->GetRoutingID()),
305 main_rfh()); 312 main_rfh());
306 } 313 }
307 314
315 void EmulateFailedNavigationAndExpectNoActivation(const GURL& url) {
316 EXPECT_CALL(*client(), ToggleNotificationVisibility(false)).Times(1);
317 EXPECT_CALL(*driver(),
318 ActivateForNextCommittedLoad(::testing::_, ::testing::_))
319 .Times(0);
320
321 // ReadyToCommitNavigation with browser-side navigation disabled is not
322 // called in production code. It is called with browser-side navigation
clamy 2017/02/09 10:32:06 I'm not sure I understand this comment. We should
engedy 2017/02/09 13:42:01 What I meant to write here: for *failed* navigatio
323 // enabled, in which case RenderFrameHostTester already calls it.
324 content::RenderFrameHostTester* rfh_tester =
325 content::RenderFrameHostTester::For(main_rfh());
326 rfh_tester->SimulateNavigationStart(url);
327 rfh_tester->SimulateNavigationError(url, 403);
328 ::testing::Mock::VerifyAndClearExpectations(driver());
329 ::testing::Mock::VerifyAndClearExpectations(client());
330 }
331
308 void EmulateInPageNavigation(const std::vector<bool>& blacklisted_urls, 332 void EmulateInPageNavigation(const std::vector<bool>& blacklisted_urls,
309 RedirectChainMatchPattern expected_pattern, 333 RedirectChainMatchPattern expected_pattern,
310 bool expected_activation) { 334 bool expected_activation) {
311 // This test examines the navigation with the following sequence of events: 335 // This test examines the navigation with the following sequence of events:
312 // DidStartProvisional(main, "example.com") 336 // DidStartProvisional(main, "example.com")
313 // ReadyToCommitNavigation(“example.com”) 337 // ReadyToCommitNavigation(“example.com”)
314 // DidCommitProvisional(main, "example.com") 338 // DidCommitProvisional(main, "example.com")
315 // DidStartProvisional(sub, "example.com/login") 339 // DidStartProvisional(sub, "example.com/login")
316 // DidCommitProvisional(sub, "example.com/login") 340 // DidCommitProvisional(sub, "example.com/login")
317 // DidCommitProvisional(main, "example.com#ref") 341 // DidCommitProvisional(main, "example.com#ref")
318 342
319 NavigateAndExpectActivation(blacklisted_urls, {GURL(kExampleUrl)}, 343 NavigateAndExpectActivation(blacklisted_urls, {GURL(kExampleUrl)},
320 expected_pattern, expected_activation); 344 expected_pattern, expected_activation);
321 EXPECT_CALL(*driver(), ActivateForProvisionalLoad( 345 EXPECT_CALL(*driver(),
322 ::testing::_, ::testing::_, ::testing::_)) 346 ActivateForNextCommittedLoad(::testing::_, ::testing::_))
323 .Times(0); 347 .Times(0);
324 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0); 348 EXPECT_CALL(*client(), ToggleNotificationVisibility(::testing::_)).Times(0);
325 content::RenderFrameHostTester::For(main_rfh()) 349 content::RenderFrameHostTester::For(main_rfh())
326 ->SimulateNavigationCommit(GURL(kExampleUrl)); 350 ->SimulateNavigationCommit(GURL(kExampleUrl));
327 ::testing::Mock::VerifyAndClearExpectations(driver()); 351 ::testing::Mock::VerifyAndClearExpectations(driver());
328 ::testing::Mock::VerifyAndClearExpectations(client()); 352 ::testing::Mock::VerifyAndClearExpectations(client());
329 } 353 }
330 354
331 private: 355 private:
332 static bool expected_measure_performance() { 356 static bool expected_measure_performance() {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 base::FieldTrialList field_trial_list(nullptr); 459 base::FieldTrialList field_trial_list(nullptr);
436 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( 460 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
437 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 461 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
438 kActivationScopeActivationList, 462 kActivationScopeActivationList,
439 kActivationListSocialEngineeringAdsInterstitial, 463 kActivationListSocialEngineeringAdsInterstitial,
440 "1" /* performance_measurement_rate */); 464 "1" /* performance_measurement_rate */);
441 EmulateInPageNavigation({true}, NO_REDIRECTS_HIT, 465 EmulateInPageNavigation({true}, NO_REDIRECTS_HIT,
442 true /* expected_activation */); 466 true /* expected_activation */);
443 } 467 }
444 468
469 TEST_F(ContentSubresourceFilterDriverFactoryTest, FailedNavigation) {
470 base::FieldTrialList field_trial_list(nullptr);
471 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
472 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
473 kActivationScopeAllSites);
474 const GURL url(kExampleUrl);
475 NavigateAndExpectActivation({false}, {url}, EMPTY,
476 true /* expected_activation */);
477 EmulateFailedNavigationAndExpectNoActivation(url);
478 }
479
445 TEST_F(ContentSubresourceFilterDriverFactoryTest, RedirectPatternTest) { 480 TEST_F(ContentSubresourceFilterDriverFactoryTest, RedirectPatternTest) {
446 base::FieldTrialList field_trial_list(nullptr); 481 base::FieldTrialList field_trial_list(nullptr);
447 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle( 482 testing::ScopedSubresourceFilterFeatureToggle scoped_feature_toggle(
448 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 483 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
449 kActivationScopeActivationList, 484 kActivationScopeActivationList,
450 kActivationListSocialEngineeringAdsInterstitial); 485 kActivationListSocialEngineeringAdsInterstitial);
451 struct RedirectRedirectChainMatchPatternTestData { 486 struct RedirectRedirectChainMatchPatternTestData {
452 std::vector<bool> blacklisted_urls; 487 std::vector<bool> blacklisted_urls;
453 std::vector<GURL> navigation_chain; 488 std::vector<GURL> navigation_chain;
454 RedirectChainMatchPattern hit_expected_pattern; 489 RedirectChainMatchPattern hit_expected_pattern;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 ActivationScopeTest, 678 ActivationScopeTest,
644 ContentSubresourceFilterDriverFactoryActivationScopeTest, 679 ContentSubresourceFilterDriverFactoryActivationScopeTest,
645 ::testing::ValuesIn(kActivationScopeTestData)); 680 ::testing::ValuesIn(kActivationScopeTestData));
646 681
647 INSTANTIATE_TEST_CASE_P( 682 INSTANTIATE_TEST_CASE_P(
648 ActivationLevelTest, 683 ActivationLevelTest,
649 ContentSubresourceFilterDriverFactoryActivationLevelTest, 684 ContentSubresourceFilterDriverFactoryActivationLevelTest,
650 ::testing::ValuesIn(kActivationLevelTestData)); 685 ::testing::ValuesIn(kActivationLevelTestData));
651 686
652 } // namespace subresource_filter 687 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698