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

Unified Diff: components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc

Issue 2696493003: Introduce SubframeNavigationFilteringThrottle (Closed)
Patch Set: fix use after stack return, make code nicer Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
diff --git a/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc b/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a30882721bb297e32610557a5fa8f8ef842b8bb2
--- /dev/null
+++ b/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
@@ -0,0 +1,220 @@
+// 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_filtering_navigation_throttle.h"
+
+#include <memory>
+
+#include "base/callback_forward.h"
+#include "base/memory/ptr_util.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "base/test/test_simple_task_runner.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"
+#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/common/referrer.h"
+#include "content/public/test/test_renderer_host.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace subresource_filter {
+
+namespace {
+
+static void UpdateThrottleCheckResult(
+ content::NavigationThrottle::ThrottleCheckResult* to_update,
+ content::NavigationThrottle::ThrottleCheckResult result) {
+ *to_update = result;
+}
+
+} // namespace
+
+class SubframeFilteringNavigationThrottleTest
+ : public content::RenderViewHostTestHarness {
+ public:
+ SubframeFilteringNavigationThrottleTest()
+ : blocking_task_runner_(new base::TestSimpleTaskRunner) {}
+ ~SubframeFilteringNavigationThrottleTest() override {}
+
+ void SetUp() override {
+ content::RenderViewHostTestHarness::SetUp();
+ NavigateAndCommit(GURL("https://example.test"));
+ }
+
+ void TearDown() override {
+ test_handle_.reset();
+ dealer_handle_.reset();
+ ruleset_handle_.reset();
+ async_filter_.reset();
+ RunUntilIdle();
+ content::RenderViewHostTestHarness::TearDown();
+ }
+
+ void InitializeDocumentSubresourceFilter(const GURL& document_url) {
+ ASSERT_NO_FATAL_FAILURE(
+ test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
+ "filter.html", &test_ruleset_pair_));
engedy 2017/02/14 22:04:33 nit: disallowed.html
Charlie Harrison 2017/02/14 23:06:56 Done.
+ dealer_handle_ =
+ base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_);
+ dealer_handle_->SetRulesetFile(
+ testing::TestRuleset::Open(test_ruleset_pair_.indexed));
+ ruleset_handle_ =
+ base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get());
+ // Unretained is safe because the test blocks waiting until the task is
+ // run.
+ async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>(
+ ruleset_handle_.get(),
+ AsyncDocumentSubresourceFilter::InitializationParams(
+ document_url, ActivationLevel::ENABLED,
+ false /* measure_performance */),
+ base::Bind(&SubframeFilteringNavigationThrottleTest::ExpectActivation,
+ base::Unretained(this),
+ ActivationState(ActivationLevel::ENABLED)),
+ base::OnceClosure());
+ RunUntilIdle();
+ }
+
+ void ExpectActivation(ActivationState expected, ActivationState state) {
engedy 2017/02/14 22:04:33 nit for future: This was needed in the tests for t
Charlie Harrison 2017/02/14 23:06:55 Good idea. I will do this for the activation compu
+ EXPECT_EQ(expected.activation_level, state.activation_level);
+ }
+
+ void RunUntilIdle() {
+ blocking_task_runner_->RunUntilIdle();
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void CreateTestSubframeNavigation(const GURL& first_url,
+ content::RenderFrameHost* parent) {
+ navigating_subframe_ =
+ content::RenderFrameHostTester::For(parent)->AppendChild(
+ base::StringPrintf("subframe-%s", first_url.spec().c_str()));
+ test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting(
+ first_url, navigating_subframe_);
+ test_handle_->RegisterThrottleForTesting(
+ base::MakeUnique<SubframeFilteringNavigationThrottle>(
+ test_handle_.get(), async_filter_.get()));
+ }
+
+ // Calls WillStartRequest and runs the run loop until the throttle has
+ // finished checking.
+ void SimulateWillStartAndExpectAsyncResult(
+ content::NavigationThrottle::ThrottleCheckResult expected_result) {
+ auto async_result = content::NavigationThrottle::DEFER;
+ auto sync_result = test_handle_->CallWillStartRequestForTesting(
+ false /* is_post */, content::Referrer(), true /* has_user_gesture */,
+ ui::PageTransition::PAGE_TRANSITION_LINK,
+ false /* is_external_protocol */,
+ base::Bind(&UpdateThrottleCheckResult, &async_result));
+ EXPECT_EQ(content::NavigationThrottle::DEFER, sync_result);
+ RunUntilIdle();
+ EXPECT_EQ(expected_result, async_result);
+ }
+
+ // Calls WillRedirectRequest and runs the run loop until the throttle has
+ // finished checking.
+ void SimulateWillRedirectAndExpectAsyncResult(
+ const GURL& new_url,
+ content::NavigationThrottle::ThrottleCheckResult expected_result) {
+ auto async_result = content::NavigationThrottle::DEFER;
+ auto sync_result = test_handle_->CallWillRedirectRequestForTesting(
+ new_url, false /* new_method_is_post */, GURL(),
+ false /* new_is_external_protocol */,
+ base::Bind(&UpdateThrottleCheckResult, &async_result));
+ EXPECT_EQ(content::NavigationThrottle::DEFER, sync_result);
+ RunUntilIdle();
+ EXPECT_EQ(expected_result, async_result);
+ }
+
+ content::NavigationThrottle::ThrottleCheckResult
+ SimulateWillProcessResponse() {
engedy 2017/02/14 22:04:33 nit: For consistency, how about making this Simula
Charlie Harrison 2017/02/14 23:06:55 Done.
+ return test_handle_->CallWillProcessResponseForTesting(
+ navigating_subframe_, "",
+ content::NavigationHandle::ThrottleChecksFinishedCallback());
+ }
+
+ private:
+ scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_;
+
+ 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_;
+
+ std::unique_ptr<content::NavigationHandle> test_handle_;
+
+ content::RenderFrameHost* navigating_subframe_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(SubframeFilteringNavigationThrottleTest);
+};
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnStart) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"));
+ CreateTestSubframeNavigation(GURL("https://example.test/filter.html"),
+ main_rfh());
+ SimulateWillStartAndExpectAsyncResult(content::NavigationThrottle::CANCEL);
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnRedirect) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"));
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"),
engedy 2017/02/14 22:04:33 nit: allowed.html
Charlie Harrison 2017/02/14 23:06:55 Done.
+ main_rfh());
+
+ SimulateWillStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
+ SimulateWillRedirectAndExpectAsyncResult(
+ GURL("https://example.test/filter.html"),
+ content::NavigationThrottle::CANCEL);
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnSecondRedirect) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"));
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"),
+ main_rfh());
+
+ SimulateWillStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
+ SimulateWillRedirectAndExpectAsyncResult(
+ GURL("https://example.test/proceed2.html"),
+ content::NavigationThrottle::PROCEED);
+ SimulateWillRedirectAndExpectAsyncResult(
+ GURL("https://example.test/filter.html"),
+ content::NavigationThrottle::CANCEL);
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, NeverFilterNonMatchingRule) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"));
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"),
+ main_rfh());
+
+ SimulateWillStartAndExpectAsyncResult(content::NavigationThrottle::PROCEED);
+ SimulateWillRedirectAndExpectAsyncResult(
+ GURL("https://example.test/proceed2.html"),
+ content::NavigationThrottle::PROCEED);
+ EXPECT_EQ(content::NavigationThrottle::PROCEED,
+ SimulateWillProcessResponse());
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, 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/filter.html"),
+ parent_subframe);
+ SimulateWillStartAndExpectAsyncResult(content::NavigationThrottle::CANCEL);
+}
+
+} // namespace subresource_filter

Powered by Google App Engine
This is Rietveld 408576698