Commit e83e409b authored by raymes@chromium.org's avatar raymes@chromium.org

Added a pepper test for SSLHandshake. This starts an SSL server which the test can connect to.

BUG=114626
TEST=Ran pepper TCP tests.


Review URL: http://codereview.chromium.org/9791003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128266 0039d316-1c4b-4281-b951-d872f2087c98
parent e0ff4deb
......@@ -160,61 +160,33 @@ void PPAPITestBase::RunTestAndReload(const std::string& test_case) {
}
void PPAPITestBase::RunTestViaHTTP(const std::string& test_case) {
// For HTTP tests, we use the output DIR to grab the generated files such
// as the NEXEs.
FilePath exe_dir = CommandLine::ForCurrentProcess()->GetProgram().DirName();
FilePath src_dir;
ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir));
// TestServer expects a path relative to source. So we must first
// generate absolute paths to SRC and EXE and from there generate
// a relative path.
if (!exe_dir.IsAbsolute()) file_util::AbsolutePath(&exe_dir);
if (!src_dir.IsAbsolute()) file_util::AbsolutePath(&src_dir);
ASSERT_TRUE(exe_dir.IsAbsolute());
ASSERT_TRUE(src_dir.IsAbsolute());
size_t match, exe_size, src_size;
std::vector<FilePath::StringType> src_parts, exe_parts;
// Determine point at which src and exe diverge, and create a relative path.
exe_dir.GetComponents(&exe_parts);
src_dir.GetComponents(&src_parts);
exe_size = exe_parts.size();
src_size = src_parts.size();
for (match = 0; match < exe_size && match < src_size; ++match) {
if (exe_parts[match] != src_parts[match])
break;
}
FilePath web_dir;
for (size_t tmp_itr = match; tmp_itr < src_size; ++tmp_itr) {
web_dir = web_dir.Append(FILE_PATH_LITERAL(".."));
}
for (; match < exe_size; ++match) {
web_dir = web_dir.Append(exe_parts[match]);
}
FilePath document_root;
ASSERT_TRUE(GetHTTPDocumentRoot(&document_root));
RunHTTPTestServer(document_root, test_case, "");
}
net::TestServer test_server(net::TestServer::TYPE_HTTP,
net::TestServer::kLocalhost,
web_dir);
void PPAPITestBase::RunTestWithSSLServer(const std::string& test_case) {
FilePath document_root;
ASSERT_TRUE(GetHTTPDocumentRoot(&document_root));
net::TestServer test_server(net::BaseTestServer::HTTPSOptions(),
document_root);
ASSERT_TRUE(test_server.Start());
std::string query = BuildQuery("files/test_case.html?", test_case);
GURL url = test_server.GetURL(query);
RunTestURL(url);
uint16_t port = test_server.host_port_pair().port();
RunHTTPTestServer(document_root, test_case,
StringPrintf("ssl_server_port=%d", port));
}
void PPAPITestBase::RunTestWithWebSocketServer(const std::string& test_case) {
FilePath websocket_root_dir;
ASSERT_TRUE(
PathService::Get(content::DIR_LAYOUT_TESTS, &websocket_root_dir));
ui_test_utils::TestWebSocketServer server;
int port = server.UseRandomPort();
ASSERT_TRUE(server.Start(websocket_root_dir));
std::string url = StringPrintf("%s&websocket_port=%d",
test_case.c_str(), port);
RunTestViaHTTP(url);
FilePath http_document_root;
ASSERT_TRUE(GetHTTPDocumentRoot(&http_document_root));
RunHTTPTestServer(http_document_root, test_case,
StringPrintf("websocket_port=%d", port));
}
std::string PPAPITestBase::StripPrefixes(const std::string& test_name) {
......@@ -242,6 +214,61 @@ void PPAPITestBase::RunTestURL(const GURL& test_url) {
EXPECT_STREQ("PASS", observer.result().c_str());
}
void PPAPITestBase::RunHTTPTestServer(
const FilePath& document_root,
const std::string& test_case,
const std::string& extra_params) {
net::TestServer test_server(net::TestServer::TYPE_HTTP,
net::TestServer::kLocalhost,
document_root);
ASSERT_TRUE(test_server.Start());
std::string query = BuildQuery("files/test_case.html?", test_case);
if (!extra_params.empty())
query = StringPrintf("%s&%s", query.c_str(), extra_params.c_str());
GURL url = test_server.GetURL(query);
RunTestURL(url);
}
bool PPAPITestBase::GetHTTPDocumentRoot(FilePath* document_root) {
// For HTTP tests, we use the output DIR to grab the generated files such
// as the NEXEs.
FilePath exe_dir = CommandLine::ForCurrentProcess()->GetProgram().DirName();
FilePath src_dir;
if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_dir))
return false;
// TestServer expects a path relative to source. So we must first
// generate absolute paths to SRC and EXE and from there generate
// a relative path.
if (!exe_dir.IsAbsolute()) file_util::AbsolutePath(&exe_dir);
if (!src_dir.IsAbsolute()) file_util::AbsolutePath(&src_dir);
if (!exe_dir.IsAbsolute())
return false;
if (!src_dir.IsAbsolute())
return false;
size_t match, exe_size, src_size;
std::vector<FilePath::StringType> src_parts, exe_parts;
// Determine point at which src and exe diverge, and create a relative path.
exe_dir.GetComponents(&exe_parts);
src_dir.GetComponents(&src_parts);
exe_size = exe_parts.size();
src_size = src_parts.size();
for (match = 0; match < exe_size && match < src_size; ++match) {
if (exe_parts[match] != src_parts[match])
break;
}
for (size_t tmp_itr = match; tmp_itr < src_size; ++tmp_itr) {
*document_root = document_root->Append(FILE_PATH_LITERAL(".."));
}
for (; match < exe_size; ++match) {
*document_root = document_root->Append(exe_parts[match]);
}
return true;
}
PPAPITest::PPAPITest() {
}
......@@ -347,6 +374,16 @@ std::string PPAPINaClTestDisallowedSockets::BuildQuery(
RunTestViaHTTP(STRIP_PREFIXES(test_name)); \
}
// Similar macros that test with an SSL server.
#define TEST_PPAPI_IN_PROCESS_WITH_SSL_SERVER(test_name) \
IN_PROC_BROWSER_TEST_F(PPAPITest, test_name) { \
RunTestWithSSLServer(STRIP_PREFIXES(test_name)); \
}
#define TEST_PPAPI_OUT_OF_PROCESS_WITH_SSL_SERVER(test_name) \
IN_PROC_BROWSER_TEST_F(OutOfProcessPPAPITest, test_name) { \
RunTestWithSSLServer(STRIP_PREFIXES(test_name)); \
}
// Similar macros that test with WebSocket server
#define TEST_PPAPI_IN_PROCESS_WITH_WS(test_name) \
IN_PROC_BROWSER_TEST_F(PPAPITest, test_name) { \
......@@ -376,6 +413,12 @@ std::string PPAPINaClTestDisallowedSockets::BuildQuery(
RunTestViaHTTP(STRIP_PREFIXES(test_name)); \
}
// NaCl based PPAPI tests with SSL server
#define TEST_PPAPI_NACL_VIA_HTTP_WITH_SSL_SERVER(test_name) \
IN_PROC_BROWSER_TEST_F(PPAPINaClTest, test_name) { \
RunTestWithSSLServer(STRIP_PREFIXES(test_name)); \
}
// NaCl based PPAPI tests with WebSocket server
#define TEST_PPAPI_NACL_VIA_HTTP_WITH_WS(test_name) \
IN_PROC_BROWSER_TEST_F(PPAPINaClTest, test_name) { \
......@@ -460,9 +503,9 @@ TEST_PPAPI_OUT_OF_PROCESS(BrowserFont)
TEST_PPAPI_IN_PROCESS(Buffer)
TEST_PPAPI_OUT_OF_PROCESS(Buffer)
TEST_PPAPI_OUT_OF_PROCESS_VIA_HTTP(TCPSocketPrivate)
TEST_PPAPI_IN_PROCESS_VIA_HTTP(TCPSocketPrivate)
TEST_PPAPI_NACL_VIA_HTTP(TCPSocketPrivate)
TEST_PPAPI_OUT_OF_PROCESS_WITH_SSL_SERVER(TCPSocketPrivate)
TEST_PPAPI_IN_PROCESS_WITH_SSL_SERVER(TCPSocketPrivate)
TEST_PPAPI_NACL_VIA_HTTP_WITH_SSL_SERVER(TCPSocketPrivate)
TEST_PPAPI_IN_PROCESS_VIA_HTTP(UDPSocketPrivate)
TEST_PPAPI_OUT_OF_PROCESS_VIA_HTTP(UDPSocketPrivate)
......
......@@ -26,6 +26,7 @@ class PPAPITestBase : public InProcessBrowserTest {
// instance object vars.
void RunTestAndReload(const std::string& test_case);
void RunTestViaHTTP(const std::string& test_case);
void RunTestWithSSLServer(const std::string& test_case);
void RunTestWithWebSocketServer(const std::string& test_case);
std::string StripPrefixes(const std::string& test_name);
......@@ -33,6 +34,15 @@ class PPAPITestBase : public InProcessBrowserTest {
// Runs the test for a tab given the tab that's already navigated to the
// given URL.
void RunTestURL(const GURL& test_url);
// Run the given |test_case| on a HTTP test server whose document root is
// specified by |document_root|. |extra_params| will be passed as URL
// parameters to the test.
void RunHTTPTestServer(const FilePath& document_root,
const std::string& test_case,
const std::string& extra_params);
// Return the document root for the HTTP server on which tests will be run.
// The result is placed in |document_root|. False is returned upon failure.
bool GetHTTPDocumentRoot(FilePath* document_root);
};
// In-process plugin test runner. See OutOfProcessPPAPITest below for the
......
......@@ -34,11 +34,14 @@ function AppendFrame(testcase, i) {
var frame = document.createElement("IFRAME");
var mode = ExtractSearchParameter("mode");
var websocket_port = ExtractSearchParameter("websocket_port");
var ssl_server_port = ExtractSearchParameter("ssl_server_port");
var src = "?testcase=" + testcase;
if (mode == "nacl")
src += "&mode=nacl";
if (websocket_port != "")
src += "&websocket_port=" + websocket_port;
if (ssl_server_port != "")
src += "&ssl_server_port=" + ssl_server_port;
frame.setAttribute("src", src);
frame.setAttribute("onload", "LoadNext(" + (i + 1) + ")");
......@@ -222,6 +225,10 @@ onload = function() {
var websocket_port = ExtractSearchParameter("websocket_port");
if (websocket_port != "")
obj.setAttribute("websocket_port", websocket_port);
var ssl_server_port = ExtractSearchParameter("ssl_server_port");
if (ssl_server_port != "")
obj.setAttribute("ssl_server_port", ssl_server_port);
var container = document.getElementById("container");
container.addEventListener("message", handleTestingMessage, true);
// Register a bad dispatchEvent to make sure it isn't used. See 'EVIL' note
......
......@@ -40,12 +40,16 @@ bool TestTCPSocketPrivate::Init() {
if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_))
return false;
// Get the port for the SSL server.
ssl_port_ = instance_->ssl_server_port();
return true;
}
void TestTCPSocketPrivate::RunTests(const std::string& filter) {
RUN_TEST_FORCEASYNC_AND_NOT(Basic, filter);
RUN_TEST_FORCEASYNC_AND_NOT(ReadWrite, filter);
RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSSL, filter);
RUN_TEST_FORCEASYNC_AND_NOT(ConnectAddress, filter);
}
......@@ -91,6 +95,34 @@ std::string TestTCPSocketPrivate::TestReadWrite() {
PASS();
}
std::string TestTCPSocketPrivate::TestReadWriteSSL() {
pp::TCPSocketPrivate socket(instance_);
TestCompletionCallback cb(instance_->pp_instance(), force_async_);
int32_t rv = socket.Connect(host_.c_str(), ssl_port_, cb);
ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
if (rv == PP_OK_COMPLETIONPENDING)
rv = cb.WaitForResult();
ASSERT_EQ(PP_OK, rv);
rv = socket.SSLHandshake(host_.c_str(), ssl_port_, cb);
ASSERT_TRUE(!force_async_ || rv == PP_OK_COMPLETIONPENDING);
if (rv == PP_OK_COMPLETIONPENDING)
rv = cb.WaitForResult();
ASSERT_EQ(PP_OK, rv);
ASSERT_EQ(PP_OK, WriteStringToSocket(&socket, "GET / HTTP/1.0\r\n\r\n"));
// Read up to the first \n and check that it looks like valid HTTP response.
std::string s;
ASSERT_EQ(PP_OK, ReadFirstLineFromSocket(&socket, &s));
ASSERT_TRUE(ValidateHttpResponse(s));
socket.Disconnect();
PASS();
}
std::string TestTCPSocketPrivate::TestConnectAddress() {
PP_NetAddress_Private address;
......@@ -128,8 +160,6 @@ std::string TestTCPSocketPrivate::TestConnectAddress() {
PASS();
}
// TODO(viettrungluu): Try testing SSL somehow.
int32_t TestTCPSocketPrivate::ReadFirstLineFromSocket(
pp::TCPSocketPrivate* socket,
std::string* s) {
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 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.
......@@ -25,6 +25,7 @@ class TestTCPSocketPrivate : public TestCase {
private:
std::string TestBasic();
std::string TestReadWrite();
std::string TestReadWriteSSL();
std::string TestConnectAddress();
int32_t ReadFirstLineFromSocket(pp::TCPSocketPrivate* socket, std::string* s);
......@@ -33,6 +34,7 @@ class TestTCPSocketPrivate : public TestCase {
std::string host_;
uint16_t port_;
uint16_t ssl_port_;
};
#endif // PAPPI_TESTS_TEST_TCP_SOCKET_PRIVATE_H_
......@@ -31,6 +31,7 @@ TestingInstance::TestingInstance(PP_Instance instance)
current_case_(NULL),
executed_tests_(false),
nacl_mode_(false),
ssl_server_port_(-1),
websocket_port_(-1) {
callback_factory_.Initialize(this);
}
......@@ -51,6 +52,8 @@ bool TestingInstance::Init(uint32_t argc,
protocol_ = argv[i];
} else if (std::strcmp(argn[i], "websocket_port") == 0) {
websocket_port_ = atoi(argv[i]);
} else if (std::strcmp(argn[i], "ssl_server_port") == 0) {
ssl_server_port_ = atoi(argv[i]);
}
}
// Create the proper test case from the argument.
......
......@@ -82,6 +82,8 @@ pp::InstancePrivate {
return protocol_;
}
int ssl_server_port() { return ssl_server_port_; }
int websocket_port() { return websocket_port_; }
// Posts a message to the test page to eval() the script.
......@@ -149,6 +151,9 @@ pp::InstancePrivate {
// with http.
std::string protocol_;
// SSL server port.
int ssl_server_port_;
// WebSocket port.
int websocket_port_;
};
......
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