Skip to content

Commit

Permalink
Add tests for several more functions (#709)
Browse files Browse the repository at this point in the history
* Add a test for `sysinfo`.

* Add a test for `pipe_with`.

* Add tests for `getpeername`.

* Add tests for `accept_from`.

* Add minimal tests for `fsync` and `fdatasync`:

* Add tests for `close` and `dup`.
  • Loading branch information
sunfishcode authored Jun 28, 2023
1 parent d98d532 commit d30f908
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 71 deletions.
10 changes: 10 additions & 0 deletions tests/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ fn test_file() {
)))]
rustix::fs::fadvise(&file, 0, 10, rustix::fs::Advice::Normal).unwrap();

rustix::fs::fsync(&file).unwrap();

#[cfg(not(any(
apple,
target_os = "dragonfly",
target_os = "haiku",
target_os = "redox",
)))]
rustix::fs::fdatasync(&file).unwrap();

assert_eq!(
rustix::io::fcntl_getfd(&file).unwrap(),
rustix::io::FdFlags::empty()
Expand Down
20 changes: 20 additions & 0 deletions tests/io/close.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use rustix::fd::IntoRawFd;

#[cfg(any(unix, target_os = "wasi"))]
#[test]
fn test_close_file() {
let file = std::fs::File::open("Cargo.toml").unwrap();
let raw = file.into_raw_fd();
unsafe {
rustix::io::close(raw);
}
}

#[test]
fn test_close_socket() {
let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let raw = socket.into_raw_fd();
unsafe {
rustix::io::close(raw);
}
}
50 changes: 50 additions & 0 deletions tests/io/dup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[cfg(feature = "fs")]
#[test]
fn test_dup() {
let file = std::fs::File::open("Cargo.toml").unwrap();
let alt = rustix::io::dup(&file).unwrap();

// Initial position is 0.
assert_eq!(
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
Ok(0)
);
assert_eq!(
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
Ok(0)
);

let mut buf = [0_u8; 4];
assert_eq!(rustix::io::read(&file, &mut buf), Ok(4));

// Both postitions updated.
assert_eq!(
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
Ok(4)
);
assert_eq!(
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
Ok(4)
);

assert_eq!(rustix::io::read(&alt, &mut buf), Ok(4));

// Both postitions updated.
assert_eq!(
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
Ok(8)
);
assert_eq!(
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
Ok(8)
);

drop(file);

assert_eq!(rustix::io::read(&alt, &mut buf), Ok(4));

assert_eq!(
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
Ok(12)
);
}
3 changes: 3 additions & 0 deletions tests/io/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Tests for [`rustix::io`].

mod close;
#[cfg(not(windows))]
mod dup;
mod error;
#[cfg(not(windows))]
mod from_into;
Expand Down
Loading

0 comments on commit d30f908

Please sign in to comment.