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 compliance test for setsockopt. #2449

Merged
merged 5 commits into from
Feb 27, 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
6 changes: 3 additions & 3 deletions starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ since the version previous to it.

## Version 16

### Added standard POSIX socket/close APIs.
The standard API socket can be used from <sys/socket.h> and
close can be called to close the socket by including <unistd.h>.
### Added standard POSIX socket/close/setsockopt APIs.
The standard API `socket`, `setsockopt` can be used from <sys/socket.h> and
`close` can be called to close the socket by including <unistd.h>.

### Changed InstallCrashpadHandler API
This API doesn't support the option to start the crashpad handler at the
Expand Down
1 change: 1 addition & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(munmap);
REGISTER_SYMBOL(posix_memalign);
REGISTER_SYMBOL(realloc);
REGISTER_SYMBOL(setsockopt);
REGISTER_SYMBOL(socket);
REGISTER_SYMBOL(snprintf);
REGISTER_SYMBOL(sprintf);
Expand Down
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_mutex_create_test.cc",
"posix_compliance/posix_mutex_destroy_test.cc",
"posix_compliance/posix_socket_create_test.cc",
"posix_compliance/posix_socket_set_options_test.cc",
"posix_compliance/posix_string_compare_no_case_n_test.cc",
"posix_compliance/posix_string_compare_no_case_test.cc",
"posix_compliance/posix_string_format_test.cc",
Expand Down
130 changes: 130 additions & 0 deletions starboard/nplb/posix_compliance/posix_socket_set_options_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2024 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 >= 16
haozheng-cobalt marked this conversation as resolved.
Show resolved Hide resolved

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <unistd.h>

#include "starboard/configuration.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

class PosixSocketSetOptionsTest : public ::testing::TestWithParam<int> {
public:
int GetSocketDomain() { return GetParam(); }
};

TEST_P(PosixSocketSetOptionsTest, TryThemAllTCP) {
int socket_fd = socket(GetSocketDomain(), SOCK_STREAM, IPPROTO_TCP);

int true_val = 1;
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &true_val,
sizeof(true_val)),
0);

int value = 16 * 1024;
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)),
0);
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)),
0);

// Test TCP keepalive option.
int period_seconds = static_cast<int>(30'000'000 / 1'000'000);
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_KEEPALIVE, &true_val,
sizeof(true_val)),
0);
#if defined(__APPLE__)
// In tvOS, TCP_KEEPIDLE and SOL_TCP are not available.
// For reference:
// https://stackoverflow.com/questions/15860127/how-to-configure-tcp-keepalive-under-mac-os-x
EXPECT_EQ(setsockopt(socket_fd, IPPROTO_TCP, TCP_KEEPALIVE, &period_seconds,
sizeof(period_seconds)),
0);
#elif !defined(_WIN32)
// In Windows, the SOL_TCP and TCP_KEEPIDLE options are not available.
haozheng-cobalt marked this conversation as resolved.
Show resolved Hide resolved
// For reference:
// https://stackoverflow.com/questions/8176821/how-to-set-the-keep-alive-interval-for-winsock
EXPECT_EQ(setsockopt(socket_fd, SOL_TCP, TCP_KEEPIDLE, &period_seconds,
sizeof(period_seconds)),
0);
EXPECT_EQ(setsockopt(socket_fd, SOL_TCP, TCP_KEEPINTVL, &period_seconds,
sizeof(period_seconds)),
0);
#endif

EXPECT_EQ(setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &true_val,
sizeof(true_val)),
0);

close(socket_fd);
}

TEST_P(PosixSocketSetOptionsTest, TryThemAllUDP) {
int socket_fd = socket(GetSocketDomain(), SOCK_DGRAM, IPPROTO_UDP);

int true_val = 1;
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &true_val,
sizeof(true_val)),
0);
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &true_val,
sizeof(true_val)),
0);

int value = 16 * 1024;
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)),
0);
EXPECT_EQ(setsockopt(socket_fd, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)),
0);

close(socket_fd);
}

TEST_P(PosixSocketSetOptionsTest, RainyDayInvalidSocket) {
int true_val = 1;
EXPECT_EQ(
setsockopt(-1, SOL_SOCKET, SO_REUSEADDR, &true_val, sizeof(true_val)),
-1);

int value = 16 * 1024;
EXPECT_EQ(setsockopt(-1, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)), -1);
EXPECT_EQ(setsockopt(-1, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)), -1);

EXPECT_EQ(
setsockopt(-1, SOL_SOCKET, SO_KEEPALIVE, &true_val, sizeof(true_val)),
-1);

EXPECT_EQ(
setsockopt(-1, IPPROTO_TCP, TCP_NODELAY, &true_val, sizeof(true_val)),
-1);
}

#if SB_HAS(IPV6)
INSTANTIATE_TEST_SUITE_P(PosixSocketAddressTypes,
PosixSocketSetOptionsTest,
::testing::Values(AF_INET, AF_INET6));
#else
INSTANTIATE_TEST_SUITE_P(PosixSocketAddressTypes,
PosixSocketSetOptionsTest,
::testing::Values(AF_INET));
#endif

} // namespace
} // namespace nplb
} // namespace starboard
#endif // SB_API_VERSION >= 16
20 changes: 20 additions & 0 deletions starboard/shared/win32/posix_emu/include/netinet/tcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 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.

#ifndef STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_NETINET_TCP_H_
#define STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_NETINET_TCP_H_

#include <sys/socket.h>

#endif // STARBOARD_SHARED_WIN32_POSIX_EMU_INCLUDE_NETINET_TCP_H_
7 changes: 7 additions & 0 deletions starboard/shared/win32/posix_emu/include/sys/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ extern "C" {
int sb_socket(int domain, int type, int protocol);
#define socket sb_socket

int sb_setsockopt(int socket,
int level,
int option_name,
const void* option_value,
int option_len);
#define setsockopt sb_setsockopt

#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 27 additions & 0 deletions starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ int sb_socket(int domain, int type, int protocol) {
SOCKET socket_handle = socket(domain, type, protocol);
if (socket_handle == INVALID_SOCKET) {
// TODO: update errno with file operation error
int last_error = WSAGetLastError();
SB_DLOG(ERROR) << "Failed to create socket, last_error = " << last_error;
return -1;
}

Expand All @@ -105,4 +107,29 @@ int close(int fd) {
return _close(fd);
}

int sb_setsockopt(int socket,
int level,
int option_name,
const void* option_value,
int option_len) {
SOCKET socket_handle = handle_db_get(socket, false);
haozheng-cobalt marked this conversation as resolved.
Show resolved Hide resolved

if (socket_handle == INVALID_SOCKET) {
return -1;
}

int result =
setsockopt(socket_handle, level, option_name,
reinterpret_cast<const char*>(option_value), option_len);
// TODO(b/321999529): Windows returns SOCKET_ERROR on failure. The specific
// error code can be retrieved by calling WSAGetLastError(), and Posix returns
// -1 on failure and sets errno to the error’s value.
if (result == SOCKET_ERROR) {
int last_error = WSAGetLastError();
SB_DLOG(ERROR) << "Failed to set " << option_name << " on socket " << socket
<< ", last_error = " << last_error;
return -1;
}
return 0;
}
} // extern "C"
1 change: 1 addition & 0 deletions starboard/tools/api_leak_detector/api_leak_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
'strcasecmp',
'strncasecmp',
'socket',
'setsockopt',
'time',
'mmap',
'munmap',
Expand Down
Loading