From d0843dffe41438d0286396bf213840ced140ccca Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Mon, 16 Oct 2023 23:47:35 +0200 Subject: [PATCH] Add test for broken symlinks --- Cargo.toml | 5 +++++ tests/fs_broken_symlink.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/fs_broken_symlink.rs diff --git a/Cargo.toml b/Cargo.toml index 7cd500e..0b07bf6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,11 @@ name = "fs_symlink_dir" path = "tests/fs_symlink_dir.rs" harness = false +[[test]] +name = "fs_broken_symlink" +path = "tests/fs_broken_symlink.rs" +harness = false + [[test]] name = "fs_null" path = "tests/fs_null.rs" diff --git a/tests/fs_broken_symlink.rs b/tests/fs_broken_symlink.rs new file mode 100644 index 0000000..e8d8bce --- /dev/null +++ b/tests/fs_broken_symlink.rs @@ -0,0 +1,30 @@ +use std::fs; +use std::os::unix::fs as unixfs; +use std::path::PathBuf; + +use birdcage::error::Error; +use birdcage::{Birdcage, Exception, Sandbox}; +use tempfile::NamedTempFile; + +fn main() { + // Setup a symlink without target. + let tempfile = NamedTempFile::new().unwrap(); + let tempfile_path = tempfile.path().to_path_buf(); + let symlink_str = tempfile_path.to_string_lossy() + "_tmpfile"; + let symlink = PathBuf::from(symlink_str.as_ref()); + unixfs::symlink(&tempfile, &symlink).unwrap(); + drop(tempfile); + assert!(!tempfile_path.exists()); + + // Sandbox exception fails with invalid path error. + let mut birdcage = Birdcage::new(); + let result = birdcage.add_exception(Exception::Read(symlink.clone())); + assert!(matches!(result, Err(Error::InvalidPath(_)))); + birdcage.lock().unwrap(); + + // Read/Write results in error. + let result = fs::read_to_string(&symlink); + assert!(result.is_err()); + let result = fs::write(&symlink, "bob"); + assert!(result.is_err()); +}