#include "base/functional/bind.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "content/shell/browser/shell.h"
#include "net/http/http_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace content {
namespace {
constexpr char kAsyncScriptThatAbortsOnEndPage[] =
"/webkit/async_script_abort_on_end.html";
constexpr char k400AbortOnEndUrl[] = "http://url.handled.by.abort.on.end/400";
bool AbortOnEndInterceptor(URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.spec() != k400AbortOnEndUrl)
return false;
std::string headers =
"HTTP/1.1 400 This is not OK\n"
"Content-type: text/plain\n";
net::HttpResponseInfo info;
info.headers = base::MakeRefCounted<net::HttpResponseHeaders>(
net::HttpUtil::AssembleRawHeaders(headers));
auto response = network::mojom::URLResponseHead::New();
response->headers = info.headers;
response->headers->GetMimeType(&response->mime_type);
std::string body = "some data\r\n";
uint32_t bytes_written = body.size();
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
CHECK_EQ(mojo::CreateDataPipe(body.size(), producer_handle, consumer_handle),
MOJO_RESULT_OK);
CHECK_EQ(MOJO_RESULT_OK,
producer_handle->WriteData(body.data(), &bytes_written,
MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
params->client->OnReceiveResponse(std::move(response),
std::move(consumer_handle), absl::nullopt);
params->client->OnComplete(
network::URLLoaderCompletionStatus(net::ERR_CONNECTION_ABORTED));
return true;
}
}
using WebKitBrowserTest = ContentBrowserTest;
IN_PROC_BROWSER_TEST_F(WebKitBrowserTest, AbortOnEnd) {
ASSERT_TRUE(embedded_test_server()->Start());
URLLoaderInterceptor interceptor(base::BindRepeating(&AbortOnEndInterceptor));
GURL url = embedded_test_server()->GetURL(kAsyncScriptThatAbortsOnEndPage);
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_FALSE(shell()->web_contents()->IsCrashed());
}
const char kXsltBadImportPage[] = "/webkit/xslt-bad-import.html";
IN_PROC_BROWSER_TEST_F(WebKitBrowserTest, XsltBadImport) {
ASSERT_TRUE(embedded_test_server()->Start());
URLLoaderInterceptor interceptor(base::BindRepeating(&AbortOnEndInterceptor));
GURL url = embedded_test_server()->GetURL(kXsltBadImportPage);
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_FALSE(shell()->web_contents()->IsCrashed());
}
IN_PROC_BROWSER_TEST_F(WebKitBrowserTest, PrerenderNoCrash) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL url =
embedded_test_server()->GetURL("/prerender/prerender-no-crash.html");
EXPECT_TRUE(NavigateToURL(shell(), url));
EXPECT_FALSE(shell()->web_contents()->IsCrashed());
}
}