Skip to content

Commit 41204bf

Browse files
authored
Merge pull request #4800 from hulxv/refactor/simplify-libc-tests/libc-fs-symlink
Refactor `libc-fs-symlink` tests to use errno_result
2 parents bf8091e + cecc26b commit 41204bf

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

src/tools/miri/tests/pass-dep/libc/libc-fs-symlink.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
//@compile-flags: -Zmiri-disable-isolation
55

66
use std::ffi::CString;
7-
use std::io::{Error, ErrorKind};
7+
use std::io::ErrorKind;
88
use std::os::unix::ffi::OsStrExt;
99

10+
#[path = "../../utils/libc.rs"]
11+
mod libc_utils;
1012
#[path = "../../utils/mod.rs"]
1113
mod utils;
14+
use libc_utils::errno_result;
1215

1316
fn main() {
1417
test_readlink();
@@ -31,44 +34,48 @@ fn test_readlink() {
3134
// Make the buf one byte larger than it needs to be,
3235
// and check that the last byte is not overwritten.
3336
let mut large_buf = vec![0xFF; expected_path.len() + 1];
34-
let res =
35-
unsafe { libc::readlink(symlink_c_ptr, large_buf.as_mut_ptr().cast(), large_buf.len()) };
37+
let res = errno_result(unsafe {
38+
libc::readlink(symlink_c_ptr, large_buf.as_mut_ptr().cast(), large_buf.len())
39+
})
40+
.unwrap();
3641
// Check that the resolved path was properly written into the buf.
3742
assert_eq!(&large_buf[..(large_buf.len() - 1)], expected_path);
3843
assert_eq!(large_buf.last(), Some(&0xFF));
39-
assert_eq!(res, large_buf.len() as isize - 1);
44+
assert_eq!(res, (large_buf.len() - 1) as isize);
4045

4146
// Test that the resolved path is truncated if the provided buffer
4247
// is too small.
4348
let mut small_buf = [0u8; 2];
44-
let res =
45-
unsafe { libc::readlink(symlink_c_ptr, small_buf.as_mut_ptr().cast(), small_buf.len()) };
49+
let res = errno_result(unsafe {
50+
libc::readlink(symlink_c_ptr, small_buf.as_mut_ptr().cast(), small_buf.len())
51+
})
52+
.unwrap();
4653
assert_eq!(small_buf, &expected_path[..small_buf.len()]);
4754
assert_eq!(res, small_buf.len() as isize);
4855

4956
// Test that we report a proper error for a missing path.
50-
let res = unsafe {
57+
let err = errno_result(unsafe {
5158
libc::readlink(
5259
c"MIRI_MISSING_FILE_NAME".as_ptr(),
5360
small_buf.as_mut_ptr().cast(),
5461
small_buf.len(),
5562
)
56-
};
57-
assert_eq!(res, -1);
58-
assert_eq!(Error::last_os_error().kind(), ErrorKind::NotFound);
63+
})
64+
.unwrap_err();
65+
assert_eq!(err.kind(), ErrorKind::NotFound);
5966
}
6067

6168
fn test_nofollow_symlink() {
62-
let bytes = b"Hello, World!\n";
63-
let path = utils::prepare_with_content("test_nofollow_symlink_target.txt", bytes);
69+
let path = utils::prepare_with_content("test_nofollow_symlink_target.txt", b"Hello, World!\n");
6470

6571
let symlink_path = utils::prepare("test_nofollow_symlink.txt");
6672
std::os::unix::fs::symlink(&path, &symlink_path).unwrap();
6773

6874
let symlink_cpath = CString::new(symlink_path.as_os_str().as_bytes()).unwrap();
6975

70-
let ret = unsafe { libc::open(symlink_cpath.as_ptr(), libc::O_NOFOLLOW | libc::O_CLOEXEC) };
71-
assert_eq!(ret, -1);
72-
let err = Error::last_os_error().raw_os_error().unwrap();
73-
assert_eq!(err, libc::ELOOP);
76+
let err = errno_result(unsafe {
77+
libc::open(symlink_cpath.as_ptr(), libc::O_NOFOLLOW | libc::O_CLOEXEC)
78+
})
79+
.unwrap_err();
80+
assert_eq!(err.raw_os_error(), Some(libc::ELOOP));
7481
}

0 commit comments

Comments
 (0)