Fix more UDP port number rollovers

Some of the UDP tests bind a socket with port 0, read the bound port number,
then connect using 1+bound port as the port number. This can roll over if the
originally bound port is 2^16-1, which makes for test cases that are very
infrequently flaky on systems with an ephemeral port range that includes
2^16-1. Fix this by using bound port-1, which should never underflow since the
bound port isn't 0, and should always be in a usable range, even if it is
below the ephemeral port range.

PiperOrigin-RevId: 496996446
This commit is contained in:
Alex Konradi
2022-12-21 13:43:37 -08:00
committed by gVisor bot
parent 611e6e1247
commit fdeab8650a
+5 -5
View File
@@ -585,7 +585,7 @@ TEST_P(UdpSocketTest, DisconnectAfterBind) {
// Bind to the next port above bind_.
struct sockaddr_storage addr_storage = InetLoopbackAddr();
struct sockaddr* addr = AsSockAddr(&addr_storage);
SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1);
SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1);
ASSERT_NO_ERRNO(BindSocket(sock_.get(), addr));
// Connect the socket.
@@ -676,7 +676,7 @@ TEST_P(UdpSocketTest, BindToAnyConnnectToLocalhost) {
struct sockaddr_storage addr_storage = InetLoopbackAddr();
struct sockaddr* addr = AsSockAddr(&addr_storage);
SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1);
SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1);
socklen_t addrlen = sizeof(addr);
// Connect the socket.
@@ -703,7 +703,7 @@ TEST_P(UdpSocketTest, DisconnectAfterBindToAny) {
struct sockaddr_storage any_storage = InetAnyAddr();
struct sockaddr* any = AsSockAddr(&any_storage);
SetPort(&any_storage, *Port(&bind_addr_storage_) + 1);
SetPort(&any_storage, *Port(&bind_addr_storage_) - 1);
ASSERT_NO_ERRNO(BindSocket(sock_.get(), any));
@@ -731,7 +731,7 @@ TEST_P(UdpSocketTest, Disconnect) {
struct sockaddr_storage any_storage = InetAnyAddr();
struct sockaddr* any = AsSockAddr(&any_storage);
SetPort(&any_storage, *Port(&bind_addr_storage_) + 1);
SetPort(&any_storage, *Port(&bind_addr_storage_) - 1);
ASSERT_NO_ERRNO(BindSocket(sock_.get(), any));
for (int i = 0; i < 2; i++) {
@@ -777,7 +777,7 @@ TEST_P(UdpSocketTest, SendToAddressOtherThanConnected) {
struct sockaddr_storage addr_storage = InetAnyAddr();
struct sockaddr* addr = AsSockAddr(&addr_storage);
SetPort(&addr_storage, *Port(&bind_addr_storage_) + 1);
SetPort(&addr_storage, *Port(&bind_addr_storage_) - 1);
ASSERT_THAT(connect(sock_.get(), bind_addr_, addrlen_), SyscallSucceeds());