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