Chromium Code Reviews| Index: components/subresource_filter/content/browser/subframe_navigation_filtering_throttle_unittest.cc |
| diff --git a/components/subresource_filter/content/browser/subframe_navigation_filtering_throttle_unittest.cc b/components/subresource_filter/content/browser/subframe_navigation_filtering_throttle_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..590771994135f2941a78f6654b0c9baec6183753 |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/subframe_navigation_filtering_throttle_unittest.cc |
| @@ -0,0 +1,196 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/subresource_filter/content/browser/subframe_navigation_filtering_throttle.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "components/subresource_filter/content/browser/async_document_subresource_filter.h" |
| +#include "components/subresource_filter/core/common/activation_level.h" |
| +#include "components/subresource_filter/core/common/activation_state.h" |
| +#include "components/subresource_filter/core/common/proto/rules.pb.h" |
|
engedy
2017/03/09 11:41:34
nit: Do we use this and test_ruleset_utils.h?
Charlie Harrison
2017/03/09 14:36:01
Nope, removed.
|
| +#include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| +#include "components/subresource_filter/core/common/test_ruleset_utils.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "content/public/test/navigation_simulator.h" |
| +#include "content/public/test/test_renderer_host.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace subresource_filter { |
| + |
| +class SubframeNavigationFilteringThrottleTest |
| + : public content::RenderViewHostTestHarness, |
| + public content::WebContentsObserver { |
| + public: |
| + SubframeNavigationFilteringThrottleTest() {} |
| + ~SubframeNavigationFilteringThrottleTest() override {} |
| + |
| + void SetUp() override { |
| + content::RenderViewHostTestHarness::SetUp(); |
| + NavigateAndCommit(GURL("https://example.test")); |
| + Observe(RenderViewHostTestHarness::web_contents()); |
| + } |
| + |
| + void TearDown() override { |
| + dealer_handle_.reset(); |
| + ruleset_handle_.reset(); |
| + async_filter_.reset(); |
| + RunUntilIdle(); |
| + content::RenderViewHostTestHarness::TearDown(); |
| + } |
| + |
| + // content::WebContentsObserver: |
| + void DidStartNavigation( |
| + content::NavigationHandle* navigation_handle) override { |
| + ASSERT_FALSE(navigation_handle->IsInMainFrame()); |
| + // The |async_filter_| is the parent frame's filter. Do not register a |
| + // throttle if the parent is not activated with a valid filter. |
| + if (async_filter_) { |
|
engedy
2017/03/09 11:41:34
nit: s/async_filter_/{main_frame|parent}_{filter|d
Charlie Harrison
2017/03/09 14:36:01
Done.
|
| + navigation_handle->RegisterThrottleForTesting( |
| + base::MakeUnique<SubframeNavigationFilteringThrottle>( |
| + navigation_handle, async_filter_.get())); |
| + } |
| + } |
| + |
| + void InitializeDocumentSubresourceFilter(const GURL& document_url) { |
| + ASSERT_NO_FATAL_FAILURE( |
| + test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix( |
| + "disallowed.html", &test_ruleset_pair_)); |
| + |
| + // Make the blocking task runner run on the current task runner for the |
| + // tests, to ensure that the NavigationSimulator properly runs all necessary |
| + // tasks while waiting for throttle checks to finish. |
| + dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>( |
| + base::MessageLoop::current()->task_runner()); |
| + dealer_handle_->SetRulesetFile( |
| + testing::TestRuleset::Open(test_ruleset_pair_.indexed)); |
| + ruleset_handle_ = |
| + base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get()); |
| + |
| + async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>( |
| + ruleset_handle_.get(), |
| + AsyncDocumentSubresourceFilter::InitializationParams( |
| + document_url, ActivationLevel::ENABLED, |
| + false /* measure_performance */), |
| + base::Bind([](ActivationState state) { |
| + EXPECT_EQ(ActivationLevel::ENABLED, state.activation_level); |
| + }), |
| + base::OnceClosure()); |
| + RunUntilIdle(); |
| + } |
| + |
| + void RunUntilIdle() { base::RunLoop().RunUntilIdle(); } |
| + |
| + void CreateTestSubframeNavigation(const GURL& first_url, |
|
engedy
2017/03/09 11:41:34
nit: CreatTestSubframeAndInitNavigation or somethi
Charlie Harrison
2017/03/09 14:36:01
Done.
|
| + content::RenderFrameHost* parent) { |
| + navigating_subframe_ = |
|
engedy
2017/03/09 11:41:34
nit: Member variable only used within this method.
Charlie Harrison
2017/03/09 14:36:01
Done, good catch.
|
| + content::RenderFrameHostTester::For(parent)->AppendChild( |
| + base::StringPrintf("subframe-%s", first_url.spec().c_str())); |
| + navigation_simulator_ = |
| + content::NavigationSimulator::CreateRendererInitiated( |
| + first_url, navigating_subframe_); |
| + } |
| + |
| + void SimulateStartAndExpectResult( |
| + content::NavigationThrottle::ThrottleCheckResult expect_result) { |
| + navigation_simulator_->Start(); |
| + EXPECT_EQ(expect_result, |
| + navigation_simulator_->GetLastThrottleCheckResult()); |
| + } |
| + |
| + void SimulateRedirectAndExpectResult( |
| + const GURL& new_url, |
| + content::NavigationThrottle::ThrottleCheckResult expect_result) { |
| + navigation_simulator_->Redirect(new_url); |
| + EXPECT_EQ(expect_result, |
| + navigation_simulator_->GetLastThrottleCheckResult()); |
| + } |
| + |
| + void SimulateCommitAndExpectResult( |
| + content::NavigationThrottle::ThrottleCheckResult expect_result) { |
| + navigation_simulator_->Commit(); |
| + EXPECT_EQ(expect_result, |
| + navigation_simulator_->GetLastThrottleCheckResult()); |
| + } |
| + |
| + private: |
| + testing::TestRulesetCreator test_ruleset_creator_; |
| + testing::TestRulesetPair test_ruleset_pair_; |
| + |
| + std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_; |
| + std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_; |
| + |
| + std::unique_ptr<AsyncDocumentSubresourceFilter> async_filter_; |
| + |
| + content::RenderFrameHost* navigating_subframe_ = nullptr; |
| + |
| + std::unique_ptr<content::NavigationSimulator> navigation_simulator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SubframeNavigationFilteringThrottleTest); |
| +}; |
| + |
| +TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnStart) { |
| + InitializeDocumentSubresourceFilter(GURL("https://example.test")); |
| + CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"), |
| + main_rfh()); |
| + SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL); |
| +} |
| + |
| +TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnRedirect) { |
| + InitializeDocumentSubresourceFilter(GURL("https://example.test")); |
| + CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"), |
| + main_rfh()); |
| + |
| + SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED); |
| + SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"), |
| + content::NavigationThrottle::CANCEL); |
| +} |
| + |
| +TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnSecondRedirect) { |
| + InitializeDocumentSubresourceFilter(GURL("https://example.test")); |
| + CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"), |
| + main_rfh()); |
| + |
| + SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED); |
| + SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"), |
| + content::NavigationThrottle::PROCEED); |
| + SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"), |
| + content::NavigationThrottle::CANCEL); |
| +} |
| + |
| +TEST_F(SubframeNavigationFilteringThrottleTest, NeverFilterNonMatchingRule) { |
| + InitializeDocumentSubresourceFilter(GURL("https://example.test")); |
| + CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"), |
| + main_rfh()); |
| + |
| + SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED); |
| + SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"), |
| + content::NavigationThrottle::PROCEED); |
| + SimulateCommitAndExpectResult(content::NavigationThrottle::PROCEED); |
| +} |
| + |
| +TEST_F(SubframeNavigationFilteringThrottleTest, FilterSubsubframe) { |
| + // Fake an activation of the subframe. |
| + content::RenderFrameHost* parent_subframe = |
| + content::RenderFrameHostTester::For(main_rfh()) |
| + ->AppendChild("parent-sub"); |
| + GURL test_url = GURL("https://example.test"); |
| + content::RenderFrameHostTester::For(parent_subframe) |
| + ->SimulateNavigationStart(test_url); |
| + InitializeDocumentSubresourceFilter(GURL("https://example.test")); |
| + content::RenderFrameHostTester::For(parent_subframe) |
| + ->SimulateNavigationCommit(test_url); |
| + |
| + CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"), |
| + parent_subframe); |
| + SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL); |
| +} |
| + |
| +} // namespace subresource_filter |