use assert_cmd::Command;
use std::path::{Path, PathBuf};
use std::process::{Command as ProcessCommand, Output};
fn fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(name)
}
fn root_fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures")
.join(name)
}
fn qpdf_available() -> bool {
ProcessCommand::new("/usr/bin/qpdf")
.arg("--version")
.output()
.is_ok()
}
fn run_qpdf(args: &[&str]) -> Output {
ProcessCommand::new("/usr/bin/qpdf")
.args(args)
.output()
.expect("qpdf 11.9.0 should spawn")
}
fn run_flpdf(args: &[&str]) -> Output {
Command::cargo_bin("flpdf")
.expect("flpdf binary should exist")
.env("FLPDF_PROGNAME", "qpdf")
.args(args)
.output()
.expect("flpdf should spawn")
}
fn run_qpdf_owned(args: &[String]) -> Output {
ProcessCommand::new("/usr/bin/qpdf")
.args(args)
.output()
.expect("qpdf 11.9.0 should spawn")
}
fn run_flpdf_owned(args: &[String]) -> Output {
Command::cargo_bin("flpdf")
.expect("flpdf binary should exist")
.env("FLPDF_PROGNAME", "qpdf")
.args(args)
.output()
.expect("flpdf should spawn")
}
#[test]
fn top_level_linearize_direct_root_errors_match_qpdf() {
if !qpdf_available() {
return;
}
for name in ["direct-root-adbe.pdf", "direct-root-one-page.pdf"] {
let temp = tempfile::tempdir().expect("temporary directory should exist");
let input = fixture(name);
let output = temp.path().join("out.pdf");
let args = vec![
"--deterministic-id".to_owned(),
"--linearize".to_owned(),
input
.to_str()
.expect("fixture path should be UTF-8")
.to_owned(),
output
.to_str()
.expect("output path should be UTF-8")
.to_owned(),
];
let expected = run_qpdf_owned(&args);
let actual = run_flpdf_owned(&args);
assert_eq!(
actual.status.code(),
expected.status.code(),
"{name} status"
);
assert_eq!(actual.stdout, expected.stdout, "{name} stdout");
assert_eq!(actual.stderr, expected.stderr, "{name} stderr");
}
}
fn assert_output_matches(actual: &Output, expected: &Output) {
assert_eq!(actual.status.code(), expected.status.code());
assert_eq!(actual.stdout, expected.stdout);
assert_eq!(actual.stderr, expected.stderr);
}
fn warning_lines(output: &Output) -> Vec<&[u8]> {
output
.stderr
.split(|&byte| byte == b'\n')
.filter(|line| line.starts_with(b"WARNING:"))
.collect()
}
#[test]
fn top_level_check_linearization_clean_matches_qpdf() {
if !qpdf_available() {
return;
}
let input = fixture("linearized-one-page.pdf");
let input = input.to_str().expect("fixture path should be UTF-8");
let expected = run_qpdf(&["--check-linearization", input]);
let actual = run_flpdf(&["--check-linearization", input]);
assert_output_matches(&actual, &expected);
}
#[test]
fn top_level_check_linearization_non_linearized_matches_qpdf() {
if !qpdf_available() {
return;
}
let input = root_fixture("minimal.pdf");
let input = input.to_str().expect("fixture path should be UTF-8");
let expected = run_qpdf(&["--check-linearization", input]);
let actual = run_flpdf(&["--check-linearization", input]);
assert_output_matches(&actual, &expected);
}
#[test]
fn top_level_check_linearization_warning_matches_qpdf() {
if !qpdf_available() {
return;
}
let temp = tempfile::tempdir().expect("temporary directory should exist");
let input = temp.path().join("o-mismatch.pdf");
let mut bytes = std::fs::read(fixture("linearized-one-page.pdf")).unwrap();
let marker = b"/O 6 /E";
let replacement = b"/O 7 /E";
let offset = bytes
.windows(marker.len())
.position(|window| window == marker)
.expect("linearization fixture should contain /O");
bytes[offset..offset + marker.len()].copy_from_slice(replacement);
std::fs::write(&input, bytes).expect("malformed fixture should be written");
let input = input.to_str().expect("temporary path should be UTF-8");
let expected = run_qpdf(&["--check-linearization", input]);
let actual = run_flpdf(&["--check-linearization", input]);
assert_eq!(expected.status.code(), Some(3));
assert_output_matches(&actual, &expected);
assert!(String::from_utf8_lossy(&actual.stderr).contains("first page object (/O) mismatch"));
assert!(
String::from_utf8_lossy(&actual.stderr).contains("qpdf: operation succeeded with warnings")
);
}
#[test]
fn top_level_check_linearization_no_warn_matches_qpdf() {
if !qpdf_available() {
return;
}
let temp = tempfile::tempdir().expect("temporary directory should exist");
let input = temp.path().join("o-mismatch.pdf");
let mut bytes = std::fs::read(fixture("linearized-one-page.pdf")).unwrap();
let marker = b"/O 6 /E";
let replacement = b"/O 7 /E";
let offset = bytes
.windows(marker.len())
.position(|window| window == marker)
.expect("linearization fixture should contain /O");
bytes[offset..offset + marker.len()].copy_from_slice(replacement);
std::fs::write(&input, bytes).expect("malformed fixture should be written");
let input = input.to_str().expect("temporary path should be UTF-8");
let expected = run_qpdf(&["--no-warn", "--check-linearization", input]);
let actual = run_flpdf(&["--no-warn", "--check-linearization", input]);
assert_eq!(expected.status.code(), Some(3));
assert_output_matches(&actual, &expected);
assert!(
String::from_utf8_lossy(&actual.stderr).is_empty(),
"--no-warn must suppress warning delivery while keeping exit status 3"
);
}
#[test]
fn top_level_show_linearization_warning_and_no_warn_match_qpdf() {
if !qpdf_available() {
return;
}
let temp = tempfile::tempdir().expect("temporary directory should exist");
let input = temp.path().join("o-mismatch.pdf");
let mut bytes = std::fs::read(fixture("linearized-one-page.pdf")).unwrap();
let marker = b"/O 6 /E";
let replacement = b"/O 7 /E";
let offset = bytes
.windows(marker.len())
.position(|window| window == marker)
.expect("linearization fixture should contain /O");
bytes[offset..offset + marker.len()].copy_from_slice(replacement);
std::fs::write(&input, bytes).expect("malformed fixture should be written");
let input = input.to_str().expect("temporary path should be UTF-8");
let expected = run_qpdf(&["--show-linearization", input]);
let actual = run_flpdf(&["--show-linearization", input]);
assert_output_matches(&actual, &expected);
let expected = run_qpdf(&["--no-warn", "--show-linearization", input]);
let actual = run_flpdf(&["--no-warn", "--show-linearization", input]);
assert_output_matches(&actual, &expected);
}
#[test]
fn top_level_show_linearization_open_failure_delivers_warnings_once() {
if !qpdf_available() {
return;
}
let input = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/test_driver/open_repair_failure.pdf");
let input = input.to_str().expect("fixture path should be UTF-8");
let expected = run_qpdf(&["--show-linearization", input]);
let actual = run_flpdf(&["--show-linearization", input]);
assert_eq!(actual.status.code(), expected.status.code());
assert_eq!(actual.stdout, expected.stdout);
assert_eq!(warning_lines(&actual), warning_lines(&expected));
assert_eq!(warning_lines(&actual).len(), 3);
let expected = run_qpdf(&["--no-warn", "--show-linearization", input]);
let actual = run_flpdf(&["--no-warn", "--show-linearization", input]);
assert_eq!(actual.status.code(), expected.status.code());
assert_eq!(actual.stdout, expected.stdout);
assert!(warning_lines(&actual).is_empty());
}
#[test]
fn top_level_show_linearization_hint_stream_warning_is_emitted_once() {
if !qpdf_available() {
return;
}
let temp = tempfile::tempdir().expect("temporary directory should exist");
let linearized = temp.path().join("linearized.pdf");
let input = temp.path().join("hint-no-endobj.pdf");
let source = fixture("two-page.pdf");
let generated = ProcessCommand::new("/usr/bin/qpdf")
.args([
"--linearize",
"--stream-data=uncompress",
"--static-id",
source.to_str().expect("source path should be UTF-8"),
linearized.to_str().expect("output path should be UTF-8"),
])
.output()
.expect("qpdf should generate a linearized fixture");
assert!(
generated.status.success(),
"qpdf linearization failed: {}",
String::from_utf8_lossy(&generated.stderr)
);
let mut bytes = std::fs::read(&linearized).expect("linearized fixture should be readable");
let endstream = 601
+ bytes[601..]
.windows(b"endstream".len())
.position(|window| window == b"endstream")
.expect("linearized fixture should contain a hint stream");
let endobj = endstream
+ bytes[endstream..]
.windows(b"endobj".len())
.position(|window| window == b"endobj")
.expect("hint stream object should have an endobj marker");
bytes[endobj..endobj + b"endobj".len()].copy_from_slice(b"endobX");
std::fs::write(&input, bytes).expect("damaged fixture should be written");
let input = input.to_str().expect("temporary path should be UTF-8");
let expected = run_qpdf(&["--show-linearization", input]);
let actual = run_flpdf(&["--show-linearization", input]);
assert_eq!(expected.status.code(), Some(3));
assert_output_matches(&actual, &expected);
assert_eq!(warning_lines(&actual).len(), 1);
assert!(String::from_utf8_lossy(&actual.stderr).contains("expected endobj"));
}
#[test]
fn top_level_check_linearization_attachment_mutations_match_qpdf() {
if !qpdf_available() {
return;
}
let temp = tempfile::tempdir().expect("temporary directory should exist");
let input_with_attachment = fixture("attachment-two-page.pdf");
let plain_input = fixture("one-page.pdf");
let payload = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/test_driver/fixture-names.txt");
let donor = input_with_attachment.clone();
let cases = [
(
"remove",
vec![
"--check-linearization".to_owned(),
"--list-attachments".to_owned(),
"--remove-attachment=attachment.txt".to_owned(),
input_with_attachment.display().to_string(),
],
temp.path().join("remove-output.pdf"),
),
(
"add",
vec![
"--check-linearization".to_owned(),
"--list-attachments".to_owned(),
"--add-attachment".to_owned(),
payload.display().to_string(),
"--".to_owned(),
plain_input.display().to_string(),
],
temp.path().join("add-output.pdf"),
),
(
"copy",
vec![
"--check-linearization".to_owned(),
"--list-attachments".to_owned(),
"--copy-attachments-from".to_owned(),
donor.display().to_string(),
"--".to_owned(),
plain_input.display().to_string(),
],
temp.path().join("copy-output.pdf"),
),
];
for (name, args, output) in cases {
let expected = run_qpdf_owned(&args);
let actual = run_flpdf_owned(&args);
assert_eq!(
expected.status.code(),
Some(0),
"qpdf output-free {name} case failed: {expected:?}"
);
assert_eq!(
actual.status.code(),
expected.status.code(),
"output-free {name} exit status"
);
assert_eq!(actual.stdout, expected.stdout, "output-free {name} stdout");
assert_eq!(actual.stderr, expected.stderr, "output-free {name} stderr");
let mut route_args = args.clone();
route_args.retain(|argument| argument != "--list-attachments");
let expected = run_qpdf_owned(&route_args);
let actual = run_flpdf_owned(&route_args);
assert_eq!(expected.status.code(), Some(0));
assert_eq!(actual.status.code(), expected.status.code());
assert_eq!(actual.stdout, expected.stdout, "combined {name} stdout");
assert_eq!(actual.stderr, expected.stderr, "combined {name} stderr");
let mut output_args = route_args;
output_args.push(output.display().to_string());
let expected = run_qpdf_owned(&output_args);
let actual = run_flpdf_owned(&output_args);
assert_eq!(
expected.status.code(),
Some(2),
"qpdf output {name} case must be a usage error: {expected:?}"
);
assert_eq!(
actual.status.code(),
expected.status.code(),
"output {name} exit status"
);
assert_eq!(actual.stdout, expected.stdout, "output {name} stdout");
assert_eq!(actual.stderr, expected.stderr, "output {name} stderr");
}
}
#[test]
fn check_linearization_subcommand_uses_the_same_canonical_route() {
let input = fixture("linearized-one-page.pdf");
let input = input.to_str().expect("fixture path should be UTF-8");
let top_level = run_flpdf(&["--check-linearization", input]);
let subcommand = run_flpdf(&["check-linearization", input]);
assert_output_matches(&subcommand, &top_level);
}