#[cfg(test)]
mod tests {
    use std::fs;

    #[test]
    fn test_create_dir_in_run_user() {
        let uid = nix::unistd::geteuid().as_raw();
        let test_dir = format!("/run/user/{}/cylo-test-{}", uid, std::process::id());

        // Try to create the directory
        match fs::create_dir(&test_dir) {
            Ok(_) => {
                println!("Successfully created directory: {}", test_dir);
                // Clean up
                fs::remove_dir(&test_dir).expect("Failed to clean up test directory");
            }
            Err(e) => {
                panic!("Failed to create directory {}: {}", test_dir, e);
            }
        }
    }

    #[test]
    #[cfg(all(target_os = "linux", feature = "landlock"))]
    fn test_mount_tmpfs_in_namespace() {
        use std::ffi::CString;
        use std::fs;
        use std::path::Path;

        use libc::mount;
        use nix::sched::CloneFlags;
        use nix::sched::unshare;

        // Check if we're running in a container environment
        let in_container = Path::new("/.dockerenv").exists()
            || std::fs::read_to_string("/proc/1/cgroup")
                .map(|s| s.contains("docker") || s.contains("kubepods"))
                .unwrap_or(false);

        println!("Running in container environment: {}", in_container);

        if in_container {
            // Skip this test in container environments where namespace operations are restricted
            println!("Skipping namespace operations test in container environment");
            return;
        }

        let uid = nix::unistd::geteuid().as_raw();
        let test_dir = format!("/run/user/{}/cylo-test-mount-{}", uid, std::process::id());

        fs::create_dir_all(&test_dir).expect("Failed to create test dir");
        println!("Created test directory: {}", test_dir);

        // Try unsharing namespaces but don't panic if it fails
        unsafe {
            println!("Attempting to unshare namespaces...");
            let result = unshare(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWNS);
            if result != 0 {
                let err = std::io::Error::last_os_error();
                println!("Unshare failed (expected in containers): {}", err);
                // Clean up and return early instead of panicking
                let _ = fs::remove_dir(&test_dir);
                return;
            }

            println!("Successfully unshared namespaces");

            // Try setting up UID/GID mappings
            println!("Setting up UID/GID mappings...");
            let uid_map = format!("0 {} 1", uid);
            match std::fs::write("/proc/self/uid_map", uid_map) {
                Ok(_) => println!("Successfully mapped UID"),
                Err(e) => {
                    println!("Failed to map UID (expected in containers): {}", e);
                    let _ = fs::remove_dir(&test_dir);
                    return;
                }
            }

            // Try to deny setgroups
            match std::fs::write("/proc/self/setgroups", "deny") {
                Ok(_) => println!("Successfully denied setgroups"),
                Err(e) => {
                    println!("Failed to deny setgroups (expected in containers): {}", e);
                    let _ = fs::remove_dir(&test_dir);
                    return;
                }
            }

            // Try to map GID
            match std::fs::write("/proc/self/gid_map", format!("0 {} 1", uid)) {
                Ok(_) => println!("Successfully mapped GID"),
                Err(e) => {
                    println!("Failed to map GID (expected in containers): {}", e);
                    let _ = fs::remove_dir(&test_dir);
                    return;
                }
            }

            println!("Successfully set up UID/GID mappings");

            // Try mounting the tmpfs
            println!("Preparing to mount tmpfs...");
            let mp_cstr = CString::new(test_dir.as_str()).unwrap();
            let source = CString::new("none").unwrap();
            let fstype = CString::new("tmpfs").unwrap();
            let data = CString::new("size=10M").unwrap();

            let mount_result = mount(source.as_ptr(), mp_cstr.as_ptr(), 0, std::ptr::null_mut());

            if mount_result != 0 {
                let err = std::io::Error::last_os_error();
                println!("Mount failed (expected in containers): {}", err);
                let _ = fs::remove_dir(&test_dir);
                return;
            }

            println!("Successfully mounted tmpfs at {}", test_dir);
            // Note: Proper cleanup would unmount first, but we'll leave that for the OS
            // since this is a test environment
        }
    }
}
