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

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

Issue 2834543003: [subresource_filter] SB throttle can send multiple speculative requests. (Closed)
Patch Set: add cancelling navigation throttle 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 <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
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 21 matching lines...) Expand all
167 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, 193 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled,
168 kActivationScopeActivationList, kActivationListSubresourceFilter)); 194 kActivationScopeActivationList, kActivationListSubresourceFilter));
169 auto client = base::MakeUnique<MockSubresourceFilterClient>(); 195 auto client = base::MakeUnique<MockSubresourceFilterClient>();
170 ContentSubresourceFilterDriverFactory::CreateForWebContents( 196 ContentSubresourceFilterDriverFactory::CreateForWebContents(
171 RenderViewHostTestHarness::web_contents(), std::move(client)); 197 RenderViewHostTestHarness::web_contents(), std::move(client));
172 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); 198 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager();
173 NavigateAndCommit(GURL("https://test.com")); 199 NavigateAndCommit(GURL("https://test.com"));
174 Observe(RenderViewHostTestHarness::web_contents()); 200 Observe(RenderViewHostTestHarness::web_contents());
175 } 201 }
176 202
203 void TearDown() override {
204 RunUntilIdle();
205 content::RenderViewHostTestHarness::TearDown();
206 }
207
177 ContentSubresourceFilterDriverFactory* factory() { 208 ContentSubresourceFilterDriverFactory* factory() {
178 return ContentSubresourceFilterDriverFactory::FromWebContents( 209 return ContentSubresourceFilterDriverFactory::FromWebContents(
179 RenderViewHostTestHarness::web_contents()); 210 RenderViewHostTestHarness::web_contents());
180 } 211 }
181 212
182 // content::WebContentsObserver: 213 // content::WebContentsObserver:
183 void DidStartNavigation( 214 void DidStartNavigation(
184 content::NavigationHandle* navigation_handle) override { 215 content::NavigationHandle* navigation_handle) override {
185 ASSERT_TRUE(navigation_handle->IsInMainFrame()); 216 ASSERT_TRUE(navigation_handle->IsInMainFrame());
186 navigation_handle_ = navigation_handle; 217 auto mock_timer = base::MakeUnique<base::MockTimer>(
218 false /* retain_user_task */, false /* is_repeating */);
219 mock_timer_ = mock_timer.get();
187 navigation_handle->RegisterThrottleForTesting( 220 navigation_handle->RegisterThrottleForTesting(
188 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( 221 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>(
189 navigation_handle, fake_safe_browsing_database_)); 222 navigation_handle, std::move(mock_timer),
223 fake_safe_browsing_database_));
190 navigation_handle->RegisterThrottleForTesting( 224 navigation_handle->RegisterThrottleForTesting(
191 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); 225 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle));
192 } 226 }
193 227
228 void DidFinishNavigation(
229 content::NavigationHandle* navigation_handle) override {
230 mock_timer_ = nullptr;
231 }
232
233 content::NavigationThrottle::ThrottleCheckResult SimulateStart() {
234 navigation_simulator_->Start();
235 auto result = navigation_simulator_->GetLastThrottleCheckResult();
236 if (result == content::NavigationThrottle::CANCEL)
237 navigation_simulator_.reset();
238 return result;
239 }
240
241 content::NavigationThrottle::ThrottleCheckResult SimulateRedirect(
242 const GURL& new_url) {
243 navigation_simulator_->Redirect(new_url);
244 auto result = navigation_simulator_->GetLastThrottleCheckResult();
245 if (result == content::NavigationThrottle::CANCEL)
246 navigation_simulator_.reset();
247 return result;
248 }
249
250 content::NavigationThrottle::ThrottleCheckResult SimulateCommit() {
251 navigation_simulator_->Commit();
252 auto result = navigation_simulator_->GetLastThrottleCheckResult();
253 if (result == content::NavigationThrottle::CANCEL)
254 navigation_simulator_.reset();
255 return result;
256 }
257
194 void SimulateStartAndExpectProceed() { 258 void SimulateStartAndExpectProceed() {
195 navigation_simulator_->Start(); 259 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateStart());
196 EXPECT_EQ(content::NavigationThrottle::PROCEED,
197 navigation_simulator_->GetLastThrottleCheckResult());
198 } 260 }
199 261
200 void SimulateRedirectAndExpectProceed(const GURL& new_url) { 262 void SimulateRedirectAndExpectProceed(const GURL& new_url) {
201 navigation_simulator_->Redirect(new_url); 263 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateRedirect(new_url));
202 EXPECT_EQ(content::NavigationThrottle::PROCEED,
203 navigation_simulator_->GetLastThrottleCheckResult());
204 } 264 }
205 265
206 void SimulateCommitAndExpectProceed() { 266 void SimulateCommitAndExpectProceed() {
207 navigation_simulator_->Commit(); 267 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateCommit());
208 EXPECT_EQ(content::NavigationThrottle::PROCEED,
209 navigation_simulator_->GetLastThrottleCheckResult());
210 } 268 }
211 269
212 void CreateTestNavigationForMainFrame(const GURL& first_url) { 270 void CreateTestNavigationForMainFrame(const GURL& first_url) {
213 navigation_simulator_ = 271 navigation_simulator_ =
214 content::NavigationSimulator::CreateRendererInitiated(first_url, 272 content::NavigationSimulator::CreateRendererInitiated(first_url,
215 main_rfh()); 273 main_rfh());
216 } 274 }
217 275
218 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { 276 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) {
219 fake_safe_browsing_database_->AddBlacklistedUrl( 277 fake_safe_browsing_database_->AddBlacklistedUrl(
220 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); 278 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER);
221 } 279 }
222 280
223 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } 281 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); }
224 282
283 void RunUntilIdle() {
284 base::RunLoop().RunUntilIdle();
285 auto callback = []() { base::RunLoop().RunUntilIdle(); };
286 base::RunLoop run_loop;
287 content::BrowserThread::PostTaskAndReply(content::BrowserThread::IO,
288 FROM_HERE, base::Bind(callback),
289 run_loop.QuitClosure());
290 run_loop.Run();
291 base::RunLoop().RunUntilIdle();
292 }
293
225 const base::HistogramTester& tester() const { return tester_; } 294 const base::HistogramTester& tester() const { return tester_; }
226 295
296 base::MockTimer* mock_timer() { return mock_timer_; }
297
227 private: 298 private:
228 base::FieldTrialList field_trial_list_; 299 base::FieldTrialList field_trial_list_;
229 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> 300 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle>
230 scoped_feature_toggle_; 301 scoped_feature_toggle_;
231 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; 302 std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
232 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; 303 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_;
233 base::HistogramTester tester_; 304 base::HistogramTester tester_;
234 content::NavigationHandle* navigation_handle_; 305
306 // Only valid during a navigation.
307 base::MockTimer* mock_timer_ = nullptr;
235 308
236 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); 309 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest);
237 }; 310 };
238 311
312 class SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling
313 : public SubresourceFilterSafeBrowsingActivationThrottleTest,
314 public ::testing::WithParamInterface<
315 std::tuple<content::CancellingNavigationThrottle::CancelTime,
316 content::CancellingNavigationThrottle::ResultSynchrony>> {
317 public:
318 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling() {}
319 ~SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling()
320 override {}
321
322 void DidStartNavigation(content::NavigationHandle* handle) override {
323 std::tie(cancel_time_, result_sync_) = GetParam();
324 handle->RegisterThrottleForTesting(
325 base::MakeUnique<content::CancellingNavigationThrottle>(
326 handle, cancel_time_, result_sync_));
327 SubresourceFilterSafeBrowsingActivationThrottleTest::DidStartNavigation(
328 handle);
329 }
330
331 content::CancellingNavigationThrottle::CancelTime cancel_time() {
332 return cancel_time_;
333 }
334
335 content::CancellingNavigationThrottle::ResultSynchrony result_sync() {
336 return result_sync_;
337 }
338
339 private:
340 content::CancellingNavigationThrottle::CancelTime cancel_time_;
341 content::CancellingNavigationThrottle::ResultSynchrony result_sync_;
342
343 DISALLOW_COPY_AND_ASSIGN(
344 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling);
345 };
346
239 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 347 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
240 ListNotMatched_NoActivation) { 348 ListNotMatched_NoActivation) {
241 const GURL url(kURL); 349 const GURL url(kURL);
242 CreateTestNavigationForMainFrame(url); 350 CreateTestNavigationForMainFrame(url);
243 SimulateStartAndExpectProceed(); 351 SimulateStartAndExpectProceed();
244 SimulateCommitAndExpectProceed(); 352 SimulateCommitAndExpectProceed();
245 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: 353 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision::
246 ACTIVATION_LIST_NOT_MATCHED, 354 ACTIVATION_LIST_NOT_MATCHED,
247 factory()->GetActivationDecisionForLastCommittedPageLoad()); 355 factory()->GetActivationDecisionForLastCommittedPageLoad());
248 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, 356 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, 405 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2,
298 1); 406 1);
299 } 407 }
300 408
301 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, 409 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
302 ListNotMatchedAndTimeout_NoActivation) { 410 ListNotMatchedAndTimeout_NoActivation) {
303 const GURL url(kURL); 411 const GURL url(kURL);
304 SimulateTimeout(); 412 SimulateTimeout();
305 CreateTestNavigationForMainFrame(url); 413 CreateTestNavigationForMainFrame(url);
306 SimulateStartAndExpectProceed(); 414 SimulateStartAndExpectProceed();
415
416 // This call blocks the current task until the timeout occurs after a few
Charlie Harrison 2017/04/19 23:58:24 Comment could be clarified.
417 // seconds. Get around this by posting a task which fires the timer.
418 auto fire_timer = [](base::MockTimer* timer) {
419 EXPECT_TRUE(timer->IsRunning());
420 EXPECT_EQ(SubresourceFilterSafeBrowsingClientRequest::kCheckURLTimeout,
421 timer->GetCurrentDelay());
422 timer->Fire();
423 };
424 content::BrowserThread::PostTask(
425 content::BrowserThread::UI, FROM_HERE,
426 base::Bind(fire_timer, base::Unretained(mock_timer())));
307 SimulateCommitAndExpectProceed(); 427 SimulateCommitAndExpectProceed();
308 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: 428 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision::
309 ACTIVATION_LIST_NOT_MATCHED, 429 ACTIVATION_LIST_NOT_MATCHED,
310 factory()->GetActivationDecisionForLastCommittedPageLoad()); 430 factory()->GetActivationDecisionForLastCommittedPageLoad());
311 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, 431 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
312 0); 432 0);
313 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); 433 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0);
434 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 1);
435 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay,
436 base::TimeDelta::FromMilliseconds(0), 0);
314 } 437 }
315 438
316 // TODO(melandory): Once non-defering check in WillStart is implemented add one 439 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
317 // more test that destroys the Navigation along with corresponding throttles 440 ListMatchedOnStart_NoDelay) {
318 // while the SB check is pending? (To be run by ASAN bots to ensure 441 const GURL url(kURL);
319 // no use-after-free.) 442 ConfigureAsSubresourceFilterOnlyURL(url);
443 CreateTestNavigationForMainFrame(url);
444 SimulateStartAndExpectProceed();
445
446 // Get the database result back before commit.
447 RunUntilIdle();
448
449 SimulateCommitAndExpectProceed();
450 EXPECT_EQ(
451 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED,
452 factory()->GetActivationDecisionForLastCommittedPageLoad());
453 tester().ExpectUniqueSample(
454 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1);
455 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1,
456 1);
457 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay,
458 base::TimeDelta::FromMilliseconds(0), 1);
459 }
460
461 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
462 ListMatchedOnRedirect_NoDelay) {
463 const GURL url(kURL);
464 const GURL redirect_url(kRedirectURL);
465 ConfigureAsSubresourceFilterOnlyURL(redirect_url);
466 CreateTestNavigationForMainFrame(url);
467
468 SimulateStartAndExpectProceed();
469 SimulateRedirectAndExpectProceed(redirect_url);
470
471 // Get the database result back before commit.
472 RunUntilIdle();
473
474 SimulateCommitAndExpectProceed();
475 EXPECT_EQ(
476 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED,
477 factory()->GetActivationDecisionForLastCommittedPageLoad());
478 tester().ExpectUniqueSample(
479 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1);
480 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2,
481 1);
482 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay,
483 base::TimeDelta::FromMilliseconds(0), 1);
484 }
485
486 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest,
487 ListMatchedOnStartWithRedirect_NoActivation) {
488 const GURL url(kURL);
489 const GURL redirect_url(kRedirectURL);
490 ConfigureAsSubresourceFilterOnlyURL(url);
491 CreateTestNavigationForMainFrame(url);
492
493 // These two lines also test how the database client reacts to two requests
494 // happening one after another.
495 SimulateStartAndExpectProceed();
496 SimulateRedirectAndExpectProceed(redirect_url);
497
498 // Get the database result back before commit.
499 RunUntilIdle();
500
501 SimulateCommitAndExpectProceed();
502 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision::
503 ACTIVATION_LIST_NOT_MATCHED,
504 factory()->GetActivationDecisionForLastCommittedPageLoad());
505 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix,
506 0);
507 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0);
508 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay,
509 base::TimeDelta::FromMilliseconds(0), 1);
510 }
511
512 TEST_P(SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling,
513 Cancel) {
514 const GURL url(kURL);
515 SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time()
516 << " ResultSynchrony: " << result_sync());
517 ConfigureAsSubresourceFilterOnlyURL(url);
518 CreateTestNavigationForMainFrame(url);
519
520 content::NavigationThrottle::ThrottleCheckResult result = SimulateStart();
521 if (cancel_time() ==
522 content::CancellingNavigationThrottle::WILL_START_REQUEST) {
523 EXPECT_EQ(content::NavigationThrottle::CANCEL, result);
524 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0);
525 return;
526 }
527 EXPECT_EQ(content::NavigationThrottle::PROCEED, result);
528
529 result = SimulateRedirect(GURL(kRedirectURL));
530 if (cancel_time() ==
531 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) {
532 EXPECT_EQ(content::NavigationThrottle::CANCEL, result);
533 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0);
534 return;
535 }
536 EXPECT_EQ(content::NavigationThrottle::PROCEED, result);
537
538 base::RunLoop().RunUntilIdle();
539
540 result = SimulateCommit();
541 EXPECT_EQ(content::NavigationThrottle::CANCEL, result);
542 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0);
543 }
544
545 INSTANTIATE_TEST_CASE_P(
546 CancelMethod,
547 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling,
548 ::testing::Combine(
549 ::testing::Values(
550 content::CancellingNavigationThrottle::WILL_START_REQUEST,
551 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST,
552 content::CancellingNavigationThrottle::WILL_PROCESS_RESPONSE),
553 ::testing::Values(
554 content::CancellingNavigationThrottle::SYNCHRONOUS,
555 content::CancellingNavigationThrottle::ASYNCHRONOUS)));
320 556
321 } // namespace subresource_filter 557 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698