#![cfg(feature = "qpdf-zlib-compat")]
use assert_cmd::Command;
use std::path::{Path, PathBuf};
use std::process::Command as StdCommand;
fn fixture(stem: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(format!("{stem}.pdf"))
}
fn golden(stem: &str, kind: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/golden/references")
.join(stem)
.join(format!("{kind}.pdf"));
std::fs::read(&path).unwrap_or_else(|e| panic!("read golden {path:?}: {e}"))
}
fn run_cli(stem: &str, extra: &[&str]) -> Vec<u8> {
let outdir = tempfile::tempdir().unwrap();
let out = outdir.path().join("out.pdf");
let input = fixture(stem);
Command::cargo_bin("flpdf")
.unwrap()
.arg("rewrite")
.arg("--linearize")
.args(extra)
.arg("--deterministic-id")
.arg(&input)
.arg(&out)
.assert()
.success();
std::fs::read(&out).unwrap_or_else(|e| panic!("read flpdf output for {stem}: {e}"))
}
fn run_cli_top_level_linearized(stem: &str, extra: &[&str]) -> Vec<u8> {
let outdir = tempfile::tempdir().unwrap();
let out = outdir.path().join("out.pdf");
let input = fixture(stem);
Command::cargo_bin("flpdf")
.unwrap()
.args(["--warning-exit-0", "--linearize"])
.args(extra)
.arg("--deterministic-id")
.arg(&input)
.arg(&out)
.assert()
.success();
std::fs::read(&out).unwrap_or_else(|e| panic!("read flpdf output for {stem}: {e}"))
}
const EXPECTED_QPDF_VERSION: &str = "qpdf version 11.9.0";
fn qpdf_available() -> bool {
StdCommand::new("qpdf")
.arg("--version")
.output()
.map(|output| {
output.status.success()
&& String::from_utf8_lossy(&output.stdout)
.lines()
.next()
.is_some_and(|line| line.trim() == EXPECTED_QPDF_VERSION)
})
.unwrap_or(false)
}
fn run_qpdf(stem: &str, extra: &[&str]) -> Option<Vec<u8>> {
if !qpdf_available() {
if std::env::var_os("CI").is_some() {
panic!(
"{EXPECTED_QPDF_VERSION} is required for linearized byte-parity assertions on CI"
);
}
eprintln!("skipping qpdf byte-parity assertion: {EXPECTED_QPDF_VERSION} is not available");
return None;
}
let outdir = tempfile::tempdir().unwrap();
let out = outdir.path().join("out.pdf");
let input = fixture(stem);
let status = StdCommand::new("qpdf")
.arg("--deterministic-id")
.arg("--linearize")
.arg("--warning-exit-0")
.args(extra)
.arg(&input)
.arg(&out)
.status()
.expect("spawn qpdf linearized byte-parity oracle");
assert!(
status.success(),
"qpdf linearized byte-parity oracle failed"
);
Some(std::fs::read(&out).unwrap_or_else(|e| panic!("read qpdf output for {stem}: {e}")))
}
fn run_cli_full_rewrite_static_id(stem: &str) -> Vec<u8> {
let outdir = tempfile::tempdir().unwrap();
let out = outdir.path().join("out.pdf");
let input = fixture(stem);
Command::cargo_bin("flpdf")
.unwrap()
.arg("rewrite")
.arg("--static-id")
.arg(&input)
.arg(&out)
.assert()
.success();
std::fs::read(&out).unwrap_or_else(|e| panic!("read flpdf output for {stem}: {e}"))
}
fn run_cli_coalesce_qdf_static_id(stem: &str) -> Vec<u8> {
let outdir = tempfile::tempdir().unwrap();
let out = outdir.path().join("out.pdf");
let input = fixture(stem);
Command::cargo_bin("flpdf")
.unwrap()
.arg("rewrite")
.arg("--qdf")
.arg("--coalesce-contents")
.arg("--static-id")
.arg(&input)
.arg(&out)
.assert()
.success();
std::fs::read(&out).unwrap_or_else(|e| panic!("read flpdf output for {stem}: {e}"))
}
fn assert_byte_identical(stem: &str, kind: &str, extra: &[&str]) {
let actual = run_cli(stem, extra);
let expected = golden(stem, kind);
if actual == expected {
return;
}
let common = actual.len().min(expected.len());
let off = (0..common)
.find(|&i| actual[i] != expected[i])
.unwrap_or(common);
let lo = off.saturating_sub(24);
panic!(
"{stem} ({kind}): CLI output diverged from qpdf golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
String::from_utf8_lossy(&actual[lo..(off + 24).min(actual.len())]),
String::from_utf8_lossy(&expected[lo..(off + 24).min(expected.len())]),
);
}
fn assert_linearize_newline_byte_identical(stem: &str, extra: &[&str]) {
let Some(expected) = run_qpdf(stem, extra) else {
return;
};
let actual = run_cli(stem, extra);
if actual == expected {
return;
}
let common = actual.len().min(expected.len());
let off = (0..common)
.find(|&i| actual[i] != expected[i])
.unwrap_or(common);
let lo = off.saturating_sub(24);
panic!(
"{stem} (linearize --newline-before-endstream): CLI output diverged from qpdf 11.9.0 \
(flpdf={} bytes, qpdf={} bytes, first diff at byte {off})\n\
flpdf: {:?}\nqpdf: {:?}",
actual.len(),
expected.len(),
String::from_utf8_lossy(&actual[lo..(off + 24).min(actual.len())]),
String::from_utf8_lossy(&expected[lo..(off + 24).min(expected.len())]),
);
}
fn assert_linearize_normalize_content_byte_identical(stem: &str) {
let Some(expected) = run_qpdf(stem, &["--normalize-content=y"]) else {
return;
};
let actual = run_cli_top_level_linearized(stem, &["--normalize-content=y"]);
if actual == expected {
return;
}
let common = actual.len().min(expected.len());
let off = (0..common)
.find(|&i| actual[i] != expected[i])
.unwrap_or(common);
let lo = off.saturating_sub(24);
panic!(
"{stem} (linearize --normalize-content=y): CLI output diverged from qpdf 11.9.0 \
(flpdf={} bytes, qpdf={} bytes, first diff at byte {off})\n\
flpdf: {:?}\nqpdf: {:?}",
actual.len(),
expected.len(),
String::from_utf8_lossy(&actual[lo..(off + 24).min(actual.len())]),
String::from_utf8_lossy(&expected[lo..(off + 24).min(expected.len())]),
);
}
#[test]
fn cli_one_page_linearize_newline_before_endstream_matches_qpdf() {
assert_linearize_newline_byte_identical("one-page", &["--newline-before-endstream"]);
}
#[test]
fn cli_three_page_linearize_objstm_newline_before_endstream_matches_qpdf() {
assert_linearize_newline_byte_identical(
"three-page",
&["--object-streams=generate", "--newline-before-endstream"],
);
}
#[test]
fn cli_two_page_linearize_objstm_byte_identical() {
assert_byte_identical(
"two-page",
"linearize-objstm",
&["--object-streams=generate"],
);
}
#[test]
fn cli_three_page_linearize_objstm_byte_identical() {
assert_byte_identical(
"three-page",
"linearize-objstm",
&["--object-streams=generate"],
);
}
#[test]
fn cli_shared_stream_linearize_objstm_byte_identical() {
assert_byte_identical(
"shared-stream-objstm",
"linearize-objstm",
&["--object-streams=generate"],
);
}
#[test]
fn cli_one_page_linearize_byte_identical() {
assert_byte_identical("one-page", "linearize", &[]);
}
#[test]
fn cli_two_page_linearize_byte_identical() {
assert_byte_identical("two-page", "linearize", &[]);
}
#[test]
fn cli_three_page_linearize_byte_identical() {
assert_byte_identical("three-page", "linearize", &[]);
}
fn malformed_live_page_contents_pdf() -> Vec<u8> {
let objects: [&[u8]; 4] = [
b"<< /Type /Catalog /Pages 2 0 R >>",
b"<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 100 100] >>",
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R /Resources << >> >>",
b"[",
];
let mut bytes = b"%PDF-1.3\n".to_vec();
let mut offsets = [0usize; 5];
for (index, body) in objects.iter().enumerate() {
let object_number = index + 1;
offsets[object_number] = bytes.len();
bytes.extend_from_slice(format!("{object_number} 0 obj\n").as_bytes());
bytes.extend_from_slice(body);
bytes.extend_from_slice(b"\nendobj\n");
}
let xref_offset = bytes.len();
bytes.extend_from_slice(b"xref\n0 5\n0000000000 65535 f \n");
for offset in offsets.iter().skip(1) {
bytes.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
}
bytes.extend_from_slice(b"trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n");
bytes.extend_from_slice(format!("{xref_offset}\n%%EOF\n").as_bytes());
bytes
}
#[test]
fn linearize_malformed_live_page_contents_matches_qpdf_recovery() {
if !qpdf_available() {
if std::env::var_os("CI").is_some() {
panic!("{EXPECTED_QPDF_VERSION} is required for linearization recovery parity");
}
eprintln!("skipping malformed-object linearization parity: qpdf 11.9.0 is unavailable");
return;
}
let directory = tempfile::tempdir().expect("temporary directory");
let input = directory.path().join("malformed-live-content.pdf");
let qpdf_output = directory.path().join("qpdf-linearized.pdf");
let flpdf_output = directory.path().join("flpdf-linearized.pdf");
std::fs::write(&input, malformed_live_page_contents_pdf()).expect("write synthetic input");
let qpdf = StdCommand::new("qpdf")
.args(["--linearize", "--static-id"])
.arg(&input)
.arg(&qpdf_output)
.output()
.expect("run qpdf 11.9.0 linearization");
let flpdf = Command::cargo_bin("flpdf")
.expect("flpdf binary")
.env("FLPDF_PROGNAME", "qpdf")
.args(["--linearize", "--static-id"])
.arg(&input)
.arg(&flpdf_output)
.output()
.expect("run flpdf linearization");
assert_eq!(
qpdf.status.code(),
Some(3),
"qpdf must report its parser warnings"
);
assert_eq!(
flpdf.status.code(),
qpdf.status.code(),
"warning exit status"
);
assert_eq!(flpdf.stdout, qpdf.stdout, "stdout");
assert_eq!(flpdf.stderr, qpdf.stderr, "ordered parser warnings");
let qpdf_bytes = std::fs::read(&qpdf_output).expect("qpdf must write despite warnings");
let flpdf_bytes = std::fs::read(&flpdf_output).expect("flpdf must write despite warnings");
assert!(
qpdf_bytes == flpdf_bytes,
"warning-to-null linearized outputs must match (flpdf {} bytes, qpdf {} bytes)",
flpdf_bytes.len(),
qpdf_bytes.len()
);
let check = StdCommand::new("qpdf")
.arg("--check")
.arg(&qpdf_output)
.output()
.expect("check qpdf linearized output");
assert!(
check.status.success(),
"qpdf output must remain readable: {}",
String::from_utf8_lossy(&check.stderr)
);
}
#[test]
fn cli_linearize_normalize_content_is_byte_identical_to_qpdf() {
for stem in [
"one-page",
"multi-contents-one-page",
"shared-stream-objstm",
"direct-leaf-kid",
] {
assert_linearize_normalize_content_byte_identical(stem);
}
}
fn assert_full_rewrite_static_id_byte_identical(stem: &str) {
let actual = run_cli_full_rewrite_static_id(stem);
let expected = golden(stem, "static-id");
if actual == expected {
return;
}
let common = actual.len().min(expected.len());
let off = (0..common)
.find(|&i| actual[i] != expected[i])
.unwrap_or(common);
let lo = off.saturating_sub(24);
panic!(
"{stem} (static-id via full-rewrite): CLI output diverged from qpdf golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
String::from_utf8_lossy(&actual[lo..(off + 24).min(actual.len())]),
String::from_utf8_lossy(&expected[lo..(off + 24).min(expected.len())]),
);
}
#[test]
fn cli_one_page_full_rewrite_static_id_byte_identical() {
assert_full_rewrite_static_id_byte_identical("one-page");
}
#[test]
fn cli_two_page_full_rewrite_static_id_byte_identical() {
assert_full_rewrite_static_id_byte_identical("two-page");
}
#[test]
fn cli_three_page_full_rewrite_static_id_byte_identical() {
assert_full_rewrite_static_id_byte_identical("three-page");
}
fn assert_coalesce_qdf_byte_identical(stem: &str) {
let actual = run_cli_coalesce_qdf_static_id(stem);
let expected = golden(stem, "coalesce-qdf");
if actual == expected {
return;
}
let common = actual.len().min(expected.len());
let off = (0..common)
.find(|&i| actual[i] != expected[i])
.unwrap_or(common);
let lo = off.saturating_sub(24);
panic!(
"{stem} (coalesce-qdf): CLI output diverged from qpdf golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
String::from_utf8_lossy(&actual[lo..(off + 24).min(actual.len())]),
String::from_utf8_lossy(&expected[lo..(off + 24).min(expected.len())]),
);
}
#[test]
fn cli_multi_contents_coalesce_qdf_byte_identical() {
assert_coalesce_qdf_byte_identical("multi-contents-one-page");
}