use easy_fuser::prelude::*;
use easy_fuser::templates::{DefaultFuseHandler, mirror_fs::*};
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};
#[test]
fn test_mirror_fs_recursion() {
let source_path = PathBuf::from("/tmp/easy_fuser_recursion_fs_source");
let mntpoint = source_path.join("mount");
let _ = fs::remove_dir_all(&source_path);
fs::create_dir_all(&source_path).unwrap();
fs::create_dir_all(&mntpoint).unwrap();
let mntpoint_clone = mntpoint.clone();
let handle = std::thread::spawn(move || {
let fs = MirrorFs::new(source_path.clone(), DefaultFuseHandler::new());
#[cfg(feature = "serial")]
mount(fs, &mntpoint_clone, &[]).unwrap();
#[cfg(not(feature = "serial"))]
mount(fs, &mntpoint_clone, &[], 4).unwrap();
});
std::thread::sleep(Duration::from_millis(50));
std::thread::sleep(Duration::from_secs(1));
let deep_path = mntpoint
.join("easy_fuser_recursion_fs_source")
.join("mount")
.join("easy_fuser_recursion_fs_source")
.join("mount")
.join("easy_fuser_recursion_fs_source")
.join("mount");
let start_time = Instant::now();
let output = Command::new("ls")
.arg(&deep_path)
.output()
.expect("Failed to execute ls command");
let elapsed_time = start_time.elapsed();
if elapsed_time >= Duration::from_secs(5) {
panic!(
"Test failed: 'ls' command took 5 seconds or more, indicating a potential infinite recursion."
);
}
let output_str = String::from_utf8_lossy(&output.stdout);
println!("ls output: {}", output_str);
println!("ls error: {}", String::from_utf8_lossy(&output.stderr));
assert!(
!output_str.contains("easy_fuser_recursion_fs_source"),
"The output suggests an infinitely recursive structure"
);
println!("Test passed: No infinite recursion detected.");
eprintln!("Unmounting filesystem...");
let _ = std::process::Command::new("fusermount")
.arg("-u")
.arg(&mntpoint)
.status();
handle.join().unwrap();
}