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 <memory> | 7 #include <memory> |
| 8 #include <tuple> |
| 9 #include <utility> |
8 | 10 |
9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
10 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/run_loop.h" |
11 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/test/test_mock_time_task_runner.h" |
12 #include "components/safe_browsing_db/test_database_manager.h" | 16 #include "components/safe_browsing_db/test_database_manager.h" |
13 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" | 17 #include "components/subresource_filter/content/browser/content_subresource_filt
er_driver_factory.h" |
14 #include "components/subresource_filter/content/browser/fake_safe_browsing_datab
ase_manager.h" | 18 #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" | 19 #include "components/subresource_filter/content/browser/subresource_filter_clien
t.h" |
| 20 #include "components/subresource_filter/content/browser/subresource_filter_safe_
browsing_client.h" |
| 21 #include "components/subresource_filter/content/browser/subresource_filter_safe_
browsing_client_request.h" |
16 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" | 22 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
17 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" | 23 #include "components/subresource_filter/core/browser/subresource_filter_features
_test_support.h" |
18 #include "components/subresource_filter/core/common/test_ruleset_creator.h" | 24 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
19 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/navigation_handle.h" | 26 #include "content/public/browser/navigation_handle.h" |
21 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
| 28 #include "content/public/test/cancelling_navigation_throttle.h" |
22 #include "content/public/test/navigation_simulator.h" | 29 #include "content/public/test/navigation_simulator.h" |
23 #include "content/public/test/test_renderer_host.h" | 30 #include "content/public/test/test_renderer_host.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
26 | 33 |
27 namespace subresource_filter { | 34 namespace subresource_filter { |
28 | 35 |
29 namespace { | 36 namespace { |
30 | 37 |
31 char kURL[] = "http://example.test/"; | 38 char kURL[] = "http://example.test/"; |
32 char kRedirectURL[] = "http://foo.test/"; | 39 char kRedirectURL[] = "http://redirect.test/"; |
33 | 40 |
34 // Names of navigation chain patterns histogram. | 41 // Names of navigation chain patterns histogram. |
35 const char kMatchesPatternHistogramNameSubresourceFilterSuffix[] = | 42 const char kMatchesPatternHistogramNameSubresourceFilterSuffix[] = |
36 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." | 43 "SubresourceFilter.PageLoad.RedirectChainMatchPattern." |
37 "SubresourceFilterOnly"; | 44 "SubresourceFilterOnly"; |
38 const char kNavigationChainSizeSubresourceFilterSuffix[] = | 45 const char kNavigationChainSizeSubresourceFilterSuffix[] = |
39 "SubresourceFilter.PageLoad.RedirectChainLength.SubresourceFilterOnly"; | 46 "SubresourceFilter.PageLoad.RedirectChainLength.SubresourceFilterOnly"; |
| 47 const char kSafeBrowsingNavigationDelay[] = |
| 48 "SubresourceFilter.PageLoad.SafeBrowsingDelay"; |
| 49 const char kSafeBrowsingNavigationDelayNoSpeculation[] = |
| 50 "SubresourceFilter.PageLoad.SafeBrowsingDelay.NoRedirectSpeculation"; |
| 51 const char kSafeBrowsingCheckTime[] = |
| 52 "SubresourceFilter.SafeBrowsing.CheckTime"; |
40 | 53 |
41 // Human readable representation of expected redirect chain match patterns. | 54 // Human readable representation of expected redirect chain match patterns. |
42 // The explanations for the buckets given for the following redirect chain: | 55 // The explanations for the buckets given for the following redirect chain: |
43 // A->B->C->D, where A is initial URL and D is a final URL. | 56 // A->B->C->D, where A is initial URL and D is a final URL. |
44 enum RedirectChainMatchPattern { | 57 enum RedirectChainMatchPattern { |
45 EMPTY, // No histograms were recorded. | 58 EMPTY, // No histograms were recorded. |
46 F0M0L1, // D is a Safe Browsing match. | 59 F0M0L1, // D is a Safe Browsing match. |
47 F0M1L0, // B or C, or both are Safe Browsing matches. | 60 F0M1L0, // B or C, or both are Safe Browsing matches. |
48 F0M1L1, // B or C, or both and D are Safe Browsing matches. | 61 F0M1L1, // B or C, or both and D are Safe Browsing matches. |
49 F1M0L0, // A is Safe Browsing match | 62 F1M0L0, // A is Safe Browsing match |
(...skipping 19 matching lines...) Expand all Loading... |
69 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); | 82 MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); |
70 | 83 |
71 private: | 84 private: |
72 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); | 85 DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); |
73 }; | 86 }; |
74 | 87 |
75 // Throttle to call WillProcessResponse on the factory, which is otherwise | 88 // Throttle to call WillProcessResponse on the factory, which is otherwise |
76 // called by the ThrottleManager. | 89 // called by the ThrottleManager. |
77 class TestForwardingNavigationThrottle : public content::NavigationThrottle { | 90 class TestForwardingNavigationThrottle : public content::NavigationThrottle { |
78 public: | 91 public: |
79 TestForwardingNavigationThrottle(content::NavigationHandle* handle) | 92 explicit TestForwardingNavigationThrottle(content::NavigationHandle* handle) |
80 : content::NavigationThrottle(handle) {} | 93 : content::NavigationThrottle(handle) {} |
81 ~TestForwardingNavigationThrottle() override {} | 94 ~TestForwardingNavigationThrottle() override {} |
82 | 95 |
83 // content::NavigationThrottle: | 96 // content::NavigationThrottle: |
84 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() | 97 content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
85 override { | 98 override { |
86 content::WebContents* web_contents = navigation_handle()->GetWebContents(); | 99 content::WebContents* web_contents = navigation_handle()->GetWebContents(); |
87 ContentSubresourceFilterDriverFactory* factory = | 100 ContentSubresourceFilterDriverFactory* factory = |
88 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); | 101 ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); |
89 factory->WillProcessResponse(navigation_handle()); | 102 factory->WillProcessResponse(navigation_handle()); |
(...skipping 12 matching lines...) Expand all Loading... |
102 class SubresourceFilterSafeBrowsingActivationThrottleTest | 115 class SubresourceFilterSafeBrowsingActivationThrottleTest |
103 : public content::RenderViewHostTestHarness, | 116 : public content::RenderViewHostTestHarness, |
104 public content::WebContentsObserver { | 117 public content::WebContentsObserver { |
105 public: | 118 public: |
106 SubresourceFilterSafeBrowsingActivationThrottleTest() | 119 SubresourceFilterSafeBrowsingActivationThrottleTest() |
107 : field_trial_list_(nullptr) {} | 120 : field_trial_list_(nullptr) {} |
108 ~SubresourceFilterSafeBrowsingActivationThrottleTest() override {} | 121 ~SubresourceFilterSafeBrowsingActivationThrottleTest() override {} |
109 | 122 |
110 void SetUp() override { | 123 void SetUp() override { |
111 content::RenderViewHostTestHarness::SetUp(); | 124 content::RenderViewHostTestHarness::SetUp(); |
| 125 |
| 126 test_io_task_runner_ = new base::TestMockTimeTaskRunner(); |
112 scoped_feature_toggle_.reset( | 127 scoped_feature_toggle_.reset( |
113 new testing::ScopedSubresourceFilterFeatureToggle( | 128 new testing::ScopedSubresourceFilterFeatureToggle( |
114 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, | 129 base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
115 kActivationScopeActivationList, kActivationListSubresourceFilter)); | 130 kActivationScopeActivationList, kActivationListSubresourceFilter)); |
116 // Note: Using NiceMock to allow uninteresting calls and suppress warnings. | 131 // Note: Using NiceMock to allow uninteresting calls and suppress warnings. |
117 auto client = | 132 auto client = |
118 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); | 133 base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); |
119 ContentSubresourceFilterDriverFactory::CreateForWebContents( | 134 ContentSubresourceFilterDriverFactory::CreateForWebContents( |
120 RenderViewHostTestHarness::web_contents(), std::move(client)); | 135 RenderViewHostTestHarness::web_contents(), std::move(client)); |
121 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); | 136 fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
122 NavigateAndCommit(GURL("https://test.com")); | 137 NavigateAndCommit(GURL("https://test.com")); |
123 Observe(RenderViewHostTestHarness::web_contents()); | 138 Observe(RenderViewHostTestHarness::web_contents()); |
124 } | 139 } |
125 | 140 |
| 141 void TearDown() override { |
| 142 RunUntilIdle(); |
| 143 content::RenderViewHostTestHarness::TearDown(); |
| 144 } |
| 145 |
126 ContentSubresourceFilterDriverFactory* factory() { | 146 ContentSubresourceFilterDriverFactory* factory() { |
127 return ContentSubresourceFilterDriverFactory::FromWebContents( | 147 return ContentSubresourceFilterDriverFactory::FromWebContents( |
128 RenderViewHostTestHarness::web_contents()); | 148 RenderViewHostTestHarness::web_contents()); |
129 } | 149 } |
130 | 150 |
131 // content::WebContentsObserver: | 151 // content::WebContentsObserver: |
132 void DidStartNavigation( | 152 void DidStartNavigation( |
133 content::NavigationHandle* navigation_handle) override { | 153 content::NavigationHandle* navigation_handle) override { |
134 ASSERT_TRUE(navigation_handle->IsInMainFrame()); | 154 ASSERT_TRUE(navigation_handle->IsInMainFrame()); |
135 navigation_handle_ = navigation_handle; | |
136 navigation_handle->RegisterThrottleForTesting( | 155 navigation_handle->RegisterThrottleForTesting( |
137 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( | 156 base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( |
138 navigation_handle, fake_safe_browsing_database_)); | 157 navigation_handle, test_io_task_runner_, |
| 158 fake_safe_browsing_database_)); |
139 navigation_handle->RegisterThrottleForTesting( | 159 navigation_handle->RegisterThrottleForTesting( |
140 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); | 160 base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); |
141 } | 161 } |
142 | 162 |
| 163 content::NavigationThrottle::ThrottleCheckResult SimulateStart() { |
| 164 navigation_simulator_->Start(); |
| 165 auto result = navigation_simulator_->GetLastThrottleCheckResult(); |
| 166 if (result == content::NavigationThrottle::CANCEL) |
| 167 navigation_simulator_.reset(); |
| 168 return result; |
| 169 } |
| 170 |
| 171 content::NavigationThrottle::ThrottleCheckResult SimulateRedirect( |
| 172 const GURL& new_url) { |
| 173 navigation_simulator_->Redirect(new_url); |
| 174 auto result = navigation_simulator_->GetLastThrottleCheckResult(); |
| 175 if (result == content::NavigationThrottle::CANCEL) |
| 176 navigation_simulator_.reset(); |
| 177 return result; |
| 178 } |
| 179 |
| 180 content::NavigationThrottle::ThrottleCheckResult SimulateCommit() { |
| 181 // Need to post a task to flush the IO thread because calling Commit() |
| 182 // blocks until the throttle checks are complete. |
| 183 // TODO(csharrison): Consider adding finer grained control to the |
| 184 // NavigationSimulator by giving it an option to be driven by a |
| 185 // TestMockTimeTaskRunner. Also see https://crbug.com/703346. |
| 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 187 FROM_HERE, base::Bind(&base::TestMockTimeTaskRunner::RunUntilIdle, |
| 188 base::Unretained(test_io_task_runner_.get()))); |
| 189 navigation_simulator_->Commit(); |
| 190 auto result = navigation_simulator_->GetLastThrottleCheckResult(); |
| 191 if (result == content::NavigationThrottle::CANCEL) |
| 192 navigation_simulator_.reset(); |
| 193 return result; |
| 194 } |
| 195 |
143 void SimulateStartAndExpectProceed() { | 196 void SimulateStartAndExpectProceed() { |
144 navigation_simulator_->Start(); | 197 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateStart()); |
145 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
146 navigation_simulator_->GetLastThrottleCheckResult()); | |
147 } | 198 } |
148 | 199 |
149 void SimulateRedirectAndExpectProceed(const GURL& new_url) { | 200 void SimulateRedirectAndExpectProceed(const GURL& new_url) { |
150 navigation_simulator_->Redirect(new_url); | 201 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateRedirect(new_url)); |
151 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
152 navigation_simulator_->GetLastThrottleCheckResult()); | |
153 } | 202 } |
154 | 203 |
155 void SimulateCommitAndExpectProceed() { | 204 void SimulateCommitAndExpectProceed() { |
156 navigation_simulator_->Commit(); | 205 EXPECT_EQ(content::NavigationThrottle::PROCEED, SimulateCommit()); |
157 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
158 navigation_simulator_->GetLastThrottleCheckResult()); | |
159 } | 206 } |
160 | 207 |
161 void CreateTestNavigationForMainFrame(const GURL& first_url) { | 208 void CreateTestNavigationForMainFrame(const GURL& first_url) { |
162 navigation_simulator_ = | 209 navigation_simulator_ = |
163 content::NavigationSimulator::CreateRendererInitiated(first_url, | 210 content::NavigationSimulator::CreateRendererInitiated(first_url, |
164 main_rfh()); | 211 main_rfh()); |
165 } | 212 } |
166 | 213 |
167 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { | 214 void ConfigureAsSubresourceFilterOnlyURL(const GURL& url) { |
168 fake_safe_browsing_database_->AddBlacklistedUrl( | 215 fake_safe_browsing_database_->AddBlacklistedUrl( |
169 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); | 216 url, safe_browsing::SB_THREAT_TYPE_SUBRESOURCE_FILTER); |
170 } | 217 } |
171 | 218 |
172 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } | 219 void SimulateTimeout() { fake_safe_browsing_database_->SimulateTimeout(); } |
173 | 220 |
| 221 void RunUntilIdle() { |
| 222 test_io_task_runner_->RunUntilIdle(); |
| 223 base::RunLoop().RunUntilIdle(); |
| 224 } |
| 225 |
174 const base::HistogramTester& tester() const { return tester_; } | 226 const base::HistogramTester& tester() const { return tester_; } |
175 | 227 |
| 228 base::TestMockTimeTaskRunner* test_io_task_runner() const { |
| 229 return test_io_task_runner_.get(); |
| 230 } |
| 231 |
176 private: | 232 private: |
177 base::FieldTrialList field_trial_list_; | 233 base::FieldTrialList field_trial_list_; |
178 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> | 234 std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> |
179 scoped_feature_toggle_; | 235 scoped_feature_toggle_; |
| 236 scoped_refptr<base::TestMockTimeTaskRunner> test_io_task_runner_; |
180 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; | 237 std::unique_ptr<content::NavigationSimulator> navigation_simulator_; |
181 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; | 238 scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
182 base::HistogramTester tester_; | 239 base::HistogramTester tester_; |
183 content::NavigationHandle* navigation_handle_; | |
184 | 240 |
185 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); | 241 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingActivationThrottleTest); |
186 }; | 242 }; |
187 | 243 |
| 244 class SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling |
| 245 : public SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 246 public ::testing::WithParamInterface< |
| 247 std::tuple<content::CancellingNavigationThrottle::CancelTime, |
| 248 content::CancellingNavigationThrottle::ResultSynchrony>> { |
| 249 public: |
| 250 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling() { |
| 251 std::tie(cancel_time_, result_sync_) = GetParam(); |
| 252 } |
| 253 ~SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling() |
| 254 override {} |
| 255 |
| 256 void DidStartNavigation(content::NavigationHandle* handle) override { |
| 257 handle->RegisterThrottleForTesting( |
| 258 base::MakeUnique<content::CancellingNavigationThrottle>( |
| 259 handle, cancel_time_, result_sync_)); |
| 260 SubresourceFilterSafeBrowsingActivationThrottleTest::DidStartNavigation( |
| 261 handle); |
| 262 } |
| 263 |
| 264 content::CancellingNavigationThrottle::CancelTime cancel_time() { |
| 265 return cancel_time_; |
| 266 } |
| 267 |
| 268 content::CancellingNavigationThrottle::ResultSynchrony result_sync() { |
| 269 return result_sync_; |
| 270 } |
| 271 |
| 272 private: |
| 273 content::CancellingNavigationThrottle::CancelTime cancel_time_; |
| 274 content::CancellingNavigationThrottle::ResultSynchrony result_sync_; |
| 275 |
| 276 DISALLOW_COPY_AND_ASSIGN( |
| 277 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling); |
| 278 }; |
| 279 |
188 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | 280 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
189 ListNotMatched_NoActivation) { | 281 ListNotMatched_NoActivation) { |
190 const GURL url(kURL); | 282 const GURL url(kURL); |
191 CreateTestNavigationForMainFrame(url); | 283 CreateTestNavigationForMainFrame(url); |
192 SimulateStartAndExpectProceed(); | 284 SimulateStartAndExpectProceed(); |
193 SimulateCommitAndExpectProceed(); | 285 SimulateCommitAndExpectProceed(); |
194 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: | 286 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
195 ACTIVATION_LIST_NOT_MATCHED, | 287 ACTIVATION_LIST_NOT_MATCHED, |
196 factory()->GetActivationDecisionForLastCommittedPageLoad()); | 288 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
197 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, | 289 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
198 0); | 290 0); |
199 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); | 291 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 292 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 1); |
| 293 tester().ExpectTotalCount(kSafeBrowsingNavigationDelayNoSpeculation, 1); |
| 294 tester().ExpectTotalCount(kSafeBrowsingCheckTime, 1); |
200 } | 295 } |
201 | 296 |
202 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | 297 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
203 ListMatched_Activation) { | 298 ListMatched_Activation) { |
204 const GURL url(kURL); | 299 const GURL url(kURL); |
205 ConfigureAsSubresourceFilterOnlyURL(url); | 300 ConfigureAsSubresourceFilterOnlyURL(url); |
206 CreateTestNavigationForMainFrame(url); | 301 CreateTestNavigationForMainFrame(url); |
207 SimulateStartAndExpectProceed(); | 302 SimulateStartAndExpectProceed(); |
208 SimulateCommitAndExpectProceed(); | 303 SimulateCommitAndExpectProceed(); |
209 EXPECT_EQ( | 304 EXPECT_EQ( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, | 341 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, |
247 1); | 342 1); |
248 } | 343 } |
249 | 344 |
250 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, | 345 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
251 ListNotMatchedAndTimeout_NoActivation) { | 346 ListNotMatchedAndTimeout_NoActivation) { |
252 const GURL url(kURL); | 347 const GURL url(kURL); |
253 SimulateTimeout(); | 348 SimulateTimeout(); |
254 CreateTestNavigationForMainFrame(url); | 349 CreateTestNavigationForMainFrame(url); |
255 SimulateStartAndExpectProceed(); | 350 SimulateStartAndExpectProceed(); |
| 351 |
| 352 // Flush the pending tasks on the IO thread, so the delayed task surely gets |
| 353 // posted. |
| 354 test_io_task_runner()->RunUntilIdle(); |
| 355 |
| 356 // Expect one delayed task, and fast forward time. |
| 357 base::TimeDelta expected_delay = |
| 358 SubresourceFilterSafeBrowsingClientRequest::kCheckURLTimeout; |
| 359 EXPECT_EQ(expected_delay, test_io_task_runner()->NextPendingTaskDelay()); |
| 360 test_io_task_runner()->FastForwardBy(expected_delay); |
256 SimulateCommitAndExpectProceed(); | 361 SimulateCommitAndExpectProceed(); |
257 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: | 362 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
258 ACTIVATION_LIST_NOT_MATCHED, | 363 ACTIVATION_LIST_NOT_MATCHED, |
| 364 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 365 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
| 366 0); |
| 367 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 368 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 1); |
| 369 tester().ExpectTotalCount(kSafeBrowsingNavigationDelayNoSpeculation, 1); |
| 370 tester().ExpectTotalCount(kSafeBrowsingCheckTime, 1); |
| 371 } |
| 372 |
| 373 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 374 ListMatchedOnStart_NoDelay) { |
| 375 const GURL url(kURL); |
| 376 ConfigureAsSubresourceFilterOnlyURL(url); |
| 377 CreateTestNavigationForMainFrame(url); |
| 378 SimulateStartAndExpectProceed(); |
| 379 |
| 380 // Get the database result back before commit. |
| 381 RunUntilIdle(); |
| 382 |
| 383 SimulateCommitAndExpectProceed(); |
| 384 EXPECT_EQ( |
| 385 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
| 386 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 387 tester().ExpectUniqueSample( |
| 388 kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1); |
| 389 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1, |
| 390 1); |
| 391 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, |
| 392 base::TimeDelta::FromMilliseconds(0), 1); |
| 393 tester().ExpectTotalCount(kSafeBrowsingNavigationDelayNoSpeculation, 1); |
| 394 } |
| 395 |
| 396 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 397 ListMatchedOnRedirect_NoDelay) { |
| 398 const GURL url(kURL); |
| 399 const GURL redirect_url(kRedirectURL); |
| 400 ConfigureAsSubresourceFilterOnlyURL(redirect_url); |
| 401 CreateTestNavigationForMainFrame(url); |
| 402 |
| 403 SimulateStartAndExpectProceed(); |
| 404 SimulateRedirectAndExpectProceed(redirect_url); |
| 405 |
| 406 // Get the database result back before commit. |
| 407 RunUntilIdle(); |
| 408 |
| 409 SimulateCommitAndExpectProceed(); |
| 410 EXPECT_EQ( |
| 411 ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
| 412 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
| 413 tester().ExpectUniqueSample( |
| 414 kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1); |
| 415 tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, |
| 416 1); |
| 417 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, |
| 418 base::TimeDelta::FromMilliseconds(0), 1); |
| 419 tester().ExpectTotalCount(kSafeBrowsingNavigationDelayNoSpeculation, 1); |
| 420 tester().ExpectTotalCount(kSafeBrowsingCheckTime, 2); |
| 421 } |
| 422 |
| 423 TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
| 424 ListMatchedOnStartWithRedirect_NoActivation) { |
| 425 const GURL url(kURL); |
| 426 const GURL redirect_url(kRedirectURL); |
| 427 ConfigureAsSubresourceFilterOnlyURL(url); |
| 428 CreateTestNavigationForMainFrame(url); |
| 429 |
| 430 // These two lines also test how the database client reacts to two requests |
| 431 // happening one after another. |
| 432 SimulateStartAndExpectProceed(); |
| 433 SimulateRedirectAndExpectProceed(redirect_url); |
| 434 |
| 435 // Get the database result back before commit. |
| 436 RunUntilIdle(); |
| 437 |
| 438 SimulateCommitAndExpectProceed(); |
| 439 EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
| 440 ACTIVATION_LIST_NOT_MATCHED, |
259 factory()->GetActivationDecisionForLastCommittedPageLoad()); | 441 factory()->GetActivationDecisionForLastCommittedPageLoad()); |
260 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, | 442 tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
261 0); | 443 0); |
262 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); | 444 tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
| 445 tester().ExpectTimeBucketCount(kSafeBrowsingNavigationDelay, |
| 446 base::TimeDelta::FromMilliseconds(0), 1); |
| 447 tester().ExpectTotalCount(kSafeBrowsingNavigationDelayNoSpeculation, 1); |
263 } | 448 } |
264 | 449 |
265 // TODO(melandory): Once non-defering check in WillStart is implemented add one | 450 TEST_P(SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling, |
266 // more test that destroys the Navigation along with corresponding throttles | 451 Cancel) { |
267 // while the SB check is pending? (To be run by ASAN bots to ensure | 452 const GURL url(kURL); |
268 // no use-after-free.) | 453 SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time() |
| 454 << " ResultSynchrony: " << result_sync()); |
| 455 ConfigureAsSubresourceFilterOnlyURL(url); |
| 456 CreateTestNavigationForMainFrame(url); |
| 457 |
| 458 content::NavigationThrottle::ThrottleCheckResult result = SimulateStart(); |
| 459 if (cancel_time() == |
| 460 content::CancellingNavigationThrottle::WILL_START_REQUEST) { |
| 461 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); |
| 462 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); |
| 463 return; |
| 464 } |
| 465 EXPECT_EQ(content::NavigationThrottle::PROCEED, result); |
| 466 |
| 467 result = SimulateRedirect(GURL(kRedirectURL)); |
| 468 if (cancel_time() == |
| 469 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) { |
| 470 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); |
| 471 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); |
| 472 return; |
| 473 } |
| 474 EXPECT_EQ(content::NavigationThrottle::PROCEED, result); |
| 475 |
| 476 base::RunLoop().RunUntilIdle(); |
| 477 |
| 478 result = SimulateCommit(); |
| 479 EXPECT_EQ(content::NavigationThrottle::CANCEL, result); |
| 480 tester().ExpectTotalCount(kSafeBrowsingNavigationDelay, 0); |
| 481 } |
| 482 |
| 483 INSTANTIATE_TEST_CASE_P( |
| 484 CancelMethod, |
| 485 SubresourceFilterSafeBrowsingActivationThrottleTestWithCancelling, |
| 486 ::testing::Combine( |
| 487 ::testing::Values( |
| 488 content::CancellingNavigationThrottle::WILL_START_REQUEST, |
| 489 content::CancellingNavigationThrottle::WILL_REDIRECT_REQUEST, |
| 490 content::CancellingNavigationThrottle::WILL_PROCESS_RESPONSE), |
| 491 ::testing::Values( |
| 492 content::CancellingNavigationThrottle::SYNCHRONOUS, |
| 493 content::CancellingNavigationThrottle::ASYNCHRONOUS))); |
269 | 494 |
270 } // namespace subresource_filter | 495 } // namespace subresource_filter |
OLD | NEW |