Commit 5d9b3418 authored by Chong Zhang's avatar Chong Zhang Committed by Commit Bot

Use same origin requests in NetworkServiceRestartBrowserTest.BasicXHR

This CL removes unnecessary cross-origin requests from the test.

Bug: 780956
Change-Id: I7835d6363d8e1b5862889c5fd9992fdb1e064dac
Reviewed-on: https://chromium-review.googlesource.com/891550Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Chong Zhang <chongz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532901}
parent b67d11ca
...@@ -83,6 +83,9 @@ class NetworkServiceRestartBrowserTest : public ContentBrowserTest { ...@@ -83,6 +83,9 @@ class NetworkServiceRestartBrowserTest : public ContentBrowserTest {
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
embedded_test_server()->RegisterRequestMonitor(
base::BindRepeating(&NetworkServiceRestartBrowserTest::MonitorRequest,
base::Unretained(this)));
EXPECT_TRUE(embedded_test_server()->Start()); EXPECT_TRUE(embedded_test_server()->Start());
ContentBrowserTest::SetUpOnMainThread(); ContentBrowserTest::SetUpOnMainThread();
} }
...@@ -97,7 +100,45 @@ class NetworkServiceRestartBrowserTest : public ContentBrowserTest { ...@@ -97,7 +100,45 @@ class NetworkServiceRestartBrowserTest : public ContentBrowserTest {
return shell()->web_contents()->GetBrowserContext(); return shell()->web_contents()->GetBrowserContext();
} }
RenderFrameHostImpl* main_frame() {
return static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetMainFrame());
}
bool CheckCanLoadHttp(const std::string& relative_url) {
GURL test_url = embedded_test_server()->GetURL(relative_url);
std::string script(
"var xhr = new XMLHttpRequest();"
"xhr.open('GET', '");
script += test_url.spec() +
"', true);"
"xhr.onload = function (e) {"
" if (xhr.readyState === 4) {"
" window.domAutomationController.send(xhr.status === 200);"
" }"
"};"
"xhr.onerror = function () {"
" window.domAutomationController.send(false);"
"};"
"xhr.send(null)";
bool xhr_result = false;
// The JS call will fail if disallowed because the process will be killed.
bool execute_result =
ExecuteScriptAndExtractBool(shell(), script, &xhr_result);
return xhr_result && execute_result;
}
// Called by |embedded_test_server()|.
void MonitorRequest(const net::test_server::HttpRequest& request) {
last_request_relative_url_ = request.relative_url;
}
std::string last_request_relative_url() const {
return last_request_relative_url_;
}
private: private:
std::string last_request_relative_url_;
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(NetworkServiceRestartBrowserTest); DISALLOW_COPY_AND_ASSIGN(NetworkServiceRestartBrowserTest);
...@@ -197,76 +238,12 @@ IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, ...@@ -197,76 +238,12 @@ IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest,
NavigateToURL(shell(), embedded_test_server()->GetURL("/title2.html"))); NavigateToURL(shell(), embedded_test_server()->GetURL("/title2.html")));
} }
class NetworkServiceRestartDisableWebSecurityTest
: public NetworkServiceRestartBrowserTest {
public:
NetworkServiceRestartDisableWebSecurityTest() {}
void SetUpCommandLine(base::CommandLine* command_line) override {
// Simulate a compromised renderer, otherwise the cross-origin request is
// blocked.
command_line->AppendSwitch(switches::kDisableWebSecurity);
NetworkServiceRestartBrowserTest::SetUpCommandLine(command_line);
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->RegisterRequestMonitor(base::BindRepeating(
&NetworkServiceRestartDisableWebSecurityTest::MonitorRequest,
base::Unretained(this)));
NetworkServiceRestartBrowserTest::SetUpOnMainThread();
}
RenderFrameHostImpl* main_frame() {
return static_cast<RenderFrameHostImpl*>(
shell()->web_contents()->GetMainFrame());
}
bool CheckCanLoadHttp(const std::string& relative_url) {
GURL test_url = embedded_test_server()->GetURL(relative_url);
std::string script(
"var xhr = new XMLHttpRequest();"
"xhr.open('GET', '");
script += test_url.spec() +
"', true);"
"xhr.onload = function (e) {"
" if (xhr.readyState === 4) {"
" window.domAutomationController.send(xhr.status === 200);"
" }"
"};"
"xhr.onerror = function () {"
" window.domAutomationController.send(false);"
"};"
"xhr.send(null)";
bool xhr_result = false;
// The JS call will fail if disallowed because the process will be killed.
bool execute_result =
ExecuteScriptAndExtractBool(shell(), script, &xhr_result);
return xhr_result && execute_result;
}
// Called by |embedded_test_server()|.
void MonitorRequest(const net::test_server::HttpRequest& request) {
last_request_relative_url_ = request.relative_url;
}
std::string last_request_relative_url() const {
return last_request_relative_url_;
}
private:
std::string last_request_relative_url_;
DISALLOW_COPY_AND_ASSIGN(NetworkServiceRestartDisableWebSecurityTest);
};
// Make sure basic XHR works after crash. // Make sure basic XHR works after crash.
IN_PROC_BROWSER_TEST_F(NetworkServiceRestartDisableWebSecurityTest, BasicXHR) { IN_PROC_BROWSER_TEST_F(NetworkServiceRestartBrowserTest, BasicXHR) {
StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
BrowserContext::GetDefaultStoragePartition(browser_context())); BrowserContext::GetDefaultStoragePartition(browser_context()));
EXPECT_TRUE(NavigateToURL( EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL("/echo")));
shell(), embedded_test_server()->GetURL("foo.com", "/echo")));
EXPECT_TRUE(CheckCanLoadHttp("/title1.html")); EXPECT_TRUE(CheckCanLoadHttp("/title1.html"));
EXPECT_EQ(last_request_relative_url(), "/title1.html"); EXPECT_EQ(last_request_relative_url(), "/title1.html");
......
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