diff options
author | Ian Gudger <igudger@google.com> | 2019-02-08 18:22:31 -0800 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2019-02-08 18:23:38 -0800 |
commit | 85d53d81d9f544277e3e86f83558cbc008ff7adb (patch) | |
tree | 17253850e3288c6e99f49ad6d2da1aba9df59582 /test/syscalls/linux/socket_ipv4_udp_unbound.cc | |
parent | f17692d8074787d058dc33cc95587b15dba3b161 (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/syscalls/linux/socket_ipv4_udp_unbound.cc')
-rw-r--r-- | test/syscalls/linux/socket_ipv4_udp_unbound.cc | 345 |
1 files changed, 166 insertions, 179 deletions
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)] = {}; |