Commit 003ae0ae authored by Charles Harrison's avatar Charles Harrison Committed by Commit Bot

Convert navigation interception unit tests to use nav simulator

This change:
1. Adds NavigationSimulator::SetMethod to set the initial HTTP method.

2. Converts navigation intercept tests to use the nav simulator. Note
   well that this conversion *relaxes* the test constraints. We now
   simply check that the request fails to commit, rather than fails
   to start. This is to allow future optimizations to apply policy
   in an async way without blocking the majority of network requests.
   For POSTs, we still check that the nav is cancelled at start.

3. Converts flash download intercept test to use the nav simulator.

Bug: 793053

Change-Id: Ic30bf123fa94abe978ca4f5b02802899bc9a423e
Reviewed-on: https://chromium-review.googlesource.com/813417
Commit-Queue: Charlie Harrison <csharrison@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Reviewed-by: default avatarTao Bai <michaelbai@chromium.org>
Reviewed-by: default avatarCamille Lamy <clamy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#527095}
parent 02a03615
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/plugins/flash_download_interception.h" #include "chrome/browser/plugins/flash_download_interception.h"
#include <memory>
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
...@@ -11,6 +13,9 @@ ...@@ -11,6 +13,9 @@
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/browser/host_content_settings_map.h"
#include "content/public/browser/navigation_handle.h" #include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -102,17 +107,16 @@ TEST_F(FlashDownloadInterceptionTest, NavigationThrottleCancelsNavigation) { ...@@ -102,17 +107,16 @@ TEST_F(FlashDownloadInterceptionTest, NavigationThrottleCancelsNavigation) {
// Set the source URL to an HTTP source. // Set the source URL to an HTTP source.
NavigateAndCommit(GURL("http://example.com")); NavigateAndCommit(GURL("http://example.com"));
std::unique_ptr<NavigationHandle> handle = content::TestNavigationThrottleInserter throttle_inserter(
NavigationHandle::CreateNavigationHandleForTesting( web_contents(),
GURL("https://get.adobe.com/flashplayer"), main_rfh(), false, net::OK, base::BindRepeating(&FlashDownloadInterception::MaybeCreateThrottleFor));
false, true);
std::unique_ptr<content::NavigationSimulator> simulator =
handle->CallWillStartRequestForTesting(); content::NavigationSimulator::CreateRendererInitiated(
std::unique_ptr<NavigationThrottle> throttle = GURL("https://get.adobe.com/flashplayer"), main_rfh());
FlashDownloadInterception::MaybeCreateThrottleFor(handle.get()); simulator->Commit();
EXPECT_NE(nullptr, throttle); EXPECT_EQ(content::NavigationThrottle::CANCEL_AND_IGNORE,
ASSERT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, simulator->GetLastThrottleCheckResult());
throttle->WillStartRequest());
} }
TEST_F(FlashDownloadInterceptionTest, OnlyInterceptOnDetectContentSetting) { TEST_F(FlashDownloadInterceptionTest, OnlyInterceptOnDetectContentSetting) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "components/navigation_interception/intercept_navigation_throttle.h" #include "components/navigation_interception/intercept_navigation_throttle.h"
#include <memory> #include <memory>
#include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
...@@ -13,9 +14,12 @@ ...@@ -13,9 +14,12 @@
#include "content/public/browser/navigation_handle.h" #include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h" #include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_navigation_throttle_inserter.h"
#include "content/public/test/test_renderer_host.h" #include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using content::NavigationThrottle; using content::NavigationThrottle;
using testing::_; using testing::_;
...@@ -59,31 +63,47 @@ class InterceptNavigationThrottleTest ...@@ -59,31 +63,47 @@ class InterceptNavigationThrottleTest
InterceptNavigationThrottleTest() InterceptNavigationThrottleTest()
: mock_callback_receiver_(new MockInterceptCallbackReceiver()) {} : mock_callback_receiver_(new MockInterceptCallbackReceiver()) {}
NavigationThrottle::ThrottleCheckResult std::unique_ptr<content::NavigationThrottle> CreateThrottle(
SimulateWillStart(const GURL& url, const GURL& sanitized_url, bool is_post) { content::NavigationHandle* handle) {
std::unique_ptr<content::NavigationHandle> test_handle = return std::make_unique<InterceptNavigationThrottle>(
content::NavigationHandle::CreateNavigationHandleForTesting( handle, base::BindRepeating(
url, main_rfh(), false, net::OK, false, is_post); &MockInterceptCallbackReceiver::ShouldIgnoreNavigation,
test_handle->RegisterThrottleForTesting( base::Unretained(mock_callback_receiver_.get())));
base::MakeUnique<InterceptNavigationThrottle>(
test_handle.get(),
base::Bind(&MockInterceptCallbackReceiver::ShouldIgnoreNavigation,
base::Unretained(mock_callback_receiver_.get()))));
return test_handle->CallWillStartRequestForTesting();
} }
NavigationThrottle::ThrottleCheckResult Simulate302() { std::unique_ptr<content::TestNavigationThrottleInserter>
std::unique_ptr<content::NavigationHandle> test_handle = CreateThrottleInserter() {
content::NavigationHandle::CreateNavigationHandleForTesting( return std::make_unique<content::TestNavigationThrottleInserter>(
GURL(kTestUrl), main_rfh(), false, net::OK, false, true); web_contents(),
test_handle->RegisterThrottleForTesting( base::BindRepeating(&InterceptNavigationThrottleTest::CreateThrottle,
base::MakeUnique<InterceptNavigationThrottle>( base::Unretained(this)));
test_handle.get(), }
base::Bind(&MockInterceptCallbackReceiver::ShouldIgnoreNavigation,
base::Unretained(mock_callback_receiver_.get())))); NavigationThrottle::ThrottleCheckResult SimulateNavigation(
test_handle->CallWillStartRequestForTesting(); const GURL& url,
return test_handle->CallWillRedirectRequestForTesting(GURL(kTestUrl), false, std::vector<GURL> redirect_chain,
GURL(), false); bool is_post) {
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(url, main_rfh());
auto failed = [](content::NavigationSimulator* sim) {
return sim->GetLastThrottleCheckResult().action() !=
NavigationThrottle::PROCEED;
};
if (is_post)
simulator->SetMethod("POST");
simulator->Start();
if (failed(simulator.get()))
return simulator->GetLastThrottleCheckResult();
for (const GURL& url : redirect_chain) {
simulator->Redirect(url);
if (failed(simulator.get()))
return simulator->GetLastThrottleCheckResult();
}
simulator->Commit();
return simulator->GetLastThrottleCheckResult();
} }
std::unique_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_; std::unique_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_;
...@@ -97,7 +117,7 @@ TEST_F(InterceptNavigationThrottleTest, ...@@ -97,7 +117,7 @@ TEST_F(InterceptNavigationThrottleTest,
*mock_callback_receiver_, *mock_callback_receiver_,
ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest()));
NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::ThrottleCheckResult result =
SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::PROCEED, result); EXPECT_EQ(NavigationThrottle::PROCEED, result);
} }
...@@ -110,7 +130,7 @@ TEST_F(InterceptNavigationThrottleTest, ...@@ -110,7 +130,7 @@ TEST_F(InterceptNavigationThrottleTest,
*mock_callback_receiver_, *mock_callback_receiver_,
ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest()));
NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::ThrottleCheckResult result =
SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result); EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result);
} }
...@@ -123,7 +143,7 @@ TEST_F(InterceptNavigationThrottleTest, CallbackIsPostFalseForGet) { ...@@ -123,7 +143,7 @@ TEST_F(InterceptNavigationThrottleTest, CallbackIsPostFalseForGet) {
.WillOnce(Return(false)); .WillOnce(Return(false));
NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::ThrottleCheckResult result =
SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); SimulateNavigation(GURL(kTestUrl), {}, false);
EXPECT_EQ(NavigationThrottle::PROCEED, result); EXPECT_EQ(NavigationThrottle::PROCEED, result);
} }
...@@ -135,7 +155,7 @@ TEST_F(InterceptNavigationThrottleTest, CallbackIsPostTrueForPost) { ...@@ -135,7 +155,7 @@ TEST_F(InterceptNavigationThrottleTest, CallbackIsPostTrueForPost) {
Property(&NavigationParams::is_post, Eq(true))))) Property(&NavigationParams::is_post, Eq(true)))))
.WillOnce(Return(false)); .WillOnce(Return(false));
NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::ThrottleCheckResult result =
SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), true); SimulateNavigation(GURL(kTestUrl), {}, true);
EXPECT_EQ(NavigationThrottle::PROCEED, result); EXPECT_EQ(NavigationThrottle::PROCEED, result);
} }
...@@ -152,9 +172,28 @@ TEST_F(InterceptNavigationThrottleTest, ...@@ -152,9 +172,28 @@ TEST_F(InterceptNavigationThrottleTest,
_, AllOf(NavigationParamsUrlIsTest(), _, AllOf(NavigationParamsUrlIsTest(),
Property(&NavigationParams::is_post, Eq(false))))) Property(&NavigationParams::is_post, Eq(false)))))
.WillOnce(Return(false)); .WillOnce(Return(false));
NavigationThrottle::ThrottleCheckResult result = Simulate302();
NavigationThrottle::ThrottleCheckResult result =
SimulateNavigation(GURL(kTestUrl), {GURL(kTestUrl)}, true);
EXPECT_EQ(NavigationThrottle::PROCEED, result); EXPECT_EQ(NavigationThrottle::PROCEED, result);
} }
// Ensure POST navigations are cancelled before the start.
TEST_F(InterceptNavigationThrottleTest, PostNavigationCancelledAtStart) {
EXPECT_CALL(*mock_callback_receiver_,
ShouldIgnoreNavigation(
_, AllOf(NavigationParamsUrlIsTest(),
Property(&NavigationParams::is_post, Eq(true)))))
.WillOnce(Return(true));
auto throttle_inserter = CreateThrottleInserter();
std::unique_ptr<content::NavigationSimulator> simulator =
content::NavigationSimulator::CreateRendererInitiated(GURL(kTestUrl),
main_rfh());
simulator->SetMethod("POST");
simulator->Start();
auto result = simulator->GetLastThrottleCheckResult();
EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result);
}
} // namespace navigation_interception } // namespace navigation_interception
...@@ -234,6 +234,7 @@ NavigationSimulator::NavigationSimulator(const GURL& original_url, ...@@ -234,6 +234,7 @@ NavigationSimulator::NavigationSimulator(const GURL& original_url,
handle_(nullptr), handle_(nullptr),
navigation_url_(original_url), navigation_url_(original_url),
socket_address_("2001:db8::1", 80), socket_address_("2001:db8::1", 80),
initial_method_("GET"),
browser_initiated_(browser_initiated), browser_initiated_(browser_initiated),
transition_(browser_initiated ? ui::PAGE_TRANSITION_TYPED transition_(browser_initiated ? ui::PAGE_TRANSITION_TYPED
: ui::PAGE_TRANSITION_LINK), : ui::PAGE_TRANSITION_LINK),
...@@ -622,6 +623,12 @@ void NavigationSimulator::SetReloadType(ReloadType reload_type) { ...@@ -622,6 +623,12 @@ void NavigationSimulator::SetReloadType(ReloadType reload_type) {
transition_ = ui::PAGE_TRANSITION_RELOAD; transition_ = ui::PAGE_TRANSITION_RELOAD;
} }
void NavigationSimulator::SetMethod(const std::string& method) {
CHECK_EQ(INITIALIZATION, state_) << "The method parameter cannot "
"be set after the navigation has started";
initial_method_ = method;
}
void NavigationSimulator::SetReferrer(const Referrer& referrer) { void NavigationSimulator::SetReferrer(const Referrer& referrer) {
CHECK_LE(state_, STARTED) << "The referrer cannot be set after the " CHECK_LE(state_, STARTED) << "The referrer cannot be set after the "
"navigation has committed or has failed"; "navigation has committed or has failed";
...@@ -822,6 +829,7 @@ bool NavigationSimulator::SimulateRendererInitiatedStart() { ...@@ -822,6 +829,7 @@ bool NavigationSimulator::SimulateRendererInitiatedStart() {
base::nullopt /* suggested_filename */); base::nullopt /* suggested_filename */);
CommonNavigationParams common_params; CommonNavigationParams common_params;
common_params.url = navigation_url_; common_params.url = navigation_url_;
common_params.method = initial_method_;
common_params.referrer = referrer_; common_params.referrer = referrer_;
common_params.transition = transition_; common_params.transition = transition_;
common_params.navigation_type = common_params.navigation_type =
......
...@@ -213,6 +213,9 @@ class NavigationSimulator : public WebContentsObserver { ...@@ -213,6 +213,9 @@ class NavigationSimulator : public WebContentsObserver {
// navigations. // navigations.
void SetReloadType(ReloadType reload_type); void SetReloadType(ReloadType reload_type);
// Sets the HTTP method for the navigation.
void SetMethod(const std::string& method);
// The following parameters can change during redirects. They should be // The following parameters can change during redirects. They should be
// specified before calling |Start| if they need to apply to the navigation to // specified before calling |Start| if they need to apply to the navigation to
// the original url. Otherwise, they should be specified before calling // the original url. Otherwise, they should be specified before calling
...@@ -343,6 +346,7 @@ class NavigationSimulator : public WebContentsObserver { ...@@ -343,6 +346,7 @@ class NavigationSimulator : public WebContentsObserver {
GURL navigation_url_; GURL navigation_url_;
net::HostPortPair socket_address_; net::HostPortPair socket_address_;
std::string initial_method_;
bool browser_initiated_; bool browser_initiated_;
bool same_document_ = false; bool same_document_ = false;
Referrer referrer_; Referrer referrer_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment