Skip to content

Commit

Permalink
Implement generic type for test_ftruncate
Browse files Browse the repository at this point in the history
  • Loading branch information
tiif committed Mar 24, 2024
1 parent 5c8f3aa commit b10901e
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions tests/pass-dep/shims/libc-fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ fn main() {
test_dup_stdout_stderr();
test_canonicalize_too_long();
test_rename();
test_ftruncate(libc::ftruncate);
test_ftruncate::<libc::off_t>(libc::ftruncate);
#[cfg(target_os = "linux")]
test_ftruncate(libc::ftruncate64);
test_ftruncate::<libc::off64_t>(libc::ftruncate64);
test_readlink();
test_file_open_unix_allow_two_args();
test_file_open_unix_needs_three_args();
Expand Down Expand Up @@ -162,7 +162,12 @@ fn test_rename() {
remove_file(&path2).unwrap();
}

fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64) -> libc::c_int) {
fn test_ftruncate<T: Length>(
ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: T) -> libc::c_int,
) {
// libc::off_t is i32 in target i686-unknown-linux-gnu
// https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/type.off_t.html

let bytes = b"hello";
let path = prepare("miri_test_libc_fs_ftruncate.txt");
let mut file = File::create(&path).unwrap();
Expand All @@ -174,7 +179,8 @@ fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64)
let fd = unsafe { libc::open(c_path.as_ptr(), libc::O_RDWR) };

// Truncate to a bigger size
let mut res = unsafe { ftruncate(fd, 10) };
let length_10: i64 = 10;
let mut res = unsafe { ftruncate(fd, Length::convert(length_10)) };
assert_eq!(res, 0);
assert_eq!(file.metadata().unwrap().len(), 10);

Expand All @@ -184,7 +190,8 @@ fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64)
assert_eq!(file.metadata().unwrap().len(), 10);

// Truncate to smaller size
res = unsafe { ftruncate(fd, 2) };
let length_2: i64 = 2;
res = unsafe { ftruncate(fd, Length::convert(length_2)) };
assert_eq!(res, 0);
assert_eq!(file.metadata().unwrap().len(), 2);

Expand Down Expand Up @@ -251,3 +258,20 @@ fn test_posix_mkstemp() {
assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput);
}
}

// Trait implemented for test_ftruncate to convert i64 to the provided type
trait Length: Sized {
fn convert(t: i64) -> Self;
}

impl Length for i32 {
fn convert(t: i64) -> i32 {
t as i32
}
}

impl Length for i64 {
fn convert(t: i64) -> i64 {
t
}
}

0 comments on commit b10901e

Please sign in to comment.