use crate::soft_canonicalize;
use tempfile::tempdir;
#[test]
fn test_broken_symlink_handling() -> std::io::Result<()> {
let _temp_dir = tempdir()?;
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
let broken_link = _temp_dir.path().join("broken_link");
let non_existing_target = _temp_dir.path().join("does_not_exist.txt");
if symlink(non_existing_target, &broken_link).is_ok() {
let result = soft_canonicalize(&broken_link);
assert!(result.is_ok(), "Should handle broken symlinks gracefully");
let path_through_broken = broken_link.join("more/components");
let result2 = soft_canonicalize(path_through_broken);
assert!(
result2.is_ok(),
"Should handle paths through broken symlinks"
);
}
}
Ok(())
}
#[test]
fn test_root_path_edge_cases() -> std::io::Result<()> {
#[cfg(unix)]
{
let root_result = soft_canonicalize("/")?;
assert_eq!(root_result, std::path::PathBuf::from("/"));
let near_root = soft_canonicalize("/non_existing_file.txt")?;
assert_eq!(
near_root,
std::path::PathBuf::from("/non_existing_file.txt")
);
}
#[cfg(windows)]
{
let drive_root = soft_canonicalize("C:\\")?;
assert!(drive_root.to_string_lossy().ends_with(":\\"));
let near_root = soft_canonicalize("C:\\non_existing_file.txt")?;
assert!(near_root
.to_string_lossy()
.contains("non_existing_file.txt"));
}
Ok(())
}
#[test]
fn test_filesystem_permission_scenarios() -> std::io::Result<()> {
let result = soft_canonicalize("/proc/1/mem/non_existing");
match result {
Ok(_) => println!("Permission test: OK"),
Err(e) => println!("Permission test error (expected): {e}"),
}
Ok(())
}
#[test]
fn test_very_long_paths() -> std::io::Result<()> {
let long_component = "a".repeat(255); let long_path = format!("some/path/{long_component}/file.txt");
let result = soft_canonicalize(long_path);
assert!(result.is_ok(), "Should handle long paths");
Ok(())
}
#[test]
fn test_special_characters_in_paths() -> std::io::Result<()> {
let special_chars = vec![
"path with spaces/file.txt",
"path-with-dashes/file.txt",
"path_with_underscores/file.txt",
"path.with.dots/file.txt",
];
for path in special_chars {
let result = soft_canonicalize(path);
assert!(
result.is_ok(),
"Should handle special characters in: {path}"
);
}
Ok(())
}
#[test]
fn test_nested_symlink_depth_boundary() -> std::io::Result<()> {
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
let temp_dir = tempdir()?;
let link1 = temp_dir.path().join("link1");
let link2 = temp_dir.path().join("link2");
let final_target = temp_dir.path().join("target/file.txt");
if symlink(&link2, &link1).is_ok() && symlink(final_target, &link2).is_ok() {
let result = soft_canonicalize(&link1);
assert!(result.is_ok(), "Should handle moderate symlink depth");
}
}
Ok(())
}