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

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

Issue 2698393002: Allow asynchronous deferral in NavigationSimulator (Closed)
Patch Set: clamy review Created 3 years, 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/subresource_filter/content/browser/subframe_navigation_filt ering_throttle.h"
6
7 #include <memory>
8
9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h"
15 #include "components/subresource_filter/core/common/activation_level.h"
16 #include "components/subresource_filter/core/common/activation_state.h"
17 #include "components/subresource_filter/core/common/proto/rules.pb.h"
18 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
19 #include "components/subresource_filter/core/common/test_ruleset_utils.h"
20 #include "content/public/browser/navigation_handle.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/test/navigation_simulator.h"
23 #include "content/public/test/test_renderer_host.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace subresource_filter {
27
28 class SubframeNavigationFilteringThrottleTest
29 : public content::RenderViewHostTestHarness,
30 public content::WebContentsObserver {
31 public:
32 SubframeNavigationFilteringThrottleTest()
33 : blocking_task_runner_(new base::TestSimpleTaskRunner) {}
34 ~SubframeNavigationFilteringThrottleTest() override {}
35
36 void SetUp() override {
37 content::RenderViewHostTestHarness::SetUp();
38 NavigateAndCommit(GURL("https://example.test"));
39 Observe(RenderViewHostTestHarness::web_contents());
40 }
41
42 void TearDown() override {
43 dealer_handle_.reset();
44 ruleset_handle_.reset();
45 async_filter_.reset();
46 RunUntilIdle();
47 content::RenderViewHostTestHarness::TearDown();
48 }
49
50 // content::WebContentsObserver:
51 void DidStartNavigation(
52 content::NavigationHandle* navigation_handle) override {
53 ASSERT_FALSE(navigation_handle->IsInMainFrame());
54 // The |async_filter_| is the parent frame's filter. Do not register a
55 // throttle if the parent is not activated with a valid filter.
56 if (async_filter_) {
57 navigation_handle->RegisterThrottleForTesting(
58 base::MakeUnique<SubframeNavigationFilteringThrottle>(
59 navigation_handle, async_filter_.get()));
60 }
61 }
62
63 void InitializeDocumentSubresourceFilter(const GURL& document_url) {
64 ASSERT_NO_FATAL_FAILURE(
65 test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
66 "disallowed.html", &test_ruleset_pair_));
67
68 dealer_handle_ =
69 base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_);
70 dealer_handle_->SetRulesetFile(
71 testing::TestRuleset::Open(test_ruleset_pair_.indexed));
72 ruleset_handle_ =
73 base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get());
74
75 async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>(
76 ruleset_handle_.get(),
77 AsyncDocumentSubresourceFilter::InitializationParams(
78 document_url, ActivationLevel::ENABLED,
79 false /* measure_performance */),
80 base::Bind([](ActivationState state) {
81 EXPECT_EQ(ActivationLevel::ENABLED, state.activation_level);
82 }),
83 base::OnceClosure());
84 RunUntilIdle();
85 }
86
87 void RunUntilIdle() {
88 blocking_task_runner_->RunUntilIdle();
89 base::RunLoop().RunUntilIdle();
90 }
91
92 void CreateTestSubframeNavigation(const GURL& first_url,
93 content::RenderFrameHost* parent) {
94 navigating_subframe_ =
95 content::RenderFrameHostTester::For(parent)->AppendChild(
96 base::StringPrintf("subframe-%s", first_url.spec().c_str()));
97 navigation_simulator_ =
98 content::NavigationSimulator::CreateRendererInitiated(
99 first_url, navigating_subframe_);
100 }
101
102 void SimulateStartAndExpectAsyncResult(
103 content::NavigationThrottle::ThrottleCheckResult expect_result) {
104 EXPECT_EQ(content::NavigationThrottle::DEFER,
105 navigation_simulator_->Start());
106 blocking_task_runner_->RunUntilIdle();
107 EXPECT_EQ(expect_result,
108 navigation_simulator_->WaitForThrottleChecksComplete());
109 }
110
111 void SimulateRedirectAndExpectAsyncResult(
112 const GURL& new_url,
113 content::NavigationThrottle::ThrottleCheckResult expect_result) {
114 EXPECT_EQ(content::NavigationThrottle::DEFER,
115 navigation_simulator_->Redirect(new_url));
116 blocking_task_runner_->RunUntilIdle();
117 EXPECT_EQ(expect_result,
118 navigation_simulator_->WaitForThrottleChecksComplete());
119 }
120
121 void SimulateCommitAndExpectSyncResult(
122 content::NavigationThrottle::ThrottleCheckResult expect_result) {
123 EXPECT_EQ(expect_result, navigation_simulator_->Commit());
124 }
125
126 private:
127 scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_;
128
129 testing::TestRulesetCreator test_ruleset_creator_;
130 testing::TestRulesetPair test_ruleset_pair_;
131
132 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
133 std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_;
134
135 std::unique_ptr<AsyncDocumentSubresourceFilter> async_filter_;
136
137 content::RenderFrameHost* navigating_subframe_ = nullptr;
138
139 std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
140
141 DISALLOW_COPY_AND_ASSIGN(SubframeNavigationFilteringThrottleTest);
142 };
143
144 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnStart) {
145 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
146 CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"),
147 main_rfh());
148 SimulateStartAndExpectAsyncResult(content::NavigationThrottle::CANCEL);
149 }
150
151 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnRedirect) {
152 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
153 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
154 main_rfh());
155
156 SimulateStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
157 SimulateRedirectAndExpectAsyncResult(
158 GURL("https://example.test/disallowed.html"),
159 content::NavigationThrottle::CANCEL);
160 }
161
162 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnSecondRedirect) {
163 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
164 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
165 main_rfh());
166
167 SimulateStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
168 SimulateRedirectAndExpectAsyncResult(
169 GURL("https://example.test/allowed2.html"),
170 content::NavigationThrottle::PROCEED);
171 SimulateRedirectAndExpectAsyncResult(
172 GURL("https://example.test/disallowed.html"),
173 content::NavigationThrottle::CANCEL);
174 }
175
176 TEST_F(SubframeNavigationFilteringThrottleTest, NeverFilterNonMatchingRule) {
177 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
178 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
179 main_rfh());
180
181 SimulateStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
182 SimulateRedirectAndExpectAsyncResult(
183 GURL("https://example.test/allowed2.html"),
184 content::NavigationThrottle::PROCEED);
185 SimulateCommitAndExpectSyncResult(content::NavigationThrottle::PROCEED);
186 }
187
188 TEST_F(SubframeNavigationFilteringThrottleTest, FilterSubsubframe) {
189 // Fake an activation of the subframe.
190 content::RenderFrameHost* parent_subframe =
191 content::RenderFrameHostTester::For(main_rfh())
192 ->AppendChild("parent-sub");
193 GURL test_url = GURL("https://example.test");
194 content::RenderFrameHostTester::For(parent_subframe)
195 ->SimulateNavigationStart(test_url);
196 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
197 content::RenderFrameHostTester::For(parent_subframe)
198 ->SimulateNavigationCommit(test_url);
199
200 CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"),
201 parent_subframe);
202 SimulateStartAndExpectAsyncResult(content::NavigationThrottle::CANCEL);
203 }
204
205 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698