Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Posix tests for sockets (#4201) #4262

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/posix_compliance/posix_socket_accept_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST(PosixSocketAcceptTest, RainyDayNoConnection) {
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());

result = bind(socket_listen_fd, reinterpret_cast<sockaddr*>(&address),
sizeof(sockaddr));
Expand Down Expand Up @@ -132,7 +132,7 @@ TEST(PosixSocketAcceptTest, RainyDayNotListening) {
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());
EXPECT_TRUE(result == 0);
if (result != 0) {
close(socket_fd);
Expand Down
18 changes: 9 additions & 9 deletions starboard/nplb/posix_compliance/posix_socket_bind_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,15 +51,15 @@ 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<sockaddr*>(&client_address),
sizeof(sockaddr_in)) == 0);

// Even though that failed, binding the same socket now with the server
// 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<sockaddr*>(&server_address),
sizeof(sockaddr_in)) == 0);
EXPECT_TRUE(close(socket_fd) == 0);
Expand All @@ -84,7 +84,7 @@ TEST(PosixSocketBindTest, SunnyDayLocalInterface) {
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());

int socket_domain = AF_INET;
int socket_type = SOCK_STREAM;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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<sockaddr*>(&address),
sizeof(sockaddr_in)) == 0);
Expand Down Expand Up @@ -222,15 +222,15 @@ 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<sockaddr*>(&client_address),
sizeof(sockaddr_in)) == 0);

// Even though that failed, binding the same socket now with the server
// 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<sockaddr*>(&server_address),
sizeof(sockaddr_in)) == 0);
EXPECT_TRUE(close(socket_fd) == 0);
Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/posix_compliance/posix_socket_connect_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
74 changes: 71 additions & 3 deletions starboard/nplb/posix_compliance/posix_socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sched.h>

#include "starboard/nplb/posix_compliance/posix_socket_helpers.h"

#include <sched.h>
#include <sys/socket.h>

#include "starboard/shared/posix/handle_eintr.h"
#include "starboard/thread.h"

namespace starboard {
Expand Down Expand Up @@ -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<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());

result = bind(*listen_socket_fd, reinterpret_cast<struct sockaddr*>(&address),
sizeof(struct sockaddr_in));
Expand Down Expand Up @@ -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<sockaddr*>(&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<socklen_t>(sizeof(addr_in));
int local_add_result =
getsockname(socket_fd, reinterpret_cast<sockaddr*>(&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,
Expand Down
13 changes: 7 additions & 6 deletions starboard/nplb/posix_compliance/posix_socket_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST(PosixSocketListenTest, SunnyDayUnbound) {
EXPECT_TRUE(
PosixGetLocalAddressIPv4(reinterpret_cast<sockaddr*>(&address)) == 0 ||
PosixGetLocalAddressIPv6(reinterpret_cast<sockaddr*>(&address)) == 0);
address.sin6_port = htons(GetPortNumberForTests());
address.sin6_port = htons(PosixGetPortNumberForTests());

result =
bind(socket_fd, reinterpret_cast<sockaddr*>(&address), sizeof(sockaddr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions starboard/nplb/posix_compliance/posix_socket_send_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -172,7 +172,7 @@ TEST(PosixSocketSendTest, RainyDaySendToSocketConnectionReset) {
// create listen socket, bind and listen on <port>
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) {
Expand Down
10 changes: 5 additions & 5 deletions starboard/nplb/posix_compliance/posix_socket_sendto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -174,9 +174,9 @@ TEST(PosixSocketSendtoTest, RainyDaySendToSocketConnectionReset) {

// create listen socket, bind and listen on <port>
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading