OLD | NEW |
---|---|
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 <map> | |
7 #include <memory> | 8 #include <memory> |
9 #include <set> | |
10 #include <string> | |
11 #include <tuple> | |
12 #include <utility> | |
8 | 13 |
9 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
10 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
16 #include "base/run_loop.h" | |
11 #include "base/test/histogram_tester.h" | 17 #include "base/test/histogram_tester.h" |
18 #include "base/timer/mock_timer.h" | |
12 #include "components/safe_browsing_db/test_database_manager.h" | 19 #include "components/safe_browsing_db/test_database_manager.h" |
13 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" | 20 #include "components/subresource_filter/content/browser/content_subresource_filt er_driver_factory.h" |
14 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h" | 21 #include "components/subresource_filter/content/browser/subresource_filter_clien t.h" |
22 #include "components/subresource_filter/content/browser/subresource_filter_safe_ browsing_client.h" | |
15 #include "components/subresource_filter/core/browser/subresource_filter_features .h" | 23 #include "components/subresource_filter/core/browser/subresource_filter_features .h" |
16 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" | 24 #include "components/subresource_filter/core/browser/subresource_filter_features _test_support.h" |
17 #include "components/subresource_filter/core/common/test_ruleset_creator.h" | 25 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
18 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/navigation_handle.h" | 27 #include "content/public/browser/navigation_handle.h" |
20 #include "content/public/browser/web_contents_observer.h" | 28 #include "content/public/browser/web_contents_observer.h" |
29 #include "content/public/test/cancelling_navigation_throttle.h" | |
21 #include "content/public/test/navigation_simulator.h" | 30 #include "content/public/test/navigation_simulator.h" |
22 #include "content/public/test/test_renderer_host.h" | 31 #include "content/public/test/test_renderer_host.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
25 | 34 |
26 namespace subresource_filter { | 35 namespace subresource_filter { |
27 | 36 |
28 namespace { | 37 namespace { |
29 | 38 |
30 char kURL[] = "http://example.test/"; | 39 char kURL[] = "http://example.test/"; |
31 char kRedirectURL[] = "http://foo.test/"; | 40 char kRedirectURL[] = "http://redirect.test/"; |
32 | 41 |
33 // Names of navigation chain patterns histogram. | 42 // Names of navigation chain patterns histogram. |
34 const char kMatchesPatternHistogramNameSubresourceFilterSuffix[] = | 43 const char kMatchesPatternHistogramNameSubresourceFilterSuffix[] = |
35 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." | 44 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." |
36 "SubresourceFilterOnly"; | 45 "SubresourceFilterOnly"; |
37 const char kNavigationChainSizeSubresourceFilterSuffix[] = | 46 const char kNavigationChainSizeSubresourceFilterSuffix[] = |
38 "SubresourceFilter.PageLoad.RedirectChainLength.SubresourceFilterOnly"; | 47 "SubresourceFilter.PageLoad.RedirectChainLength.SubresourceFilterOnly"; |
48 const char kSafeBrowsingNavigationDelay[] = | |
49 "SubresourceFilter.SafeBrowsing.NavigationDelay"; | |
39 | 50 |
40 // Human readable representation of expected redirect chain match patterns. | 51 // Human readable representation of expected redirect chain match patterns. |
41 // The explanations for the buckets given for the following redirect chain: | 52 // 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. | 53 // A->B->C->D, where A is initial URL and D is a final URL. |
43 enum RedirectChainMatchPattern { | 54 enum RedirectChainMatchPattern { |
44 EMPTY, // No histograms were recorded. | 55 EMPTY, // No histograms were recorded. |
45 F0M0L1, // D is a Safe Browsing match. | 56 F0M0L1, // D is a Safe Browsing match. |
46 F0M1L0, // B or C, or both are Safe Browsing matches. | 57 F0M1L0, // B or C, or both are Safe Browsing matches. |
47 F0M1L1, // B or C, or both and D are Safe Browsing matches. | 58 F0M1L1, // B or C, or both and D are Safe Browsing matches. |
48 F1M0L0, // A is Safe Browsing match | 59 F1M0L0, // A is Safe Browsing match |
(...skipping 16 matching lines...) Expand all Loading... | |
65 safe_browsing::SBThreatType threat_type) { | 76 safe_browsing::SBThreatType threat_type) { |
66 url_to_threat_type_[url] = threat_type; | 77 url_to_threat_type_[url] = threat_type; |
67 } | 78 } |
68 | 79 |
69 void SimulateTimeout() { simulate_timeout_ = true; } | 80 void SimulateTimeout() { simulate_timeout_ = true; } |
70 | 81 |
71 protected: | 82 protected: |
72 ~FakeSafeBrowsingDatabaseManager() override {} | 83 ~FakeSafeBrowsingDatabaseManager() override {} |
73 | 84 |
74 bool CheckUrlForSubresourceFilter(const GURL& url, Client* client) override { | 85 bool CheckUrlForSubresourceFilter(const GURL& url, Client* client) override { |
86 // Maintain the invariant that each client only makes a single request. | |
87 DCHECK(checks_.find(client) == checks_.end()); | |
75 if (simulate_timeout_) | 88 if (simulate_timeout_) |
76 return false; | 89 return false; |
77 if (!url_to_threat_type_.count(url)) | 90 if (!url_to_threat_type_.count(url)) |
78 return true; | 91 return true; |
79 | 92 |
93 checks_.insert(client); | |
94 | |
80 content::BrowserThread::PostTask( | 95 content::BrowserThread::PostTask( |
81 content::BrowserThread::IO, FROM_HERE, | 96 content::BrowserThread::IO, FROM_HERE, |
82 base::Bind(&Client::OnCheckBrowseUrlResult, base::Unretained(client), | 97 base::Bind(&FakeSafeBrowsingDatabaseManager:: |
83 url, url_to_threat_type_[url], | 98 OnCheckUrlForSubresourceFilterComplete, |
84 safe_browsing::ThreatMetadata())); | 99 base::Unretained(this), base::Unretained(client), url)); |
85 return false; | 100 return false; |
86 } | 101 } |
87 | 102 |
103 void OnCheckUrlForSubresourceFilterComplete(Client* client, const GURL& url) { | |
104 if (checks_.find(client) == checks_.end()) | |
105 return; | |
106 client->OnCheckBrowseUrlResult(url, url_to_threat_type_[url], | |
107 safe_browsing::ThreatMetadata()); | |
108 } | |
109 | |
88 bool CheckResourceUrl(const GURL& url, Client* client) override { | 110 bool CheckResourceUrl(const GURL& url, Client* client) override { |
89 return true; | 111 return true; |
90 } | 112 } |
91 | 113 |
92 bool IsSupported() const override { return true; } | 114 bool IsSupported() const override { return true; } |
93 bool ChecksAreAlwaysAsync() const override { return false; } | 115 bool ChecksAreAlwaysAsync() const override { return false; } |
94 bool CanCheckResourceType( | 116 bool CanCheckResourceType( |
95 content::ResourceType /* resource_type */) const override { | 117 content::ResourceType /* resource_type */) const override { |
96 return true; | 118 return true; |
97 } | 119 } |
98 | 120 |
99 safe_browsing::ThreatSource GetThreatSource() const override { | 121 safe_browsing::ThreatSource GetThreatSource() const override { |
100 return safe_browsing::ThreatSource::LOCAL_PVER4; | 122 return safe_browsing::ThreatSource::LOCAL_PVER4; |
101 } | 123 } |
102 | 124 |
103 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, | 125 bool CheckExtensionIDs(const std::set<std::string>& extension_ids, |
104 Client* client) override { | 126 Client* client) override { |
105 return true; | 127 return true; |
106 } | 128 } |
107 | 129 |
130 void CancelCheck(Client* client) override { checks_.erase(client); } | |
131 | |
108 private: | 132 private: |
109 std::map<GURL, safe_browsing::SBThreatType> url_to_threat_type_; | 133 std::map<GURL, safe_browsing::SBThreatType> url_to_threat_type_; |
110 bool simulate_timeout_; | 134 bool simulate_timeout_; |
111 | 135 |
136 std::set<Client*> checks_; | |
137 | |
112 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); | 138 DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager); |
113 }; | 139 }; |
114 | 140 |
115 class MockSubresourceFilterClient | 141 class MockSubresourceFilterClient |
116 : public subresource_filter::SubresourceFilterClient { | 142 : public subresource_filter::SubresourceFilterClient { |
117 public: | 143 public: |
118 MockSubresourceFilterClient() {} | 144 MockSubresourceFilterClient() {} |
119 | 145 |
120 ~MockSubresourceFilterClient() override = default; | 146 ~MockSubresourceFilterClient() override = default; |
121 | 147 |
122 MOCK_METHOD1(ToggleNotificationVisibility, void(bool)); | 148 MOCK_METHOD1(ToggleNotificationVisibility, void(bool)); |
123 MOCK_METHOD1(IsWhitelistedByContentSettings, bool(const GURL&)); | 149 MOCK_METHOD1(IsWhitelistedByContentSettings, bool(const GURL&)); |
124 MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&)); | 150 MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&)); |
125 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); | 151 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); |
126 | 152 |
127 private: | 153 private: |
128 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); | 154 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); |
129 }; | 155 }; |
130 | 156 |
131 // Throttle to call WillProcessResponse on the factory, which is otherwise | 157 // Throttle to call WillProcessResponse on the factory, which is otherwise |
132 // called by the ThrottleManager. | 158 // called by the ThrottleManager. |
133 class TestForwardingNavigationThrottle : public content::NavigationThrottle { | 159 class TestForwardingNavigationThrottle : public content::NavigationThrottle { |
134 public: | 160 public: |
135 TestForwardingNavigationThrottle(content::NavigationHandle* handle) | 161 explicit TestForwardingNavigationThrottle(content::NavigationHandle* handle) |
136 : content::NavigationThrottle(handle) {} | 162 : content::NavigationThrottle(handle) {} |
137 ~TestForwardingNavigationThrottle() override {} | 163 ~TestForwardingNavigationThrottle() override {} |
138 | 164 |
139 // content::NavigationThrottle: | 165 // content::NavigationThrottle: |
140 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() | 166 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
141 override { | 167 override { |
142 content::WebContents* web_contents = navigation_handle()->GetWebContents(); | 168 content::WebContents* web_contents = navigation_handle()->GetWebContents(); |
143 ContentSubresourceFilterDriverFactory* factory = | 169 ContentSubresourceFilterDriverFactory* factory = |
144 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); | 170 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); |
145 factory->WillProcessResponse(navigation_handle()); | 171 factory->WillProcessResponse(navigation_handle()); |
(...skipping 23 matching lines...) Expand all Loading... | |
169 // Note: Using NiceMock to allow uninteresting calls and suppress warnings. | 195 // Note: Using NiceMock to allow uninteresting calls and suppress warnings. |
170 auto client = | 196 auto client = |
171 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); | 197 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); |
172 ContentSubresourceFilterDriverFactory::CreateForWebContents( | 198 ContentSubresourceFilterDriverFactory::CreateForWebContents( |
173 RenderViewHostTestHarness::web_contents(), std::move(client)); | 199 RenderViewHostTestHarness::web_contents(), std::move(client)); |
174 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); | 200 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
175 NavigateAndCommit(GURL("https://test.com")); | 201 NavigateAndCommit(GURL("https://test.com")); |
176 Observe(RenderViewHostTestHarness::web_contents()); | 202 Observe(RenderViewHostTestHarness::web_contents()); |
177 } | 203 } |
178 | 204 |
205 void TearDown() override { | |
206 RunUntilIdle(); | |
207 content::RenderViewHostTestHarness::TearDown(); | |
208 } | |
209 | |
179 ContentSubresourceFilterDriverFactory* factory() { | 210 ContentSubresourceFilterDriverFactory* factory() { |
180 return ContentSubresourceFilterDriverFactory::FromWebContents( | 211 return ContentSubresourceFilterDriverFactory::FromWebContents( |
181 RenderViewHostTestHarness::web_contents()); | 212 RenderViewHostTestHarness::web_contents()); |
182 } | 213 } |
183 | 214 |
184 // content::WebContentsObserver: | 215 // content::WebContentsObserver: |
185 void DidStartNavigation( | 216 void DidStartNavigation( |
186 content::NavigationHandle* navigation_handle) override { | 217 content::NavigationHandle* navigation_handle) override { |
187 ASSERT_TRUE(navigation_handle->IsInMainFrame()); | 218 ASSERT_TRUE(navigation_handle->IsInMainFrame()); |
188 navigation_handle_ = navigation_handle; | 219 auto mock_timer = base::MakeUnique<base::MockTimer>( |
220 false /* retain_user_task */, false /* is_repeating */); | |
221 mock_timer_ = mock_timer.get(); | |
189 navigation_handle->RegisterThrottleForTesting( | 222 navigation_handle->RegisterThrottleForTesting( |
190 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( | 223 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( |
191 navigation_handle, fake_safe_browsing_database_)); | 224 navigation_handle, std::move(mock_timer), |
225 fake_safe_browsing_database_)); | |
192 navigation_handle->RegisterThrottleForTesting( | 226 navigation_handle->RegisterThrottleForTesting( |
193 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); | 227 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); |
194 } | 228 } |
195 | 229 |
230 void DidFinishNavigation( | |
231 content::NavigationHandle* navigation_handle) override { | |
232 mock_timer_ = nullptr; | |
233 } | |
234 | |
235 content::NavigationThrottle::ThrottleCheckResult SimulateStart() { | |
236 navigation_simulator_->Start(); | |
237 auto result = navigation_simulator_->GetLastThrottleCheckResult(); | |
238 if (result == content::NavigationThrottle::CANCEL) | |
239 navigation_simulator_.reset(); | |
240 return result; | |
241 } | |
242 | |
243 content::NavigationThrottle::ThrottleCheckResult SimulateRedirect( | |
244 const GURL& new_url) { | |
245 navigation_simulator_->Redirect(new_url); | |
246 auto result = navigation_simulator_->GetLastThrottleCheckResult(); | |
247 if (result == content::NavigationThrottle::CANCEL) | |
248 navigation_simulator_.reset(); | |
249 return result; | |
250 } | |
251 | |
252 content::NavigationThrottle::ThrottleCheckResult SimulateCommit() { | |
253 navigation_simulator_->Commit(); | |
254 auto result = navigation_simulator_->GetLastThrottleCheckResult(); | |
255 if (result == content::NavigationThrottle::CANCEL) | |
256 navigation_simulator_.reset(); | |
257 return result; | |
258 } | |
259 | |
196 void SimulateStartAndExpectProceed() { | 260 void SimulateStartAndExpectProceed() { |
197 navigation_simulator_->Start(); | 261 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateStart()); |
198 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
199 navigation_simulator_->GetLastThrottleCheckResult()); | |
200 } | 262 } |
201 | 263 |
202 void SimulateRedirectAndExpectProceed(const GURL& new_url) { | 264 void SimulateRedirectAndExpectProceed(const GURL& new_url) { |
203 navigation_simulator_->Redirect(new_url); | 265 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateRedirect(new_url)); |
204 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
205 navigation_simulator_->GetLastThrottleCheckResult()); | |
206 } | 266 } |
207 | 267 |
208 void SimulateCommitAndExpectProceed() { | 268 void SimulateCommitAndExpectProceed() { |
209 navigation_simulator_->Commit(); | 269 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateCommit()); |
210 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
211 navigation_simulator_->GetLastThrottleCheckResult()); | |
212 } | 270 } |
213 | 271 |
214 void CreateTestNavigationForMainFrame(const GURL& first_url) { | 272 void CreateTestNavigationForMainFrame(const GURL& first_url) { |
215 navigation_simulator_ = | 273 navigation_simulator_ = |
216 content::NavigationSimulator::CreateRendererInitiated(first_url, | 274 content::NavigationSimulator::CreateRendererInitiated(first_url, |
217 main_rfh()); | 275 main_rfh()); |
218 } | 276 } |
219 | 277 |
220 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { | 278 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
221 fake_safe_browsing_database_->AddBlacklistedUrl( | 279 fake_safe_browsing_database_->AddBlacklistedUrl( |
222 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); | 280 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); |
223 } | 281 } |
224 | 282 |
225 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } | 283 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } |
226 | 284 |
285 void RunUntilIdle() { | |
286 base::RunLoop().RunUntilIdle(); | |
287 auto callback = []() { base::RunLoop().RunUntilIdle(); }; | |
288 base::RunLoop run_loop; | |
289 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO, | |
290 FROM_HERE, base::Bind(callback), | |
291 run_loop.QuitClosure()); | |
292 run_loop.Run(); | |
293 base::RunLoop().RunUntilIdle(); | |
294 } | |
295 | |
227 const base::HistogramTester& tester() const { return tester_; } | 296 const base::HistogramTester& tester() const { return tester_; } |
228 | 297 |
298 base::MockTimer* mock_timer() { return mock_timer_; } | |
engedy
2017/04/20 18:56:23
Without having read this unit test too thoroughly:
| |
299 | |
229 private: | 300 private: |
230 base::FieldTrialList field_trial_list_; | 301 base::FieldTrialList field_trial_list_; |
231 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> | 302 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> |
232 scoped_feature_toggle_; | 303 scoped_feature_toggle_; |
233 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; | 304 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; |
234 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; | 305 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
235 base::HistogramTester tester_; | 306 base::HistogramTester tester_; |
236 content::NavigationHandle* navigation_handle_; | 307 |
308 // Only valid during a navigation. | |
309 base::MockTimer* mock_timer_ = nullptr; | |
237 | 310 |
238 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); | 311 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); |
239 }; | 312 }; |
240 | 313 |
314 class SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling | |
315 : public SubresourceFilterSafeBrowsingActivationThrottleTest, | |
316 public ::testing::WithParamInterface< | |
317 std::tuple<content::CancellingNavigationThrottle::CancelTime, | |
318 content::CancellingNavigationThrottle::ResultSynchrony>> { | |
319 public: | |
320 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling() {} | |
321 ~SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling() | |
322 override {} | |
323 | |
324 void DidStartNavigation(content::NavigationHandle* handle) override { | |
325 std::tie(cancel_time_, result_sync_) = GetParam(); | |
326 handle->RegisterThrottleForTesting( | |
327 base::MakeUnique<content::CancellingNavigationThrottle>( | |
328 handle, cancel_time_, result_sync_)); | |
329 SubresourceFilterSafeBrowsingActivationThrottleTest::DidStartNavigation( | |
330 handle); | |
331 } | |
332 | |
333 content::CancellingNavigationThrottle::CancelTime cancel_time() { | |
334 return cancel_time_; | |
335 } | |
336 | |
337 content::CancellingNavigationThrottle::ResultSynchrony result_sync() { | |
338 return result_sync_; | |
339 } | |
340 | |
341 private: | |
342 content::CancellingNavigationThrottle::CancelTime cancel_time_; | |
343 content::CancellingNavigationThrottle::ResultSynchrony result_sync_; | |
344 | |
345 DISALLOW_COPY_AND_ASSIGN( | |
346 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling); | |
347 }; | |
348 | |
241 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | 349 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
242 ListNotMatched_NoActivation) { | 350 ListNotMatched_NoActivation) { |
243 const GURL url(kURL); | 351 const GURL url(kURL); |
244 CreateTestNavigationForMainFrame(url); | 352 CreateTestNavigationForMainFrame(url); |
245 SimulateStartAndExpectProceed(); | 353 SimulateStartAndExpectProceed(); |
246 SimulateCommitAndExpectProceed(); | 354 SimulateCommitAndExpectProceed(); |
247 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: | 355 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
248 ACTIVATION_LIST_NOT_MATCHED, | 356 ACTIVATION_LIST_NOT_MATCHED, |
249 factory()->GetActivationDecisionForLastCommittedPageLoad()); | 357 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
250 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, | 358 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, | 407 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, |
300 1); | 408 1); |
301 } | 409 } |
302 | 410 |
303 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | 411 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
304 ListNotMatchedAndTimeout_NoActivation) { | 412 ListNotMatchedAndTimeout_NoActivation) { |
305 const GURL url(kURL); | 413 const GURL url(kURL); |
306 SimulateTimeout(); | 414 SimulateTimeout(); |
307 CreateTestNavigationForMainFrame(url); | 415 CreateTestNavigationForMainFrame(url); |
308 SimulateStartAndExpectProceed(); | 416 SimulateStartAndExpectProceed(); |
417 | |
418 // The call to simulate a commit blocks the current task waiting for the | |
419 // navigation to proceed. Get around this by posting a task which verifies the | |
420 // timer is correct and fires it. | |
421 auto fire_timer = [](base::MockTimer* timer) { | |
422 EXPECT_TRUE(timer->IsRunning()); | |
423 EXPECT_EQ(SubresourceFilterSafeBrowsingClientRequest::kCheckURLTimeout, | |
424 timer->GetCurrentDelay()); | |
425 timer->Fire(); | |
426 }; | |
427 content::BrowserThread::PostTask( | |
428 content::BrowserThread::UI, FROM_HERE, | |
429 base::Bind(fire_timer, base::Unretained(mock_timer()))); | |
309 SimulateCommitAndExpectProceed(); | 430 SimulateCommitAndExpectProceed(); |
310 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: | 431 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
311 ACTIVATION_LIST_NOT_MATCHED, | 432 ACTIVATION_LIST_NOT_MATCHED, |
312 factory()->GetActivationDecisionForLastCommittedPageLoad()); | 433 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
313 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, | 434 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
314 0); | 435 0); |
315 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); | 436 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
437 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 1); | |
438 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, | |
439 base::TimeDelta::FromMilliseconds(0), 0); | |
316 } | 440 } |
317 | 441 |
318 // TODO(melandory): Once non-defering check in WillStart is implemented add one | 442 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
319 // more test that destroys the Navigation along with corresponding throttles | 443 ListMatchedOnStart_NoDelay) { |
320 // while the SB check is pending? (To be run by ASAN bots to ensure | 444 const GURL url(kURL); |
321 // no use-after-free.) | 445 ConfigureAsSubresourceFilterOnlyURL(url); |
446 CreateTestNavigationForMainFrame(url); | |
447 SimulateStartAndExpectProceed(); | |
448 | |
449 // Get the database result back before commit. | |
450 RunUntilIdle(); | |
451 | |
452 SimulateCommitAndExpectProceed(); | |
453 EXPECT_EQ( | |
454 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, | |
455 factory()->GetActivationDecisionForLastCommittedPageLoad()); | |
456 tester().ExpectUniqueSample( | |
457 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1); | |
458 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1, | |
459 1); | |
460 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, | |
461 base::TimeDelta::FromMilliseconds(0), 1); | |
462 } | |
463 | |
464 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | |
465 ListMatchedOnRedirect_NoDelay) { | |
466 const GURL url(kURL); | |
467 const GURL redirect_url(kRedirectURL); | |
468 ConfigureAsSubresourceFilterOnlyURL(redirect_url); | |
469 CreateTestNavigationForMainFrame(url); | |
470 | |
471 SimulateStartAndExpectProceed(); | |
472 SimulateRedirectAndExpectProceed(redirect_url); | |
473 | |
474 // Get the database result back before commit. | |
475 RunUntilIdle(); | |
476 | |
477 SimulateCommitAndExpectProceed(); | |
478 EXPECT_EQ( | |
479 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, | |
480 factory()->GetActivationDecisionForLastCommittedPageLoad()); | |
481 tester().ExpectUniqueSample( | |
482 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1); | |
483 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, | |
484 1); | |
485 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, | |
486 base::TimeDelta::FromMilliseconds(0), 1); | |
487 } | |
488 | |
489 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | |
490 ListMatchedOnStartWithRedirect_NoActivation) { | |
491 const GURL url(kURL); | |
492 const GURL redirect_url(kRedirectURL); | |
493 ConfigureAsSubresourceFilterOnlyURL(url); | |
494 CreateTestNavigationForMainFrame(url); | |
495 | |
496 // These two lines also test how the database client reacts to two requests | |
497 // happening one after another. | |
498 SimulateStartAndExpectProceed(); | |
499 SimulateRedirectAndExpectProceed(redirect_url); | |
500 | |
501 // Get the database result back before commit. | |
502 RunUntilIdle(); | |
503 | |
504 SimulateCommitAndExpectProceed(); | |
505 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: | |
506 ACTIVATION_LIST_NOT_MATCHED, | |
507 factory()->GetActivationDecisionForLastCommittedPageLoad()); | |
508 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, | |
509 0); | |
510 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); | |
511 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, | |
512 base::TimeDelta::FromMilliseconds(0), 1); | |
513 } | |
514 | |
515 TEST_P(SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling, | |
516 Cancel) { | |
517 const GURL url(kURL); | |
518 SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time() | |
519 << " ResultSynchrony: " << result_sync()); | |
520 ConfigureAsSubresourceFilterOnlyURL(url); | |
521 CreateTestNavigationForMainFrame(url); | |
522 | |
523 content::NavigationThrottle::ThrottleCheckResult result = SimulateStart(); | |
524 if (cancel_time() == | |
525 content::CancellingNavigationThrottle::WILL_START_REQUEST) { | |
526 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); | |
527 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); | |
528 return; | |
529 } | |
530 EXPECT_EQ(content::NavigationThrottle::PROCEED, result); | |
531 | |
532 result = SimulateRedirect(GURL(kRedirectURL)); | |
533 if (cancel_time() == | |
534 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) { | |
535 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); | |
536 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); | |
537 return; | |
538 } | |
539 EXPECT_EQ(content::NavigationThrottle::PROCEED, result); | |
540 | |
541 base::RunLoop().RunUntilIdle(); | |
542 | |
543 result = SimulateCommit(); | |
544 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); | |
545 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); | |
546 } | |
547 | |
548 INSTANTIATE_TEST_CASE_P( | |
549 CancelMethod, | |
550 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling, | |
551 ::testing::Combine( | |
552 ::testing::Values( | |
553 content::CancellingNavigationThrottle::WILL_START_REQUEST, | |
554 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST, | |
555 content::CancellingNavigationThrottle::WILL_PROCESS_RESPONSE), | |
556 ::testing::Values( | |
557 content::CancellingNavigationThrottle::SYNCHRONOUS, | |
558 content::CancellingNavigationThrottle::ASYNCHRONOUS))); | |
322 | 559 |
323 } // namespace subresource_filter | 560 } // namespace subresource_filter |
OLD | NEW |