Skip to content

Commit d30f908

Browse files
authored
Add tests for several more functions (#709)
* 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`.
1 parent d98d532 commit d30f908

File tree

8 files changed

+254
-71
lines changed

8 files changed

+254
-71
lines changed

tests/fs/file.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ fn test_file() {
8484
)))]
8585
rustix::fs::fadvise(&file, 0, 10, rustix::fs::Advice::Normal).unwrap();
8686

87+
rustix::fs::fsync(&file).unwrap();
88+
89+
#[cfg(not(any(
90+
apple,
91+
target_os = "dragonfly",
92+
target_os = "haiku",
93+
target_os = "redox",
94+
)))]
95+
rustix::fs::fdatasync(&file).unwrap();
96+
8797
assert_eq!(
8898
rustix::io::fcntl_getfd(&file).unwrap(),
8999
rustix::io::FdFlags::empty()

tests/io/close.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use rustix::fd::IntoRawFd;
2+
3+
#[cfg(any(unix, target_os = "wasi"))]
4+
#[test]
5+
fn test_close_file() {
6+
let file = std::fs::File::open("Cargo.toml").unwrap();
7+
let raw = file.into_raw_fd();
8+
unsafe {
9+
rustix::io::close(raw);
10+
}
11+
}
12+
13+
#[test]
14+
fn test_close_socket() {
15+
let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
16+
let raw = socket.into_raw_fd();
17+
unsafe {
18+
rustix::io::close(raw);
19+
}
20+
}

tests/io/dup.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#[cfg(feature = "fs")]
2+
#[test]
3+
fn test_dup() {
4+
let file = std::fs::File::open("Cargo.toml").unwrap();
5+
let alt = rustix::io::dup(&file).unwrap();
6+
7+
// Initial position is 0.
8+
assert_eq!(
9+
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
10+
Ok(0)
11+
);
12+
assert_eq!(
13+
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
14+
Ok(0)
15+
);
16+
17+
let mut buf = [0_u8; 4];
18+
assert_eq!(rustix::io::read(&file, &mut buf), Ok(4));
19+
20+
// Both postitions updated.
21+
assert_eq!(
22+
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
23+
Ok(4)
24+
);
25+
assert_eq!(
26+
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
27+
Ok(4)
28+
);
29+
30+
assert_eq!(rustix::io::read(&alt, &mut buf), Ok(4));
31+
32+
// Both postitions updated.
33+
assert_eq!(
34+
rustix::fs::seek(&file, rustix::fs::SeekFrom::Current(0)),
35+
Ok(8)
36+
);
37+
assert_eq!(
38+
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
39+
Ok(8)
40+
);
41+
42+
drop(file);
43+
44+
assert_eq!(rustix::io::read(&alt, &mut buf), Ok(4));
45+
46+
assert_eq!(
47+
rustix::fs::seek(&alt, rustix::fs::SeekFrom::Current(0)),
48+
Ok(12)
49+
);
50+
}

tests/io/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Tests for [`rustix::io`].
22
3+
mod close;
4+
#[cfg(not(windows))]
5+
mod dup;
36
mod error;
47
#[cfg(not(windows))]
58
mod from_into;

0 commit comments

Comments
 (0)