use assert_cmd::Command as CargoCommand;
use predicates::prelude::*;
use std::path::{Path, PathBuf};
use tempfile::tempdir;
const COMPAT_FIXTURE_DIR: &str = "../../tests/fixtures/compat";
fn fixture_path(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join(COMPAT_FIXTURE_DIR)
.join(name)
}
fn run_rewrite_det_id(input: &Path, output: &Path) {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["rewrite", "--deterministic-id"])
.arg(input)
.arg(output)
.assert()
.success();
}
fn extract_id_array(pdf_bytes: &[u8]) -> (Vec<u8>, Vec<u8>) {
let idx = pdf_bytes
.windows(3)
.enumerate()
.filter(|(_, w)| *w == b"/ID")
.map(|(i, _)| i)
.next_back()
.expect("/ID not found");
let after = &pdf_bytes[idx + 3..];
let open = after.iter().position(|&b| b == b'[').expect("expected [");
let body = &after[open + 1..];
let lt0 = body.iter().position(|&b| b == b'<').expect("expected <");
let gt0 = body[lt0 + 1..]
.iter()
.position(|&b| b == b'>')
.expect("expected >");
let first = body[lt0 + 1..lt0 + 1 + gt0].to_vec();
let rest = &body[lt0 + gt0 + 2..];
let lt1 = rest.iter().position(|&b| b == b'<').expect("expected <");
let gt1 = rest[lt1 + 1..]
.iter()
.position(|&b| b == b'>')
.expect("expected >");
let second = rest[lt1 + 1..lt1 + 1 + gt1].to_vec();
(first, second)
}
#[test]
fn deterministic_id_is_stable_across_runs() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let a = tmp.path().join("a.pdf");
let b = tmp.path().join("b.pdf");
run_rewrite_det_id(&input, &a);
run_rewrite_det_id(&input, &b);
assert_eq!(
std::fs::read(&a).expect("read a"),
std::fs::read(&b).expect("read b"),
"two --deterministic-id runs must be byte-identical"
);
}
#[test]
fn deterministic_id_preserves_permanent_id_and_is_content_dependent() {
let tmp = tempdir().expect("tempdir");
let out1 = tmp.path().join("one.pdf");
let out2 = tmp.path().join("two.pdf");
run_rewrite_det_id(&fixture_path("one-page.pdf"), &out1);
run_rewrite_det_id(&fixture_path("two-page.pdf"), &out2);
let (id0_1, id1_1) = extract_id_array(&std::fs::read(&out1).expect("read one"));
let (_id0_2, id1_2) = extract_id_array(&std::fs::read(&out2).expect("read two"));
assert_eq!(
id0_1.as_slice(),
b"2dc780102304e5176780e8127fa6438c",
"/ID[0] must be preserved from the source permanent identifier"
);
assert_ne!(
id0_1, id1_1,
"permanent and changing identifiers must differ"
);
assert_ne!(
id1_1, id1_2,
"different input content must yield a different /ID[1]"
);
assert_ne!(
id1_1.as_slice(),
b"31415926535897932384626433832795",
"deterministic /ID[1] must not be the --static-id constant"
);
}
#[test]
fn static_id_takes_precedence_over_deterministic_id_on_both_cli_surfaces() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let rewrite_both = tmp.path().join("rewrite-both.pdf");
let rewrite_static = tmp.path().join("rewrite-static.pdf");
let top_level_both = tmp.path().join("top-level-both.pdf");
let top_level_static = tmp.path().join("top-level-static.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["rewrite", "--deterministic-id", "--static-id"])
.arg(&input)
.arg(&rewrite_both)
.assert()
.success();
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["rewrite", "--static-id"])
.arg(&input)
.arg(&rewrite_static)
.assert()
.success();
assert_eq!(
std::fs::read(&rewrite_both).expect("read rewrite both"),
std::fs::read(&rewrite_static).expect("read rewrite static"),
"rewrite --static-id must take precedence over --deterministic-id"
);
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["--deterministic-id", "--static-id"])
.arg(&input)
.arg(&top_level_both)
.assert()
.success();
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--static-id")
.arg(&input)
.arg(&top_level_static)
.assert()
.success();
assert_eq!(
std::fs::read(&top_level_both).expect("read top-level both"),
std::fs::read(&top_level_static).expect("read top-level static"),
"top-level --static-id must take precedence over --deterministic-id"
);
let rewrite_linearized_both = tmp.path().join("rewrite-linearized-both.pdf");
let rewrite_linearized_static = tmp.path().join("rewrite-linearized-static.pdf");
for (output, flags) in [
(
&rewrite_linearized_both,
[
"rewrite",
"--linearize",
"--deterministic-id",
"--static-id",
]
.as_slice(),
),
(
&rewrite_linearized_static,
["rewrite", "--linearize", "--static-id"].as_slice(),
),
] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(flags)
.arg(&input)
.arg(output)
.assert()
.success();
}
assert_eq!(
std::fs::read(&rewrite_linearized_both).expect("read linearized both"),
std::fs::read(&rewrite_linearized_static).expect("read linearized static"),
"linearized --static-id must take precedence over --deterministic-id"
);
let top_level_objstm_both = tmp.path().join("top-level-objstm-both.pdf");
let top_level_objstm_static = tmp.path().join("top-level-objstm-static.pdf");
for (output, flags) in [
(
&top_level_objstm_both,
[
"--object-streams=generate",
"--deterministic-id",
"--static-id",
]
.as_slice(),
),
(
&top_level_objstm_static,
["--object-streams=generate", "--static-id"].as_slice(),
),
] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(flags)
.arg(&input)
.arg(output)
.assert()
.success();
}
assert_eq!(
std::fs::read(&top_level_objstm_both).expect("read object-stream both"),
std::fs::read(&top_level_objstm_static).expect("read object-stream static"),
"object-stream output must use static ID precedence"
);
}
#[test]
fn deterministic_id_incompatible_with_encryption() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["--deterministic-id", "--encrypt", "u", "o", "256", "--"])
.arg(&input)
.arg(&output)
.assert()
.failure()
.stderr(predicate::str::contains(
"INTERNAL ERROR: QPDFWriter::generateID has no data for deterministic ID",
));
}
#[test]
fn static_and_deterministic_id_remain_incompatible_with_encryption() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
for extra in [&[][..], &["--linearize"][..]] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["--static-id", "--deterministic-id"])
.args(extra)
.args(["--encrypt", "u", "o", "256", "--"])
.arg(&input)
.arg(&output)
.assert()
.failure()
.code(2)
.stderr(predicate::str::contains(
"flpdf: Deterministic ID computation enabled after ID generation has already occurred.",
))
.stderr(predicate::str::contains("generateID has no data").not());
}
}
#[test]
fn deterministic_id_is_accepted_with_json() {
let input = fixture_path("one-page.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["--json", "--deterministic-id"])
.arg(&input)
.assert()
.success()
.stdout(predicate::str::contains("\"version\": 2"));
}
fn run_rewrite_linearize_det_id(input: &Path, output: &Path) {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["rewrite", "--linearize", "--deterministic-id"])
.arg(input)
.arg(output)
.assert()
.success();
}
#[test]
fn deterministic_id_with_linearize_is_stable_across_runs() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let a = tmp.path().join("lin-a.pdf");
let b = tmp.path().join("lin-b.pdf");
run_rewrite_linearize_det_id(&input, &a);
run_rewrite_linearize_det_id(&input, &b);
let bytes_a = std::fs::read(&a).expect("read a");
let bytes_b = std::fs::read(&b).expect("read b");
assert_eq!(
bytes_a, bytes_b,
"two --linearize --deterministic-id runs must be byte-identical"
);
assert!(
bytes_a
.windows(b"/Linearized".len())
.any(|w| w == b"/Linearized"),
"output must be linearized"
);
let (id0, id1) = extract_id_array(&bytes_a);
assert_ne!(
id0,
vec![b'0'; 32],
"/ID[0] placeholder must be patched to a real value"
);
assert_ne!(
id1,
vec![b'0'; 32],
"/ID[1] placeholder must be patched to a real value"
);
}
#[test]
fn deterministic_id_emits_no_testing_warning() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let output = tmp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["rewrite", "--deterministic-id"])
.arg(&input)
.arg(&output)
.assert()
.success()
.stderr(predicate::str::contains("testing only").not());
}
#[test]
fn deterministic_id_is_honored_in_page_ops_pipeline() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("three-page.pdf");
let a = tmp.path().join("a.pdf");
let b = tmp.path().join("b.pdf");
for out in [&a, &b] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--deterministic-id")
.arg(&input)
.args(["--pages", ".", "1-2", "--"])
.arg(out)
.assert()
.success();
}
let (id0, id1) = extract_id_array(&std::fs::read(&a).expect("read a"));
assert_ne!(id0, id1, "permanent /ID[0] and changing /ID[1] must differ");
assert_eq!(
std::fs::read(&a).expect("read a"),
std::fs::read(&b).expect("read b"),
"page-ops --deterministic-id must be byte-stable across runs"
);
}
#[test]
fn deterministic_id_honored_in_split_pages() {
let tmp = tempdir().expect("tempdir");
let a = tmp.path().join("a");
let b = tmp.path().join("b");
std::fs::create_dir_all(&a).expect("mkdir a");
std::fs::create_dir_all(&b).expect("mkdir b");
let input = fixture_path("three-page.pdf");
for dir in [&a, &b] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--deterministic-id")
.arg(&input)
.arg("--split-pages")
.arg("--")
.arg(dir.join("out.pdf"))
.assert()
.success();
}
assert_eq!(
std::fs::read(a.join("out-1.pdf")).expect("chunk a"),
std::fs::read(b.join("out-1.pdf")).expect("chunk b"),
"split chunk must be byte-stable under --deterministic-id"
);
}
#[test]
fn deterministic_id_honored_in_add_attachment() {
let tmp = tempdir().expect("tempdir");
let att = tmp.path().join("payload.txt");
std::fs::write(&att, b"attachment payload").expect("write attachment");
let input = fixture_path("three-page.pdf");
let o1 = tmp.path().join("a.pdf");
let o2 = tmp.path().join("b.pdf");
for out in [&o1, &o2] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--deterministic-id")
.arg("--add-attachment")
.arg(&att)
.args([
"--creationdate=D:20240101120000Z",
"--moddate=D:20240102130000Z",
])
.arg("--")
.arg(&input)
.arg(out)
.assert()
.success();
}
assert_eq!(
std::fs::read(&o1).expect("read a"),
std::fs::read(&o2).expect("read b"),
"add-attachment output must be byte-stable under --deterministic-id"
);
}
#[test]
fn deterministic_id_accepted_with_check_like_qpdf() {
let input = fixture_path("one-page.pdf");
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.args(["--check", "--deterministic-id"])
.arg(&input)
.assert()
.success();
}
#[test]
fn deterministic_id_top_level_alias_is_stable() {
let tmp = tempdir().expect("tempdir");
let input = fixture_path("one-page.pdf");
let a = tmp.path().join("a.pdf");
let b = tmp.path().join("b.pdf");
for out in [&a, &b] {
CargoCommand::cargo_bin("flpdf")
.expect("flpdf binary")
.arg("--deterministic-id")
.arg(&input)
.arg(out)
.assert()
.success();
}
assert_eq!(
std::fs::read(&a).expect("read a"),
std::fs::read(&b).expect("read b"),
"two top-level --deterministic-id runs must be byte-identical"
);
}