use assert_cmd::Command;
use predicates::prelude::*;
const R4_EMPTY_PW: &str = "../../tests/fixtures/compat/encrypted-r4-three-page.pdf";
const V4_AES: &str = "../../tests/fixtures/encrypted/v4-aes-128-r4.pdf";
const V5_R6: &str = "../../tests/fixtures/encrypted/v5-aes-256-r6.pdf";
const V2_RC4: &str = "../../tests/fixtures/encrypted/v2-rc4-128-r3.pdf";
const V5_R5: &str = "../../tests/fixtures/encrypted/v5-aes-256-r5.pdf";
const UNENCRYPTED: &str = "../../tests/fixtures/minimal.pdf";
fn flpdf() -> Command {
Command::cargo_bin("flpdf").unwrap()
}
#[test]
fn is_encrypted_encrypted_no_password_exits_0() {
flpdf().args(["is-encrypted", V4_AES]).assert().success();
}
#[test]
fn is_encrypted_encrypted_empty_password_exits_0() {
flpdf()
.args(["is-encrypted", R4_EMPTY_PW])
.assert()
.success();
}
#[test]
fn is_encrypted_unencrypted_exits_2() {
flpdf().args(["is-encrypted", UNENCRYPTED]).assert().code(2);
}
#[test]
fn is_encrypted_weak_rc4_no_password_exits_0() {
flpdf().args(["is-encrypted", V2_RC4]).assert().success();
}
#[test]
fn requires_password_unencrypted_exits_2() {
flpdf()
.args(["requires-password", UNENCRYPTED])
.assert()
.code(2);
}
#[test]
fn requires_password_encrypted_empty_password_opens_exits_3() {
flpdf()
.args(["requires-password", R4_EMPTY_PW])
.assert()
.code(3);
}
#[test]
fn requires_password_encrypted_correct_password_exits_3() {
flpdf()
.args(["requires-password", "--password=user-v4-aes", V4_AES])
.assert()
.code(3);
}
#[test]
fn requires_password_encrypted_wrong_or_no_password_exits_0() {
flpdf()
.args(["requires-password", V4_AES])
.assert()
.success();
}
#[test]
fn requires_password_weak_rc4_correct_password_exits_3() {
flpdf()
.args(["requires-password", "--password=user-v2", V2_RC4])
.assert()
.code(3);
}
#[test]
fn requires_password_weak_rc4_wrong_or_no_password_exits_0() {
flpdf()
.args(["requires-password", V2_RC4])
.assert()
.success();
}
#[test]
fn requires_password_weak_r5_correct_password_exits_3() {
flpdf()
.args(["requires-password", "--password=user-v5-r5", V5_R5])
.assert()
.code(3);
}
#[test]
fn requires_password_weak_r5_wrong_or_no_password_exits_0() {
flpdf()
.args(["requires-password", V5_R5])
.assert()
.success();
}
#[test]
fn show_encryption_key_v4_aes_matches_qpdf() {
flpdf()
.args(["show-encryption-key", "--password=user-v4-aes", V4_AES])
.assert()
.success()
.stdout("5042ec4efa389ea32a149ab2a34e84fc\n");
}
#[test]
fn show_encryption_key_v5_r6_matches_qpdf() {
flpdf()
.args(["show-encryption-key", "--password=user-v5-r6", V5_R6])
.assert()
.success()
.stdout("fc459408a5282b7c59daa5162f860e82315679cc04942ef57993bfd287f30290\n");
}
#[test]
fn show_encryption_key_unencrypted_errors() {
flpdf()
.args(["show-encryption-key", UNENCRYPTED])
.assert()
.code(2);
}
#[test]
fn show_encryption_key_wrong_password_errors() {
flpdf()
.args(["show-encryption-key", "--password=wrong", V4_AES])
.assert()
.code(2);
}
#[test]
fn show_encryption_key_weak_rc4_correct_password_matches_qpdf() {
flpdf()
.args(["show-encryption-key", "--password=user-v2", V2_RC4])
.assert()
.success()
.stdout("09d56583e16481df964f95df779c97d4\n");
}
#[test]
fn show_encryption_key_weak_r5_correct_password_matches_qpdf() {
flpdf()
.args(["show-encryption-key", "--password=user-v5-r5", V5_R5])
.assert()
.success()
.stdout("c3d812902c9433c0cc9648e00ccf66c205b6b1563feb7d5d31a66bd762ed8614\n");
}
#[test]
fn show_encryption_key_weak_correct_password_emits_no_weak_crypto_warning() {
flpdf()
.args(["show-encryption-key", "--password=user-v2", V2_RC4])
.assert()
.success()
.stderr(predicate::str::contains("weak crypto").not());
}
#[test]
fn show_encryption_key_weak_with_allow_weak_crypto_emits_no_warning() {
flpdf()
.args([
"show-encryption-key",
"--allow-weak-crypto",
"--password=user-v2",
V2_RC4,
])
.assert()
.success()
.stdout("09d56583e16481df964f95df779c97d4\n")
.stderr(predicate::str::contains("weak crypto").not());
}
#[test]
fn show_encryption_key_weak_wrong_password_still_errors() {
flpdf()
.args(["show-encryption-key", "--password=wrong", V2_RC4])
.assert()
.code(2);
}
#[test]
fn show_encryption_unencrypted_prints_qpdf_message_exits_0() {
flpdf()
.args(["show-encryption", UNENCRYPTED])
.assert()
.success()
.stdout(predicate::str::contains("File is not encrypted"));
}
#[test]
fn show_encryption_v4_aes_lists_required_fields() {
let out = flpdf()
.args(["show-encryption", "--password=user-v4-aes", V4_AES])
.assert()
.success()
.get_output()
.stdout
.clone();
let text = String::from_utf8(out).unwrap();
for needle in [
"R = 4",
"P = -4",
"User password = user-v4-aes",
"stream encryption method: AESv2",
"string encryption method: AESv2",
"file encryption method: AESv2",
"Supplied password is user password",
] {
assert!(
text.contains(needle),
"show-encryption output missing {needle:?}; full output:\n{text}"
);
}
}
#[test]
fn show_encryption_qpdf_lines_match_qpdf_verbatim() {
let expected_qpdf_block = "\
R = 4
P = -4
User password = user-v4-aes
Supplied password is user password
extract for accessibility: allowed
extract for any purpose: allowed
print low resolution: allowed
print high resolution: allowed
modify document assembly: allowed
modify forms: allowed
modify annotations: allowed
modify other: allowed
modify anything: allowed
stream encryption method: AESv2
string encryption method: AESv2
file encryption method: AESv2
";
let out = flpdf()
.args(["show-encryption", "--password=user-v4-aes", V4_AES])
.assert()
.success()
.get_output()
.stdout
.clone();
let text = String::from_utf8(out).unwrap();
assert_eq!(
text, expected_qpdf_block,
"qpdf-compatible block diverged from qpdf 11.9.0 output"
);
}
#[test]
fn show_encryption_weak_rc4_correct_password_exits_0() {
let out = flpdf()
.args(["show-encryption", "--password=user-v2", V2_RC4])
.assert()
.success()
.get_output()
.stdout
.clone();
let text = String::from_utf8(out).unwrap();
for needle in ["R = 3", "P = -4", "Supplied password is user password"] {
assert!(
text.contains(needle),
"show-encryption (weak RC4) output missing {needle:?}; full output:\n{text}"
);
}
}
#[test]
fn show_encryption_weak_r5_correct_password_exits_0() {
let out = flpdf()
.args(["show-encryption", "--password=user-v5-r5", V5_R5])
.assert()
.success()
.get_output()
.stdout
.clone();
let text = String::from_utf8(out).unwrap();
for needle in ["R = 5", "Supplied password is user password"] {
assert!(
text.contains(needle),
"show-encryption (weak R5) output missing {needle:?}; full output:\n{text}"
);
}
}
#[test]
fn show_encryption_weak_correct_password_emits_no_weak_crypto_warning() {
flpdf()
.args(["show-encryption", "--password=user-v2", V2_RC4])
.assert()
.success()
.stderr(predicate::str::contains("weak crypto").not());
}
#[test]
fn show_encryption_output_is_stable() {
let run = || {
String::from_utf8(
flpdf()
.args(["show-encryption", "--password=user-v5-r6", V5_R6])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
};
assert_eq!(run(), run());
}
#[test]
fn all_subcommands_have_help() {
for sub in [
"show-encryption",
"is-encrypted",
"requires-password",
"show-encryption-key",
] {
flpdf()
.args([sub, "--help"])
.assert()
.success()
.stdout(predicate::str::contains(sub));
}
}
fn v4_aes_with_cfm(replacement: &[u8], dir: &tempfile::TempDir) -> std::path::PathBuf {
const CFM: &[u8] = b"/CFM /AESV2";
assert_eq!(
replacement.len(),
CFM.len(),
"substitution must be in place"
);
let bytes = std::fs::read(V4_AES).expect("committed V=4 AES fixture");
let at = bytes
.windows(CFM.len())
.position(|window| window == CFM)
.expect("fixture declares /CFM /AESV2");
let mut rewritten = bytes.clone();
rewritten[at..at + CFM.len()].copy_from_slice(replacement);
let path = dir.path().join("rewritten.pdf");
std::fs::write(&path, rewritten).expect("write rewritten fixture");
path
}
#[test]
fn show_encryption_reports_an_unrecognised_cfm_as_unknown() {
let dir = tempfile::tempdir().unwrap();
let path = v4_aes_with_cfm(b"/CFM /AESVX", &dir);
flpdf()
.args(["show-encryption", "--password=user-v4-aes"])
.arg(&path)
.assert()
.success()
.stdout(predicate::str::contains(
"stream encryption method: unknown",
))
.stdout(predicate::str::contains(
"string encryption method: unknown",
))
.stdout(predicate::str::contains("file encryption method: unknown"));
}
#[test]
fn show_encryption_reports_a_missing_cfm_as_none() {
let dir = tempfile::tempdir().unwrap();
let path = v4_aes_with_cfm(b" ", &dir);
flpdf()
.args(["show-encryption", "--password=user-v4-aes"])
.arg(&path)
.assert()
.success()
.stdout(predicate::str::contains("stream encryption method: none"))
.stdout(predicate::str::contains("string encryption method: none"))
.stdout(predicate::str::contains("file encryption method: none"));
}