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

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: engedy/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/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.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/test_ruleset_creator.h"
18 #include "content/public/browser/navigation_handle.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/test/navigation_simulator.h"
21 #include "content/public/test/test_renderer_host.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace subresource_filter {
25
26 class SubframeNavigationFilteringThrottleTest
27 : public content::RenderViewHostTestHarness,
28 public content::WebContentsObserver {
29 public:
30 SubframeNavigationFilteringThrottleTest() {}
31 ~SubframeNavigationFilteringThrottleTest() override {}
32
33 void SetUp() override {
34 content::RenderViewHostTestHarness::SetUp();
35 NavigateAndCommit(GURL("https://example.test"));
36 Observe(RenderViewHostTestHarness::web_contents());
37 }
38
39 void TearDown() override {
40 dealer_handle_.reset();
41 ruleset_handle_.reset();
42 parent_filter_.reset();
43 RunUntilIdle();
44 content::RenderViewHostTestHarness::TearDown();
45 }
46
47 // content::WebContentsObserver:
48 void DidStartNavigation(
49 content::NavigationHandle* navigation_handle) override {
50 ASSERT_FALSE(navigation_handle->IsInMainFrame());
51 // The |parent_filter_| is the parent frame's filter. Do not register a
52 // throttle if the parent is not activated with a valid filter.
53 if (parent_filter_) {
54 navigation_handle->RegisterThrottleForTesting(
55 base::MakeUnique<SubframeNavigationFilteringThrottle>(
56 navigation_handle, parent_filter_.get()));
57 }
58 }
59
60 void InitializeDocumentSubresourceFilter(const GURL& document_url) {
61 ASSERT_NO_FATAL_FAILURE(
62 test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
63 "disallowed.html", &test_ruleset_pair_));
64
65 // Make the blocking task runner run on the current task runner for the
66 // tests, to ensure that the NavigationSimulator properly runs all necessary
67 // tasks while waiting for throttle checks to finish.
68 dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>(
69 base::MessageLoop::current()->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 parent_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() { base::RunLoop().RunUntilIdle(); }
88
89 void CreateTestSubframeAndInitNavigation(const GURL& first_url,
90 content::RenderFrameHost* parent) {
91 content::RenderFrameHost* render_frame =
92 content::RenderFrameHostTester::For(parent)->AppendChild(
93 base::StringPrintf("subframe-%s", first_url.spec().c_str()));
94 navigation_simulator_ =
95 content::NavigationSimulator::CreateRendererInitiated(first_url,
96 render_frame);
97 }
98
99 void SimulateStartAndExpectResult(
100 content::NavigationThrottle::ThrottleCheckResult expect_result) {
101 navigation_simulator_->Start();
102 EXPECT_EQ(expect_result,
103 navigation_simulator_->GetLastThrottleCheckResult());
104 }
105
106 void SimulateRedirectAndExpectResult(
107 const GURL& new_url,
108 content::NavigationThrottle::ThrottleCheckResult expect_result) {
109 navigation_simulator_->Redirect(new_url);
110 EXPECT_EQ(expect_result,
111 navigation_simulator_->GetLastThrottleCheckResult());
112 }
113
114 void SimulateCommitAndExpectResult(
115 content::NavigationThrottle::ThrottleCheckResult expect_result) {
116 navigation_simulator_->Commit();
117 EXPECT_EQ(expect_result,
118 navigation_simulator_->GetLastThrottleCheckResult());
119 }
120
121 private:
122 testing::TestRulesetCreator test_ruleset_creator_;
123 testing::TestRulesetPair test_ruleset_pair_;
124
125 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
126 std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_;
127
128 std::unique_ptr<AsyncDocumentSubresourceFilter> parent_filter_;
129
130 std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
131
132 DISALLOW_COPY_AND_ASSIGN(SubframeNavigationFilteringThrottleTest);
133 };
134
135 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnStart) {
136 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
137 CreateTestSubframeAndInitNavigation(
138 GURL("https://example.test/disallowed.html"), main_rfh());
139 SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL);
140 }
141
142 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnRedirect) {
143 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
144 CreateTestSubframeAndInitNavigation(GURL("https://example.test/allowed.html"),
145 main_rfh());
146
147 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
148 SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"),
149 content::NavigationThrottle::CANCEL);
150 }
151
152 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnSecondRedirect) {
153 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
154 CreateTestSubframeAndInitNavigation(GURL("https://example.test/allowed.html"),
155 main_rfh());
156
157 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
158 SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"),
159 content::NavigationThrottle::PROCEED);
160 SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"),
161 content::NavigationThrottle::CANCEL);
162 }
163
164 TEST_F(SubframeNavigationFilteringThrottleTest, NeverFilterNonMatchingRule) {
165 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
166 CreateTestSubframeAndInitNavigation(GURL("https://example.test/allowed.html"),
167 main_rfh());
168
169 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
170 SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"),
171 content::NavigationThrottle::PROCEED);
172 SimulateCommitAndExpectResult(content::NavigationThrottle::PROCEED);
173 }
174
175 TEST_F(SubframeNavigationFilteringThrottleTest, FilterSubsubframe) {
176 // Fake an activation of the subframe.
177 content::RenderFrameHost* parent_subframe =
178 content::RenderFrameHostTester::For(main_rfh())
179 ->AppendChild("parent-sub");
180 GURL test_url = GURL("https://example.test");
181 content::RenderFrameHostTester::For(parent_subframe)
182 ->SimulateNavigationStart(test_url);
183 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
184 content::RenderFrameHostTester::For(parent_subframe)
185 ->SimulateNavigationCommit(test_url);
186
187 CreateTestSubframeAndInitNavigation(
188 GURL("https://example.test/disallowed.html"), parent_subframe);
189 SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL);
190 }
191
192 } // namespace subresource_filter
OLDNEW
« no previous file with comments | « components/subresource_filter/content/browser/BUILD.gn ('k') | content/browser/frame_host/navigation_handle_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698