Commit 68c45c59 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Migrate extensions TCP socket API tests to the EmbeddedTestServer.

In particular, migrate SocketsTcpApiTest.SocketTcpExtension,
SocketsTcpApiTest.SocketTcpExtensionTLS, and
SocketApiTest.SocketTCPExtension.

The second test was using SpawnedTestServer in HTTPS mode, which
could be directly replaced with EmbeddedTestServer in HTTPS mode.
The other two uses the SpawnedTestServer in TCP echo mode. Rather
than write a TCP echo server in C++, this CL switches them over
to use the EmbeddedTestServer in HTTP mode, matching the TLS socket
API test.

The reason for migrating tests off of SpawnedTestServer is that
it has been a source of flaky failures and timeouts for years,
so everything that can use the EmbeddedTestServer or other in-process
C++ servers should be doing so.

Bug: 492672
Change-Id: I655a77abc5a552f6a4edc5edfe3302abdf67fd7c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2513568Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823304}
parent 187642e6
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include "extensions/test/extension_test_message_listener.h" #include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h" #include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h" #include "net/dns/mock_host_resolver.h"
#include "net/test/spawned_test_server/spawned_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
using extensions::Extension; using extensions::Extension;
using extensions::ResultCatcher; using extensions::ResultCatcher;
...@@ -62,13 +62,11 @@ IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) { ...@@ -62,13 +62,11 @@ IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketUDPExtension) {
} }
IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) { IN_PROC_BROWSER_TEST_F(SocketApiTest, SocketTCPExtension) {
std::unique_ptr<net::SpawnedTestServer> test_server( net::EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTP);
new net::SpawnedTestServer( test_server.AddDefaultHandlers();
net::SpawnedTestServer::TYPE_TCP_ECHO, EXPECT_TRUE(test_server.Start());
base::FilePath(FILE_PATH_LITERAL("net/data"))));
EXPECT_TRUE(test_server->Start());
net::HostPortPair host_port_pair = test_server->host_port_pair(); net::HostPortPair host_port_pair = test_server.host_port_pair();
int port = host_port_pair.port(); int port = host_port_pair.port();
ASSERT_GT(port, 0); ASSERT_GT(port, 0);
......
...@@ -2,13 +2,20 @@ ...@@ -2,13 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// net/tools/testserver/testserver.py is picky about the format of what it // TCP tests use an HTTP server configured to echo back the request body
// calls its "echo" messages. One might go so far as to mutter to oneself that // as the response body.
// it isn't an echo server at all. const tcpRequest = "POST /echo HTTP/1.1\r\n" +
"Content-Length: 19\r\n\r\n" +
"0100000005320000005";
const tcpExpectedResponsePattern = /\n0100000005320000005$/;
// UDP tests use net/tools/testserver/testserver.py, which is picky about the
// format of what it calls its UDP "echo" messages. One might go so far as to
// mutter to oneself that it isn't an echo server at all.
// //
// The response is based on the request but obfuscated using a random key. // The response is based on the request but obfuscated using a random key.
const request = "0100000005320000005hello"; const udpRequest = "0100000005320000005hello";
var expectedResponsePattern = /0100000005320000005.{11}/; const udpExpectedResponsePattern = /0100000005320000005.{11}/;
const socket = chrome.socket; const socket = chrome.socket;
var address; var address;
...@@ -20,6 +27,8 @@ var protocol = "none"; ...@@ -20,6 +27,8 @@ var protocol = "none";
var socketId = 0; var socketId = 0;
var succeeded = false; var succeeded = false;
var waitCount = 0; var waitCount = 0;
var request = "<this should be set based on protocol>";
var expectedResponsePattern = "<this, too>";
// Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx // Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx
// Since amended to handle BlobBuilder deprecation. // Since amended to handle BlobBuilder deprecation.
...@@ -416,7 +425,7 @@ var onMessageReply = function(message) { ...@@ -416,7 +425,7 @@ var onMessageReply = function(message) {
var test_type = parts[0]; var test_type = parts[0];
address = parts[1]; address = parts[1];
port = parseInt(parts[2]); port = parseInt(parts[2]);
console.log("Running tests, protocol " + protocol + ", echo server " + console.log("Running tests, protocol " + test_type + ", echo server " +
address + ":" + port); address + ":" + port);
if (test_type == 'tcp_server') { if (test_type == 'tcp_server') {
chrome.test.runTests([ chrome.test.runTests([
...@@ -428,6 +437,13 @@ var onMessageReply = function(message) { ...@@ -428,6 +437,13 @@ var onMessageReply = function(message) {
chrome.test.runTests([ testMulticast ]); chrome.test.runTests([ testMulticast ]);
} else { } else {
protocol = test_type; protocol = test_type;
if (protocol == "udp") {
request = udpRequest;
expectedResponsePattern = udpExpectedResponsePattern;
} else {
request = tcpRequest;
expectedResponsePattern = tcpExpectedResponsePattern;
}
chrome.test.runTests([ chrome.test.runTests([
testSocketCreation, testSocketCreation,
testSending, testSending,
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"
#include "net/base/network_isolation_key.h" #include "net/base/network_isolation_key.h"
#include "net/dns/mock_host_resolver.h" #include "net/dns/mock_host_resolver.h"
#include "net/test/spawned_test_server/spawned_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/test/test_dns_util.h" #include "services/network/test/test_dns_util.h"
namespace extensions { namespace extensions {
...@@ -77,13 +77,11 @@ IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketsTcpCreateGood) { ...@@ -77,13 +77,11 @@ IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketsTcpCreateGood) {
} }
IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtension) { IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtension) {
std::unique_ptr<net::SpawnedTestServer> test_server( net::EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTP);
new net::SpawnedTestServer( test_server.AddDefaultHandlers();
net::SpawnedTestServer::TYPE_TCP_ECHO, EXPECT_TRUE(test_server.Start());
base::FilePath(FILE_PATH_LITERAL("net/data"))));
EXPECT_TRUE(test_server->Start());
net::HostPortPair host_port_pair = test_server->host_port_pair(); net::HostPortPair host_port_pair = test_server.host_port_pair();
int port = host_port_pair.port(); int port = host_port_pair.port();
ASSERT_TRUE(port > 0); ASSERT_TRUE(port > 0);
...@@ -137,21 +135,21 @@ IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtension) { ...@@ -137,21 +135,21 @@ IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtension) {
IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtensionTLS) { IN_PROC_BROWSER_TEST_F(SocketsTcpApiTest, SocketTcpExtensionTLS) {
// Because the network service runs in a utility process, the cert of the // Because the network service runs in a utility process, the cert of the
// SpawnedTestServer won't be recognized, so inject mock cert verifier through // EmbeddedTestServer won't be recognized, so inject mock cert verifier
// the test helper interface. // through the test helper interface.
mojo::Remote<network::mojom::NetworkServiceTest> network_service_test; mojo::Remote<network::mojom::NetworkServiceTest> network_service_test;
content::GetNetworkService()->BindTestInterface( content::GetNetworkService()->BindTestInterface(
network_service_test.BindNewPipeAndPassReceiver()); network_service_test.BindNewPipeAndPassReceiver());
mojo::ScopedAllowSyncCallForTesting allow_sync_call; mojo::ScopedAllowSyncCallForTesting allow_sync_call;
network_service_test->MockCertVerifierSetDefaultResult(net::OK); network_service_test->MockCertVerifierSetDefaultResult(net::OK);
std::unique_ptr<net::SpawnedTestServer> test_https_server( net::EmbeddedTestServer test_https_server(
new net::SpawnedTestServer( net::EmbeddedTestServer::TYPE_HTTPS);
net::SpawnedTestServer::TYPE_HTTPS, net::BaseTestServer::SSLOptions(), test_https_server.AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("net/data")))); base::FilePath(FILE_PATH_LITERAL("net/data")));
EXPECT_TRUE(test_https_server->Start()); EXPECT_TRUE(test_https_server.Start());
net::HostPortPair https_host_port_pair = test_https_server->host_port_pair(); net::HostPortPair https_host_port_pair = test_https_server.host_port_pair();
int https_port = https_host_port_pair.port(); int https_port = https_host_port_pair.port();
ASSERT_GT(https_port, 0); ASSERT_GT(https_port, 0);
......
...@@ -2,15 +2,13 @@ ...@@ -2,15 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// net/tools/testserver/testserver.py is picky about the format of what it // Use an HTTP server configured to echo back the request body as the response
// calls its "echo" messages. One might go so far as to mutter to oneself that // body.
// it isn't an echo server at all. const httpPost =
// "POST /echo HTTP/1.1\r\n" +
// The response is based on the request but obfuscated using a random key. "Content-Length: 19\r\n\r\n" +
const request = "0100000005320000005hello"; "0100000005320000005";
var expectedResponsePattern = /0100000005320000005.{11}/; var expectedResponsePattern = /\n0100000005320000005$/;
const http_get = "GET test.html HTTP/1.0\r\n\r\n";
var expectedHTTPPattern = /HTTP.1.0 200 OK/;
var tcp_address; var tcp_address;
var https_address; var https_address;
...@@ -251,7 +249,7 @@ var testSending = function() { ...@@ -251,7 +249,7 @@ var testSending = function() {
"lastError=" + chrome.runtime.lastError.message); "lastError=" + chrome.runtime.lastError.message);
} }
string2ArrayBuffer(request, function(arrayBuffer) { string2ArrayBuffer(httpPost, function(arrayBuffer) {
echoDataSent = true; echoDataSent = true;
chrome.sockets.tcp.send(tcp_socketId, arrayBuffer, onSendComplete); chrome.sockets.tcp.send(tcp_socketId, arrayBuffer, onSendComplete);
}); });
...@@ -278,8 +276,8 @@ var testSending = function() { ...@@ -278,8 +276,8 @@ var testSending = function() {
} }
function onSocketReceiveError(receiveErrorInfo) { function onSocketReceiveError(receiveErrorInfo) {
// Note: Once we have sent the "echo" message, the echo server sends back // Note: Once we have sent the echo message, the server sends back
// the "echo" response and closes the connection right away. This means // the echo response and closes the connection right away. This means
// we get a "connection closed" error very quickly after sending our // we get a "connection closed" error very quickly after sending our
// message. This is why we ignore errors from that point on. // message. This is why we ignore errors from that point on.
if (echoDataSent) if (echoDataSent)
...@@ -365,7 +363,7 @@ var testSecure = function () { ...@@ -365,7 +363,7 @@ var testSecure = function () {
function onUnpauseComplete() { function onUnpauseComplete() {
console.log("HTTPS onUnpauseComplete"); console.log("HTTPS onUnpauseComplete");
string2ArrayBuffer(http_get, function(arrayBuffer) { string2ArrayBuffer(httpPost, function(arrayBuffer) {
request_sent = true; request_sent = true;
chrome.sockets.tcp.send(https_socketId, arrayBuffer, onSendComplete); chrome.sockets.tcp.send(https_socketId, arrayBuffer, onSendComplete);
}); });
...@@ -389,7 +387,7 @@ var testSecure = function () { ...@@ -389,7 +387,7 @@ var testSecure = function () {
if (succeeded == false) { if (succeeded == false) {
dataAsString = s; // for waitForBlockingOperation(). dataAsString = s; // for waitForBlockingOperation().
console.log("HTTPS receive: got " + s); console.log("HTTPS receive: got " + s);
var match = !!s.match(expectedHTTPPattern); var match = !!s.match(expectedResponsePattern);
chrome.test.assertTrue(match, "Received data does not match."); chrome.test.assertTrue(match, "Received data does not match.");
console.log("HTTPS response received, closing socket."); console.log("HTTPS response received, closing socket.");
chrome.sockets.tcp.close(https_socketId, onCloseComplete); chrome.sockets.tcp.close(https_socketId, onCloseComplete);
...@@ -439,7 +437,7 @@ var onMessageReply = function(message) { ...@@ -439,7 +437,7 @@ var onMessageReply = function(message) {
if (test_type == 'tcp') { if (test_type == 'tcp') {
tcp_address = parts[1]; tcp_address = parts[1];
tcp_port = parseInt(parts[2]); tcp_port = parseInt(parts[2]);
console.log("Running tests for TCP, echo server " + console.log("Running tests for TCP, server " +
tcp_address + ":" + tcp_port); tcp_address + ":" + tcp_port);
tests = tests.concat([testSocketCreation, testSending]); tests = tests.concat([testSocketCreation, testSending]);
} else if (test_type == 'https') { } else if (test_type == 'https') {
......
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