Skip to content

Commit

Permalink
Clean up socket_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
madhurajayaraman committed Oct 4, 2024
1 parent c38983e commit 100bdab
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 219 deletions.
6 changes: 0 additions & 6 deletions starboard/nplb/socket_get_interface_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions starboard/nplb/socket_get_local_address_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,6 @@ TEST_P(PairSbSocketGetLocalAddressTest, SunnyDayConnected) {
EXPECT_TRUE(SbSocketDestroy(trio.listen_socket));
}

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
} // namespace starboard
Expand Down
160 changes: 0 additions & 160 deletions starboard/nplb/socket_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,84 +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;
SB_DLOG(INFO) << "Socket fn port : " << socket_address.port;

// Clean up the socket.
result = SbSocketDestroy(socket);
SB_DCHECK(result);
}
} // namespace

int GetPortNumberForTests() {
pthread_once(&valid_port_once_control, &InitializePortNumberForTests);
return port_number_for_tests;
}

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);
SB_DLOG(INFO) << "CreateBoundTCPSocket Port: " << port;
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) {
SB_DLOG(INFO) << "CreateListeningTcpSocket Port: " << 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();
Expand All @@ -125,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<SbSocketAddressType> info) {
return FormatString("type_%s", SbSocketAddressTypeName(info.param));
}
std::string GetSbSocketAddressTypePairName(
::testing::TestParamInfo<
std::pair<SbSocketAddressType, SbSocketAddressType>> info) {
return FormatString("type_%s_type_%s",
SbSocketAddressTypeName(info.param.first),
SbSocketAddressTypeName(info.param.second));
}

std::string GetSbSocketAddressTypeFilterPairName(
::testing::TestParamInfo<
std::pair<SbSocketAddressType, SbSocketResolveFilter>> info) {
return FormatString("type_%s_filter_%s",
SbSocketAddressTypeName(info.param.first),
SbSocketAddressFilterName(info.param.second));
}

std::string GetSbSocketFilterAddressTypePairName(
::testing::TestParamInfo<
std::pair<SbSocketResolveFilter, SbSocketAddressType>> info) {
return FormatString("filter_%s_type_%s",
SbSocketAddressFilterName(info.param.first),
SbSocketAddressTypeName(info.param.second));
}

std::string GetSbSocketAddressTypeProtocolPairName(
::testing::TestParamInfo<std::pair<SbSocketAddressType, SbSocketProtocol>>
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
30 changes: 0 additions & 30 deletions starboard/nplb/socket_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -105,23 +92,6 @@ static inline bool SocketErrorIn(
<< "With " #error " = " << error; \
} while (false)

#if !defined(COBALT_BUILD_TYPE_GOLD)
std::string GetSbSocketAddressTypeName(
::testing::TestParamInfo<SbSocketAddressType> info);
std::string GetSbSocketAddressTypePairName(
::testing::TestParamInfo<
std::pair<SbSocketAddressType, SbSocketAddressType>> info);
std::string GetSbSocketAddressTypeFilterPairName(
::testing::TestParamInfo<
std::pair<SbSocketAddressType, SbSocketResolveFilter>> info);
std::string GetSbSocketFilterAddressTypePairName(
::testing::TestParamInfo<
std::pair<SbSocketResolveFilter, SbSocketAddressType>> info);
std::string GetSbSocketAddressTypeProtocolPairName(
::testing::TestParamInfo<std::pair<SbSocketAddressType, SbSocketProtocol>>
info);
#endif // #if !defined(COBALT_BUILD_TYPE_GOLD)

} // namespace nplb
} // namespace starboard

Expand Down
9 changes: 0 additions & 9 deletions starboard/nplb/socket_wrapper_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 100bdab

Please sign in to comment.