Skip to content

Commit

Permalink
Add test for broken symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
cd-work committed Oct 16, 2023
1 parent 74349dd commit d0843df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions tests/fs_broken_symlink.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit d0843df

Please sign in to comment.