#![allow(unused_variables)]
#![allow(unused_imports)]
#![allow(dead_code)]
use super::{wipe_with_zeros, BATCH_SIZE, BUFFER_SIZE}; use std::fs;
use std::io;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
fn create_temp_file(name: &str, content: &[u8]) -> PathBuf {
let mut path = std::env::temp_dir();
path.push(name);
if path.exists() {
let _ = fs::remove_file(&path);
}
{
let mut file = fs::File::create(&path).expect("Failed to create temp file for test");
file.write_all(content).expect("Failed to write to temp file for test");
}
path
}
fn check_file_for_zeros(path: &Path, expected_original_size: u64) -> bool {
if !path.exists() {
return false;
}
let mut file = fs::File::open(path).expect("Failed to open file for zero check");
let metadata = file.metadata().expect("Failed to get metadata for zero check");
let actual_size = metadata.len();
if actual_size < expected_original_size {
return false;
}
let mut buffer = vec![0u8; 1024];
let mut bytes_read_total = 0u64;
while bytes_read_total < expected_original_size {
let bytes_to_read = (expected_original_size - bytes_read_total).min(buffer.len() as u64) as usize;
let bytes_read = file
.read(&mut buffer[..bytes_to_read])
.expect("Failed to read from file for zero check");
if bytes_read == 0 {
return false;
}
for i in 0..bytes_read {
if buffer[i] != 0 {
println!("Found non-zero byte at offset {}", bytes_read_total + i as u64);
return false;
}
}
bytes_read_total += bytes_read as u64;
}
true
}
#[test]
fn test_wipe_with_zeros_small_file() {
let content = b"Original small data.";
let path = create_temp_file("wipe_zeros_small.txt", content);
let original_size = content.len() as u64;
assert!(wipe_with_zeros(&path).is_ok());
assert!(path.exists()); assert!(check_file_for_zeros(&path, original_size)); let _ = fs::remove_file(&path); }
#[test]
fn test_wipe_with_zeros_larger_file() {
let content = vec![0xAA; BATCH_SIZE * 3 + 500]; let path = create_temp_file("wipe_zeros_large.bin", &content);
let original_size = content.len() as u64;
assert!(wipe_with_zeros(&path).is_ok());
assert!(path.exists());
assert!(check_file_for_zeros(&path, original_size));
let _ = fs::remove_file(&path);
}
#[test]
fn test_wipe_with_zeros_non_existent_file_error() {
let path = PathBuf::from("non_existent_wipe_zeros.txt");
let result = wipe_with_zeros(&path);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
}
#[test]
fn test_wipe_and_delete_small_file() {
let content = b"This data should be wiped and deleted by macro.";
let path = create_temp_file("macro_wipe_small.txt", content);
assert!(path.exists());
assert!(wipe_and_delete!(&path).is_ok());
assert!(!path.exists()); }
#[test]
fn test_wipe_and_delete_large_file() {
let content = vec![0xDE; BUFFER_SIZE + BATCH_SIZE]; let path = create_temp_file("macro_wipe_large.bin", &content);
assert!(path.exists());
assert!(wipe_and_delete!(&path).is_ok());
assert!(!path.exists());
}
#[test]
fn test_wipe_and_delete_non_existent_file_error() {
let path = PathBuf::from("non_existent_macro_wipe.txt");
let result = wipe_and_delete!(&path);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
}
#[test]
#[ignore = "Might fail on some systems due to permission issues"]
fn test_wipe_and_delete_read_only_file() {
let path = create_temp_file("macro_wipe_readonly.txt", b"secret data");
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_readonly(true);
fs::set_permissions(&path, perms).unwrap();
let result = wipe_and_delete!(&path);
assert!(
result.is_ok(),
"Failed to wipe read-only file with macro: {:?}",
result.unwrap_err()
);
assert!(!path.exists());
if path.exists() {
let mut perms = fs::metadata(&path).unwrap().permissions();
perms.set_readonly(false);
fs::set_permissions(&path, perms).unwrap();
let _ = fs::remove_file(&path);
}
}