Chromium Code Reviews| Index: ppapi/tests/test_tcp_socket_private_shared.cc |
| diff --git a/ppapi/tests/test_tcp_socket_private_shared.cc b/ppapi/tests/test_tcp_socket_private_shared.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1a07cabe58fcc4ca213c8ea71a0d0f5802ef565 |
| --- /dev/null |
| +++ b/ppapi/tests/test_tcp_socket_private_shared.cc |
| @@ -0,0 +1,232 @@ |
| +// Copyright (c) 2011 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. |
| + |
| +#include <string.h> |
| + |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/tests/test_tcp_socket_private_shared.h" |
|
polina
2011/11/29 05:20:47
this should be the first include (see style guide)
ygorshenin
2011/11/29 13:25:18
Done.
|
| +#include "ppapi/tests/test_utils.h" |
| +#include "ppapi/tests/testing_instance.h" |
| + |
| +REGISTER_TEST_CASE(TCPSocketPrivateShared); |
| + |
| +static const char* const kSuccess = ""; |
| + |
| +bool TestTCPSocketPrivateShared::Init() { |
| + if (!InitHostAndPort()) |
| + return false; |
| + |
| + tcp_socket_private_interface_ = |
| + reinterpret_cast<PPB_TCPSocket_Private const*>( |
| + pp::Module::Get()->GetBrowserInterface( |
| + PPB_TCPSOCKET_PRIVATE_INTERFACE)); |
| + return tcp_socket_private_interface_ && InitTestingInterface(); |
|
polina
2011/11/29 05:20:47
It would be helpful to set the error string, at le
ygorshenin
2011/11/29 13:25:18
Done.
|
| +} |
| + |
| +void TestTCPSocketPrivateShared::RunTests(const std::string& filter) { |
| + RUN_TEST(Create, filter); |
| + RUN_TEST(GetAddress, filter); |
| + RUN_TEST(Connect, filter); |
| + RUN_TEST(Reconnect, filter); |
|
polina
2011/11/29 05:20:47
For tests running callbacks, you might want to tes
ygorshenin
2011/11/29 13:25:18
Done.
|
| +} |
| + |
| +void TestTCPSocketPrivateShared::QuitMessageLoop() { |
| + testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| +} |
| + |
| +bool TestTCPSocketPrivateShared::InitHostAndPort() { |
| + host_ = "www.google.com"; |
| + port_ = 443; |
| + return true; |
|
polina
2011/11/29 05:20:47
Why does this return a bool if it always succeeds?
ygorshenin
2011/11/29 13:25:18
Fields initialization moved to constructor.
|
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::CreateSocket(PP_Resource* socket) { |
| + *socket = tcp_socket_private_interface_->Create(instance_->pp_instance()); |
| + ASSERT_NE(0, *socket); |
| + ASSERT_TRUE(tcp_socket_private_interface_->IsTCPSocket(*socket)); |
|
polina
2011/11/29 05:20:47
For diagnostics purposes, it would be more helpful
ygorshenin
2011/11/29 13:25:18
Good idea. But in this particular method ReportErr
|
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::SyncConnect(PP_Resource socket, |
| + const char* host, |
| + int port) { |
| + TestCompletionCallback callback(instance_->pp_instance(), true); |
|
polina
2011/11/29 05:20:47
or use force_async_setting instead of true so you
ygorshenin
2011/11/29 13:25:18
Done.
|
| + pp::CompletionCallback cb = (pp::CompletionCallback) callback; |
|
polina
2011/11/29 05:20:47
must not use c-style casts (see style guide)
and y
ygorshenin
2011/11/29 13:25:18
Done.
|
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| + tcp_socket_private_interface_->Connect( |
| + socket, host, port, |
| + cb.pp_completion_callback())); |
| + ASSERT_EQ(PP_OK, callback.WaitForResult()); |
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::SyncConnectWithNetAddress( |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + PP_Resource socket, const PP_NetAddress_Private& addr) { |
| + TestCompletionCallback callback(instance_->pp_instance(), true); |
| + pp::CompletionCallback cb = (pp::CompletionCallback) callback; |
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| + tcp_socket_private_interface_->ConnectWithNetAddress( |
| + socket, &addr, cb.pp_completion_callback())); |
| + ASSERT_EQ(PP_OK, callback.WaitForResult()); |
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::SyncSSLHandshake(PP_Resource socket, |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + const char* host, |
| + int port) { |
| + TestCompletionCallback callback(instance_->pp_instance(), true); |
| + pp::CompletionCallback cb = (pp::CompletionCallback) callback; |
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| + tcp_socket_private_interface_->SSLHandshake( |
| + socket, host, port, cb.pp_completion_callback())); |
| + ASSERT_EQ(PP_OK, callback.WaitForResult()); |
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::SyncRead(PP_Resource socket, |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + char* buffer, |
| + int32_t num_bytes, |
| + int32_t* bytes_read) { |
| + TestCompletionCallback callback(instance_->pp_instance(), true); |
| + pp::CompletionCallback cb = (pp::CompletionCallback) callback; |
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| + tcp_socket_private_interface_->Read(socket, buffer, num_bytes, |
| + cb.pp_completion_callback())); |
| + *bytes_read = callback.WaitForResult(); |
|
polina
2011/11/29 05:20:47
no bytes_read validation?
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::SyncWrite(PP_Resource socket, |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + const char* buffer, |
| + int32_t num_bytes, |
| + int32_t* bytes_wrote) { |
| + TestCompletionCallback callback(instance_->pp_instance(), true); |
| + pp::CompletionCallback cb = (pp::CompletionCallback) callback; |
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| + tcp_socket_private_interface_->Write(socket, buffer, num_bytes, |
| + cb.pp_completion_callback())); |
| + *bytes_wrote = callback.WaitForResult(); |
|
polina
2011/11/29 05:20:47
no bytes_wrote validation?
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::CheckHTTPResponse( |
|
polina
2011/11/29 05:20:47
same comments for this function as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + PP_Resource socket, const char* request, const char* response) { |
| + int32_t rv; |
| + std::string error_message; |
| + |
| + error_message = SyncWrite(socket, request, strlen(request), &rv); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
error.empty()
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + ASSERT_TRUE(0 < rv); |
| + ASSERT_EQ(strlen(request), static_cast<size_t>(rv)); |
| + |
| + static const size_t kResponseBufferSize = 1024; |
| + char response_buffer[kResponseBufferSize]; |
|
polina
2011/11/29 05:20:47
use a small buffer, so there are multiple reads
ygorshenin
2011/11/29 13:25:18
Done.
|
| + |
| + error_message = SyncRead(socket, response_buffer, kResponseBufferSize, &rv); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + ASSERT_TRUE(0 < rv); |
| + |
| + ASSERT_EQ(0, strncmp(response, response_buffer, |
|
polina
2011/11/29 05:20:47
ppapi tests usually take advantage of std::string
ygorshenin
2011/11/29 13:25:18
Done.
|
| + std::min(static_cast<size_t>(rv), |
| + strlen(response)))); |
| + return kSuccess; |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::TestCreate() { |
| + PP_Resource socket; |
|
polina
2011/11/29 05:20:47
please combine lines 139 and 141
ygorshenin
2011/11/29 13:25:18
Done.
|
| + |
| + socket = tcp_socket_private_interface_->Create(0); |
| + ASSERT_EQ(0, socket); |
| + |
| + std::string error_message = CreateSocket(&socket); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
just return CreateSocket(...)
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + PASS(); |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::TestGetAddress() { |
| + PP_Resource socket; |
| + std::string error_message; |
| + |
| + error_message = CreateSocket(&socket); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + |
| + error_message = SyncConnect(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
ditto
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + |
| + PP_NetAddress_Private local_address, remote_address; |
| + |
| + ASSERT_EQ(PP_TRUE, tcp_socket_private_interface_->GetLocalAddress( |
| + socket, &local_address)); |
| + ASSERT_EQ(PP_TRUE, tcp_socket_private_interface_->GetRemoteAddress( |
| + socket, &remote_address)); |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + |
| + tcp_socket_private_interface_->Disconnect(socket); |
| + |
| + PASS(); |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::TestConnect() { |
| + PP_Resource socket; |
| + std::string error_message; |
| + |
| + error_message = CreateSocket(&socket); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = SyncConnect(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = SyncSSLHandshake(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = |
| + CheckHTTPResponse(socket, "GET /robots.txt\r\n", "HTTP/1.0 200 OK"); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + tcp_socket_private_interface_->Disconnect(socket); |
| + |
| + PASS(); |
| +} |
| + |
| +std::string TestTCPSocketPrivateShared::TestReconnect() { |
| + PP_Resource socket; |
| + std::string error_message; |
| + |
| + error_message = CreateSocket(&socket); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = SyncConnect(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = SyncSSLHandshake(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + |
| + PP_NetAddress_Private remote_address; |
| + ASSERT_EQ(PP_TRUE, |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + tcp_socket_private_interface_->GetRemoteAddress( |
| + socket, &remote_address)); |
| + tcp_socket_private_interface_->Disconnect(socket); |
| + |
| + error_message = CreateSocket(&socket); |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + if (error_message != kSuccess) |
| + return error_message; |
| + error_message = SyncConnectWithNetAddress(socket, remote_address); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = SyncSSLHandshake(socket, host_.c_str(), port_); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + error_message = |
| + CheckHTTPResponse(socket, "GET /robots.txt\r\n", "HTTP/1.0 200 OK"); |
| + if (error_message != kSuccess) |
|
polina
2011/11/29 05:20:47
same as above
ygorshenin
2011/11/29 13:25:18
Done.
|
| + return error_message; |
| + tcp_socket_private_interface_->Disconnect(socket); |
| + |
| + PASS(); |
| +} |