diff options
author | ygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-13 09:45:14 +0000 |
---|---|---|
committer | ygorshenin@chromium.org <ygorshenin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-13 09:45:14 +0000 |
commit | 4f9e7222c8086d1484d6002f5977c8774e86448f (patch) | |
tree | d7dd966c2c3754ef2c3d8a88793d6319c4a6bf8d | |
parent | 18bcaed2ab84b5259f2e392a843a5ea5b5661043 (diff) | |
download | chromium_src-4f9e7222c8086d1484d6002f5977c8774e86448f.zip chromium_src-4f9e7222c8086d1484d6002f5977c8774e86448f.tar.gz chromium_src-4f9e7222c8086d1484d6002f5977c8774e86448f.tar.bz2 |
Added CPP wrapper for PPB_TCPServerSocket_Private.
BUG=
TEST=PPAPI(NaCl?)Test.TestTCPServerSocketPrivate
Review URL: http://codereview.chromium.org/9568007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126367 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ppapi/cpp/private/tcp_server_socket_private.cc | 59 | ||||
-rw-r--r-- | ppapi/cpp/private/tcp_server_socket_private.h | 38 | ||||
-rw-r--r-- | ppapi/cpp/private/tcp_socket_private.cc | 6 | ||||
-rw-r--r-- | ppapi/cpp/private/tcp_socket_private.h | 5 | ||||
-rw-r--r-- | ppapi/ppapi_sources.gypi | 2 | ||||
-rw-r--r-- | ppapi/tests/test_tcp_server_socket_private.cc | 296 | ||||
-rw-r--r-- | ppapi/tests/test_tcp_server_socket_private.h | 48 |
7 files changed, 230 insertions, 224 deletions
diff --git a/ppapi/cpp/private/tcp_server_socket_private.cc b/ppapi/cpp/private/tcp_server_socket_private.cc new file mode 100644 index 0000000..5f315bf --- /dev/null +++ b/ppapi/cpp/private/tcp_server_socket_private.cc @@ -0,0 +1,59 @@ +// 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. + +#include "ppapi/cpp/private/tcp_server_socket_private.h" + +#include "ppapi/c/pp_errors.h" +#include "ppapi/cpp/completion_callback.h" +#include "ppapi/cpp/instance_handle.h" +#include "ppapi/cpp/module.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +namespace { + +template <> const char* interface_name<PPB_TCPServerSocket_Private>() { + return PPB_TCPSERVERSOCKET_PRIVATE_INTERFACE; +} + +} // namespace + +TCPServerSocketPrivate::TCPServerSocketPrivate(const InstanceHandle& instance) { + if (has_interface<PPB_TCPServerSocket_Private>()) { + PassRefFromConstructor(get_interface<PPB_TCPServerSocket_Private>()->Create( + instance.pp_instance())); + } +} + +// static +bool TCPServerSocketPrivate::IsAvailable() { + return has_interface<PPB_TCPServerSocket_Private>(); +} + +int32_t TCPServerSocketPrivate::Listen(const PP_NetAddress_Private* addr, + int32_t backlog, + const CompletionCallback& callback) { + if (!has_interface<PPB_TCPServerSocket_Private>()) + return callback.MayForce(PP_ERROR_NOINTERFACE); + return get_interface<PPB_TCPServerSocket_Private>()->Listen( + pp_resource(), addr, backlog, callback.pp_completion_callback()); +} + +int32_t TCPServerSocketPrivate::Accept(PP_Resource* tcp_socket, + const CompletionCallback& callback) { + if (!has_interface<PPB_TCPServerSocket_Private>()) + return callback.MayForce(PP_ERROR_NOINTERFACE); + return get_interface<PPB_TCPServerSocket_Private>()->Accept( + pp_resource(), tcp_socket, callback.pp_completion_callback()); +} + +void TCPServerSocketPrivate::StopListening() { + if (!has_interface<PPB_TCPServerSocket_Private>()) + return; + return get_interface<PPB_TCPServerSocket_Private>()->StopListening( + pp_resource()); +} + +} // namespace pp diff --git a/ppapi/cpp/private/tcp_server_socket_private.h b/ppapi/cpp/private/tcp_server_socket_private.h new file mode 100644 index 0000000..3d40cea --- /dev/null +++ b/ppapi/cpp/private/tcp_server_socket_private.h @@ -0,0 +1,38 @@ +// 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. + +#ifndef PPAPI_CPP_PRIVATE_TCP_SERVER_SOCKET_PRIVATE_H_ +#define PPAPI_CPP_PRIVATE_TCP_SERVER_SOCKET_PRIVATE_H_ + +#include "ppapi/c/pp_stdint.h" +#include "ppapi/c/private/ppb_tcp_server_socket_private.h" +#include "ppapi/cpp/resource.h" + +namespace pp { + +class CompletionCallback; +class InstanceHandle; + +class TCPServerSocketPrivate : public Resource { + public: + explicit TCPServerSocketPrivate(const InstanceHandle& instance); + + // Returns true if the required interface is available. + static bool IsAvailable(); + + int32_t Listen(const PP_NetAddress_Private* addr, + int32_t backlog, + const CompletionCallback& callback); + // Accepts incoming connection and stores resource of accepted + // socket into |socket|. If Accept returns PP_OK_COMPLETIONPENDING, + // the memory pointed by |socket| should stay valid until the + // |callback| is called or StopListening method is called. + int32_t Accept(PP_Resource* socket, + const CompletionCallback& callback); + void StopListening(); +}; + +} // namespace pp + +#endif // PPAPI_CPP_PRIVATE_TCP_SERVER_SOCKET_PRIVATE_H_ diff --git a/ppapi/cpp/private/tcp_socket_private.cc b/ppapi/cpp/private/tcp_socket_private.cc index 9e2c1f6..e15f537 100644 --- a/ppapi/cpp/private/tcp_socket_private.cc +++ b/ppapi/cpp/private/tcp_socket_private.cc @@ -1,4 +1,4 @@ -// 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. @@ -28,6 +28,10 @@ TCPSocketPrivate::TCPSocketPrivate(const InstanceHandle& instance) { } } +TCPSocketPrivate::TCPSocketPrivate(PassRef, PP_Resource resource) + : Resource(PASS_REF, resource) { +} + // static bool TCPSocketPrivate::IsAvailable() { return has_interface<PPB_TCPSocket_Private>(); diff --git a/ppapi/cpp/private/tcp_socket_private.h b/ppapi/cpp/private/tcp_socket_private.h index 415a1ed..839c7cf 100644 --- a/ppapi/cpp/private/tcp_socket_private.h +++ b/ppapi/cpp/private/tcp_socket_private.h @@ -1,4 +1,4 @@ -// 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. @@ -7,6 +7,7 @@ #include "ppapi/c/pp_stdint.h" #include "ppapi/c/private/ppb_tcp_socket_private.h" +#include "ppapi/cpp/pass_ref.h" #include "ppapi/cpp/resource.h" namespace pp { @@ -18,6 +19,8 @@ class TCPSocketPrivate : public Resource { public: explicit TCPSocketPrivate(const InstanceHandle& instance); + TCPSocketPrivate(PassRef, PP_Resource resource); + // Returns true if the required interface is available. static bool IsAvailable(); diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index a000fbe..f32e2b6 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -260,6 +260,8 @@ 'cpp/private/network_list_private.h', 'cpp/private/network_monitor_private.cc', 'cpp/private/network_monitor_private.h', + 'cpp/private/tcp_server_socket_private.cc', + 'cpp/private/tcp_server_socket_private.h', 'cpp/private/tcp_socket_private.cc', 'cpp/private/tcp_socket_private.h', 'cpp/private/udp_socket_private.cc', diff --git a/ppapi/tests/test_tcp_server_socket_private.cc b/ppapi/tests/test_tcp_server_socket_private.cc index e9b28e3..ed1ab63 100644 --- a/ppapi/tests/test_tcp_server_socket_private.cc +++ b/ppapi/tests/test_tcp_server_socket_private.cc @@ -4,17 +4,17 @@ #include "ppapi/tests/test_tcp_server_socket_private.h" -#include <cstddef> -#include <cstring> #include <vector> -#include "ppapi/cpp/module.h" +#include "ppapi/cpp/pass_ref.h" #include "ppapi/cpp/private/net_address_private.h" +#include "ppapi/cpp/private/tcp_server_socket_private.h" #include "ppapi/cpp/private/tcp_socket_private.h" #include "ppapi/tests/test_utils.h" #include "ppapi/tests/testing_instance.h" using pp::NetAddressPrivate; +using pp::TCPServerSocketPrivate; using pp::TCPSocketPrivate; namespace { @@ -27,35 +27,17 @@ const uint16_t kPortScanTo = 4096; REGISTER_TEST_CASE(TCPServerSocketPrivate); TestTCPServerSocketPrivate::TestTCPServerSocketPrivate( - TestingInstance* instance) - : TestCase(instance), - core_interface_(NULL), - tcp_server_socket_private_interface_(NULL), - tcp_socket_private_interface_(NULL), - port_(0) { + TestingInstance* instance) : TestCase(instance) { } bool TestTCPServerSocketPrivate::Init() { - core_interface_ = static_cast<const PPB_Core*>( - pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); - if (!core_interface_) - instance_->AppendError("PPB_Core interface not available"); - tcp_server_socket_private_interface_ = - static_cast<const PPB_TCPServerSocket_Private*>( - pp::Module::Get()->GetBrowserInterface( - PPB_TCPSERVERSOCKET_PRIVATE_INTERFACE)); - if (!tcp_server_socket_private_interface_) { + bool tcp_server_socket_private_is_available = + TCPServerSocketPrivate::IsAvailable(); + if (!tcp_server_socket_private_is_available) { instance_->AppendError( "PPB_TCPServerSocket_Private interface not available"); } - tcp_socket_private_interface_ = - static_cast<const PPB_TCPSocket_Private*>( - pp::Module::Get()->GetBrowserInterface( - PPB_TCPSOCKET_PRIVATE_INTERFACE)); - if (!tcp_socket_private_interface_) - instance_->AppendError("PPB_TCPSocket_Private interface not available"); - bool tcp_socket_private_is_available = TCPSocketPrivate::IsAvailable(); if (!tcp_socket_private_is_available) instance_->AppendError("PPB_TCPSocket_Private interface not available"); @@ -64,21 +46,20 @@ bool TestTCPServerSocketPrivate::Init() { if (!net_address_private_is_available) instance_->AppendError("PPB_NetAddress_Private interface not available"); - bool init_host_port = GetLocalHostPort( - instance_->pp_instance(), &host_, &port_); + bool init_host_port = GetLocalHostPort(instance_->pp_instance(), + &host_, &port_); if (!init_host_port) instance_->AppendError("Can't init host and port"); - return core_interface_ && - tcp_server_socket_private_interface_ && + return tcp_server_socket_private_is_available && tcp_socket_private_is_available && net_address_private_is_available && init_host_port && - CheckTestingInterface(); + CheckTestingInterface() && + EnsureRunningOverHTTP(); } void TestTCPServerSocketPrivate::RunTests(const std::string& filter) { - RUN_TEST_FORCEASYNC_AND_NOT(Create, filter); RUN_TEST_FORCEASYNC_AND_NOT(Listen, filter); RUN_TEST_FORCEASYNC_AND_NOT(Backlog, filter); } @@ -86,7 +67,6 @@ void TestTCPServerSocketPrivate::RunTests(const std::string& filter) { std::string TestTCPServerSocketPrivate::GetLocalAddress( PP_NetAddress_Private* address) { TCPSocketPrivate socket(instance_); - TestCompletionCallback callback(instance_->pp_instance(), force_async_); int32_t rv = socket.Connect(host_.c_str(), port_, callback); if (force_async_ && rv != PP_OK_COMPLETIONPENDING) @@ -95,104 +75,91 @@ std::string TestTCPServerSocketPrivate::GetLocalAddress( rv = callback.WaitForResult(); if (rv != PP_OK) return ReportError("PPB_TCPSocket_Private::Connect", rv); - if (!socket.GetLocalAddress(address)) return ReportError("PPB_TCPSocket_Private::GetLocalAddress", 0); socket.Disconnect(); PASS(); } -std::string TestTCPServerSocketPrivate::SyncRead(PP_Resource socket, +std::string TestTCPServerSocketPrivate::SyncRead(TCPSocketPrivate* socket, char* buffer, - int32_t num_bytes) { - TestCompletionCallback callback(instance_->pp_instance(), force_async_); - - int32_t rv = tcp_socket_private_interface_->Read( - socket, buffer, num_bytes, - static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); - - if (force_async_ && rv != PP_OK_COMPLETIONPENDING) - return ReportError("PPB_TCPSocket_Private::Read force_async", rv); - if (rv == PP_OK_COMPLETIONPENDING) - rv = callback.WaitForResult(); - - if (num_bytes != rv) - return ReportError("PPB_TCPSocket_Private::Read", rv); - + size_t num_bytes) { + while (num_bytes > 0) { + TestCompletionCallback callback(instance_->pp_instance(), force_async_); + int32_t rv = socket->Read(buffer, num_bytes, callback); + if (force_async_ && rv != PP_OK_COMPLETIONPENDING) + return ReportError("PPB_TCPSocket_Private::Read force_async", rv); + if (rv == PP_OK_COMPLETIONPENDING) + rv = callback.WaitForResult(); + if (rv < 0) + return ReportError("PPB_TCPSocket_Private::Read", rv); + buffer += rv; + num_bytes -= rv; + } PASS(); } -std::string TestTCPServerSocketPrivate::SyncWrite(PP_Resource socket, +std::string TestTCPServerSocketPrivate::SyncWrite(TCPSocketPrivate* socket, const char* buffer, - int32_t num_bytes) { - TestCompletionCallback callback(instance_->pp_instance(), force_async_); - int32_t rv = tcp_socket_private_interface_->Write( - socket, buffer, num_bytes, - static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); - - if (force_async_ && rv != PP_OK_COMPLETIONPENDING) - return ReportError("PPB_TCPSocket_Private::Write force_async", rv); - if (rv == PP_OK_COMPLETIONPENDING) - rv = callback.WaitForResult(); - if (num_bytes != rv) - return ReportError("PPB_TCPSocket_Private::Write", rv); - + size_t num_bytes) { + while (num_bytes > 0) { + TestCompletionCallback callback(instance_->pp_instance(), force_async_); + int32_t rv = socket->Write(buffer, num_bytes, callback); + if (force_async_ && rv != PP_OK_COMPLETIONPENDING) + return ReportError("PPB_TCPSocket_Private::Write force_async", rv); + if (rv == PP_OK_COMPLETIONPENDING) + rv = callback.WaitForResult(); + if (rv < 0) + return ReportError("PPB_TCPSocket_Private::Write", rv); + buffer += rv; + num_bytes -= rv; + } PASS(); } -std::string TestTCPServerSocketPrivate::SyncConnect(PP_Resource socket, - const char* host, - uint16_t port) { +std::string TestTCPServerSocketPrivate::SyncConnect( + TCPSocketPrivate* socket, + PP_NetAddress_Private* address) { TestCompletionCallback callback(instance_->pp_instance(), force_async_); - int32_t rv = tcp_socket_private_interface_->Connect( - socket, - host_.c_str(), - port, - static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); + int32_t rv = socket->ConnectWithNetAddress(address, callback); if (force_async_ && rv != PP_OK_COMPLETIONPENDING) return ReportError("PPB_TCPSocket_Private::Connect force_async", rv); if (rv == PP_OK_COMPLETIONPENDING) rv = callback.WaitForResult(); if (rv != PP_OK) return ReportError("PPB_TCPSocket_Private::Connect", rv); - PASS(); } -void TestTCPServerSocketPrivate::ForceConnect(PP_Resource socket, - const char* host, - uint16_t port) { +void TestTCPServerSocketPrivate::ForceConnect(TCPSocketPrivate* socket, + PP_NetAddress_Private* address) { std::string error_message; do { - error_message = SyncConnect(socket, host, port); + error_message = SyncConnect(socket, address); } while (!error_message.empty()); } -std::string TestTCPServerSocketPrivate::SyncListen(PP_Resource socket, - uint16_t* port, - int32_t backlog) { - PP_NetAddress_Private base_address, local_address; +std::string TestTCPServerSocketPrivate::SyncListen( + TCPServerSocketPrivate* socket, + PP_NetAddress_Private* address, + int32_t backlog) { + PP_NetAddress_Private base_address; ASSERT_SUBTEST_SUCCESS(GetLocalAddress(&base_address)); // TODO (ygorshenin): find more efficient way to select available // ports. bool is_free_port_found = false; - for (uint16_t p = kPortScanFrom; p < kPortScanTo; ++p) { - if (!NetAddressPrivate::ReplacePort(base_address, p, &local_address)) + for (uint16_t port = kPortScanFrom; port < kPortScanTo; ++port) { + if (!NetAddressPrivate::ReplacePort(base_address, port, address)) return ReportError("PPB_NetAddress_Private::ReplacePort", 0); TestCompletionCallback callback(instance_->pp_instance(), force_async_); - int32_t rv = tcp_server_socket_private_interface_->Listen( - socket, - &local_address, - backlog, - static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); + int32_t rv = socket->Listen(address, backlog, callback); if (force_async_ && rv != PP_OK_COMPLETIONPENDING) return ReportError("PPB_TCPServerSocket_Private::Listen force_async", rv); if (rv == PP_OK_COMPLETIONPENDING) rv = callback.WaitForResult(); if (rv == PP_OK) { - *port = p; is_free_port_found = true; break; } @@ -203,119 +170,59 @@ std::string TestTCPServerSocketPrivate::SyncListen(PP_Resource socket, PASS(); } -bool TestTCPServerSocketPrivate::IsSocketsConnected( - PP_Resource lhs, - PP_Resource rhs) { - PP_NetAddress_Private lhs_local_addr, lhs_remote_addr; - - if (!tcp_socket_private_interface_->GetLocalAddress(lhs, &lhs_local_addr)) - return false; - if (!tcp_socket_private_interface_->GetRemoteAddress(lhs, &lhs_remote_addr)) - return false; - - PP_NetAddress_Private rhs_local_addr, rhs_remote_addr; - if (!tcp_socket_private_interface_->GetLocalAddress(rhs, &rhs_local_addr)) - return false; - if (!tcp_socket_private_interface_->GetRemoteAddress(rhs, &rhs_remote_addr)) - return false; - - return NetAddressPrivate::AreEqual(lhs_local_addr, rhs_remote_addr) && - NetAddressPrivate::AreEqual(lhs_remote_addr, rhs_local_addr); -} - -std::string TestTCPServerSocketPrivate::SendMessage(PP_Resource dst, - PP_Resource src, - const char* message) { - const size_t message_size = strlen(message); - - ASSERT_SUBTEST_SUCCESS(SyncWrite(src, message, message_size)); - - std::vector<char> message_buffer(message_size); - ASSERT_SUBTEST_SUCCESS(SyncRead(dst, &message_buffer[0], message_size)); - ASSERT_EQ(0, strncmp(message, &message_buffer[0], message_size)); - PASS(); -} - -std::string TestTCPServerSocketPrivate::TestConnectedSockets(PP_Resource lhs, - PP_Resource rhs) { - static const char* const kMessage = "simple message"; - ASSERT_SUBTEST_SUCCESS(SendMessage(lhs, rhs, kMessage)); - ASSERT_SUBTEST_SUCCESS(SendMessage(rhs, lhs, kMessage)); - PASS(); -} - -std::string TestTCPServerSocketPrivate::TestCreate() { - PP_Resource server_socket; - - server_socket = tcp_server_socket_private_interface_->Create(0); - ASSERT_EQ(0, server_socket); - core_interface_->ReleaseResource(server_socket); - - server_socket = - tcp_server_socket_private_interface_->Create(instance_->pp_instance()); - ASSERT_TRUE(server_socket != 0); - ASSERT_TRUE(tcp_server_socket_private_interface_->IsTCPServerSocket( - server_socket)); - core_interface_->ReleaseResource(server_socket); - PASS(); -} - std::string TestTCPServerSocketPrivate::TestListen() { static const int kBacklog = 2; - PP_Resource server_socket = - tcp_server_socket_private_interface_->Create(instance_->pp_instance()); - ASSERT_TRUE(server_socket != 0); + TCPServerSocketPrivate server_socket(instance_); - uint16_t port; - ASSERT_SUBTEST_SUCCESS(SyncListen(server_socket, &port, kBacklog)); + PP_NetAddress_Private address; + ASSERT_SUBTEST_SUCCESS(SyncListen(&server_socket, &address, kBacklog)); TestCompletionCallback accept_callback(instance_->pp_instance(), force_async_); - PP_Resource accepted_socket; - int32_t accept_rv = tcp_server_socket_private_interface_->Accept( - server_socket, - &accepted_socket, - static_cast<pp::CompletionCallback>( - accept_callback).pp_completion_callback()); + PP_Resource resource; + int32_t accept_rv = server_socket.Accept(&resource, accept_callback); TCPSocketPrivate client_socket(instance_); + ForceConnect(&client_socket, &address); - ForceConnect(client_socket.pp_resource(), host_.c_str(), port); - - if (force_async_ && accept_rv != PP_OK_COMPLETIONPENDING) + if (force_async_ && accept_rv != PP_OK_COMPLETIONPENDING) { return ReportError("PPB_TCPServerSocket_Private::Accept force_async", accept_rv); + } if (accept_rv == PP_OK_COMPLETIONPENDING) accept_rv = accept_callback.WaitForResult(); if (accept_rv != PP_OK) return ReportError("PPB_TCPServerSocket_Private::Accept", accept_rv); - ASSERT_TRUE(accepted_socket != 0); - ASSERT_TRUE(tcp_socket_private_interface_->IsTCPSocket(accepted_socket)); + ASSERT_TRUE(resource != 0); + TCPSocketPrivate accepted_socket(pp::PassRef(), resource); + + const char kSentByte = 'a'; + ASSERT_SUBTEST_SUCCESS(SyncWrite(&client_socket, + &kSentByte, + sizeof(kSentByte))); - ASSERT_TRUE(IsSocketsConnected(client_socket.pp_resource(), accepted_socket)); - ASSERT_SUBTEST_SUCCESS(TestConnectedSockets(client_socket.pp_resource(), - accepted_socket)); + char received_byte; + ASSERT_SUBTEST_SUCCESS(SyncRead(&accepted_socket, + &received_byte, + sizeof(received_byte))); + ASSERT_EQ(kSentByte, received_byte); - tcp_socket_private_interface_->Disconnect(accepted_socket); + accepted_socket.Disconnect(); client_socket.Disconnect(); - tcp_server_socket_private_interface_->StopListening(server_socket); + server_socket.StopListening(); - core_interface_->ReleaseResource(accepted_socket); - core_interface_->ReleaseResource(server_socket); PASS(); } std::string TestTCPServerSocketPrivate::TestBacklog() { static const size_t kBacklog = 5; - PP_Resource server_socket = - tcp_server_socket_private_interface_->Create(instance_->pp_instance()); - ASSERT_TRUE(server_socket != 0); + TCPServerSocketPrivate server_socket(instance_); - uint16_t port; - ASSERT_SUBTEST_SUCCESS(SyncListen(server_socket, &port, 2 * kBacklog)); + PP_NetAddress_Private address; + ASSERT_SUBTEST_SUCCESS(SyncListen(&server_socket, &address, 2 * kBacklog)); std::vector<TCPSocketPrivate*> client_sockets(kBacklog); std::vector<TestCompletionCallback*> connect_callbacks(kBacklog); @@ -324,21 +231,19 @@ std::string TestTCPServerSocketPrivate::TestBacklog() { client_sockets[i] = new TCPSocketPrivate(instance_); connect_callbacks[i] = new TestCompletionCallback(instance_->pp_instance(), force_async_); - connect_rv[i] = client_sockets[i]->Connect(host_.c_str(), - port, - *connect_callbacks[i]); + connect_rv[i] = client_sockets[i]->ConnectWithNetAddress( + &address, + *connect_callbacks[i]); if (force_async_ && connect_rv[i] != PP_OK_COMPLETIONPENDING) return ReportError("PPB_TCPSocket_Private::Connect force_async", connect_rv[i]); } - std::vector<PP_Resource> accepted_sockets(kBacklog); + std::vector<PP_Resource> resources(kBacklog); + std::vector<TCPSocketPrivate*> accepted_sockets(kBacklog); for (size_t i = 0; i < kBacklog; ++i) { TestCompletionCallback callback(instance_->pp_instance(), force_async_); - int32_t rv = tcp_server_socket_private_interface_->Accept( - server_socket, - &accepted_sockets[i], - static_cast<pp::CompletionCallback>(callback).pp_completion_callback()); + int32_t rv = server_socket.Accept(&resources[i], callback); if (force_async_ && rv != PP_OK_COMPLETIONPENDING) return ReportError("PPB_TCPServerSocket_Private::Accept force_async", rv); if (rv == PP_OK_COMPLETIONPENDING) @@ -346,9 +251,8 @@ std::string TestTCPServerSocketPrivate::TestBacklog() { if (rv != PP_OK) return ReportError("PPB_TCPServerSocket_Private::Accept", rv); - ASSERT_TRUE(accepted_sockets[i] != 0); - ASSERT_TRUE(tcp_socket_private_interface_->IsTCPSocket( - accepted_sockets[i])); + ASSERT_TRUE(resources[i] != 0); + accepted_sockets[i] = new TCPSocketPrivate(pp::PassRef(), resources[i]); } for (size_t i = 0; i < kBacklog; ++i) { @@ -359,29 +263,27 @@ std::string TestTCPServerSocketPrivate::TestBacklog() { } for (size_t i = 0; i < kBacklog; ++i) { - bool found = false; - for (size_t j = 0; j < kBacklog && !found; ++j) - if (IsSocketsConnected(client_sockets[i]->pp_resource(), - accepted_sockets[j])) { - TestConnectedSockets(client_sockets[i]->pp_resource(), - accepted_sockets[j]); - found = true; - } - ASSERT_TRUE(found); + const char byte = 'a' + i; + ASSERT_SUBTEST_SUCCESS(SyncWrite(client_sockets[i], &byte, sizeof(byte))); } + bool byte_received[kBacklog] = {}; for (size_t i = 0; i < kBacklog; ++i) { - tcp_socket_private_interface_->Disconnect(accepted_sockets[i]); - core_interface_->ReleaseResource(accepted_sockets[i]); + char byte; + ASSERT_SUBTEST_SUCCESS(SyncRead(accepted_sockets[i], &byte, sizeof(byte))); + const size_t index = byte - 'a'; + ASSERT_FALSE(byte_received[index]); + byte_received[index] = true; } for (size_t i = 0; i < kBacklog; ++i) { client_sockets[i]->Disconnect(); delete client_sockets[i]; delete connect_callbacks[i]; + accepted_sockets[i]->Disconnect(); + delete accepted_sockets[i]; } - tcp_server_socket_private_interface_->StopListening(server_socket); - core_interface_->ReleaseResource(server_socket); + server_socket.StopListening(); PASS(); } diff --git a/ppapi/tests/test_tcp_server_socket_private.h b/ppapi/tests/test_tcp_server_socket_private.h index 5ada1c1..1e25381 100644 --- a/ppapi/tests/test_tcp_server_socket_private.h +++ b/ppapi/tests/test_tcp_server_socket_private.h @@ -5,15 +5,21 @@ #ifndef PPAPI_TESTS_TEST_TCP_SERVER_SOCKET_PRIVATE_H_ #define PPAPI_TESTS_TEST_TCP_SERVER_SOCKET_PRIVATE_H_ +#include <cstddef> #include <string> #include "ppapi/c/pp_stdint.h" -#include "ppapi/c/ppb_core.h" -#include "ppapi/c/private/ppb_net_address_private.h" -#include "ppapi/c/private/ppb_tcp_server_socket_private.h" -#include "ppapi/c/private/ppb_tcp_socket_private.h" #include "ppapi/tests/test_case.h" +struct PP_NetAddress_Private; + +namespace pp { + +class TCPServerSocketPrivate; +class TCPSocketPrivate; + +} // namespace pp + class TestTCPServerSocketPrivate : public TestCase { public: explicit TestTCPServerSocketPrivate(TestingInstance* instance); @@ -24,31 +30,23 @@ class TestTCPServerSocketPrivate : public TestCase { private: std::string GetLocalAddress(PP_NetAddress_Private* address); - std::string SyncRead(PP_Resource socket, char* buffer, int32_t num_bytes); - std::string SyncWrite(PP_Resource socket, + std::string SyncRead(pp::TCPSocketPrivate* socket, + char* buffer, + size_t num_bytes); + std::string SyncWrite(pp::TCPSocketPrivate* socket, const char* buffer, - int32_t num_bytes); - std::string SyncConnect(PP_Resource socket, const char* host, uint16_t port); - void ForceConnect(PP_Resource socket, const char* host, uint16_t port); - std::string SyncListen(PP_Resource socket, uint16_t* port, int32_t backlog); - - bool IsSocketsConnected(PP_Resource lhs, PP_Resource rhs); - std::string SendMessage(PP_Resource dst, - PP_Resource src, - const char* message); - std::string TestConnectedSockets(PP_Resource lhs, PP_Resource rhs); - - std::string CheckIOOfConnectedSockets(PP_Resource src, PP_Resource dst); - std::string CheckAddressesOfConnectedSockets(PP_Resource lhs, - PP_Resource rhs); - - std::string TestCreate(); + size_t num_bytes); + std::string SyncConnect(pp::TCPSocketPrivate* socket, + PP_NetAddress_Private* address); + void ForceConnect(pp::TCPSocketPrivate* socket, + PP_NetAddress_Private* address); + std::string SyncListen(pp::TCPServerSocketPrivate* socket, + PP_NetAddress_Private* address, + int32_t backlog); + std::string TestListen(); std::string TestBacklog(); - const PPB_Core* core_interface_; - const PPB_TCPServerSocket_Private* tcp_server_socket_private_interface_; - const PPB_TCPSocket_Private* tcp_socket_private_interface_; std::string host_; uint16_t port_; }; |