From a0f07ff1caba81efa1397fade9e775dcedee5b3a Mon Sep 17 00:00:00 2001 From: Madhura Jayaraman Date: Tue, 15 Oct 2024 11:18:47 -0700 Subject: [PATCH] Add Posix tests for sockets (#4201) - Create Posix version of GetPortForTests - Remove references to socket_helper b/369182833 --- starboard/nplb/BUILD.gn | 5 - .../posix_socket_accept_test.cc | 4 +- .../posix_socket_bind_test.cc | 18 +- .../posix_socket_connect_test.cc | 4 +- .../posix_compliance/posix_socket_helpers.cc | 74 +++++++- .../posix_compliance/posix_socket_helpers.h | 13 +- .../posix_socket_join_multicast_group_test.cc | 2 +- .../posix_socket_listen_test.cc | 2 +- .../posix_socket_receive_test.cc | 2 +- .../posix_socket_recvfrom_test.cc | 2 +- .../posix_socket_send_test.cc | 6 +- .../posix_socket_sendto_test.cc | 10 +- .../posix_socket_waiter_wait_test.cc | 6 +- .../posix_socket_waiter_wait_timed_test.cc | 2 +- .../nplb/socket_clear_last_error_test.cc | 49 ------ .../nplb/socket_get_interface_address_test.cc | 6 - starboard/nplb/socket_get_last_error_test.cc | 32 ---- .../nplb/socket_get_local_address_test.cc | 18 +- starboard/nplb/socket_helpers.cc | 165 ------------------ starboard/nplb/socket_helpers.h | 30 ---- .../nplb/socket_is_connected_and_idle_test.cc | 117 ------------- starboard/nplb/socket_is_connected_test.cc | 99 ----------- starboard/nplb/socket_receive_from_test.cc | 141 --------------- starboard/nplb/socket_wrapper_test.cc | 11 +- 24 files changed, 109 insertions(+), 709 deletions(-) delete mode 100644 starboard/nplb/socket_clear_last_error_test.cc delete mode 100644 starboard/nplb/socket_get_last_error_test.cc delete mode 100644 starboard/nplb/socket_is_connected_and_idle_test.cc delete mode 100644 starboard/nplb/socket_is_connected_test.cc delete mode 100644 starboard/nplb/socket_receive_from_test.cc diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 1e71aed0dcd1..73d9a13ce1ac 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -177,15 +177,10 @@ target(gtest_target_type, "nplb") { "random_helpers.cc", "recursive_mutex_test.cc", "semaphore_test.cc", - "socket_clear_last_error_test.cc", "socket_destroy_test.cc", "socket_get_interface_address_test.cc", - "socket_get_last_error_test.cc", "socket_get_local_address_test.cc", "socket_helpers.cc", - "socket_is_connected_and_idle_test.cc", - "socket_is_connected_test.cc", - "socket_receive_from_test.cc", "socket_waiter_create_test.cc", "socket_waiter_destroy_test.cc", "socket_waiter_wake_up_test.cc", diff --git a/starboard/nplb/posix_compliance/posix_socket_accept_test.cc b/starboard/nplb/posix_compliance/posix_socket_accept_test.cc index 0ef44a83bfa2..015ab2168f3e 100644 --- a/starboard/nplb/posix_compliance/posix_socket_accept_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_accept_test.cc @@ -55,7 +55,7 @@ TEST(PosixSocketAcceptTest, RainyDayNoConnection) { EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&address)) == 0); - address.sin6_port = htons(GetPortNumberForTests()); + address.sin6_port = htons(PosixGetPortNumberForTests()); result = bind(socket_listen_fd, reinterpret_cast(&address), sizeof(sockaddr)); @@ -132,7 +132,7 @@ TEST(PosixSocketAcceptTest, RainyDayNotListening) { EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&address)) == 0); - address.sin6_port = htons(GetPortNumberForTests()); + address.sin6_port = htons(PosixGetPortNumberForTests()); EXPECT_TRUE(result == 0); if (result != 0) { close(socket_fd); diff --git a/starboard/nplb/posix_compliance/posix_socket_bind_test.cc b/starboard/nplb/posix_compliance/posix_socket_bind_test.cc index 04eb28449ca0..e9aa8853632c 100644 --- a/starboard/nplb/posix_compliance/posix_socket_bind_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_bind_test.cc @@ -22,7 +22,7 @@ namespace nplb { namespace { TEST(PosixSocketBindTest, RainyDayNullSocket) { - int port = htons(GetPortNumberForTests()); + int port = htons(PosixGetPortNumberForTests()); sockaddr_in address = {}; address.sin_family = AF_INET; int invalid_socket_fd = -1; @@ -51,7 +51,7 @@ TEST(PosixSocketBindTest, RainyDayWrongAddressType) { // Binding with the wrong address type should fail. sockaddr_in client_address = {}; client_address.sin_family = AF_INET6; - client_address.sin_port = htons(GetPortNumberForTests()); + client_address.sin_port = htons(PosixGetPortNumberForTests()); EXPECT_FALSE(bind(socket_fd, reinterpret_cast(&client_address), sizeof(sockaddr_in)) == 0); @@ -59,7 +59,7 @@ TEST(PosixSocketBindTest, RainyDayWrongAddressType) { // address type should work. sockaddr_in server_address = {}; server_address.sin_family = AF_INET; - server_address.sin_port = htons(GetPortNumberForTests()); + server_address.sin_port = htons(PosixGetPortNumberForTests()); EXPECT_TRUE(bind(socket_fd, reinterpret_cast(&server_address), sizeof(sockaddr_in)) == 0); EXPECT_TRUE(close(socket_fd) == 0); @@ -84,7 +84,7 @@ TEST(PosixSocketBindTest, SunnyDayLocalInterface) { EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&address)) == 0); - address.sin6_port = htons(GetPortNumberForTests()); + address.sin6_port = htons(PosixGetPortNumberForTests()); int socket_domain = AF_INET; int socket_type = SOCK_STREAM; @@ -103,7 +103,7 @@ TEST(PosixSocketBindTest, SunnyDayAnyAddr) { // should work. sockaddr_in address = {}; address.sin_family = AF_INET; - address.sin_port = htons(GetPortNumberForTests()); + address.sin_port = htons(PosixGetPortNumberForTests()); address.sin_addr.s_addr = INADDR_ANY; int socket_domain = AF_INET; @@ -139,7 +139,7 @@ class PosixSocketBindPairCSTest TEST_P(PosixSocketBindPairFilterTest, RainyDayNullSocketPair) { sockaddr_in address = {}; address.sin_family = GetAddressType(); - address.sin_port = htons(GetPortNumberForTests()); + address.sin_port = htons(PosixGetPortNumberForTests()); int invalid_socket_fd = -1; @@ -159,7 +159,7 @@ TEST_P(PosixSocketBindPairFilterTest, RainyDayNullAddressPair) { // should work. sockaddr_in address = {}; address.sin_family = GetAddressType(); - address.sin_port = htons(GetPortNumberForTests()); + address.sin_port = htons(PosixGetPortNumberForTests()); EXPECT_TRUE(bind(socket_fd, reinterpret_cast(&address), sizeof(sockaddr_in)) == 0); @@ -222,7 +222,7 @@ TEST_P(PosixSocketBindPairCSTest, RainyDayWrongAddressTypePair) { // Binding with the wrong address type should fail. sockaddr_in client_address = {}; client_address.sin_family = GetClientAddressType(); - client_address.sin_port = htons(GetPortNumberForTests()); + client_address.sin_port = htons(PosixGetPortNumberForTests()); EXPECT_FALSE(bind(socket_fd, reinterpret_cast(&client_address), sizeof(sockaddr_in)) == 0); @@ -230,7 +230,7 @@ TEST_P(PosixSocketBindPairCSTest, RainyDayWrongAddressTypePair) { // address type should work. sockaddr_in server_address = {}; server_address.sin_family = GetServerAddressType(); - server_address.sin_port = htons(GetPortNumberForTests()); + server_address.sin_port = htons(PosixGetPortNumberForTests()); EXPECT_TRUE(bind(socket_fd, reinterpret_cast(&server_address), sizeof(sockaddr_in)) == 0); EXPECT_TRUE(close(socket_fd) == 0); diff --git a/starboard/nplb/posix_compliance/posix_socket_connect_test.cc b/starboard/nplb/posix_compliance/posix_socket_connect_test.cc index 36a34efa14a1..dff95fefced2 100644 --- a/starboard/nplb/posix_compliance/posix_socket_connect_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_connect_test.cc @@ -50,7 +50,7 @@ TEST(PosixSocketConnectTest, RainyDayNullAddress) { TEST(PosixSocketConnectTest, SunnyDayConnectToServer) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); EXPECT_TRUE(close(listen_socket_fd) == 0); @@ -61,7 +61,7 @@ TEST(PosixSocketConnectTest, SunnyDayConnectToServer) { TEST(PosixSocketConnectTest, SunnyDayConnectToServerAgain) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); EXPECT_TRUE(close(listen_socket_fd) == 0); diff --git a/starboard/nplb/posix_compliance/posix_socket_helpers.cc b/starboard/nplb/posix_compliance/posix_socket_helpers.cc index cfc91f3c8389..c64b3c63f6fd 100644 --- a/starboard/nplb/posix_compliance/posix_socket_helpers.cc +++ b/starboard/nplb/posix_compliance/posix_socket_helpers.cc @@ -12,10 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include "starboard/nplb/posix_compliance/posix_socket_helpers.h" +#include +#include + +#include "starboard/shared/posix/handle_eintr.h" #include "starboard/thread.h" namespace starboard { @@ -44,12 +46,13 @@ int PosixSocketCreateAndConnect(int server_domain, close(*listen_socket_fd); return -1; } + // bind socket with local address sockaddr_in6 address = {}; EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&address)) == 0); - address.sin6_port = htons(GetPortNumberForTests()); + address.sin6_port = htons(PosixGetPortNumberForTests()); result = bind(*listen_socket_fd, reinterpret_cast(&address), sizeof(struct sockaddr_in)); @@ -182,6 +185,71 @@ int PosixGetLocalAddressIPv6(sockaddr* address_ptr) { return result; } +int port_number_for_tests = 0; +pthread_once_t valid_port_once_control = PTHREAD_ONCE_INIT; + +void PosixInitializePortNumberForTests() { + // Create a listening socket. Let the system choose a port for us. + int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (socket_fd < 0) { + ADD_FAILURE() << "Socket create failed errno: " << errno; + return; + } + + int on = 1; + if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) { + ADD_FAILURE() << "Socket Set Reuse Address failed, errno: " << errno; + HANDLE_EINTR(close(socket_fd)); + return; + } + + // bind socket with local address + struct sockaddr_in address = {}; + address.sin_port = 0; + address.sin_family = AF_INET; + + int bind_result = + bind(socket_fd, reinterpret_cast(&address), sizeof(sockaddr)); + + if (bind_result != 0) { + ADD_FAILURE() << "Socket Bind to 0 failed, errno: " << errno; + HANDLE_EINTR(close(socket_fd)); + return; + } + + int listen_result = listen(socket_fd, kMaxConn); + if (listen_result != 0) { + ADD_FAILURE() << "Socket Listen failed, errno: " << errno; + HANDLE_EINTR(close(socket_fd)); + return; + } + + // Query which port this socket was bound to and save it to valid_port_number. + struct sockaddr_in addr_in = {0}; + socklen_t socklen = static_cast(sizeof(addr_in)); + int local_add_result = + getsockname(socket_fd, reinterpret_cast(&addr_in), &socklen); + + if (local_add_result < 0) { + ADD_FAILURE() << "Socket get local address failed: " << local_add_result + << " Errno: " << errno; + HANDLE_EINTR(close(socket_fd)); + return; + } + port_number_for_tests = addr_in.sin_port; + + // Clean up the socket. + bool result = HANDLE_EINTR(close(socket_fd)) >= 0; + SB_DCHECK(result); +} + +int PosixGetPortNumberForTests() { + pthread_once(&valid_port_once_control, &PosixInitializePortNumberForTests); + SB_DLOG(INFO) << "PosixGetPortNumberForTests port_number_for_tests : " + << port_number_for_tests; + return port_number_for_tests; +} + bool PosixWriteBySpinning(int socket, const char* data, int data_size, diff --git a/starboard/nplb/posix_compliance/posix_socket_helpers.h b/starboard/nplb/posix_compliance/posix_socket_helpers.h index 45fe99c08558..5b14126834ad 100644 --- a/starboard/nplb/posix_compliance/posix_socket_helpers.h +++ b/starboard/nplb/posix_compliance/posix_socket_helpers.h @@ -33,12 +33,6 @@ namespace nplb { #if defined(SOMAXCONN) const int kMaxConn = SOMAXCONN; #else -// Some posix platforms such as FreeBSD do not define SOMAXCONN. -// In this case, set the value to an arbitrary number large enough to -// satisfy most use-cases and tests, empirically we have found that 128 -// is sufficient. All implementations of listen() specify that a backlog -// parameter larger than the system max will be silently truncated to the -// system's max. const int kMaxConn = 128; #endif @@ -55,6 +49,13 @@ int PosixGetLocalAddressIPv6(sockaddr* address_ptr); int PosixSocketSetReceiveBufferSize(int socket_fd, int32_t size); int PosixSocketSetSendBufferSize(int socket_fd, int32_t size); +// Returns a valid port number that can be bound to for use in nplb tests. +// This will always return the same port number. +int PosixGetPortNumberForTests(); + +// Creates a TCP/IP socket listening on all interfaces on the given port. +// int PosixCreateBoundListeningTcpSocket(); + #if defined(MSG_NOSIGNAL) const int kSendFlags = MSG_NOSIGNAL; #else diff --git a/starboard/nplb/posix_compliance/posix_socket_join_multicast_group_test.cc b/starboard/nplb/posix_compliance/posix_socket_join_multicast_group_test.cc index c7bfd4c6a49c..c8a8a2776829 100644 --- a/starboard/nplb/posix_compliance/posix_socket_join_multicast_group_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_join_multicast_group_test.cc @@ -150,7 +150,7 @@ TEST(PosixSocketJoinMulticastGroupTest, RainyDayInvalidSocket) { EXPECT_EQ(-1, setsockopt(-1, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq))); - EXPECT_TRUE(errno == EBADF || errno == ECONNREFUSED); + EXPECT_TRUE(errno == EBADF || errno == ECONNREFUSED || errno == EINVAL); } TEST(PosixSocketJoinMulticastGroupTest, RainyDayInvalidAddress) { diff --git a/starboard/nplb/posix_compliance/posix_socket_listen_test.cc b/starboard/nplb/posix_compliance/posix_socket_listen_test.cc index a2d03a0da11f..db81e4809707 100644 --- a/starboard/nplb/posix_compliance/posix_socket_listen_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_listen_test.cc @@ -46,7 +46,7 @@ TEST(PosixSocketListenTest, SunnyDayUnbound) { EXPECT_TRUE( PosixGetLocalAddressIPv4(reinterpret_cast(&address)) == 0 || PosixGetLocalAddressIPv6(reinterpret_cast(&address)) == 0); - address.sin6_port = htons(GetPortNumberForTests()); + address.sin6_port = htons(PosixGetPortNumberForTests()); result = bind(socket_fd, reinterpret_cast(&address), sizeof(sockaddr)); diff --git a/starboard/nplb/posix_compliance/posix_socket_receive_test.cc b/starboard/nplb/posix_compliance/posix_socket_receive_test.cc index f8480612cb87..2bb6bee776d0 100644 --- a/starboard/nplb/posix_compliance/posix_socket_receive_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_receive_test.cc @@ -66,7 +66,7 @@ TEST(PosixSocketReceiveTest, SunnyDay) { const int kSockBufSize = kBufSize / 8; int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); diff --git a/starboard/nplb/posix_compliance/posix_socket_recvfrom_test.cc b/starboard/nplb/posix_compliance/posix_socket_recvfrom_test.cc index eb4f1be4fc5a..369831c87d78 100644 --- a/starboard/nplb/posix_compliance/posix_socket_recvfrom_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_recvfrom_test.cc @@ -66,7 +66,7 @@ TEST(PosixSocketRecvfromTest, SunnyDay) { const int kSockBufSize = kBufSize / 8; int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); diff --git a/starboard/nplb/posix_compliance/posix_socket_send_test.cc b/starboard/nplb/posix_compliance/posix_socket_send_test.cc index 1869f83b036b..c2c158b5309f 100644 --- a/starboard/nplb/posix_compliance/posix_socket_send_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_send_test.cc @@ -79,7 +79,7 @@ TEST(PosixSocketSendTest, RainyDayUnconnectedSocket) { TEST(PosixSocketSendTest, RainyDaySendToClosedSocket) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); EXPECT_TRUE(result == 0); @@ -125,7 +125,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketUntilBlocking) { int result = -1; int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, GetPortNumberForTests(), kSocketTimeout, + AF_INET, AF_INET, PosixGetPortNumberForTests(), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); @@ -172,7 +172,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketConnectionReset) { // create listen socket, bind and listen on int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); EXPECT_TRUE(result == 0); if (result != 0) { diff --git a/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc b/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc index 08d3ce279b45..a39ada050044 100644 --- a/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_sendto_test.cc @@ -82,7 +82,7 @@ TEST(PosixSocketSendtoTest, RainyDayUnconnectedSocket) { TEST(PosixSocketSendtoTest, RainyDaySendToClosedSocket) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); @@ -128,7 +128,7 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketUntilBlocking) { int result = -1; int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); @@ -174,9 +174,9 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketConnectionReset) { // create listen socket, bind and listen on int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; - PosixSocketCreateAndConnect(AF_INET, AF_INET, htons(GetPortNumberForTests()), - kSocketTimeout, &listen_socket_fd, - &client_socket_fd, &server_socket_fd); + PosixSocketCreateAndConnect( + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, + &listen_socket_fd, &client_socket_fd, &server_socket_fd); // Kills the server, the client socket will have it's connection reset during // one of the subsequent writes. diff --git a/starboard/nplb/posix_compliance/posix_socket_waiter_wait_test.cc b/starboard/nplb/posix_compliance/posix_socket_waiter_wait_test.cc index 07c9f2539e9f..6d13cd23ca77 100644 --- a/starboard/nplb/posix_compliance/posix_socket_waiter_wait_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_waiter_wait_test.cc @@ -67,7 +67,7 @@ TEST(SbPosixSocketWaiterWaitTest, SunnyDay) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); ASSERT_TRUE(server_socket_fd >= 0); @@ -103,7 +103,7 @@ TEST(SbPosixSocketWaiterWaitTest, SunnyDay) { EXPECT_TRUE(close(*trio.client_socket_fd_ptr) == 0); EXPECT_TRUE(close(*trio.listen_socket_fd_ptr) == 0); result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); ASSERT_TRUE(server_socket_fd >= 0); @@ -227,7 +227,7 @@ TEST(SbPosixSocketWaiterWaitTest, SunnyDayAlreadyReady) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); ASSERT_TRUE(server_socket_fd >= 0); diff --git a/starboard/nplb/posix_compliance/posix_socket_waiter_wait_timed_test.cc b/starboard/nplb/posix_compliance/posix_socket_waiter_wait_timed_test.cc index 21078b686b1f..e5ecfb94f8af 100644 --- a/starboard/nplb/posix_compliance/posix_socket_waiter_wait_timed_test.cc +++ b/starboard/nplb/posix_compliance/posix_socket_waiter_wait_timed_test.cc @@ -58,7 +58,7 @@ TEST(SbPosixSocketWaiterWaitTimedTest, SunnyDay) { int listen_socket_fd = -1, client_socket_fd = -1, server_socket_fd = -1; int result = PosixSocketCreateAndConnect( - AF_INET, AF_INET, htons(GetPortNumberForTests()), kSocketTimeout, + AF_INET, AF_INET, htons(PosixGetPortNumberForTests()), kSocketTimeout, &listen_socket_fd, &client_socket_fd, &server_socket_fd); ASSERT_TRUE(result == 0); ASSERT_TRUE(server_socket_fd >= 0); diff --git a/starboard/nplb/socket_clear_last_error_test.cc b/starboard/nplb/socket_clear_last_error_test.cc deleted file mode 100644 index 6b2d76351543..000000000000 --- a/starboard/nplb/socket_clear_last_error_test.cc +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2016 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// The Sunny Day test is used in several other tests as the way to detect if the -// error is correct, so it is not repeated here. -#if SB_API_VERSION < 17 -#include "starboard/common/socket.h" -#include "starboard/nplb/socket_helpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -TEST(SbSocketClearLastErrorTest, SunnyDay) { - // Set up a socket, but don't Bind or Listen. - SbSocket server_socket = CreateServerTcpSocket(kSbSocketAddressTypeIpv4); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - - // Accept on the unbound socket should result in an error. - EXPECT_EQ(kSbSocketInvalid, SbSocketAccept(server_socket)); - EXPECT_SB_SOCKET_ERROR_IS_ERROR(SbSocketGetLastError(server_socket)); - - // After we clear the error, it should be OK. - EXPECT_TRUE(SbSocketClearLastError(server_socket)); - EXPECT_EQ(kSbSocketOk, SbSocketGetLastError(server_socket)); - - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -TEST(SbSocketClearLastErrorTest, RainyDayInvalidSocket) { - EXPECT_FALSE(SbSocketClearLastError(kSbSocketInvalid)); -} - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_get_interface_address_test.cc b/starboard/nplb/socket_get_interface_address_test.cc index 0176c4a167df..17ae3f3f4149 100644 --- a/starboard/nplb/socket_get_interface_address_test.cc +++ b/starboard/nplb/socket_get_interface_address_test.cc @@ -177,12 +177,6 @@ TEST_P(SbSocketGetInterfaceAddressTest, SunnyDaySourceNotLoopback) { SB_ARRAY_SIZE(source.address))); } -INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, - SbSocketGetInterfaceAddressTest, - ::testing::Values(kSbSocketAddressTypeIpv4, - kSbSocketAddressTypeIpv6), - GetSbSocketAddressTypeName); - } // namespace } // namespace nplb } // namespace starboard diff --git a/starboard/nplb/socket_get_last_error_test.cc b/starboard/nplb/socket_get_last_error_test.cc deleted file mode 100644 index 54aa398371e8..000000000000 --- a/starboard/nplb/socket_get_last_error_test.cc +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// The Sunny Day test is used in several other tests as the way to detect if the -// error is correct, so it is not repeated here. -#if SB_API_VERSION < 17 -#include "starboard/common/socket.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -TEST(SbSocketGetLastErrorTest, RainyDayInvalidSocket) { - EXPECT_EQ(kSbSocketErrorFailed, SbSocketGetLastError(kSbSocketInvalid)); -} - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_get_local_address_test.cc b/starboard/nplb/socket_get_local_address_test.cc index 82ee3980e844..4673ab4096fa 100644 --- a/starboard/nplb/socket_get_local_address_test.cc +++ b/starboard/nplb/socket_get_local_address_test.cc @@ -80,7 +80,6 @@ TEST_P(SbSocketGetLocalAddressTest, SunnyDayBoundUnspecified) { EXPECT_TRUE(SbSocketDestroy(server_socket)); } -#if SB_API_VERSION < 17 TEST_F(SbSocketGetLocalAddressTest, SunnyDayBoundSpecified) { SbSocketAddress interface_address = {0}; EXPECT_TRUE(SbSocketGetInterfaceAddress(NULL, &interface_address, NULL)); @@ -99,7 +98,7 @@ TEST_F(SbSocketGetLocalAddressTest, SunnyDayBoundSpecified) { } TEST_P(PairSbSocketGetLocalAddressTest, SunnyDayConnected) { - const int kPort = GetPortNumberForTests(); + const int kPort = PosixGetPortNumberForTests(); ConnectedTrio trio = CreateAndConnect( GetServerAddressType(), GetClientAddressType(), kPort, kSocketTimeout); ASSERT_TRUE(SbSocketIsValid(trio.server_socket)); @@ -123,21 +122,6 @@ TEST_P(PairSbSocketGetLocalAddressTest, SunnyDayConnected) { EXPECT_TRUE(SbSocketDestroy(trio.client_socket)); EXPECT_TRUE(SbSocketDestroy(trio.listen_socket)); } -#endif // SB_API_VERSION < 17 - -INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, - SbSocketGetLocalAddressTest, - ::testing::Values(kSbSocketAddressTypeIpv4, - kSbSocketAddressTypeIpv6), - GetSbSocketAddressTypeName); -INSTANTIATE_TEST_CASE_P( - SbSocketAddressTypes, - PairSbSocketGetLocalAddressTest, - ::testing::Values( - std::make_pair(kSbSocketAddressTypeIpv4, kSbSocketAddressTypeIpv4), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv6), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv4)), - GetSbSocketAddressTypePairName); } // namespace } // namespace nplb diff --git a/starboard/nplb/socket_helpers.cc b/starboard/nplb/socket_helpers.cc index 603efeab8857..284b82b08616 100644 --- a/starboard/nplb/socket_helpers.cc +++ b/starboard/nplb/socket_helpers.cc @@ -33,89 +33,6 @@ namespace starboard { namespace nplb { -namespace { - -int port_number_for_tests = 0; -pthread_once_t valid_port_once_control = PTHREAD_ONCE_INIT; - -void InitializePortNumberForTests() { - // Create a listening socket. Let the system choose a port for us. - SbSocket socket = CreateListeningTcpSocket(kSbSocketAddressTypeIpv4, 0); - SB_DCHECK(socket != kSbSocketInvalid); - - // Query which port this socket was bound to and save it to valid_port_number. - SbSocketAddress socket_address = {0}; - bool result = SbSocketGetLocalAddress(socket, &socket_address); - SB_DCHECK(result); - port_number_for_tests = socket_address.port; - - // Clean up the socket. - result = SbSocketDestroy(socket); - SB_DCHECK(result); -} -} // namespace - -int GetPortNumberForTests() { -#if defined(SB_SOCKET_OVERRIDE_PORT_FOR_TESTS) - static int incremental = 0; - if (incremental + SB_SOCKET_OVERRIDE_PORT_FOR_TESTS == 65535) { - incremental = 0; - } - return SB_SOCKET_OVERRIDE_PORT_FOR_TESTS + ++incremental; -#else - pthread_once(&valid_port_once_control, &InitializePortNumberForTests); - return port_number_for_tests; -#endif -} - -SbSocket CreateServerTcpSocket(SbSocketAddressType address_type) { - SbSocket server_socket = SbSocketCreate(address_type, kSbSocketProtocolTcp); - if (!SbSocketIsValid(server_socket)) { - ADD_FAILURE() << "SbSocketCreate failed"; - return kSbSocketInvalid; - } - - if (!SbSocketSetReuseAddress(server_socket, true)) { - ADD_FAILURE() << "SbSocketSetReuseAddress failed"; - SbSocketDestroy(server_socket); - return kSbSocketInvalid; - } - - return server_socket; -} - -SbSocket CreateBoundTcpSocket(SbSocketAddressType address_type, int port) { - SbSocket server_socket = CreateServerTcpSocket(address_type); - if (!SbSocketIsValid(server_socket)) { - return kSbSocketInvalid; - } - - SbSocketAddress address = GetUnspecifiedAddress(address_type, port); - SbSocketError result = SbSocketBind(server_socket, &address); - if (result != kSbSocketOk) { - ADD_FAILURE() << "SbSocketBind to " << port << " failed: " << result; - SbSocketDestroy(server_socket); - return kSbSocketInvalid; - } - - return server_socket; -} - -SbSocket CreateListeningTcpSocket(SbSocketAddressType address_type, int port) { - SbSocket server_socket = CreateBoundTcpSocket(address_type, port); - if (!SbSocketIsValid(server_socket)) { - return kSbSocketInvalid; - } - - SbSocketError result = SbSocketListen(server_socket); - if (result != kSbSocketOk) { - ADD_FAILURE() << "SbSocketListen failed: " << result; - SbSocketDestroy(server_socket); - return kSbSocketInvalid; - } - - return server_socket; -} int64_t TimedWait(SbSocketWaiter waiter) { int64_t start = CurrentMonotonicTime(); @@ -130,87 +47,5 @@ int64_t TimedWaitTimed(SbSocketWaiter waiter, int64_t timeout) { return CurrentMonotonicTime() - start; } -#if !defined(COBALT_BUILD_TYPE_GOLD) -namespace { -const char* SbSocketAddressTypeName(SbSocketAddressType type) { - const char* name = "unknown"; - switch (type) { - case kSbSocketAddressTypeIpv4: - name = "ipv4"; - break; - case kSbSocketAddressTypeIpv6: - name = "ipv6"; - break; - } - return name; -} - -const char* SbSocketAddressFilterName(SbSocketResolveFilter filter) { - const char* name = "unknown"; - switch (filter) { - case kSbSocketResolveFilterNone: - name = "none"; - break; - case kSbSocketResolveFilterIpv4: - name = "ipv4"; - break; - case kSbSocketResolveFilterIpv6: - name = "ipv6"; - break; - } - return name; -} - -const char* SbSocketProtocolName(SbSocketProtocol protocol) { - const char* name = "unknown"; - switch (protocol) { - case kSbSocketProtocolTcp: - name = "tcp"; - break; - case kSbSocketProtocolUdp: - name = "udp"; - break; - } - return name; -} -} // namespace - -std::string GetSbSocketAddressTypeName( - ::testing::TestParamInfo info) { - return FormatString("type_%s", SbSocketAddressTypeName(info.param)); -} -std::string GetSbSocketAddressTypePairName( - ::testing::TestParamInfo< - std::pair> info) { - return FormatString("type_%s_type_%s", - SbSocketAddressTypeName(info.param.first), - SbSocketAddressTypeName(info.param.second)); -} - -std::string GetSbSocketAddressTypeFilterPairName( - ::testing::TestParamInfo< - std::pair> info) { - return FormatString("type_%s_filter_%s", - SbSocketAddressTypeName(info.param.first), - SbSocketAddressFilterName(info.param.second)); -} - -std::string GetSbSocketFilterAddressTypePairName( - ::testing::TestParamInfo< - std::pair> info) { - return FormatString("filter_%s_type_%s", - SbSocketAddressFilterName(info.param.first), - SbSocketAddressTypeName(info.param.second)); -} - -std::string GetSbSocketAddressTypeProtocolPairName( - ::testing::TestParamInfo> - info) { - return FormatString("type_%s_%s", SbSocketAddressTypeName(info.param.first), - SbSocketProtocolName(info.param.second)); -} - -#endif // #if !defined(COBALT_BUILD_TYPE_GOLD) - } // namespace nplb } // namespace starboard diff --git a/starboard/nplb/socket_helpers.h b/starboard/nplb/socket_helpers.h index 783cb68e13dc..56d641f941bf 100644 --- a/starboard/nplb/socket_helpers.h +++ b/starboard/nplb/socket_helpers.h @@ -29,19 +29,6 @@ namespace nplb { const int64_t kSocketTimeout = 200'000; // 200ms -// Returns a valid port number that can be bound to for use in nplb tests. -// This will always return the same port number. -int GetPortNumberForTests(); - -// Creates a TCP/IP server socket (sets Reuse Address option). -SbSocket CreateServerTcpSocket(SbSocketAddressType address_type); - -// Creates a TCP/IP socket bound to all interfaces on the given port. -SbSocket CreateBoundTcpSocket(SbSocketAddressType address_type, int port); - -// Creates a TCP/IP socket listening on all interfaces on the given port. -SbSocket CreateListeningTcpSocket(SbSocketAddressType address_type, int port); - // Waits on the given waiter, and returns the elapsed time in microseconds. int64_t TimedWait(SbSocketWaiter waiter); @@ -105,23 +92,6 @@ static inline bool SocketErrorIn( << "With " #error " = " << error; \ } while (false) -#if !defined(COBALT_BUILD_TYPE_GOLD) -std::string GetSbSocketAddressTypeName( - ::testing::TestParamInfo info); -std::string GetSbSocketAddressTypePairName( - ::testing::TestParamInfo< - std::pair> info); -std::string GetSbSocketAddressTypeFilterPairName( - ::testing::TestParamInfo< - std::pair> info); -std::string GetSbSocketFilterAddressTypePairName( - ::testing::TestParamInfo< - std::pair> info); -std::string GetSbSocketAddressTypeProtocolPairName( - ::testing::TestParamInfo> - info); -#endif // #if !defined(COBALT_BUILD_TYPE_GOLD) - } // namespace nplb } // namespace starboard diff --git a/starboard/nplb/socket_is_connected_and_idle_test.cc b/starboard/nplb/socket_is_connected_and_idle_test.cc deleted file mode 100644 index 2dfb6e89c81b..000000000000 --- a/starboard/nplb/socket_is_connected_and_idle_test.cc +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#if SB_API_VERSION < 17 -#include - -#include "starboard/common/log.h" -#include "starboard/common/socket.h" -#include "starboard/common/time.h" -#include "starboard/nplb/socket_helpers.h" -#include "starboard/thread.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -class SbSocketIsConnectedAndIdleTest - : public ::testing::TestWithParam { - public: - SbSocketAddressType GetAddressType() { return GetParam(); } -}; - -class PairSbSocketIsConnectedAndIdleTest - : public ::testing::TestWithParam< - std::pair> { - public: - SbSocketAddressType GetServerAddressType() { return GetParam().first; } - SbSocketAddressType GetClientAddressType() { return GetParam().second; } -}; - -bool IsNonIdleWithin(SbSocket socket, int64_t timeout) { - int64_t deadline = CurrentMonotonicTime() + timeout; - while (CurrentMonotonicTime() < deadline) { - if (!SbSocketIsConnectedAndIdle(socket)) { - return true; - } - } - return false; -} - -TEST_F(SbSocketIsConnectedAndIdleTest, RainyDayInvalidSocket) { - EXPECT_FALSE(SbSocketIsConnectedAndIdle(kSbSocketInvalid)); -} - -#if SB_API_VERSION < 17 -TEST_P(PairSbSocketIsConnectedAndIdleTest, SunnyDay) { - ConnectedTrio trio = - CreateAndConnect(GetServerAddressType(), GetClientAddressType(), - GetPortNumberForTests(), kSocketTimeout); - ASSERT_TRUE(SbSocketIsValid(trio.server_socket)); - - EXPECT_FALSE(SbSocketIsConnectedAndIdle(trio.listen_socket)); - EXPECT_TRUE(SbSocketIsConnectedAndIdle(trio.server_socket)); - EXPECT_TRUE(SbSocketIsConnectedAndIdle(trio.client_socket)); - - char buf[512] = {0}; - SbSocketSendTo(trio.server_socket, buf, sizeof(buf), NULL); - // It may take some time after this call for trio.client_socket to show up as - // having new data available, even though it is a loopback connection, so we - // give it a grace period. - EXPECT_TRUE(IsNonIdleWithin(trio.client_socket, kSocketTimeout)); - - // Should be the same in the other direction. - SbSocketSendTo(trio.client_socket, buf, sizeof(buf), NULL); - EXPECT_TRUE(IsNonIdleWithin(trio.server_socket, kSocketTimeout)); - - EXPECT_TRUE(SbSocketDestroy(trio.server_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.client_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.listen_socket)); -} - -TEST_P(SbSocketIsConnectedAndIdleTest, SunnyDayNotConnected) { - SbSocket socket = SbSocketCreate(GetAddressType(), kSbSocketProtocolTcp); - ASSERT_TRUE(SbSocketIsValid(socket)); - EXPECT_TRUE(IsNonIdleWithin(socket, kSocketTimeout)); - EXPECT_TRUE(SbSocketDestroy(socket)); -} - -TEST_P(SbSocketIsConnectedAndIdleTest, SunnyDayListeningNotConnected) { - SbSocket server_socket = - CreateListeningTcpSocket(GetAddressType(), GetPortNumberForTests()); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - EXPECT_FALSE(SbSocketIsConnectedAndIdle(server_socket)); - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} -#endif // SB_API_VERSION < 17 - -INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, - SbSocketIsConnectedAndIdleTest, - ::testing::Values(kSbSocketAddressTypeIpv4, - kSbSocketAddressTypeIpv6), - GetSbSocketAddressTypeName); -INSTANTIATE_TEST_CASE_P( - SbSocketAddressTypes, - PairSbSocketIsConnectedAndIdleTest, - ::testing::Values( - std::make_pair(kSbSocketAddressTypeIpv4, kSbSocketAddressTypeIpv4), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv6), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv4)), - GetSbSocketAddressTypePairName); - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_is_connected_test.cc b/starboard/nplb/socket_is_connected_test.cc deleted file mode 100644 index eb78939324b7..000000000000 --- a/starboard/nplb/socket_is_connected_test.cc +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#if SB_API_VERSION < 17 - -#include - -#include "starboard/common/log.h" -#include "starboard/common/socket.h" -#include "starboard/nplb/socket_helpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -class SbSocketIsConnectedTest - : public ::testing::TestWithParam { - public: - SbSocketAddressType GetAddressType() { return GetParam(); } -}; - -class PairSbSocketIsConnectedTest - : public ::testing::TestWithParam< - std::pair> { - public: - SbSocketAddressType GetServerAddressType() { return GetParam().first; } - SbSocketAddressType GetClientAddressType() { return GetParam().second; } -}; - -TEST_F(SbSocketIsConnectedTest, RainyDayInvalidSocket) { - EXPECT_FALSE(SbSocketIsConnected(kSbSocketInvalid)); -} - -TEST_P(PairSbSocketIsConnectedTest, SunnyDay) { - ConnectedTrio trio = - CreateAndConnect(GetServerAddressType(), GetClientAddressType(), - GetPortNumberForTests(), kSocketTimeout); - ASSERT_TRUE(SbSocketIsValid(trio.server_socket)); - EXPECT_FALSE(SbSocketIsConnected(trio.listen_socket)); - EXPECT_TRUE(SbSocketIsConnected(trio.server_socket)); - EXPECT_TRUE(SbSocketIsConnected(trio.client_socket)); - - // This is just to contrast the behavior with SbSocketIsConnectedAndIdle. - char buf[512] = {0}; - SbSocketSendTo(trio.server_socket, buf, sizeof(buf), NULL); - EXPECT_TRUE(SbSocketIsConnected(trio.client_socket)); - SbSocketSendTo(trio.client_socket, buf, sizeof(buf), NULL); - EXPECT_TRUE(SbSocketIsConnected(trio.server_socket)); - - EXPECT_TRUE(SbSocketDestroy(trio.server_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.client_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.listen_socket)); -} - -TEST_P(SbSocketIsConnectedTest, SunnyDayNotConnected) { - SbSocket socket = SbSocketCreate(GetAddressType(), kSbSocketProtocolTcp); - ASSERT_TRUE(SbSocketIsValid(socket)); - EXPECT_FALSE(SbSocketIsConnected(socket)); - EXPECT_TRUE(SbSocketDestroy(socket)); -} - -TEST_P(SbSocketIsConnectedTest, SunnyDayListeningNotConnected) { - SbSocket server_socket = - CreateListeningTcpSocket(GetAddressType(), GetPortNumberForTests()); - ASSERT_TRUE(SbSocketIsValid(server_socket)); - EXPECT_FALSE(SbSocketIsConnected(server_socket)); - EXPECT_TRUE(SbSocketDestroy(server_socket)); -} - -INSTANTIATE_TEST_CASE_P(SbSocketAddressTypes, - SbSocketIsConnectedTest, - ::testing::Values(kSbSocketAddressTypeIpv4, - kSbSocketAddressTypeIpv6), - GetSbSocketAddressTypeName); -INSTANTIATE_TEST_CASE_P( - SbSocketAddressTypes, - PairSbSocketIsConnectedTest, - ::testing::Values( - std::make_pair(kSbSocketAddressTypeIpv4, kSbSocketAddressTypeIpv4), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv6), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv4)), - GetSbSocketAddressTypePairName); - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_receive_from_test.cc b/starboard/nplb/socket_receive_from_test.cc deleted file mode 100644 index 36fa0ec307b4..000000000000 --- a/starboard/nplb/socket_receive_from_test.cc +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2015 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#if SB_API_VERSION < 17 - -#include - -#include "starboard/common/socket.h" -#include "starboard/nplb/socket_helpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace starboard { -namespace nplb { -namespace { - -class PairSbSocketReceiveFromTest - : public ::testing::TestWithParam< - std::pair> { - public: - SbSocketAddressType GetServerAddressType() { return GetParam().first; } - SbSocketAddressType GetClientAddressType() { return GetParam().second; } -}; - -// Transfers data between the two connected local sockets, spinning until |size| -// has been transferred, or an error occurs. -int Transfer(SbSocket receive_socket, - char* out_data, - SbSocket send_socket, - const char* send_data, - int size) { - int send_total = 0; - int receive_total = 0; - while (receive_total < size) { - if (send_total < size) { - int bytes_sent = SbSocketSendTo(send_socket, send_data + send_total, - size - send_total, NULL); - if (bytes_sent < 0) { - if (SbSocketGetLastError(send_socket) != kSbSocketPending) { - return -1; - } - bytes_sent = 0; - } - - send_total += bytes_sent; - } - - int bytes_received = SbSocketReceiveFrom( - receive_socket, out_data + receive_total, size - receive_total, NULL); - if (bytes_received < 0) { - if (SbSocketGetLastError(receive_socket) != kSbSocketPending) { - return -1; - } - bytes_received = 0; - } - - receive_total += bytes_received; - } - - return size; -} - -TEST_P(PairSbSocketReceiveFromTest, SunnyDay) { - const int kBufSize = 256 * 1024; - const int kSockBufSize = kBufSize / 8; - - ConnectedTrio trio = - CreateAndConnect(GetServerAddressType(), GetClientAddressType(), - GetPortNumberForTests(), kSocketTimeout); - ASSERT_TRUE(SbSocketIsValid(trio.server_socket)); - - // Let's set the buffers small to create partial reads and writes. - SbSocketSetReceiveBufferSize(trio.client_socket, kSockBufSize); - SbSocketSetReceiveBufferSize(trio.server_socket, kSockBufSize); - SbSocketSetSendBufferSize(trio.client_socket, kSockBufSize); - SbSocketSetSendBufferSize(trio.server_socket, kSockBufSize); - - // Create the buffers and fill the send buffer with a pattern, the receive - // buffer with zeros. - char* send_buf = new char[kBufSize]; - char* receive_buf = new char[kBufSize]; - for (int i = 0; i < kBufSize; ++i) { - send_buf[i] = static_cast(i); - receive_buf[i] = 0; - } - - // Send from server to client and verify the pattern. - int transferred = Transfer(trio.client_socket, receive_buf, - trio.server_socket, send_buf, kBufSize); - EXPECT_EQ(kBufSize, transferred); - for (int i = 0; i < kBufSize; ++i) { - EXPECT_EQ(send_buf[i], receive_buf[i]) << "Position " << i; - } - - // Try the other way now, first clearing the target buffer again. - for (int i = 0; i < kBufSize; ++i) { - receive_buf[i] = 0; - } - transferred = Transfer(trio.server_socket, receive_buf, trio.client_socket, - send_buf, kBufSize); - EXPECT_EQ(kBufSize, transferred); - for (int i = 0; i < kBufSize; ++i) { - EXPECT_EQ(send_buf[i], receive_buf[i]) << "Position " << i; - } - - EXPECT_TRUE(SbSocketDestroy(trio.server_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.client_socket)); - EXPECT_TRUE(SbSocketDestroy(trio.listen_socket)); - delete[] send_buf; - delete[] receive_buf; -} - -TEST(SbSocketReceiveFromTest, RainyDayInvalidSocket) { - char buf[16]; - int result = SbSocketReceiveFrom(NULL, buf, sizeof(buf), NULL); - EXPECT_EQ(-1, result); -} - -INSTANTIATE_TEST_CASE_P( - SbSocketAddressTypes, - PairSbSocketReceiveFromTest, - ::testing::Values( - std::make_pair(kSbSocketAddressTypeIpv4, kSbSocketAddressTypeIpv4), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv6), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv4)), - GetSbSocketAddressTypePairName); - -} // namespace -} // namespace nplb -} // namespace starboard -#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/socket_wrapper_test.cc b/starboard/nplb/socket_wrapper_test.cc index 361cdd6c4209..fe9a4f0bbf27 100644 --- a/starboard/nplb/socket_wrapper_test.cc +++ b/starboard/nplb/socket_wrapper_test.cc @@ -37,7 +37,7 @@ TEST_P(PairSbSocketWrapperTest, SunnyDay) { std::unique_ptr trio = CreateAndConnectWrapped(GetServerAddressType(), GetClientAddressType(), - GetPortNumberForTests(), kSocketTimeout); + PosixGetPortNumberForTests(), kSocketTimeout); ASSERT_TRUE(trio); ASSERT_TRUE(trio->server_socket); ASSERT_TRUE(trio->server_socket->IsValid()); @@ -82,15 +82,6 @@ TEST_P(PairSbSocketWrapperTest, SunnyDay) { } } -INSTANTIATE_TEST_CASE_P( - SbSocketAddressTypes, - PairSbSocketWrapperTest, - ::testing::Values( - std::make_pair(kSbSocketAddressTypeIpv4, kSbSocketAddressTypeIpv4), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv6), - std::make_pair(kSbSocketAddressTypeIpv6, kSbSocketAddressTypeIpv4)), - GetSbSocketAddressTypePairName); - } // namespace } // namespace nplb } // namespace starboard