xref: /third_party/rust/crates/rustix/tests/io/seals.rs (revision b8a62b91)
1#[cfg(feature = "fs")]
2#[test]
3fn test_seals() {
4    use rustix::fs::{
5        fcntl_add_seals, fcntl_get_seals, ftruncate, memfd_create, MemfdFlags, SealFlags,
6    };
7    use std::fs::File;
8    use std::io::Write;
9
10    let fd = match memfd_create("test", MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING) {
11        Ok(fd) => fd,
12        Err(rustix::io::Errno::NOSYS) => return,
13        Err(err) => Err(err).unwrap(),
14    };
15    let mut file = File::from(fd);
16
17    let old = fcntl_get_seals(&file).unwrap();
18    assert_eq!(old, SealFlags::empty());
19
20    writeln!(&mut file, "Hello!").unwrap();
21
22    fcntl_add_seals(&file, SealFlags::GROW).unwrap();
23
24    let now = fcntl_get_seals(&file).unwrap();
25    assert_eq!(now, SealFlags::GROW);
26
27    // We sealed growing, so this should fail.
28    writeln!(&mut file, "World?").unwrap_err();
29
30    // We can still shrink for now.
31    ftruncate(&mut file, 1).unwrap();
32
33    fcntl_add_seals(&file, SealFlags::SHRINK).unwrap();
34
35    let now = fcntl_get_seals(&file).unwrap();
36    assert_eq!(now, SealFlags::GROW | SealFlags::SHRINK);
37
38    // We sealed shrinking, so this should fail.
39    ftruncate(&mut file, 0).unwrap_err();
40}
41