summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorIan Gudger <igudger@google.com>2019-02-08 18:22:31 -0800
committerShentubot <shentubot@google.com>2019-02-08 18:23:38 -0800
commit85d53d81d9f544277e3e86f83558cbc008ff7adb (patch)
tree17253850e3288c6e99f49ad6d2da1aba9df59582 /test
parentf17692d8074787d058dc33cc95587b15dba3b161 (diff)
Use socket address functions in unbound IPv4 UDP tests.
Also switch to the correct casing style for local address variables. PiperOrigin-RevId: 233161128 Change-Id: Ibc4a10a5d3ab04c40907aa885e8237b255c65a79
Diffstat (limited to 'test')
-rw-r--r--test/syscalls/linux/socket_inet_loopback.cc61
-rw-r--r--test/syscalls/linux/socket_ipv4_udp_unbound.cc345
-rw-r--r--test/syscalls/linux/socket_test_util.cc51
-rw-r--r--test/syscalls/linux/socket_test_util.h17
4 files changed, 234 insertions, 240 deletions
diff --git a/test/syscalls/linux/socket_inet_loopback.cc b/test/syscalls/linux/socket_inet_loopback.cc
index d83fb1543..4195e402c 100644
--- a/test/syscalls/linux/socket_inet_loopback.cc
+++ b/test/syscalls/linux/socket_inet_loopback.cc
@@ -69,67 +69,6 @@ PosixError SetAddrPort(int family, sockaddr_storage* addr, uint16_t port) {
}
}
-struct TestAddress {
- std::string description;
- sockaddr_storage addr;
- socklen_t addr_len;
-
- int family() const { return addr.ss_family; }
- explicit TestAddress(std::string description = "")
- : description(std::move(description)), addr(), addr_len() {}
-};
-
-TestAddress V4Any() {
- TestAddress t("V4Any");
- t.addr.ss_family = AF_INET;
- t.addr_len = sizeof(sockaddr_in);
- reinterpret_cast<sockaddr_in*>(&t.addr)->sin_addr.s_addr = htonl(INADDR_ANY);
- return t;
-}
-
-TestAddress V4Loopback() {
- TestAddress t("V4Loopback");
- t.addr.ss_family = AF_INET;
- t.addr_len = sizeof(sockaddr_in);
- reinterpret_cast<sockaddr_in*>(&t.addr)->sin_addr.s_addr =
- htonl(INADDR_LOOPBACK);
- return t;
-}
-
-TestAddress V4MappedAny() {
- TestAddress t("V4MappedAny");
- t.addr.ss_family = AF_INET6;
- t.addr_len = sizeof(sockaddr_in6);
- inet_pton(AF_INET6, "::ffff:0.0.0.0",
- reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr.s6_addr);
- return t;
-}
-
-TestAddress V4MappedLoopback() {
- TestAddress t("V4MappedLoopback");
- t.addr.ss_family = AF_INET6;
- t.addr_len = sizeof(sockaddr_in6);
- inet_pton(AF_INET6, "::ffff:127.0.0.1",
- reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr.s6_addr);
- return t;
-}
-
-TestAddress V6Any() {
- TestAddress t("V6Any");
- t.addr.ss_family = AF_INET6;
- t.addr_len = sizeof(sockaddr_in6);
- reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr = in6addr_any;
- return t;
-}
-
-TestAddress V6Loopback() {
- TestAddress t("V6Loopback");
- t.addr.ss_family = AF_INET6;
- t.addr_len = sizeof(sockaddr_in6);
- reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr = in6addr_loopback;
- return t;
-}
-
struct TestParam {
TestAddress listener;
TestAddress connector;
diff --git a/test/syscalls/linux/socket_ipv4_udp_unbound.cc b/test/syscalls/linux/socket_ipv4_udp_unbound.cc
index 1b47139e4..4058324a2 100644
--- a/test/syscalls/linux/socket_ipv4_udp_unbound.cc
+++ b/test/syscalls/linux/socket_ipv4_udp_unbound.cc
@@ -29,6 +29,17 @@
namespace gvisor {
namespace testing {
+constexpr char kMulticastAddress[] = "224.0.2.1";
+
+TestAddress V4Multicast() {
+ TestAddress t("V4Multicast");
+ t.addr.ss_family = AF_INET;
+ t.addr_len = sizeof(sockaddr_in);
+ reinterpret_cast<sockaddr_in*>(&t.addr)->sin_addr.s_addr =
+ inet_addr(kMulticastAddress);
+ return t;
+}
+
// Check that packets are not received without a group memebership. Default send
// interface configured by bind.
TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackNoGroup) {
@@ -36,41 +47,37 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackNoGroup) {
// Bind the first FD to the loopback. This is an alternative to
// IP_MULTICAST_IF for setting the default send interface.
- sockaddr_in senderAddr = {};
- senderAddr.sin_family = AF_INET;
- senderAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ auto sender_addr = V4Loopback();
EXPECT_THAT(
- bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&senderAddr),
- sizeof(senderAddr)),
+ bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&sender_addr.addr),
+ sender_addr.addr_len),
SyscallSucceeds());
// Bind the second FD to the v4 any address. If multicast worked like unicast,
// this would ensure that we get the packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Send the multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallSucceedsWithValue(sizeof(send_buf)));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallSucceedsWithValue(sizeof(send_buf)));
// Check that we did not receive the multicast packet.
char recv_buf[sizeof(send_buf)] = {};
@@ -86,39 +93,37 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackAddrNoDefaultSendIf) {
// Bind the second FD to the v4 any address to ensure that we can receive any
// unicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register to receive multicast packets.
ip_mreq group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallFailsWithErrno(ENETUNREACH));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallFailsWithErrno(ENETUNREACH));
}
// Check that not setting a default send interface prevents multicast packets
@@ -128,39 +133,37 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackNicNoDefaultSendIf) {
// Bind the second FD to the v4 any address to ensure that we can receive any
// unicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register to receive multicast packets.
ip_mreqn group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_ifindex = ASSERT_NO_ERRNO_AND_VALUE(InterfaceIndex("lo"));
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallFailsWithErrno(ENETUNREACH));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallFailsWithErrno(ENETUNREACH));
}
// Check that multicast works when the default send interface is configured by
@@ -170,49 +173,45 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackAddr) {
// Bind the first FD to the loopback. This is an alternative to
// IP_MULTICAST_IF for setting the default send interface.
- sockaddr_in senderAddr = {};
- senderAddr.sin_family = AF_INET;
- senderAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ auto sender_addr = V4Loopback();
EXPECT_THAT(
- bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&senderAddr),
- sizeof(senderAddr)),
+ bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&sender_addr.addr),
+ sender_addr.addr_len),
SyscallSucceeds());
// Bind the second FD to the v4 any address to ensure that we can receive the
// multicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register to receive multicast packets.
ip_mreq group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallSucceedsWithValue(sizeof(send_buf)));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallSucceedsWithValue(sizeof(send_buf)));
// Check that we received the multicast packet.
char recv_buf[sizeof(send_buf)] = {};
@@ -230,49 +229,45 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastLoopbackNic) {
// Bind the first FD to the loopback. This is an alternative to
// IP_MULTICAST_IF for setting the default send interface.
- sockaddr_in senderAddr = {};
- senderAddr.sin_family = AF_INET;
- senderAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ auto sender_addr = V4Loopback();
EXPECT_THAT(
- bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&senderAddr),
- sizeof(senderAddr)),
+ bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&sender_addr.addr),
+ sender_addr.addr_len),
SyscallSucceeds());
// Bind the second FD to the v4 any address to ensure that we can receive the
// multicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register to receive multicast packets.
ip_mreqn group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_ifindex = ASSERT_NO_ERRNO_AND_VALUE(InterfaceIndex("lo"));
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallSucceedsWithValue(sizeof(send_buf)));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallSucceedsWithValue(sizeof(send_buf)));
// Check that we received the multicast packet.
char recv_buf[sizeof(send_buf)] = {};
@@ -289,7 +284,7 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastInvalidDrop) {
// Unregister from a membership that we didn't have.
ip_mreq group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_IP, IP_DROP_MEMBERSHIP,
&group, sizeof(group)),
@@ -304,33 +299,29 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastDropAddr) {
// Bind the first FD to the loopback. This is an alternative to
// IP_MULTICAST_IF for setting the default send interface.
- sockaddr_in senderAddr = {};
- senderAddr.sin_family = AF_INET;
- senderAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ auto sender_addr = V4Loopback();
EXPECT_THAT(
- bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&senderAddr),
- sizeof(senderAddr)),
+ bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&sender_addr.addr),
+ sender_addr.addr_len),
SyscallSucceeds());
// Bind the second FD to the v4 any address to ensure that we can receive the
// multicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register and unregister to receive multicast packets.
ip_mreq group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_interface.s_addr = htonl(INADDR_LOOPBACK);
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
@@ -340,16 +331,16 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastDropAddr) {
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallSucceedsWithValue(sizeof(send_buf)));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallSucceedsWithValue(sizeof(send_buf)));
// Check that we did not receive the multicast packet.
char recv_buf[sizeof(send_buf)] = {};
@@ -366,33 +357,29 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastDropNic) {
// Bind the first FD to the loopback. This is an alternative to
// IP_MULTICAST_IF for setting the default send interface.
- sockaddr_in senderAddr = {};
- senderAddr.sin_family = AF_INET;
- senderAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ auto sender_addr = V4Loopback();
EXPECT_THAT(
- bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&senderAddr),
- sizeof(senderAddr)),
+ bind(sockets->first_fd(), reinterpret_cast<sockaddr*>(&sender_addr.addr),
+ sender_addr.addr_len),
SyscallSucceeds());
// Bind the second FD to the v4 any address to ensure that we can receive the
// multicast packet.
- sockaddr_in receiverAddr = {};
- receiverAddr.sin_family = AF_INET;
- receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- EXPECT_THAT(
- bind(sockets->second_fd(), reinterpret_cast<sockaddr*>(&receiverAddr),
- sizeof(receiverAddr)),
- SyscallSucceeds());
- socklen_t receiverAddrLen = sizeof(receiverAddr);
- EXPECT_THAT(
- getsockname(sockets->second_fd(),
- reinterpret_cast<sockaddr*>(&receiverAddr), &receiverAddrLen),
- SyscallSucceeds());
- EXPECT_EQ(receiverAddrLen, sizeof(receiverAddr));
+ auto receiver_addr = V4Any();
+ EXPECT_THAT(bind(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ receiver_addr.addr_len),
+ SyscallSucceeds());
+ socklen_t receiver_addr_len = receiver_addr.addr_len;
+ EXPECT_THAT(getsockname(sockets->second_fd(),
+ reinterpret_cast<sockaddr*>(&receiver_addr.addr),
+ &receiver_addr_len),
+ SyscallSucceeds());
+ EXPECT_EQ(receiver_addr_len, receiver_addr.addr_len);
// Register and unregister to receive multicast packets.
ip_mreqn group = {};
- group.imr_multiaddr.s_addr = inet_addr("224.0.2.1");
+ group.imr_multiaddr.s_addr = inet_addr(kMulticastAddress);
group.imr_ifindex = ASSERT_NO_ERRNO_AND_VALUE(InterfaceIndex("lo"));
EXPECT_THAT(setsockopt(sockets->second_fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP,
&group, sizeof(group)),
@@ -402,16 +389,16 @@ TEST_P(IPv4UDPUnboundSocketPairTest, IpMulticastDropNic) {
SyscallSucceeds());
// Send a multicast packet.
- sockaddr_in sendAddr = {};
- sendAddr.sin_family = AF_INET;
- sendAddr.sin_port = receiverAddr.sin_port;
- sendAddr.sin_addr.s_addr = inet_addr("224.0.2.1");
+ auto send_addr = V4Multicast();
+ reinterpret_cast<sockaddr_in*>(&send_addr.addr)->sin_port =
+ reinterpret_cast<sockaddr_in*>(&receiver_addr.addr)->sin_port;
char send_buf[200];
RandomizeBuffer(send_buf, sizeof(send_buf));
- EXPECT_THAT(RetryEINTR(sendto)(
- sockets->first_fd(), send_buf, sizeof(send_buf), 0,
- reinterpret_cast<sockaddr*>(&sendAddr), sizeof(sendAddr)),
- SyscallSucceedsWithValue(sizeof(send_buf)));
+ EXPECT_THAT(
+ RetryEINTR(sendto)(sockets->first_fd(), send_buf, sizeof(send_buf), 0,
+ reinterpret_cast<sockaddr*>(&send_addr.addr),
+ send_addr.addr_len),
+ SyscallSucceedsWithValue(sizeof(send_buf)));
// Check that we did not receive the multicast packet.
char recv_buf[sizeof(send_buf)] = {};
diff --git a/test/syscalls/linux/socket_test_util.cc b/test/syscalls/linux/socket_test_util.cc
index 49b8c583f..8d19f79ac 100644
--- a/test/syscalls/linux/socket_test_util.cc
+++ b/test/syscalls/linux/socket_test_util.cc
@@ -680,5 +680,56 @@ void RecvNoData(int sock) {
SyscallFailsWithErrno(EAGAIN));
}
+TestAddress V4Any() {
+ TestAddress t("V4Any");
+ t.addr.ss_family = AF_INET;
+ t.addr_len = sizeof(sockaddr_in);
+ reinterpret_cast<sockaddr_in*>(&t.addr)->sin_addr.s_addr = htonl(INADDR_ANY);
+ return t;
+}
+
+TestAddress V4Loopback() {
+ TestAddress t("V4Loopback");
+ t.addr.ss_family = AF_INET;
+ t.addr_len = sizeof(sockaddr_in);
+ reinterpret_cast<sockaddr_in*>(&t.addr)->sin_addr.s_addr =
+ htonl(INADDR_LOOPBACK);
+ return t;
+}
+
+TestAddress V4MappedAny() {
+ TestAddress t("V4MappedAny");
+ t.addr.ss_family = AF_INET6;
+ t.addr_len = sizeof(sockaddr_in6);
+ inet_pton(AF_INET6, "::ffff:0.0.0.0",
+ reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr.s6_addr);
+ return t;
+}
+
+TestAddress V4MappedLoopback() {
+ TestAddress t("V4MappedLoopback");
+ t.addr.ss_family = AF_INET6;
+ t.addr_len = sizeof(sockaddr_in6);
+ inet_pton(AF_INET6, "::ffff:127.0.0.1",
+ reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr.s6_addr);
+ return t;
+}
+
+TestAddress V6Any() {
+ TestAddress t("V6Any");
+ t.addr.ss_family = AF_INET6;
+ t.addr_len = sizeof(sockaddr_in6);
+ reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr = in6addr_any;
+ return t;
+}
+
+TestAddress V6Loopback() {
+ TestAddress t("V6Loopback");
+ t.addr.ss_family = AF_INET6;
+ t.addr_len = sizeof(sockaddr_in6);
+ reinterpret_cast<sockaddr_in6*>(&t.addr)->sin6_addr = in6addr_loopback;
+ return t;
+}
+
} // namespace testing
} // namespace gvisor
diff --git a/test/syscalls/linux/socket_test_util.h b/test/syscalls/linux/socket_test_util.h
index 826374dc6..906b3e929 100644
--- a/test/syscalls/linux/socket_test_util.h
+++ b/test/syscalls/linux/socket_test_util.h
@@ -449,6 +449,23 @@ void RecvNoData(int sock);
// sockets.
using AllSocketPairTest = SocketPairTest;
+struct TestAddress {
+ std::string description;
+ sockaddr_storage addr;
+ socklen_t addr_len;
+
+ int family() const { return addr.ss_family; }
+ explicit TestAddress(std::string description = "")
+ : description(std::move(description)), addr(), addr_len() {}
+};
+
+TestAddress V4Any();
+TestAddress V4Loopback();
+TestAddress V4MappedAny();
+TestAddress V4MappedLoopback();
+TestAddress V6Any();
+TestAddress V6Loopback();
+
} // namespace testing
} // namespace gvisor