xref: /third_party/rust/crates/rustix/tests/fs/statx.rs (revision b8a62b91)
1#[test]
2fn test_statx_unknown_flags() {
3    use rustix::fs::{AtFlags, StatxFlags};
4
5    let f = std::fs::File::open(".").unwrap();
6
7    // It's ok (though still unwise) to construct flags values that have
8    // unknown bits. Exclude `STATX__RESERVED` here as that evokes an explicit
9    // failure; that's tested separately below.
10    let too_many_flags =
11        unsafe { StatxFlags::from_bits_unchecked(!0 & !linux_raw_sys::general::STATX__RESERVED) };
12
13    // It's also ok to pass such flags to `statx`.
14    let result = rustix::fs::statx(&f, "Cargo.toml", AtFlags::empty(), too_many_flags).unwrap();
15
16    // But, rustix should mask off bits it doesn't recognize, because these
17    // extra flags may tell future kernels to set extra fields beyond the
18    // extend of rustix's statx buffer. So make sure we didn't get extra
19    // fields.
20    assert_eq!(result.stx_mask & !StatxFlags::all().bits(), 0);
21}
22
23#[test]
24fn test_statx_reserved() {
25    use rustix::fs::{AtFlags, StatxFlags};
26
27    let f = std::fs::File::open(".").unwrap();
28
29    // It's ok (though still unwise) to construct a `STATX__RESERVED` flag
30    // value but `statx` should reliably fail with `INVAL`.
31    let reserved =
32        unsafe { StatxFlags::from_bits_unchecked(linux_raw_sys::general::STATX__RESERVED) };
33    match rustix::fs::statx(&f, "Cargo.toml", AtFlags::empty(), reserved) {
34        Ok(_) => panic!("statx succeeded with `STATX__RESERVED`"),
35        Err(err) => assert_eq!(err, rustix::io::Errno::INVAL),
36    }
37}
38