Skip to content

Commit 5402be8

Browse files
committed
socketpair: test behavior when one end got closed
1 parent 99d742e commit 5402be8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/tools/miri/tests/pass-dep/libc/libc-socketpair.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ fn test_socketpair() {
5858
};
5959
assert_eq!(res, 3);
6060
assert_eq!(&buf4[0..3], "123".as_bytes());
61+
62+
// Test when happens when we close one end, with some data in the buffer.
63+
res = unsafe { libc::write(fds[0], data as *const libc::c_void, 3).try_into().unwrap() };
64+
assert_eq!(res, 3);
65+
unsafe { libc::close(fds[0]) };
66+
// Reading the other end should return that data, then EOF.
67+
let mut buf: [u8; 5] = [0; 5];
68+
res = unsafe {
69+
libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
70+
};
71+
assert_eq!(res, 3);
72+
assert_eq!(&buf[0..3], "123".as_bytes());
73+
res = unsafe {
74+
libc::read(fds[1], buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
75+
};
76+
assert_eq!(res, 0); // 0-sized read: EOF.
77+
// Writing the other end should emit EPIPE.
78+
res = unsafe { libc::write(fds[1], data as *const libc::c_void, 1).try_into().unwrap() };
79+
assert_eq!(res, -1);
80+
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::EPIPE));
6181
}
6282

6383
fn test_socketpair_threaded() {

0 commit comments

Comments
 (0)