use crate::Fixture;
use assert_cmd::prelude::*;
use std::fs;
use std::process::Command;
use tempfile::tempdir;
#[test]
fn compress_encrypt_decrypt_round_trip() {
let casdir = tempdir().unwrap();
let ept = Fixture::copy("sample/test.ept");
Command::cargo_bin("enprot")
.unwrap()
.arg("-c")
.arg(casdir.path())
.arg("-k")
.arg("Agent_007=password123")
.arg("encrypt")
.arg("--compress")
.arg("-w")
.arg("Agent_007")
.arg("--pbkdf")
.arg("pbkdf2-sha256")
.arg("--pbkdf-msec")
.arg("10")
.arg(&ept.path)
.assert()
.success();
let content = fs::read_to_string(&ept.path).unwrap();
assert!(
content.contains("ENCRYPTED"),
"file should have ENCRYPTED block after encrypt"
);
Command::cargo_bin("enprot")
.unwrap()
.arg("-c")
.arg(casdir.path())
.arg("-k")
.arg("Agent_007=password123")
.arg("decrypt")
.arg("-w")
.arg("Agent_007")
.arg(&ept.path)
.assert()
.success();
let decrypted = fs::read_to_string(&ept.path).unwrap();
let original = fs::read_to_string(&ept.source).unwrap();
assert_eq!(
decrypted, original,
"decrypt(compress(encrypt(pt))) == pt — round-trip must recover original"
);
}
#[test]
fn compress_without_compress_flag_still_round_trips() {
let casdir = tempdir().unwrap();
let ept = Fixture::copy("sample/test.ept");
Command::cargo_bin("enprot")
.unwrap()
.arg("-c")
.arg(casdir.path())
.arg("-k")
.arg("Agent_007=password123")
.arg("encrypt")
.arg("-w")
.arg("Agent_007")
.arg("--pbkdf")
.arg("pbkdf2-sha256")
.arg("--pbkdf-msec")
.arg("10")
.arg(&ept.path)
.assert()
.success();
Command::cargo_bin("enprot")
.unwrap()
.arg("-c")
.arg(casdir.path())
.arg("-k")
.arg("Agent_007=password123")
.arg("decrypt")
.arg("-w")
.arg("Agent_007")
.arg(&ept.path)
.assert()
.success();
let decrypted = fs::read_to_string(&ept.path).unwrap();
let original = fs::read_to_string(&ept.source).unwrap();
assert_eq!(
decrypted, original,
"round-trip without compression must work"
);
}