diff options
author | Ting-Yu Wang <anivia@google.com> | 2021-04-12 18:33:22 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-04-12 18:35:09 -0700 |
commit | e5f58e89bbd376469073c749592d0fb0e3b4c6cb (patch) | |
tree | 6015e2edf4229f85f8a8d247c791295837416b69 /test/syscalls/linux/udp_bind.cc | |
parent | 90900e4f8f6760f1de34e660030a0155cfd6b40a (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/udp_bind.cc')
-rw-r--r-- | test/syscalls/linux/udp_bind.cc | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/test/syscalls/linux/udp_bind.cc b/test/syscalls/linux/udp_bind.cc index 6d92bdbeb..f68d78aa2 100644 --- a/test/syscalls/linux/udp_bind.cc +++ b/test/syscalls/linux/udp_bind.cc @@ -83,27 +83,24 @@ TEST_P(SendtoTest, Sendto) { ASSERT_NO_ERRNO_AND_VALUE(Socket(param.recv_domain, SOCK_DGRAM, 0)); if (param.send_addr_len > 0) { - ASSERT_THAT(bind(s1.get(), reinterpret_cast<sockaddr*>(¶m.send_addr), - param.send_addr_len), - SyscallSucceeds()); + ASSERT_THAT( + bind(s1.get(), AsSockAddr(¶m.send_addr), param.send_addr_len), + SyscallSucceeds()); } if (param.connect_addr_len > 0) { - ASSERT_THAT( - connect(s1.get(), reinterpret_cast<sockaddr*>(¶m.connect_addr), - param.connect_addr_len), - SyscallSucceeds()); + ASSERT_THAT(connect(s1.get(), AsSockAddr(¶m.connect_addr), + param.connect_addr_len), + SyscallSucceeds()); } - ASSERT_THAT(bind(s2.get(), reinterpret_cast<sockaddr*>(¶m.recv_addr), - param.recv_addr_len), + ASSERT_THAT(bind(s2.get(), AsSockAddr(¶m.recv_addr), param.recv_addr_len), SyscallSucceeds()); struct sockaddr_storage real_recv_addr = {}; socklen_t real_recv_addr_len = param.recv_addr_len; ASSERT_THAT( - getsockname(s2.get(), reinterpret_cast<sockaddr*>(&real_recv_addr), - &real_recv_addr_len), + getsockname(s2.get(), AsSockAddr(&real_recv_addr), &real_recv_addr_len), SyscallSucceeds()); ASSERT_EQ(real_recv_addr_len, param.recv_addr_len); @@ -116,23 +113,22 @@ TEST_P(SendtoTest, Sendto) { char buf[20] = {}; if (!param.sendto_errnos.empty()) { - ASSERT_THAT(RetryEINTR(sendto)(s1.get(), buf, sizeof(buf), 0, - reinterpret_cast<sockaddr*>(&sendto_addr), - param.sendto_addr_len), - SyscallFailsWithErrno(ElementOf(param.sendto_errnos))); + ASSERT_THAT( + RetryEINTR(sendto)(s1.get(), buf, sizeof(buf), 0, + AsSockAddr(&sendto_addr), param.sendto_addr_len), + SyscallFailsWithErrno(ElementOf(param.sendto_errnos))); return; } - ASSERT_THAT(RetryEINTR(sendto)(s1.get(), buf, sizeof(buf), 0, - reinterpret_cast<sockaddr*>(&sendto_addr), - param.sendto_addr_len), - SyscallSucceedsWithValue(sizeof(buf))); + ASSERT_THAT( + RetryEINTR(sendto)(s1.get(), buf, sizeof(buf), 0, + AsSockAddr(&sendto_addr), param.sendto_addr_len), + SyscallSucceedsWithValue(sizeof(buf))); struct sockaddr_storage got_addr = {}; socklen_t got_addr_len = sizeof(sockaddr_storage); ASSERT_THAT(RetryEINTR(recvfrom)(s2.get(), buf, sizeof(buf), 0, - reinterpret_cast<sockaddr*>(&got_addr), - &got_addr_len), + AsSockAddr(&got_addr), &got_addr_len), SyscallSucceedsWithValue(sizeof(buf))); ASSERT_GT(got_addr_len, sizeof(sockaddr_in_common)); @@ -140,8 +136,7 @@ TEST_P(SendtoTest, Sendto) { struct sockaddr_storage sender_addr = {}; socklen_t sender_addr_len = sizeof(sockaddr_storage); - ASSERT_THAT(getsockname(s1.get(), reinterpret_cast<sockaddr*>(&sender_addr), - &sender_addr_len), + ASSERT_THAT(getsockname(s1.get(), AsSockAddr(&sender_addr), &sender_addr_len), SyscallSucceeds()); ASSERT_GT(sender_addr_len, sizeof(sockaddr_in_common)); |