Fix error codes in send, recv, accept

This commit is contained in:
Ruihan Li 2024-07-27 14:50:27 +08:00 committed by Tate, Hongliang Tian
parent a8592a16ea
commit a345e11b96
3 changed files with 49 additions and 4 deletions

View File

@ -54,7 +54,10 @@ impl TryFrom<SocketAddr> for UnixSocketAddr {
fn try_from(value: SocketAddr) -> Result<Self> {
match value {
SocketAddr::Unix(unix_socket_addr) => Ok(unix_socket_addr),
_ => return_errno_with_message!(Errno::EINVAL, "Invalid unix socket addr"),
_ => return_errno_with_message!(
Errno::EINVAL,
"the socket address is not a valid UNIX domain socket address"
),
}
}
}

View File

@ -78,7 +78,9 @@ impl UnixStreamSocket {
fn try_send(&self, buf: &[u8], _flags: SendRecvFlags) -> Result<usize> {
match self.state.read().as_ref() {
State::Connected(connected) => connected.try_write(buf),
_ => return_errno_with_message!(Errno::ENOTCONN, "the socket is not connected"),
State::Init(_) | State::Listen(_) => {
return_errno_with_message!(Errno::ENOTCONN, "the socket is not connected")
}
}
}
@ -93,7 +95,9 @@ impl UnixStreamSocket {
fn try_recv(&self, buf: &mut [u8], _flags: SendRecvFlags) -> Result<usize> {
match self.state.read().as_ref() {
State::Connected(connected) => connected.try_read(buf),
_ => return_errno_with_message!(Errno::ENOTCONN, "the socket is not connected"),
State::Init(_) | State::Listen(_) => {
return_errno_with_message!(Errno::EINVAL, "the socket is not connected")
}
}
}
@ -135,7 +139,9 @@ impl UnixStreamSocket {
fn try_accept(&self) -> Result<(Arc<dyn FileLike>, SocketAddr)> {
match self.state.read().as_ref() {
State::Listen(listen) => listen.try_accept() as _,
_ => return_errno_with_message!(Errno::EINVAL, "the socket is not listening"),
State::Init(_) | State::Connected(_) => {
return_errno_with_message!(Errno::EINVAL, "the socket is not listening")
}
}
}

View File

@ -247,6 +247,42 @@ FN_TEST(listen)
}
END_TEST()
FN_TEST(accept)
{
TEST_ERRNO(accept(sk_unbound, NULL, NULL), EINVAL);
TEST_ERRNO(accept(sk_bound, NULL, NULL), EINVAL);
TEST_ERRNO(accept(sk_connected, NULL, NULL), EINVAL);
TEST_ERRNO(accept(sk_accepted, NULL, NULL), EINVAL);
}
END_TEST()
FN_TEST(send)
{
char buf[1] = { 'z' };
TEST_ERRNO(send(sk_unbound, buf, 1, 0), ENOTCONN);
TEST_ERRNO(send(sk_bound, buf, 1, 0), ENOTCONN);
TEST_ERRNO(send(sk_listen, buf, 1, 0), ENOTCONN);
}
END_TEST()
FN_TEST(recv)
{
char buf[1] = { 'z' };
TEST_ERRNO(recv(sk_unbound, buf, 1, 0), EINVAL);
TEST_ERRNO(recv(sk_bound, buf, 1, 0), EINVAL);
TEST_ERRNO(recv(sk_listen, buf, 1, 0), EINVAL);
}
END_TEST()
FN_TEST(ns_path)
{
int fd;