summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/accept_bind.cc
diff options
context:
space:
mode:
authorTing-Yu Wang <anivia@google.com>2021-04-12 18:33:22 -0700
committergVisor bot <gvisor-bot@google.com>2021-04-12 18:35:09 -0700
commite5f58e89bbd376469073c749592d0fb0e3b4c6cb (patch)
tree6015e2edf4229f85f8a8d247c791295837416b69 /test/syscalls/linux/accept_bind.cc
parent90900e4f8f6760f1de34e660030a0155cfd6b40a (diff)
Make AsSockAddr() to replace reinterpret_cast<sockaddr*>
It's a common pattern in test code to reinterpret_cast<sockaddr*> from sockaddr_* structs. Make AsSockAddr() for them so code looks better. Note: Why not a wrapper type for `sockaddr_storage` and etc? It's also a common need to have a local in-out variable of socklen_t. Creating a wrapper type may however lead to this wrong code: Wrapper addr; socklen_t addrlen = sizeof(addr); where sizeof(Wrapper) may not equal to sizeof(sockaddr_storage). PiperOrigin-RevId: 368126229
Diffstat (limited to 'test/syscalls/linux/accept_bind.cc')
-rw-r--r--test/syscalls/linux/accept_bind.cc34
1 files changed, 14 insertions, 20 deletions
diff --git a/test/syscalls/linux/accept_bind.cc b/test/syscalls/linux/accept_bind.cc
index 119a1466b..d8fdb5f10 100644
--- a/test/syscalls/linux/accept_bind.cc
+++ b/test/syscalls/linux/accept_bind.cc
@@ -321,8 +321,7 @@ TEST_P(AllSocketPairTest, AcceptValidAddrLen) {
struct sockaddr_un addr = {};
socklen_t addr_len = sizeof(addr);
ASSERT_THAT(
- accepted = accept(sockets->first_fd(),
- reinterpret_cast<struct sockaddr*>(&addr), &addr_len),
+ accepted = accept(sockets->first_fd(), AsSockAddr(&addr), &addr_len),
SyscallSucceeds());
ASSERT_THAT(close(accepted), SyscallSucceeds());
}
@@ -343,8 +342,7 @@ TEST_P(AllSocketPairTest, AcceptNegativeAddrLen) {
// With a negative addr_len, accept returns EINVAL,
struct sockaddr_un addr = {};
socklen_t addr_len = -1;
- ASSERT_THAT(accept(sockets->first_fd(),
- reinterpret_cast<struct sockaddr*>(&addr), &addr_len),
+ ASSERT_THAT(accept(sockets->first_fd(), AsSockAddr(&addr), &addr_len),
SyscallFailsWithErrno(EINVAL));
}
@@ -535,10 +533,9 @@ TEST_P(AllSocketPairTest, UnboundSenderAddr) {
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
- ASSERT_THAT(
- RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_len),
- SyscallSucceedsWithValue(sizeof(i)));
+ ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
+ AsSockAddr(&addr), &addr_len),
+ SyscallSucceedsWithValue(sizeof(i)));
EXPECT_EQ(addr_len, 0);
}
@@ -570,10 +567,9 @@ TEST_P(AllSocketPairTest, BoundSenderAddr) {
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
- ASSERT_THAT(
- RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_len),
- SyscallSucceedsWithValue(sizeof(i)));
+ ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
+ AsSockAddr(&addr), &addr_len),
+ SyscallSucceedsWithValue(sizeof(i)));
EXPECT_EQ(addr_len, sockets->second_addr_len());
EXPECT_EQ(
memcmp(&addr, sockets->second_addr(),
@@ -609,10 +605,9 @@ TEST_P(AllSocketPairTest, BindAfterConnectSenderAddr) {
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
- ASSERT_THAT(
- RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_len),
- SyscallSucceedsWithValue(sizeof(i)));
+ ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
+ AsSockAddr(&addr), &addr_len),
+ SyscallSucceedsWithValue(sizeof(i)));
EXPECT_EQ(addr_len, sockets->second_addr_len());
EXPECT_EQ(
memcmp(&addr, sockets->second_addr(),
@@ -648,10 +643,9 @@ TEST_P(AllSocketPairTest, BindAfterAcceptSenderAddr) {
struct sockaddr_storage addr;
socklen_t addr_len = sizeof(addr);
- ASSERT_THAT(
- RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
- reinterpret_cast<sockaddr*>(&addr), &addr_len),
- SyscallSucceedsWithValue(sizeof(i)));
+ ASSERT_THAT(RetryEINTR(recvfrom)(accepted_fd.get(), &i, sizeof(i), 0,
+ AsSockAddr(&addr), &addr_len),
+ SyscallSucceedsWithValue(sizeof(i)));
EXPECT_EQ(addr_len, sockets->second_addr_len());
EXPECT_EQ(
memcmp(&addr, sockets->second_addr(),