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

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

Issue 2841933003: [subresource_filter] Remove some state from the driver factory (Closed)
Patch Set: no more dep branch Created 3 years, 8 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/subresource_filter_safe_ browsing_activation_throttle.h" 5 #include "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_activation_throttle.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/field_trial.h" 10 #include "base/metrics/field_trial.h"
11 #include "base/test/histogram_tester.h" 11 #include "base/test/histogram_tester.h"
12 #include "base/threading/thread_task_runner_handle.h"
12 #include "components/safe_browsing_db/test_database_manager.h" 13 #include "components/safe_browsing_db/test_database_manager.h"
13 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" 14 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h"
15 #include "components/subresource_filter/content/browser/content_subresource_filt er_throttle_manager.h"
14 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab ase_manager.h" 16 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab ase_manager.h"
15 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h" 17 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h"
18 #include "components/subresource_filter/content/browser/verified_ruleset_dealer. h"
16 #include "components/subresource_filter/core/browser/subresource_filter_features .h" 19 #include "components/subresource_filter/core/browser/subresource_filter_features .h"
17 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" 20 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h"
21 #include "components/subresource_filter/core/common/activation_decision.h"
22 #include "components/subresource_filter/core/common/proto/rules.pb.h"
18 #include "components/subresource_filter/core/common/test_ruleset_creator.h" 23 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
19 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/navigation_handle.h" 25 #include "content/public/browser/navigation_handle.h"
21 #include "content/public/browser/web_contents_observer.h" 26 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/test/navigation_simulator.h" 27 #include "content/public/test/navigation_simulator.h"
23 #include "content/public/test/test_renderer_host.h" 28 #include "content/public/test/test_renderer_host.h"
24 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
26 31
27 namespace subresource_filter { 32 namespace subresource_filter {
(...skipping 23 matching lines...) Expand all
51 F1M1L0, // B and/or C and A are Safe Browsing matches. 56 F1M1L0, // B and/or C and A are Safe Browsing matches.
52 F1M1L1, // B and/or C and A and D are Safe Browsing matches. 57 F1M1L1, // B and/or C and A and D are Safe Browsing matches.
53 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects 58 NO_REDIRECTS_HIT, // Redirect chain consists of single URL, aka no redirects
54 // has happened, and this URL was a Safe Browsing hit. 59 // has happened, and this URL was a Safe Browsing hit.
55 NUM_HIT_PATTERNS, 60 NUM_HIT_PATTERNS,
56 }; 61 };
57 62
58 class MockSubresourceFilterClient 63 class MockSubresourceFilterClient
59 : public subresource_filter::SubresourceFilterClient { 64 : public subresource_filter::SubresourceFilterClient {
60 public: 65 public:
61 MockSubresourceFilterClient() {} 66 MockSubresourceFilterClient(VerifiedRulesetDealer::Handle* dealer_handle)
67 : dealer_handle_(dealer_handle) {}
62 68
63 ~MockSubresourceFilterClient() override = default; 69 ~MockSubresourceFilterClient() override = default;
64 70
65 MOCK_METHOD1(ToggleNotificationVisibility, void(bool)); 71 MOCK_METHOD1(ToggleNotificationVisibility, void(bool));
66 MOCK_METHOD1(ShouldSuppressActivation, bool(content::NavigationHandle*)); 72 MOCK_METHOD1(ShouldSuppressActivation, bool(content::NavigationHandle*));
67 MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&)); 73 MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&));
68 MOCK_METHOD1(WhitelistInCurrentWebContents, void(const GURL&)); 74 MOCK_METHOD1(WhitelistInCurrentWebContents, void(const GURL&));
69 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); 75
76 VerifiedRulesetDealer::Handle* GetRulesetDealer() override {
77 return dealer_handle_;
78 }
70 79
71 private: 80 private:
81 // Owned by the test harness.
82 VerifiedRulesetDealer::Handle* dealer_handle_;
72 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); 83 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient);
73 }; 84 };
74 85
75 // Throttle to call WillProcessResponse on the factory, which is otherwise
76 // called by the ThrottleManager.
77 class TestForwardingNavigationThrottle : public content::NavigationThrottle {
78 public:
79 TestForwardingNavigationThrottle(content::NavigationHandle* handle)
80 : content::NavigationThrottle(handle) {}
81 ~TestForwardingNavigationThrottle() override {}
82
83 // content::NavigationThrottle:
84 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse()
85 override {
86 content::WebContents* web_contents = navigation_handle()->GetWebContents();
87 ContentSubresourceFilterDriverFactory* factory =
88 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents);
89 factory->WillProcessResponse(navigation_handle());
90 return content::NavigationThrottle::PROCEED;
91 }
92 const char* GetNameForLogging() override {
93 return "TestForwardingNavigationThrottle";
94 }
95
96 private:
97 DISALLOW_COPY_AND_ASSIGN(TestForwardingNavigationThrottle);
98 };
99
100 } // namespace 86 } // namespace
101 87
102 class SubresourceFilterSafeBrowsingActivationThrottleTest 88 class SubresourceFilterSafeBrowsingActivationThrottleTest
103 : public content::RenderViewHostTestHarness, 89 : public content::RenderViewHostTestHarness,
104 public content::WebContentsObserver { 90 public content::WebContentsObserver {
105 public: 91 public:
106 SubresourceFilterSafeBrowsingActivationThrottleTest() 92 SubresourceFilterSafeBrowsingActivationThrottleTest()
107 : field_trial_list_(nullptr) {} 93 : field_trial_list_(nullptr) {}
108 ~SubresourceFilterSafeBrowsingActivationThrottleTest() override {} 94 ~SubresourceFilterSafeBrowsingActivationThrottleTest() override {}
109 95
110 void SetUp() override { 96 void SetUp() override {
111 content::RenderViewHostTestHarness::SetUp(); 97 content::RenderViewHostTestHarness::SetUp();
112 scoped_feature_toggle_.reset( 98 scoped_feature_toggle_.reset(
113 new testing::ScopedSubresourceFilterFeatureToggle( 99 new testing::ScopedSubresourceFilterFeatureToggle(
114 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 100 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
115 kActivationScopeActivationList, kActivationListSubresourceFilter)); 101 kActivationScopeActivationList, kActivationListSubresourceFilter));
102
103 // Initialize the ruleset dealer. This is needed because the throttle
104 // manager requires a valid ruleset dealer to create throttles, which are
105 // necessary for checking that the activation decision was plumbed through.
106 testing::TestRulesetCreator test_ruleset_creator;
107 testing::TestRulesetPair ruleset_pair;
108 std::vector<proto::UrlRule> rules;
109 ASSERT_NO_FATAL_FAILURE(
110 test_ruleset_creator.CreateRulesetWithRules(rules, &ruleset_pair));
111 dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>(
112 base::ThreadTaskRunnerHandle::Get());
113 dealer_handle_->SetRulesetFile(
114 testing::TestRuleset::Open(ruleset_pair.indexed));
115
116 // Make the blocking task runner run on the current task runner for the
117 // tests, to ensure that the NavigationSimulator properly runs all necessary
118 // tasks while waiting for throttle checks to finish.
119 dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>(
120 base::ThreadTaskRunnerHandle::Get());
121 dealer_handle_->SetRulesetFile(
122 testing::TestRuleset::Open(ruleset_pair.indexed));
116 // Note: Using NiceMock to allow uninteresting calls and suppress warnings. 123 // Note: Using NiceMock to allow uninteresting calls and suppress warnings.
117 auto client = 124 auto client =
118 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); 125 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(
126 dealer_handle_.get());
119 ContentSubresourceFilterDriverFactory::CreateForWebContents( 127 ContentSubresourceFilterDriverFactory::CreateForWebContents(
120 RenderViewHostTestHarness::web_contents(), std::move(client)); 128 RenderViewHostTestHarness::web_contents(), std::move(client));
121 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); 129 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager();
122 NavigateAndCommit(GURL("https://test.com")); 130 NavigateAndCommit(GURL("https://test.com"));
123 Observe(RenderViewHostTestHarness::web_contents()); 131 Observe(RenderViewHostTestHarness::web_contents());
124 } 132 }
125 133
126 ContentSubresourceFilterDriverFactory* factory() { 134 ContentSubresourceFilterThrottleManager* throttle_manager() {
127 return ContentSubresourceFilterDriverFactory::FromWebContents( 135 return ContentSubresourceFilterDriverFactory::FromWebContents(
128 RenderViewHostTestHarness::web_contents()); 136 RenderViewHostTestHarness::web_contents())
137 ->throttle_manager();
129 } 138 }
130 139
131 // content::WebContentsObserver: 140 // content::WebContentsObserver:
132 void DidStartNavigation( 141 void DidStartNavigation(
133 content::NavigationHandle* navigation_handle) override { 142 content::NavigationHandle* navigation_handle) override {
134 ASSERT_TRUE(navigation_handle->IsInMainFrame()); 143 ASSERT_TRUE(navigation_handle->IsInMainFrame());
135 navigation_handle_ = navigation_handle; 144 navigation_handle_ = navigation_handle;
136 navigation_handle->RegisterThrottleForTesting( 145 navigation_handle->RegisterThrottleForTesting(
137 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( 146 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>(
138 navigation_handle, fake_safe_browsing_database_)); 147 navigation_handle, fake_safe_browsing_database_));
139 navigation_handle->RegisterThrottleForTesting( 148 std::vector<std::unique_ptr<content::NavigationThrottle>> throttles;
140 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); 149 throttle_manager()->MaybeAppendNavigationThrottles(navigation_handle,
150 &throttles);
151 for (auto& it : throttles) {
152 navigation_handle->RegisterThrottleForTesting(std::move(it));
153 }
141 } 154 }
142 155
143 void SimulateStartAndExpectProceed() { 156 void SimulateStartAndExpectProceed() {
144 navigation_simulator_->Start(); 157 navigation_simulator_->Start();
145 EXPECT_EQ(content::NavigationThrottle::PROCEED, 158 EXPECT_EQ(content::NavigationThrottle::PROCEED,
146 navigation_simulator_->GetLastThrottleCheckResult()); 159 navigation_simulator_->GetLastThrottleCheckResult());
147 } 160 }
148 161
149 void SimulateRedirectAndExpectProceed(const GURL& new_url) { 162 void SimulateRedirectAndExpectProceed(const GURL& new_url) {
150 navigation_simulator_->Redirect(new_url); 163 navigation_simulator_->Redirect(new_url);
(...skipping 19 matching lines...) Expand all
170 } 183 }
171 184
172 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } 185 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); }
173 186
174 const base::HistogramTester& tester() const { return tester_; } 187 const base::HistogramTester& tester() const { return tester_; }
175 188
176 private: 189 private:
177 base::FieldTrialList field_trial_list_; 190 base::FieldTrialList field_trial_list_;
178 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> 191 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle>
179 scoped_feature_toggle_; 192 scoped_feature_toggle_;
193 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
180 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; 194 std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
181 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; 195 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
182 base::HistogramTester tester_; 196 base::HistogramTester tester_;
183 content::NavigationHandle* navigation_handle_; 197 content::NavigationHandle* navigation_handle_;
184 198
185 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); 199 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest);
186 }; 200 };
187 201
188 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 202 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
189 ListNotMatched_NoActivation) { 203 ListNotMatched_NoActivation) {
190 const GURL url(kURL); 204 const GURL url(kURL);
191 CreateTestNavigationForMainFrame(url); 205 CreateTestNavigationForMainFrame(url);
192 SimulateStartAndExpectProceed(); 206 SimulateStartAndExpectProceed();
193 SimulateCommitAndExpectProceed(); 207 SimulateCommitAndExpectProceed();
194 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: 208 EXPECT_EQ(
195 ACTIVATION_LIST_NOT_MATCHED, 209 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED,
196 factory()->GetActivationDecisionForLastCommittedPageLoad()); 210 throttle_manager()->GetActivationDecisionForLastCommittedPageLoad());
197 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, 211 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
198 0); 212 0);
199 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); 213 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0);
200 } 214 }
201 215
202 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 216 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
203 ListMatched_Activation) { 217 ListMatched_Activation) {
204 const GURL url(kURL); 218 const GURL url(kURL);
205 ConfigureAsSubresourceFilterOnlyURL(url); 219 ConfigureAsSubresourceFilterOnlyURL(url);
206 CreateTestNavigationForMainFrame(url); 220 CreateTestNavigationForMainFrame(url);
207 SimulateStartAndExpectProceed(); 221 SimulateStartAndExpectProceed();
208 SimulateCommitAndExpectProceed(); 222 SimulateCommitAndExpectProceed();
209 EXPECT_EQ( 223 EXPECT_EQ(
210 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, 224 ActivationDecision::ACTIVATED,
211 factory()->GetActivationDecisionForLastCommittedPageLoad()); 225 throttle_manager()->GetActivationDecisionForLastCommittedPageLoad());
212 tester().ExpectUniqueSample( 226 tester().ExpectUniqueSample(
213 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1); 227 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1);
214 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1, 228 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1,
215 1); 229 1);
216 } 230 }
217 231
218 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 232 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
219 ListNotMatchedAfterRedirect_NoActivation) { 233 ListNotMatchedAfterRedirect_NoActivation) {
220 const GURL url(kURL); 234 const GURL url(kURL);
221 CreateTestNavigationForMainFrame(url); 235 CreateTestNavigationForMainFrame(url);
222 SimulateStartAndExpectProceed(); 236 SimulateStartAndExpectProceed();
223 SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); 237 SimulateRedirectAndExpectProceed(GURL(kRedirectURL));
224 SimulateCommitAndExpectProceed(); 238 SimulateCommitAndExpectProceed();
225 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: 239 EXPECT_EQ(
226 ACTIVATION_LIST_NOT_MATCHED, 240 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED,
227 factory()->GetActivationDecisionForLastCommittedPageLoad()); 241 throttle_manager()->GetActivationDecisionForLastCommittedPageLoad());
228 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, 242 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
229 0); 243 0);
230 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); 244 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0);
231 } 245 }
232 246
233 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 247 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
234 ListMatchedAfterRedirect_Activation) { 248 ListMatchedAfterRedirect_Activation) {
235 const GURL url(kURL); 249 const GURL url(kURL);
236 ConfigureAsSubresourceFilterOnlyURL(GURL(kRedirectURL)); 250 ConfigureAsSubresourceFilterOnlyURL(GURL(kRedirectURL));
237 CreateTestNavigationForMainFrame(url); 251 CreateTestNavigationForMainFrame(url);
238 SimulateStartAndExpectProceed(); 252 SimulateStartAndExpectProceed();
239 SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); 253 SimulateRedirectAndExpectProceed(GURL(kRedirectURL));
240 SimulateCommitAndExpectProceed(); 254 SimulateCommitAndExpectProceed();
241 EXPECT_EQ( 255 EXPECT_EQ(
242 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, 256 ActivationDecision::ACTIVATED,
243 factory()->GetActivationDecisionForLastCommittedPageLoad()); 257 throttle_manager()->GetActivationDecisionForLastCommittedPageLoad());
244 tester().ExpectUniqueSample( 258 tester().ExpectUniqueSample(
245 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1); 259 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1);
246 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, 260 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2,
247 1); 261 1);
248 } 262 }
249 263
250 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 264 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
251 ListNotMatchedAndTimeout_NoActivation) { 265 ListNotMatchedAndTimeout_NoActivation) {
252 const GURL url(kURL); 266 const GURL url(kURL);
253 SimulateTimeout(); 267 SimulateTimeout();
254 CreateTestNavigationForMainFrame(url); 268 CreateTestNavigationForMainFrame(url);
255 SimulateStartAndExpectProceed(); 269 SimulateStartAndExpectProceed();
256 SimulateCommitAndExpectProceed(); 270 SimulateCommitAndExpectProceed();
257 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: 271 EXPECT_EQ(
258 ACTIVATION_LIST_NOT_MATCHED, 272 ActivationDecision::ACTIVATION_LIST_NOT_MATCHED,
259 factory()->GetActivationDecisionForLastCommittedPageLoad()); 273 throttle_manager()->GetActivationDecisionForLastCommittedPageLoad());
260 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, 274 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
261 0); 275 0);
262 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); 276 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0);
263 } 277 }
264 278
265 // TODO(melandory): Once non-defering check in WillStart is implemented add one 279 // TODO(melandory): Once non-defering check in WillStart is implemented add one
266 // more test that destroys the Navigation along with corresponding throttles 280 // more test that destroys the Navigation along with corresponding throttles
267 // while the SB check is pending? (To be run by ASAN bots to ensure 281 // while the SB check is pending? (To be run by ASAN bots to ensure
268 // no use-after-free.) 282 // no use-after-free.)
269 283
270 } // namespace subresource_filter 284 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698