use enc_file::{EncryptOptions, AeadAlg};
use enc_file::encrypt_file_streaming;
use secrecy::SecretString;
use std::fs;
use tempfile::tempdir;
#[test]
fn adaptive_sizing_selects_appropriate_chunks() {
let dir = tempdir().unwrap();
let password = SecretString::new("test_password".into());
let test_cases = vec![
(64 * 1024, "small file - 64 KB"), (512 * 1024, "medium-small file - 512 KB"), (50 * 1024 * 1024, "medium file - 50 MB"), (150 * 1024 * 1024, "large file - 150 MB"), ];
for (file_size, description) in test_cases {
println!("Testing {}", description);
let input_path = dir.path().join(format!("test_{}.bin", file_size));
let encrypted_path = input_path.with_extension("enc");
let test_data = vec![0x42u8; file_size];
fs::write(&input_path, &test_data).unwrap();
let opts = EncryptOptions {
alg: AeadAlg::XChaCha20Poly1305,
stream: true,
chunk_size: 0, force: true,
..Default::default()
};
let result = encrypt_file_streaming(&input_path, Some(&encrypted_path), password.clone(), opts);
assert!(result.is_ok(), "Encryption failed for {}: {:?}", description, result.err());
assert!(encrypted_path.exists(), "Encrypted file not created for {}", description);
let encrypted_size = fs::metadata(&encrypted_path).unwrap().len();
let original_size = fs::metadata(&input_path).unwrap().len();
assert!(encrypted_size > original_size, "Encrypted file should be larger than original for {}", description);
if original_size > 1024 {
let overhead_ratio = (encrypted_size as f64) / (original_size as f64);
assert!(overhead_ratio < 1.3, "Encryption overhead too high for {}: {:.1}%", description, (overhead_ratio - 1.0) * 100.0);
}
println!(" Original: {} bytes, Encrypted: {} bytes", original_size, encrypted_size);
let _ = fs::remove_file(&input_path);
let _ = fs::remove_file(&encrypted_path);
}
}
#[test]
fn explicit_chunk_size_overrides_adaptive() {
let dir = tempdir().unwrap();
let password = SecretString::new("test_password".into());
let file_size = 50 * 1024 * 1024;
let input_path = dir.path().join("test_override.bin");
let encrypted_path = input_path.with_extension("enc");
let test_data = vec![0x42u8; file_size];
fs::write(&input_path, &test_data).unwrap();
let explicit_chunk_size = 2 * 1024 * 1024; let opts = EncryptOptions {
alg: AeadAlg::XChaCha20Poly1305,
stream: true,
chunk_size: explicit_chunk_size,
force: true,
..Default::default()
};
let result = encrypt_file_streaming(&input_path, Some(&encrypted_path), password, opts);
assert!(result.is_ok(), "Encryption with explicit chunk size failed: {:?}", result.err());
assert!(encrypted_path.exists(), "Encrypted file not created with explicit chunk size");
let encrypted_size = fs::metadata(&encrypted_path).unwrap().len();
let original_size = fs::metadata(&input_path).unwrap().len();
assert!(encrypted_size > original_size, "Encrypted file should be larger than original");
println!("Explicit chunk size test - Original: {} bytes, Encrypted: {} bytes", original_size, encrypted_size);
}