use assert_cmd::Command;
use std::path::{Path, PathBuf};
use std::process::Command as ShellCommand;
const UNENCRYPTED_FIXTURE: &str = "../../tests/fixtures/minimal.pdf";
const ONE_PAGE_FIXTURE: &str = "../../tests/fixtures/compat/one-page.pdf";
fn fixture(rel: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(rel)
}
fn ensure_qpdf_or_skip() -> bool {
let available = ShellCommand::new("qpdf")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if available {
return true;
}
if std::env::var_os("CI").is_some() {
panic!("qpdf required for --encrypt CLI tests on CI");
}
eprintln!("skipping: qpdf not available");
false
}
#[test]
fn top_level_encrypt_v4_aes_128_round_trips_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--encrypt",
"user-pw",
"owner-pw",
"128",
"--use-aes=y",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let bytes = std::fs::read(&output).unwrap();
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"encrypted output must carry /Encrypt"
);
let check = ShellCommand::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(stdout.contains("R = 4"), "qpdf must report R=4: {stdout}");
assert!(
stdout.contains("Supplied password is user password"),
"qpdf must accept user password: {stdout}"
);
}
#[test]
fn rewrite_subcommand_encrypt_v4_aes_128_round_trips_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--encrypt",
"user-pw",
"owner-pw",
"128",
"--use-aes=y",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed: stderr={}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 4") && stdout.contains("Supplied password is user password"),
"qpdf must report R=4 + user-password match: {stdout}"
);
}
#[test]
fn top_level_encrypt_v5_r6_aes256_round_trips_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted-v5.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "owner-pw", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let bytes = std::fs::read(&output).unwrap();
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"encrypted output must carry /Encrypt"
);
let user = ShellCommand::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(
user.status.success(),
"qpdf --show-encryption (user) failed: stderr={}",
String::from_utf8_lossy(&user.stderr)
);
let user_out = String::from_utf8_lossy(&user.stdout);
assert!(
user_out.contains("R = 6") && user_out.contains("Supplied password is user password"),
"qpdf must report R=6 + user-password match: {user_out}"
);
let owner = ShellCommand::new("qpdf")
.arg("--password=owner-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(owner.status.success());
let owner_out = String::from_utf8_lossy(&owner.stdout);
assert!(
owner_out.contains("Supplied password is owner password"),
"qpdf must accept the owner password: {owner_out}"
);
}
#[test]
fn encrypt_v5_r6_aes256_flpdf_show_encryption_reports_scheme() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted-v5.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "owner-pw", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let show = Command::cargo_bin("flpdf")
.unwrap()
.args(["show-encryption"])
.arg(&output)
.arg("--password=user-pw")
.assert()
.success();
let stdout = String::from_utf8_lossy(&show.get_output().stdout).into_owned();
for needle in ["V = 5", "Length = 256", "R = 6", "AESv3"] {
assert!(
stdout.contains(needle),
"flpdf show-encryption must report {needle:?} for V=5 R=6 output: {stdout}"
);
}
}
#[test]
fn encrypt_owner_password_authenticates_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user", "owner", "128", "--use-aes=y", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=owner")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("Supplied password is owner password"),
"qpdf must accept owner password: {stdout}"
);
}
#[test]
fn encrypt_round_trip_on_one_page_decrypts_cleanly_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let encrypted = tmp.path().join("encrypted.pdf");
let decrypted = tmp.path().join("decrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user", "owner", "128", "--use-aes=y", "--"])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&encrypted)
.assert()
.success();
let decrypt = ShellCommand::new("qpdf")
.arg("--password=user")
.arg("--decrypt")
.arg(&encrypted)
.arg(&decrypted)
.output()
.unwrap();
assert!(
decrypt.status.success(),
"qpdf --decrypt failed: {}",
String::from_utf8_lossy(&decrypt.stderr)
);
let check = ShellCommand::new("qpdf")
.arg("--check")
.arg(&decrypted)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --check on round-tripped plaintext failed: {}",
String::from_utf8_lossy(&check.stderr)
);
}
#[test]
fn encrypt_key_len_40_v1_rc4_requires_allow_weak_crypto() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "40", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("RC4"))
.stderr(predicates::str::contains("--allow-weak-crypto"));
assert!(!output.exists(), "no output without --allow-weak-crypto");
if !ensure_qpdf_or_skip() {
return;
}
let ok = tmp.path().join("v1.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--allow-weak-crypto", "--encrypt", "u", "o", "40", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&ok)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--show-encryption")
.arg(&ok)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 2") && stdout.contains("Supplied password is user password"),
"qpdf must report R=2 + user-password match for V=1 RC4-40: {stdout}"
);
}
#[test]
fn encrypt_key_len_256_is_accepted_as_v5_r6() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("ok.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
assert!(
output.exists(),
"256 encryption must produce an output file"
);
let bytes = std::fs::read(&output).unwrap();
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"V=5 R=6 output must carry /Encrypt"
);
}
#[test]
fn encrypt_v5_r6_empty_owner_nonempty_user_requires_allow_insecure() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("insecure"))
.stderr(predicates::str::contains("--allow-insecure"));
assert!(!output.exists(), "no output must be written when rejected");
}
#[test]
fn encrypt_v5_r6_empty_owner_with_allow_insecure_succeeds_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("insecure.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "", "256", "--allow-insecure", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed: stderr={}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 6") && stdout.contains("Supplied password is user password"),
"qpdf must report R=6 + user-password match for the --allow-insecure output: {stdout}"
);
}
#[test]
fn encrypt_v5_r6_both_passwords_no_allow_insecure_succeeds() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("secure.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "owner-pw", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
assert!(output.exists(), "both-password 256 encryption must succeed");
}
#[test]
fn encrypt_allow_insecure_rejects_a_value() {
for form in ["--allow-insecure=false", "--allow-insecure="] {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "", "256", form, "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains(
"--allow-insecure does not take a value",
));
assert!(
!output.exists(),
"no output must be written when {form:?} is rejected"
);
}
}
#[test]
fn encrypt_128_no_aes_is_v2_rc4_gated_by_weak_crypto() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "128", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("RC4"))
.stderr(predicates::str::contains("--allow-weak-crypto"));
assert!(!output.exists(), "no output without --allow-weak-crypto");
if !ensure_qpdf_or_skip() {
return;
}
let ok = tmp.path().join("v2.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--allow-weak-crypto", "--encrypt", "u", "o", "128", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&ok)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--show-encryption")
.arg(&ok)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 3") && stdout.contains("Supplied password is user password"),
"qpdf must report R=3 for V=2 RC4-128: {stdout}"
);
}
#[test]
fn encrypt_128_force_v4_no_aes_is_v4_rc4() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let ok = tmp.path().join("v4rc4.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--allow-weak-crypto",
"--encrypt",
"u",
"o",
"128",
"--force-V4",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&ok)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--show-encryption")
.arg(&ok)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 4") && stdout.contains("Supplied password is user password"),
"qpdf must report R=4 for V=4 RC4-128: {stdout}"
);
}
#[test]
fn encrypt_incompatible_subflags_for_key_len_are_rejected() {
let cases: &[(&[&str], &str)] = &[
(&["u", "o", "40", "--use-aes=y"], "KEY-LEN=40"),
(&["u", "o", "40", "--force-V4"], "KEY-LEN=40"),
(&["u", "o", "40", "--allow-insecure"], "KEY-LEN=40"),
(&["u", "o", "256", "--use-aes=y"], "KEY-LEN=256"),
(&["u", "o", "256", "--force-V4"], "KEY-LEN=256"),
(&["u", "", "128", "--allow-insecure"], "KEY-LEN=128"),
];
for (enc_args, needle) in cases {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
let mut cmd = Command::cargo_bin("flpdf").unwrap();
cmd.arg("--allow-weak-crypto").arg("--encrypt");
for a in *enc_args {
cmd.arg(a);
}
cmd.arg("--")
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains(*needle));
assert!(
!output.exists(),
"no output for incompatible combo {enc_args:?}"
);
}
}
#[test]
fn encrypt_permission_sub_flags_match_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
fn perm_lines(p: &Path) -> Vec<String> {
let out = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--show-encryption")
.arg(p)
.output()
.unwrap();
assert!(out.status.success());
let s = String::from_utf8_lossy(&out.stdout);
let mut v: Vec<String> = s
.lines()
.filter(|l| l.ends_with("allowed"))
.map(|l| l.to_string())
.collect();
v.sort();
v
}
let profiles: &[(&str, &[&str], &[&str])] = &[
("256-r6", &[], &["--encrypt", "u", "o", "256"]),
(
"128-aes-r4",
&[],
&["--encrypt", "u", "o", "128", "--use-aes=y"],
),
(
"128-rc4-r3",
&["--allow-weak-crypto"],
&["--encrypt", "u", "o", "128"],
),
];
let combos: &[&[&str]] = &[
&["--modify=none"],
&["--modify=annotate"],
&["--print=low", "--extract=n"],
&["--modify=none", "--annotate=y"], &["--annotate=y", "--modify=none"], &["--print=none", "--modify=none", "--extract=n"], &["--accessibility=n"], ];
let tmp = tempfile::tempdir().unwrap();
for (label, prefix, enc_base) in profiles {
for combo in combos {
let flpdf_out = tmp.path().join("flpdf.pdf");
let qpdf_out = tmp.path().join("qpdf.pdf");
let mut c = Command::cargo_bin("flpdf").unwrap();
c.args(*prefix).args(*enc_base);
for a in *combo {
c.arg(a);
}
c.arg("--")
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&flpdf_out)
.assert()
.success();
let mut q = ShellCommand::new("qpdf");
q.args(*prefix).args(*enc_base);
for a in *combo {
q.arg(a);
}
let st = q
.arg("--")
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&qpdf_out)
.status()
.unwrap();
assert!(st.success(), "qpdf encrypt failed for {label} {combo:?}");
assert_eq!(
perm_lines(&flpdf_out),
perm_lines(&qpdf_out),
"/P permissions differ from qpdf for {label} {combo:?}"
);
}
}
}
#[test]
fn encrypt_cleartext_metadata_accept_reject_matrix() {
let tmp = tempfile::tempdir().unwrap();
let ok = tmp.path().join("ct256.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "256", "--cleartext-metadata", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&ok)
.assert()
.success();
let bytes = std::fs::read(&ok).unwrap();
assert!(
bytes
.windows(b"/EncryptMetadata false".len())
.any(|w| w == b"/EncryptMetadata false"),
"256 --cleartext-metadata must emit /EncryptMetadata false"
);
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--encrypt",
"u",
"o",
"128",
"--use-aes=y",
"--cleartext-metadata",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(tmp.path().join("ct128aes.pdf"))
.assert()
.success();
for args in [
vec![
"--allow-weak-crypto",
"--encrypt",
"u",
"o",
"40",
"--cleartext-metadata",
"--",
],
vec![
"--allow-weak-crypto",
"--encrypt",
"u",
"o",
"128",
"--cleartext-metadata",
"--",
],
] {
let nope = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(&args)
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&nope)
.assert()
.failure()
.stderr(predicates::str::contains(
"--cleartext-metadata requires V=4 or V=5",
));
assert!(!nope.exists(), "no output for rejected combo {args:?}");
}
}
#[test]
fn encrypt_invalid_key_len_value_is_rejected() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "not-a-number", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("KEY-LEN"));
}
#[test]
fn encrypt_conflicts_with_check_inspection_path() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--check", "--encrypt", "u", "o", "128", "--use-aes=y", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("cannot be used"));
}
#[test]
fn encrypt_is_rejected_when_combined_with_page_operations_top_level() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--encrypt",
"u",
"o",
"128",
"--use-aes=y",
"--",
"--pages",
".",
"1-z",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("--encrypt"))
.stderr(predicates::str::contains("--pages"));
assert!(!output.exists());
}
#[test]
fn encrypt_is_rejected_when_combined_with_page_operations_subcommand() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--encrypt",
"u",
"o",
"128",
"--use-aes=y",
"--",
"--pages",
".",
"1-z",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("--encrypt"))
.stderr(predicates::str::contains("--pages"));
assert!(!output.exists());
}
#[test]
fn encrypt_conflicts_with_decrypt_flag() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--decrypt",
"--encrypt",
"u",
"o",
"128",
"--use-aes=y",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("cannot be used"));
}
#[test]
fn static_aes_iv_with_static_id_produces_deterministic_output() {
let tmp = tempfile::tempdir().unwrap();
let out1 = tmp.path().join("encrypted1.pdf");
let out2 = tmp.path().join("encrypted2.pdf");
let input = fixture(UNENCRYPTED_FIXTURE);
for output in [&out1, &out2] {
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--static-id",
"--static-aes-iv",
"--encrypt",
"user",
"owner",
"128",
"--use-aes=y",
"--",
])
.arg(&input)
.arg(output)
.assert()
.success();
}
let bytes1 = std::fs::read(&out1).unwrap();
let bytes2 = std::fs::read(&out2).unwrap();
assert_eq!(
bytes1, bytes2,
"--static-id --static-aes-iv must produce byte-identical output on two runs"
);
assert!(
bytes1.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"output must carry /Encrypt"
);
}
#[test]
fn without_static_aes_iv_two_runs_produce_different_bytes() {
let tmp = tempfile::tempdir().unwrap();
let out1 = tmp.path().join("encrypted1.pdf");
let out2 = tmp.path().join("encrypted2.pdf");
let input = fixture(ONE_PAGE_FIXTURE);
for output in [&out1, &out2] {
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--static-id",
"--encrypt",
"user",
"owner",
"128",
"--use-aes=y",
"--",
])
.arg(&input)
.arg(output)
.assert()
.success();
}
let bytes1 = std::fs::read(&out1).unwrap();
let bytes2 = std::fs::read(&out2).unwrap();
assert_ne!(
bytes1, bytes2,
"without --static-aes-iv, two encrypted runs with streams must differ (random IVs)"
);
}
#[test]
fn static_aes_iv_output_decrypts_cleanly_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let encrypted = tmp.path().join("encrypted.pdf");
let decrypted = tmp.path().join("decrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--static-id",
"--static-aes-iv",
"--encrypt",
"user",
"owner",
"128",
"--use-aes=y",
"--",
])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&encrypted)
.assert()
.success();
let decrypt = std::process::Command::new("qpdf")
.arg("--password=user")
.arg("--decrypt")
.arg(&encrypted)
.arg(&decrypted)
.output()
.unwrap();
assert!(
decrypt.status.success(),
"qpdf --decrypt failed on --static-aes-iv output: {}",
String::from_utf8_lossy(&decrypt.stderr)
);
}
#[test]
fn rewrite_subcommand_static_aes_iv_produces_deterministic_output() {
let tmp = tempfile::tempdir().unwrap();
let out1 = tmp.path().join("encrypted1.pdf");
let out2 = tmp.path().join("encrypted2.pdf");
let input = fixture(UNENCRYPTED_FIXTURE);
for output in [&out1, &out2] {
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--static-id",
"--static-aes-iv",
"--encrypt",
"user",
"owner",
"128",
"--use-aes=y",
"--",
])
.arg(&input)
.arg(output)
.assert()
.success();
}
let bytes1 = std::fs::read(&out1).unwrap();
let bytes2 = std::fs::read(&out2).unwrap();
assert_eq!(
bytes1, bytes2,
"rewrite --static-id --static-aes-iv must produce byte-identical output"
);
}
fn make_donor_pdf(tmp: &tempfile::TempDir, user_pw: &str, owner_pw: &str) -> PathBuf {
let donor = tmp.path().join("donor.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--static-id",
"--static-aes-iv",
"--encrypt",
user_pw,
owner_pw,
"128",
"--use-aes=y",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&donor)
.assert()
.success();
donor
}
#[test]
fn copy_encryption_from_output_has_encrypt_dict() {
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "secretuser", "secretowner");
let out = tmp.path().join("copy_out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"secretuser",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.success();
let bytes = std::fs::read(&out).unwrap();
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"copy-encryption-from output must carry /Encrypt"
);
}
#[test]
fn copy_encryption_from_decrypts_with_donor_user_password_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "donoruser", "donorowner");
let out = tmp.path().join("copy_out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"donoruser",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=donoruser")
.arg("--show-encryption")
.arg(&out)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed with donor user password: {}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 4"),
"qpdf must report R=4 on copy-encryption-from output: {stdout}"
);
assert!(
stdout.contains("Supplied password is user password"),
"qpdf must accept donor user password: {stdout}"
);
}
#[test]
fn copy_encryption_from_decrypts_with_donor_owner_password_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "userpass", "ownerpass");
let out = tmp.path().join("copy_out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"userpass",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=ownerpass")
.arg("--show-encryption")
.arg(&out)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("Supplied password is owner password"),
"qpdf must accept donor owner password: {stdout}"
);
}
#[test]
fn copy_encryption_from_round_trip_decrypts_cleanly_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "pw", "pw");
let encrypted = tmp.path().join("encrypted.pdf");
let decrypted = tmp.path().join("decrypted.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"pw",
])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&encrypted)
.assert()
.success();
let decrypt = ShellCommand::new("qpdf")
.arg("--password=pw")
.arg("--decrypt")
.arg(&encrypted)
.arg(&decrypted)
.output()
.unwrap();
assert!(
decrypt.status.success(),
"qpdf --decrypt failed on copy-encryption-from output: {}",
String::from_utf8_lossy(&decrypt.stderr)
);
}
#[test]
fn rewrite_subcommand_copy_encryption_from_succeeds() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "donorpw", "ownerpw");
let out = tmp.path().join("out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"donorpw",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=donorpw")
.arg("--show-encryption")
.arg(&out)
.output()
.unwrap();
assert!(check.status.success());
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 4") && stdout.contains("Supplied password is user password"),
"rewrite --copy-encryption-from: qpdf must report R=4 + user-password match: {stdout}"
);
}
#[test]
fn copy_encryption_floors_pdf_header_to_1_6() {
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "user-pw", "owner-pw");
let out = tmp.path().join("copied.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.arg("--copy-encryption-from")
.arg(&donor)
.args(["--encryption-file-password", "user-pw", "--"])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&out)
.assert()
.success();
let bytes = std::fs::read(&out).unwrap();
assert!(
bytes.starts_with(b"%PDF-1.6"),
"copy-encryption (V=4 AES-128 donor) must floor the header to 1.6, got {:?}",
String::from_utf8_lossy(&bytes[..bytes.len().min(12)])
);
}
#[test]
fn copy_encryption_from_unencrypted_donor_is_rejected() {
let tmp = tempfile::tempdir().unwrap();
let out = tmp.path().join("out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
fixture(UNENCRYPTED_FIXTURE).to_str().unwrap(),
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.failure()
.stderr(predicates::str::contains("not encrypted"));
}
#[test]
fn copy_encryption_from_wrong_password_is_rejected() {
let tmp = tempfile::tempdir().unwrap();
let donor = make_donor_pdf(&tmp, "correctpw", "ownerpw");
let out = tmp.path().join("out.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--copy-encryption-from",
donor.to_str().unwrap(),
"--encryption-file-password",
"wrongpw",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&out)
.assert()
.failure()
.stderr(predicates::str::contains("--copy-encryption-from"));
assert!(!out.exists());
}
#[test]
fn encrypt_force_r5_flpdf_show_encryption_reports_r5() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("r5.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--allow-weak-crypto",
"--encrypt",
"user-pw",
"owner-pw",
"256",
"--force-R5",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let show = Command::cargo_bin("flpdf")
.unwrap()
.args([
"show-encryption",
"--allow-weak-crypto",
"--password=user-pw",
])
.arg(&output)
.assert()
.success();
let stdout = String::from_utf8_lossy(&show.get_output().stdout).into_owned();
for needle in ["V = 5", "R = 5", "Length = 256", "AESv3"] {
assert!(
stdout.contains(needle),
"flpdf show-encryption must report {needle:?} for --force-R5 output: {stdout}"
);
}
assert!(
!stdout.contains("R = 6"),
"flpdf show-encryption must NOT report R=6 for --force-R5 output: {stdout}"
);
}
#[test]
fn encrypt_force_r5_rejected_for_128_bit() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "128", "--force-R5", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("--force-R5"));
}
#[test]
fn encrypt_force_r5_rejected_for_40_bit() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--allow-weak-crypto",
"--encrypt",
"u",
"o",
"40",
"--force-R5",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("--force-R5"));
}
#[test]
fn encrypt_force_r5_rejects_value_form() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("nope.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "u", "o", "256", "--force-R5=y", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("does not take a value"));
}
#[test]
fn encrypt_force_r5_rejected_without_allow_weak_crypto() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("r5-gated.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--encrypt",
"user-pw",
"owner-pw",
"256",
"--force-R5",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.failure()
.stderr(predicates::str::contains("--allow-weak-crypto"));
assert!(
!output.exists(),
"no output file must be written when the R=5 weak-crypto gate fires"
);
}
#[test]
fn encrypt_r6_default_not_gated_without_allow_weak_crypto() {
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("r6.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "user-pw", "owner-pw", "256", "--"])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let show = Command::cargo_bin("flpdf")
.unwrap()
.args(["show-encryption", "--password=user-pw"])
.arg(&output)
.assert()
.success();
let stdout = String::from_utf8_lossy(&show.get_output().stdout).into_owned();
assert!(
stdout.contains("R = 6"),
"default 256-bit output must be R=6: {stdout}"
);
}
#[test]
fn encrypt_force_r5_round_trips_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("r5-qpdf.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"--allow-weak-crypto",
"--encrypt",
"user-pw",
"owner-pw",
"256",
"--force-R5",
"--",
])
.arg(fixture(UNENCRYPTED_FIXTURE))
.arg(&output)
.assert()
.success();
let check = ShellCommand::new("qpdf")
.arg("--password=user-pw")
.arg("--allow-weak-crypto")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed: {}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(
stdout.contains("R = 5") && stdout.contains("Supplied password is user password"),
"qpdf must report R=5 + user password match: {stdout}"
);
let owner_check = ShellCommand::new("qpdf")
.arg("--password=owner-pw")
.arg("--allow-weak-crypto")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(owner_check.status.success());
let owner_out = String::from_utf8_lossy(&owner_check.stdout);
assert!(
owner_out.contains("Supplied password is owner password"),
"qpdf must accept the owner password: {owner_out}"
);
}
#[test]
fn encrypt_with_generate_object_streams_round_trips_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let output = tmp.path().join("encrypted_objstm.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--object-streams=generate",
"--encrypt",
"user-pw",
"owner-pw",
"128",
"--use-aes=y",
"--",
])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&output)
.assert()
.success();
let bytes = std::fs::read(&output).unwrap();
assert!(
bytes.windows(b"/ObjStm".len()).any(|w| w == b"/ObjStm"),
"output must contain at least one /ObjStm container"
);
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"output must carry /Encrypt"
);
let check = std::process::Command::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&output)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed:\n{}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(stdout.contains("R = 4"), "qpdf must report R=4: {stdout}");
let decrypted = tmp.path().join("decrypted_from_objstm.pdf");
let decrypt_result = std::process::Command::new("qpdf")
.arg("--password=user-pw")
.arg("--decrypt")
.arg("--static-id")
.arg(&output)
.arg(&decrypted)
.output()
.unwrap();
assert!(
decrypt_result.status.success(),
"qpdf --decrypt failed:\n{}",
String::from_utf8_lossy(&decrypt_result.stderr)
);
let check_result = std::process::Command::new("qpdf")
.arg("--check")
.arg(&decrypted)
.output()
.unwrap();
assert!(
check_result.status.success(),
"qpdf --check on decrypted PDF failed:\n{}",
String::from_utf8_lossy(&check_result.stderr)
);
}
#[test]
fn encrypt_preserves_xref_stream_form_when_objstm_disabled() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let xref_stream_source = tmp.path().join("xref_stream_source.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["rewrite", "--full-rewrite", "--object-streams=generate"])
.arg(fixture(ONE_PAGE_FIXTURE))
.arg(&xref_stream_source)
.assert()
.success();
let encrypted = tmp.path().join("encrypted_xref_stream.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args([
"rewrite",
"--full-rewrite",
"--object-streams=disable",
"--encrypt",
"user-pw",
"owner-pw",
"128",
"--use-aes=y",
"--",
])
.arg(&xref_stream_source)
.arg(&encrypted)
.assert()
.success();
let bytes = std::fs::read(&encrypted).unwrap();
assert!(
bytes.windows(b"/XRef".len()).any(|w| w == b"/XRef"),
"output must use xref stream form (/Type /XRef), not a classic xref table"
);
assert!(
!bytes.windows(b"\nxref\n".len()).any(|w| w == b"\nxref\n"),
"output must not contain a classic xref table (\\nxref\\n keyword found)"
);
assert!(
!bytes.windows(b"/ObjStm".len()).any(|w| w == b"/ObjStm"),
"output must not contain ObjStm containers when --object-streams=disable"
);
assert!(
bytes.windows(b"/Encrypt".len()).any(|w| w == b"/Encrypt"),
"output must carry /Encrypt"
);
let check = std::process::Command::new("qpdf")
.arg("--password=user-pw")
.arg("--show-encryption")
.arg(&encrypted)
.output()
.unwrap();
assert!(
check.status.success(),
"qpdf --show-encryption failed:\n{}",
String::from_utf8_lossy(&check.stderr)
);
let stdout = String::from_utf8_lossy(&check.stdout);
assert!(stdout.contains("R = 4"), "qpdf must report R=4: {stdout}");
let decrypted = tmp.path().join("decrypted_xref_stream.pdf");
let decrypt_result = std::process::Command::new("qpdf")
.arg("--password=user-pw")
.arg("--decrypt")
.arg("--static-id")
.arg(&encrypted)
.arg(&decrypted)
.output()
.unwrap();
assert!(
decrypt_result.status.success(),
"qpdf --decrypt failed:\n{}",
String::from_utf8_lossy(&decrypt_result.stderr)
);
let check_result = std::process::Command::new("qpdf")
.arg("--check")
.arg(&decrypted)
.output()
.unwrap();
assert!(
check_result.status.success(),
"qpdf --check on decrypted PDF failed:\n{}",
String::from_utf8_lossy(&check_result.stderr)
);
}