#[path = "support/mod.rs"]
#[allow(dead_code, unused_imports)]
mod support;
#[path = "support/eol.rs"]
mod eol;
use eol::EOL;
#[path = "support/text_newlines.rs"]
mod text_newlines;
use text_newlines::normalize_text_newlines;
use assert_cmd::Command as CargoCommand;
use std::io::Write;
use std::path::Path;
use std::process::Command as ShellCommand;
fn minimal_pdf_temp() -> tempfile::NamedTempFile {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(include_bytes!("../../../tests/fixtures/minimal.pdf"))
.unwrap();
f
}
fn png_like_payload() -> Vec<u8> {
vec![
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0x0f, 0x00, 0x00, 0x01, 0x01, 0x00, 0x05, 0x18, 0xd8,
0x4e, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, ]
}
fn zip_like_payload() -> Vec<u8> {
let mut v = vec![
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, b't', b'e', b's', b't', ];
v.extend_from_slice(b"\x00\x01\x7f\x80\xff NUL-and-high-bytes");
v
}
fn qpdf_list_attachments(pdf_path: &Path) -> Option<String> {
if !support::is_qpdf_available() {
return None;
}
let out = ShellCommand::new("qpdf")
.arg("--list-attachments")
.arg(pdf_path)
.output()
.expect("qpdf is available but `qpdf --list-attachments` failed to spawn");
assert!(
out.status.success(),
"qpdf --list-attachments {pdf_path:?} exited {:?}; stderr:\n{}",
out.status.code(),
String::from_utf8_lossy(&out.stderr),
);
Some(String::from_utf8(out.stdout).expect("qpdf --list-attachments output is not UTF-8"))
}
fn qpdf_show_attachment(pdf_path: &Path, key: &str) -> Option<Vec<u8>> {
if !support::is_qpdf_available() {
return None;
}
let out = ShellCommand::new("qpdf")
.arg(format!("--show-attachment={key}"))
.arg(pdf_path)
.output()
.expect("qpdf is available but `qpdf --show-attachment` failed to spawn");
assert!(
out.status.success(),
"qpdf --show-attachment={key} {pdf_path:?} exited {:?} \
(flpdf-generated PDF unreadable by qpdf?); stderr:\n{}",
out.status.code(),
String::from_utf8_lossy(&out.stderr),
);
Some(out.stdout)
}
fn qpdf_add_attachment(
in_pdf: &Path,
file: &Path,
key: &str,
out_pdf: &Path,
extra_args: &[&str],
) -> bool {
if !support::is_qpdf_available() {
return false;
}
let out = ShellCommand::new("qpdf")
.arg("--add-attachment")
.arg(file)
.arg(format!("--key={key}"))
.args(extra_args)
.arg("--")
.arg(in_pdf)
.arg(out_pdf)
.output()
.expect("qpdf is available but `qpdf --add-attachment` failed to spawn");
assert!(
out.status.success(),
"qpdf --add-attachment --key={key} {in_pdf:?} -> {out_pdf:?} exited {:?}; stderr:\n{}",
out.status.code(),
String::from_utf8_lossy(&out.stderr),
);
true
}
fn assert_split_attachment_parity<F>(label: &str, expected_chunks: usize, run: F)
where
F: FnOnce(&Path, &Path) -> (std::process::Output, std::process::Output),
{
if !support::is_qpdf_available() {
eprintln!("{label}: qpdf not available, skipping");
return;
}
let temp = tempfile::tempdir().unwrap();
let qpdf_dir = temp.path().join("qpdf");
let flpdf_dir = temp.path().join("flpdf");
std::fs::create_dir(&qpdf_dir).unwrap();
std::fs::create_dir(&flpdf_dir).unwrap();
let qpdf_template = qpdf_dir.join("out.pdf");
let flpdf_template = flpdf_dir.join("out.pdf");
let (qpdf, flpdf) = run(&qpdf_template, &flpdf_template);
assert!(
qpdf.status.success(),
"{label}: qpdf split attachment oracle failed: {}",
String::from_utf8_lossy(&qpdf.stderr)
);
assert!(
flpdf.status.success(),
"{label}: flpdf split attachment route failed: stdout={} stderr={}",
String::from_utf8_lossy(&flpdf.stdout),
String::from_utf8_lossy(&flpdf.stderr)
);
for page in 1..=expected_chunks {
let qpdf_output = qpdf_dir.join(format!("out-{page}.pdf"));
let flpdf_output = flpdf_dir.join(format!("out-{page}.pdf"));
assert!(
qpdf_output.is_file(),
"{label}: qpdf must write {qpdf_output:?}"
);
assert!(
flpdf_output.is_file(),
"{label}: flpdf must write the qpdf-compatible split output {flpdf_output:?}"
);
assert_eq!(
std::fs::read(qpdf_output).unwrap(),
std::fs::read(flpdf_output).unwrap(),
"{label}: split page {page} must match qpdf"
);
}
assert!(
!flpdf_template.exists(),
"{label}: split output template must not be emitted as a single PDF"
);
}
#[test]
fn split_pages_is_honored_after_add_attachment() {
let input =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/compat/two-page.pdf");
let attachment = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/external-file-stream.bin");
assert_split_attachment_parity("add-attachment", 2, |qpdf_template, flpdf_template| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--static-id",
"--qdf",
input.to_str().unwrap(),
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"--split-pages",
])
.arg(qpdf_template)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.to_str().unwrap(),
"--static-id",
"--qdf",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"--split-pages",
])
.arg(flpdf_template)
.output()
.unwrap();
(qpdf, flpdf)
});
}
#[test]
fn split_pages_is_honored_after_remove_attachment() {
let input = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/attachment-two-page.pdf");
assert_split_attachment_parity("remove-attachment", 2, |qpdf_template, flpdf_template| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--static-id",
"--qdf",
input.to_str().unwrap(),
"--remove-attachment=attachment.txt",
"--",
"--split-pages",
])
.arg(qpdf_template)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.to_str().unwrap(),
"--static-id",
"--qdf",
"--remove-attachment=attachment.txt",
"--",
"--split-pages",
])
.arg(flpdf_template)
.output()
.unwrap();
(qpdf, flpdf)
});
}
#[test]
fn split_pages_is_honored_after_copying_attachments() {
let input =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/compat/two-page.pdf");
let donor = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/attachment-two-page.pdf");
assert_split_attachment_parity(
"copy-attachments-from",
2,
|qpdf_template, flpdf_template| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--static-id",
"--qdf",
input.to_str().unwrap(),
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
"--split-pages",
])
.arg(qpdf_template)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.to_str().unwrap(),
"--static-id",
"--qdf",
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
"--split-pages",
])
.arg(flpdf_template)
.output()
.unwrap();
(qpdf, flpdf)
},
);
}
#[test]
fn split_pages_is_honored_after_page_selection_and_add_attachment() {
let input =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/compat/two-page.pdf");
let attachment = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/external-file-stream.bin");
assert_split_attachment_parity(
"pages-plus-add-attachment",
1,
|qpdf_template, flpdf_template| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--static-id",
"--qdf",
input.to_str().unwrap(),
"--pages",
".",
"1",
"--",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"--split-pages",
])
.arg(qpdf_template)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.to_str().unwrap(),
"--static-id",
"--qdf",
"--pages",
".",
"1",
"--",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"--split-pages",
])
.arg(flpdf_template)
.output()
.unwrap();
(qpdf, flpdf)
},
);
}
fn assert_empty_attachment_parity<F>(label: &str, run: F)
where
F: FnOnce(&Path, &Path) -> (std::process::Output, std::process::Output),
{
if !support::is_qpdf_available() {
eprintln!("{label}: qpdf not available, skipping");
return;
}
let temp = tempfile::tempdir().unwrap();
let qpdf_output = temp.path().join("qpdf.pdf");
let flpdf_output = temp.path().join("flpdf.pdf");
let (qpdf, flpdf) = run(&qpdf_output, &flpdf_output);
assert!(
qpdf.status.success(),
"{label}: qpdf empty attachment oracle failed: stdout={} stderr={}",
String::from_utf8_lossy(&qpdf.stdout),
String::from_utf8_lossy(&qpdf.stderr)
);
assert!(
flpdf.status.success(),
"{label}: flpdf empty attachment route failed: stdout={} stderr={}",
String::from_utf8_lossy(&flpdf.stdout),
String::from_utf8_lossy(&flpdf.stderr)
);
assert!(
qpdf_output.is_file(),
"{label}: qpdf must write {qpdf_output:?}"
);
assert!(
flpdf_output.is_file(),
"{label}: flpdf must write {flpdf_output:?}"
);
assert_eq!(
std::fs::read(qpdf_output).unwrap(),
std::fs::read(flpdf_output).unwrap(),
"{label}: empty attachment output must match qpdf"
);
}
#[test]
fn empty_input_add_attachment_writes_qpdf_compatible_output() {
let attachment = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/external-file-stream.bin");
assert_empty_attachment_parity("empty-add-attachment", |qpdf_output, flpdf_output| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--empty",
"--static-id",
"--qdf",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
])
.arg(qpdf_output)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--empty",
"--static-id",
"--qdf",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
])
.arg(flpdf_output)
.output()
.unwrap();
(qpdf, flpdf)
});
}
#[test]
fn empty_input_add_attachment_to_stdout_matches_qpdf() {
let attachment = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/external-file-stream.bin");
if !support::is_qpdf_available() {
eprintln!(
"empty_input_add_attachment_to_stdout_matches_qpdf: qpdf not available, skipping"
);
return;
}
let qpdf = ShellCommand::new("qpdf")
.args([
"--empty",
"--static-id",
"--qdf",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"-",
])
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--empty",
"--static-id",
"--qdf",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
"-",
])
.output()
.unwrap();
assert!(
qpdf.status.success(),
"qpdf empty attachment stdout oracle failed: {}",
String::from_utf8_lossy(&qpdf.stderr)
);
assert!(
flpdf.status.success(),
"flpdf empty attachment stdout route failed: stdout={} stderr={}",
String::from_utf8_lossy(&flpdf.stdout),
String::from_utf8_lossy(&flpdf.stderr)
);
assert_eq!(
qpdf.stdout, flpdf.stdout,
"empty add-attachment stdout must match qpdf"
);
}
#[test]
fn empty_input_copy_attachments_writes_qpdf_compatible_output() {
let donor = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/attachment-two-page.pdf");
assert_empty_attachment_parity("empty-copy-attachments", |qpdf_output, flpdf_output| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--empty",
"--static-id",
"--qdf",
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
])
.arg(qpdf_output)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--empty",
"--static-id",
"--qdf",
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
])
.arg(flpdf_output)
.output()
.unwrap();
(qpdf, flpdf)
});
}
#[test]
fn empty_pages_add_attachment_writes_qpdf_compatible_output() {
let source =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/compat/two-page.pdf");
let attachment = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/external-file-stream.bin");
assert_empty_attachment_parity("empty-pages-add-attachment", |qpdf_output, flpdf_output| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--empty",
"--static-id",
"--qdf",
"--pages",
source.to_str().unwrap(),
"1",
"--",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
])
.arg(qpdf_output)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--empty",
"--static-id",
"--qdf",
"--pages",
source.to_str().unwrap(),
"1",
"--",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=payload",
"--creationdate=D:20240101000000Z",
"--moddate=D:20240101000000Z",
"--",
])
.arg(flpdf_output)
.output()
.unwrap();
(qpdf, flpdf)
});
}
#[test]
fn empty_pages_copy_attachments_writes_qpdf_compatible_output() {
let source =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/compat/two-page.pdf");
let donor = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/attachment-two-page.pdf");
assert_empty_attachment_parity(
"empty-pages-copy-attachments",
|qpdf_output, flpdf_output| {
let qpdf = ShellCommand::new("qpdf")
.args([
"--empty",
"--static-id",
"--qdf",
"--pages",
source.to_str().unwrap(),
"1",
"--",
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
])
.arg(qpdf_output)
.output()
.unwrap();
let flpdf = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--empty",
"--static-id",
"--qdf",
"--pages",
source.to_str().unwrap(),
"1",
"--",
"--copy-attachments-from",
donor.to_str().unwrap(),
"--prefix=P",
"--",
])
.arg(flpdf_output)
.output()
.unwrap();
(qpdf, flpdf)
},
);
}
#[test]
fn lifecycle_1_add_then_list_flpdf_and_qpdf() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let att_path = temp.path().join("hello.txt");
std::fs::write(&att_path, b"hello from lifecycle test").unwrap();
let out_pdf = temp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att_path.to_str().unwrap(),
"--key=lc1key",
"--",
out_pdf.to_str().unwrap(),
])
.assert()
.success();
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", out_pdf.to_str().unwrap()])
.assert()
.success()
.stdout(predicates::str::contains("lc1key"));
match qpdf_list_attachments(&out_pdf) {
None => eprintln!(
"lifecycle_1_add_then_list_flpdf_and_qpdf: qpdf not available, skipping cross-check"
),
Some(listing) => {
assert!(
listing.contains("lc1key"),
"qpdf --list-attachments should contain 'lc1key'; got: {listing}"
);
}
}
}
#[test]
fn direct_catalog_root_add_then_list_matches_qpdf() {
if !support::is_qpdf_available() {
return;
}
let temp = tempfile::tempdir().unwrap();
let input = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat/direct-root-one-page.pdf");
let attachment = temp.path().join("direct-root.txt");
std::fs::write(&attachment, b"direct root payload").unwrap();
let qpdf_output = temp.path().join("qpdf.pdf");
let flpdf_output = temp.path().join("flpdf.pdf");
let qpdf = ShellCommand::new("qpdf")
.args(["--static-id", "--add-attachment"])
.arg(&attachment)
.arg("--key=direct-root")
.args(["--", input.to_str().unwrap(), qpdf_output.to_str().unwrap()])
.output()
.expect("qpdf direct-root attachment oracle");
assert!(
qpdf.status.success(),
"qpdf direct-root attachment failed: {}",
String::from_utf8_lossy(&qpdf.stderr)
);
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--static-id",
input.to_str().unwrap(),
"--add-attachment",
attachment.to_str().unwrap(),
"--key=direct-root",
"--",
flpdf_output.to_str().unwrap(),
])
.assert()
.success();
let qpdf_listing = qpdf_list_attachments(&qpdf_output).expect("qpdf listing");
let flpdf_listing = qpdf_list_attachments(&flpdf_output).expect("flpdf listing");
assert!(
qpdf_listing.contains("direct-root"),
"qpdf listing: {qpdf_listing}"
);
assert!(
flpdf_listing.contains("direct-root"),
"flpdf listing: {flpdf_listing}"
);
}
fn check_add_show_roundtrip(label: &str, payload: &[u8], key: &str, filename: &str) {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let att_path = temp.path().join(filename);
std::fs::write(&att_path, payload).unwrap();
let out_pdf = temp.path().join("out.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att_path.to_str().unwrap(),
format!("--key={key}").as_str(),
"--",
out_pdf.to_str().unwrap(),
])
.assert()
.success();
let show_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
&format!("--show-attachment={key}"),
out_pdf.to_str().unwrap(),
])
.output()
.unwrap();
assert!(
show_output.status.success(),
"{label}: flpdf --show-attachment={key} exited {:?}; stderr:\n{}",
show_output.status.code(),
String::from_utf8_lossy(&show_output.stderr)
);
let stdout_bytes = show_output.stdout;
assert_eq!(
stdout_bytes,
payload,
"{label}: flpdf round-trip payload mismatch (len={} vs {})",
stdout_bytes.len(),
payload.len()
);
match qpdf_show_attachment(&out_pdf, key) {
None => eprintln!("{label}: qpdf not available, skipping qpdf show-attachment cross-check"),
Some(qpdf_bytes) => {
assert_eq!(
qpdf_bytes,
payload,
"{label}: qpdf --show-attachment payload mismatch (len={} vs {})",
qpdf_bytes.len(),
payload.len()
);
}
}
}
#[test]
fn lifecycle_2a_text_payload_roundtrip() {
check_add_show_roundtrip("text", b"plain ASCII text content", "textkey", "text.txt");
}
#[test]
fn lifecycle_2b_png_like_binary_roundtrip() {
let payload = png_like_payload();
check_add_show_roundtrip("png-like", &payload, "pngkey", "image.png");
}
#[test]
fn lifecycle_2c_zip_like_binary_roundtrip() {
let payload = zip_like_payload();
check_add_show_roundtrip("zip-like", &payload, "zipkey", "archive.zip");
}
#[test]
fn lifecycle_2d_nul_rich_binary_roundtrip() {
let payload: Vec<u8> = (0u8..=255u8)
.chain(b"extra\x00\x00tail".iter().copied())
.collect();
check_add_show_roundtrip("nul-rich", &payload, "binarykey", "binary.bin");
}
#[test]
fn lifecycle_3_remove_then_list_flpdf_and_qpdf() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let att1 = temp.path().join("keep.txt");
let att2 = temp.path().join("drop.txt");
std::fs::write(&att1, b"keeper").unwrap();
std::fs::write(&att2, b"to be dropped").unwrap();
let with_both = temp.path().join("with_both.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att1.to_str().unwrap(),
"--key=keepkey",
"--",
with_both.to_str().unwrap(),
])
.assert()
.success();
let with_both2 = temp.path().join("with_both2.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
with_both.to_str().unwrap(),
"--add-attachment",
att2.to_str().unwrap(),
"--key=dropkey",
"--",
with_both2.to_str().unwrap(),
])
.assert()
.success();
let after_remove = temp.path().join("after_remove.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
with_both2.to_str().unwrap(),
"--remove-attachment=dropkey",
after_remove.to_str().unwrap(),
])
.assert()
.success();
let list_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", after_remove.to_str().unwrap()])
.output()
.unwrap();
assert!(
list_output.status.success(),
"flpdf --list-attachments {after_remove:?} exited {:?}; stderr:\n{}",
list_output.status.code(),
String::from_utf8_lossy(&list_output.stderr)
);
let listing = String::from_utf8(list_output.stdout).unwrap();
assert!(
listing.contains("keepkey"),
"flpdf: keepkey should still be present; listing: {listing}"
);
assert!(
!listing.contains("dropkey"),
"flpdf: dropkey should be absent after removal; listing: {listing}"
);
match qpdf_list_attachments(&after_remove) {
None => {
eprintln!("lifecycle_3: qpdf not available, skipping qpdf cross-check");
}
Some(qpdf_listing) => {
assert!(
qpdf_listing.contains("keepkey"),
"qpdf: keepkey should still be present; listing: {qpdf_listing}"
);
assert!(
!qpdf_listing.contains("dropkey"),
"qpdf: dropkey should be absent after removal; listing: {qpdf_listing}"
);
}
}
}
#[test]
fn lifecycle_4_copy_preserves_payload_and_metadata() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let png = png_like_payload();
let att_path = temp.path().join("copy-test.png");
std::fs::write(&att_path, &png).unwrap();
let src_pdf = temp.path().join("source.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att_path.to_str().unwrap(),
"--key=cpykey",
"--mimetype=image/png",
"--description=Copy test image",
"--creationdate=D:20240315090000",
"--moddate=D:20240316100000",
"--",
src_pdf.to_str().unwrap(),
])
.assert()
.success();
let dst_pdf = temp.path().join("dest.pdf");
let target_input = minimal_pdf_temp();
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
target_input.path().to_str().unwrap(),
"--copy-attachments-from",
src_pdf.to_str().unwrap(),
"--",
dst_pdf.to_str().unwrap(),
])
.assert()
.success();
let show_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--show-attachment=cpykey", dst_pdf.to_str().unwrap()])
.output()
.unwrap();
assert!(
show_output.status.success(),
"flpdf --show-attachment=cpykey {dst_pdf:?} exited {:?}; stderr:\n{}",
show_output.status.code(),
String::from_utf8_lossy(&show_output.stderr)
);
let extracted = show_output.stdout;
assert_eq!(
extracted,
png,
"copy: PNG payload must be byte-identical (len={} vs {})",
extracted.len(),
png.len()
);
let verbose_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", "--verbose", dst_pdf.to_str().unwrap()])
.output()
.unwrap();
assert!(
verbose_output.status.success(),
"flpdf --list-attachments --verbose {dst_pdf:?} exited {:?}; stderr:\n{}",
verbose_output.status.code(),
String::from_utf8_lossy(&verbose_output.stderr)
);
let verbose = String::from_utf8(verbose_output.stdout).unwrap();
assert!(
verbose.contains("cpykey"),
"copy: verbose listing must contain key 'cpykey'; got: {verbose}"
);
assert!(
verbose.contains("image/png"),
"copy: verbose listing must preserve mimetype; got: {verbose}"
);
assert!(
verbose.contains("D:20240315090000"),
"copy: verbose listing must preserve creation date; got: {verbose}"
);
assert!(
verbose.contains("D:20240316100000"),
"copy: verbose listing must preserve mod date; got: {verbose}"
);
let expected_checksum_hex: String = flpdf::md5_checksum(&png)
.iter()
.map(|b| format!("{b:02x}"))
.collect();
assert!(
verbose.contains(format!(" checksum: {expected_checksum_hex}{EOL}").as_str()),
"copy: verbose listing must preserve /CheckSum={expected_checksum_hex}; got: {verbose}"
);
assert!(
verbose.contains(format!(" description: Copy test image{EOL}").as_str()),
"copy: verbose listing must preserve /Desc 'Copy test image'; got: {verbose}"
);
let json_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--json=2", dst_pdf.to_str().unwrap()])
.output()
.unwrap();
assert!(
json_output.status.success(),
"flpdf --json=2 {dst_pdf:?} exited {:?}; stderr:\n{}",
json_output.status.code(),
String::from_utf8_lossy(&json_output.stderr)
);
let objects = String::from_utf8(json_output.stdout).unwrap();
assert!(
objects.contains(&format!("\"/Size\": {}", png.len())),
"copy: /Size={} must be preserved; got: {objects}",
png.len()
);
}
#[test]
fn lifecycle_4b_copy_with_prefix() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let src_input = minimal_pdf_temp();
let att = temp.path().join("pfxtest.txt");
std::fs::write(&att, b"prefix test content").unwrap();
let src_pdf = temp.path().join("src.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
src_input.path().to_str().unwrap(),
"--add-attachment",
att.to_str().unwrap(),
"--key=original",
"--",
src_pdf.to_str().unwrap(),
])
.assert()
.success();
let dst_pdf = temp.path().join("dst.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--copy-attachments-from",
src_pdf.to_str().unwrap(),
"--prefix=x-",
"--",
dst_pdf.to_str().unwrap(),
])
.assert()
.success();
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", dst_pdf.to_str().unwrap()])
.assert()
.success()
.stdout(predicates::str::contains("x-original"));
let show_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--show-attachment=x-original", dst_pdf.to_str().unwrap()])
.output()
.unwrap();
assert!(
show_output.status.success(),
"flpdf --show-attachment=x-original {dst_pdf:?} exited {:?}; stderr:\n{}",
show_output.status.code(),
String::from_utf8_lossy(&show_output.stderr)
);
assert_eq!(show_output.stdout, b"prefix test content");
}
#[test]
fn lifecycle_4c_copy_from_repeated_donor_groups() {
let temp = tempfile::tempdir().unwrap();
let donor_a_base = minimal_pdf_temp();
let donor_b_base = minimal_pdf_temp();
let target = minimal_pdf_temp();
let attachment_a = temp.path().join("donor-a.txt");
let attachment_b = temp.path().join("donor-b.txt");
std::fs::write(&attachment_a, b"donor A payload").unwrap();
std::fs::write(&attachment_b, b"donor B payload").unwrap();
let donor_a = temp.path().join("donor-a.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
donor_a_base.path().to_str().unwrap(),
"--add-attachment",
attachment_a.to_str().unwrap(),
"--key=from-a",
"--",
donor_a.to_str().unwrap(),
])
.assert()
.success();
let donor_b = temp.path().join("donor-b.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
donor_b_base.path().to_str().unwrap(),
"--add-attachment",
attachment_b.to_str().unwrap(),
"--key=from-b",
"--",
donor_b.to_str().unwrap(),
])
.assert()
.success();
let output = temp.path().join("repeated-copy.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
target.path().to_str().unwrap(),
"--copy-attachments-from",
donor_a.to_str().unwrap(),
"--",
"--copy-attachments-from",
donor_b.to_str().unwrap(),
"--",
output.to_str().unwrap(),
])
.assert()
.success();
let listing = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", output.to_str().unwrap()])
.output()
.unwrap();
assert!(
listing.status.success(),
"flpdf --list-attachments {output:?} exited {:?}; stderr:\n{}",
listing.status.code(),
String::from_utf8_lossy(&listing.stderr)
);
let listing = String::from_utf8(listing.stdout).unwrap();
assert!(listing.contains("from-a"), "listing: {listing}");
assert!(listing.contains("from-b"), "listing: {listing}");
for (key, payload) in [
("from-a", b"donor A payload"),
("from-b", b"donor B payload"),
] {
let extracted = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
&format!("--show-attachment={key}"),
output.to_str().unwrap(),
])
.output()
.unwrap();
assert!(
extracted.status.success(),
"flpdf --show-attachment={key} {output:?} exited {:?}; stderr:\n{}",
extracted.status.code(),
String::from_utf8_lossy(&extracted.stderr)
);
assert_eq!(extracted.stdout, payload);
}
}
#[test]
fn lifecycle_5_metadata_survives_plain_rewrite() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let att = temp.path().join("metadata.txt");
std::fs::write(&att, b"metadata content").unwrap();
let with_meta = temp.path().join("with_meta.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att.to_str().unwrap(),
"--key=metakey",
"--mimetype=text/plain",
"--description=Test description",
"--creationdate=D:20231201080000",
"--moddate=D:20231215090000",
"--",
with_meta.to_str().unwrap(),
])
.assert()
.success();
let rewritten = temp.path().join("rewritten.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([with_meta.to_str().unwrap(), rewritten.to_str().unwrap()])
.assert()
.success();
let verbose_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--list-attachments",
"--verbose",
rewritten.to_str().unwrap(),
])
.output()
.unwrap();
assert!(
verbose_output.status.success(),
"flpdf --list-attachments --verbose {rewritten:?} exited {:?}; stderr:\n{}",
verbose_output.status.code(),
String::from_utf8_lossy(&verbose_output.stderr)
);
let verbose = String::from_utf8(verbose_output.stdout).unwrap();
assert!(
verbose.contains("metakey"),
"rewrite: key must be preserved; got: {verbose}"
);
assert!(
verbose.contains("text/plain"),
"rewrite: mimetype must be preserved; got: {verbose}"
);
assert!(
verbose.contains("Test description"),
"rewrite: description must be preserved; got: {verbose}"
);
assert!(
verbose.contains("D:20231201080000"),
"rewrite: creation date must be preserved; got: {verbose}"
);
assert!(
verbose.contains("D:20231215090000"),
"rewrite: mod date must be preserved; got: {verbose}"
);
let show_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--show-attachment=metakey", rewritten.to_str().unwrap()])
.output()
.unwrap();
assert!(
show_output.status.success(),
"flpdf --show-attachment=metakey {rewritten:?} exited {:?}; stderr:\n{}",
show_output.status.code(),
String::from_utf8_lossy(&show_output.stderr)
);
assert_eq!(
show_output.stdout, b"metadata content",
"rewrite: payload must be byte-identical after rewrite"
);
}
#[test]
fn lifecycle_6_qpdf_authored_readable_by_flpdf() {
if !support::is_qpdf_available() {
eprintln!("lifecycle_6_qpdf_authored_readable_by_flpdf: qpdf not available, skipping");
return;
}
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let png = png_like_payload();
let att_path = temp.path().join("from-qpdf.png");
std::fs::write(&att_path, &png).unwrap();
let qpdf_made = temp.path().join("qpdf-made.pdf");
let ok = qpdf_add_attachment(
input.path(),
&att_path,
"qpdfkey",
&qpdf_made,
&["--mimetype=image/png"],
);
assert!(ok, "qpdf --add-attachment failed during test setup");
let list_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", qpdf_made.to_str().unwrap()])
.output()
.unwrap();
assert!(
list_output.status.success(),
"flpdf --list-attachments {qpdf_made:?} exited {:?}; stderr:\n{}",
list_output.status.code(),
String::from_utf8_lossy(&list_output.stderr)
);
let listing = String::from_utf8(list_output.stdout).unwrap();
assert!(
listing.contains("qpdfkey"),
"flpdf must list 'qpdfkey' from qpdf-produced PDF; listing: {listing}"
);
let show_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--show-attachment=qpdfkey", qpdf_made.to_str().unwrap()])
.output()
.unwrap();
assert!(
show_output.status.success(),
"flpdf --show-attachment=qpdfkey {qpdf_made:?} exited {:?}; stderr:\n{}",
show_output.status.code(),
String::from_utf8_lossy(&show_output.stderr)
);
assert_eq!(
show_output.stdout, png,
"flpdf must extract the original PNG payload from qpdf-produced PDF byte-identically"
);
let qpdf_listing = qpdf_list_attachments(&qpdf_made).expect("qpdf available but failed");
assert!(
qpdf_listing.contains("qpdfkey"),
"qpdf sanity: 'qpdfkey' must appear in qpdf's own listing; got: {qpdf_listing}"
);
}
#[test]
fn lifecycle_7_add_success_and_duplicate_diagnostics_match_qpdf() {
if !support::is_qpdf_available() {
eprintln!(
"lifecycle_7_add_success_and_duplicate_diagnostics_match_qpdf: qpdf not available, skipping"
);
return;
}
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let attachment = temp.path().join("probe.txt");
std::fs::write(&attachment, b"diagnostic payload").unwrap();
let qpdf_output = temp.path().join("qpdf-output.pdf");
let flpdf_output = temp.path().join("flpdf-output.pdf");
let add_args = [
"--verbose",
"--add-attachment",
attachment.to_str().unwrap(),
"--key=probe-key",
"--filename=renamed.txt",
"--mimetype=text/plain",
"--description=probe description",
"--creationdate=D:20240101120000Z",
"--moddate=D:20240102130000Z",
"--",
input.path().to_str().unwrap(),
];
let qpdf_add = ShellCommand::new("qpdf")
.args(add_args)
.arg(&qpdf_output)
.output()
.expect("qpdf add must spawn");
let flpdf_add = CargoCommand::cargo_bin("flpdf")
.unwrap()
.env("FLPDF_PROGNAME", "qpdf")
.args(add_args)
.arg(&flpdf_output)
.output()
.unwrap();
assert_eq!(qpdf_add.status.code(), Some(0));
assert_eq!(flpdf_add.status.code(), Some(0));
assert_eq!(
normalize_text_newlines(&qpdf_add.stderr),
normalize_text_newlines(&flpdf_add.stderr)
);
let qpdf_verbose = qpdf_add
.stdout
.split(|byte| *byte == b'\n')
.next()
.unwrap_or_default();
let qpdf_verbose = qpdf_verbose.strip_suffix(b"\r").unwrap_or(qpdf_verbose);
let flpdf_verbose = flpdf_add
.stdout
.split(|byte| *byte == b'\n')
.next()
.unwrap_or_default();
let flpdf_verbose = flpdf_verbose.strip_suffix(b"\r").unwrap_or(flpdf_verbose);
assert_eq!(
qpdf_verbose, flpdf_verbose,
"the verbose attached diagnostic must match qpdf"
);
assert!(qpdf_output.exists());
assert!(flpdf_output.exists());
let qpdf_duplicate = ShellCommand::new("qpdf")
.args([
"--add-attachment",
attachment.to_str().unwrap(),
"--key=probe-key",
"--",
qpdf_output.to_str().unwrap(),
])
.arg(temp.path().join("qpdf-duplicate.pdf"))
.output()
.expect("qpdf duplicate add must spawn");
let flpdf_duplicate = CargoCommand::cargo_bin("flpdf")
.unwrap()
.env("FLPDF_PROGNAME", "qpdf")
.args([
"--add-attachment",
attachment.to_str().unwrap(),
"--key=probe-key",
"--",
qpdf_output.to_str().unwrap(),
])
.arg(temp.path().join("flpdf-duplicate.pdf"))
.output()
.unwrap();
assert_eq!(qpdf_duplicate.status.code(), Some(2));
assert_eq!(flpdf_duplicate.status.code(), Some(2));
assert_eq!(
normalize_text_newlines(&qpdf_duplicate.stdout),
normalize_text_newlines(&flpdf_duplicate.stdout)
);
assert_eq!(
normalize_text_newlines(&qpdf_duplicate.stderr),
normalize_text_newlines(&flpdf_duplicate.stderr)
);
}
fn assert_listing_matches_qpdf(pdf_path: &Path, args: &[&str]) -> Option<String> {
if !support::is_qpdf_available() {
eprintln!("qpdf not available — skipping listing cross-check");
return None;
}
let qpdf_out = ShellCommand::new("qpdf")
.args(args)
.arg(pdf_path)
.output()
.expect("qpdf is available but failed to spawn");
assert!(
qpdf_out.status.success(),
"qpdf {args:?} {pdf_path:?} exited {:?}; stderr:\n{}",
qpdf_out.status.code(),
String::from_utf8_lossy(&qpdf_out.stderr)
);
let flpdf_out = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(args)
.arg(pdf_path)
.output()
.unwrap();
assert!(
flpdf_out.status.success(),
"flpdf {args:?} {pdf_path:?} exited {:?}; stderr:\n{}",
flpdf_out.status.code(),
String::from_utf8_lossy(&flpdf_out.stderr)
);
assert_eq!(
String::from_utf8_lossy(&flpdf_out.stdout).replace("\r\n", "\n"),
String::from_utf8_lossy(&qpdf_out.stdout).replace("\r\n", "\n"),
"flpdf and qpdf must agree on `{args:?}` stdout for {pdf_path:?}",
);
Some(String::from_utf8(flpdf_out.stdout).expect("listing is not UTF-8"))
}
#[test]
fn listing_reports_documents_with_no_embedded_files() {
let input = minimal_pdf_temp();
let out = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", input.path().to_str().unwrap()])
.output()
.unwrap();
assert!(out.status.success(), "listing must exit 0");
assert_eq!(
String::from_utf8_lossy(&out.stdout),
format!("{} has no embedded files{EOL}", input.path().display()),
"qpdf names the input file when there is no /Names /EmbeddedFiles tree",
);
assert_listing_matches_qpdf(input.path(), &["--list-attachments"]);
}
#[test]
fn plain_listing_is_one_line_per_attachment() {
let temp = tempfile::tempdir().unwrap();
let input = minimal_pdf_temp();
let att = temp.path().join("plain.txt");
std::fs::write(&att, b"plain listing payload").unwrap();
let with_attachment = temp.path().join("with_attachment.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
input.path().to_str().unwrap(),
"--add-attachment",
att.to_str().unwrap(),
"--key=plainkey",
"--mimetype=text/plain",
"--description=Plain listing",
"--creationdate=D:20240101000000",
"--",
with_attachment.to_str().unwrap(),
])
.assert()
.success();
let list_output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args(["--list-attachments", with_attachment.to_str().unwrap()])
.output()
.unwrap();
assert!(
list_output.status.success(),
"flpdf --list-attachments {with_attachment:?} exited {:?}; stderr:\n{}",
list_output.status.code(),
String::from_utf8_lossy(&list_output.stderr)
);
let listing = String::from_utf8(list_output.stdout).unwrap();
let lines: Vec<&str> = listing.lines().collect();
assert_eq!(
lines.len(),
1,
"qpdf writes the header line outside its verbose block: {listing:?}"
);
assert!(
lines[0].starts_with("plainkey -> "),
"the single line names the key: {listing:?}"
);
for absent in [
"preferred name:",
"display name:",
"size:",
"mime type:",
"creation date:",
"description:",
] {
assert!(
!listing.contains(absent),
"plain listing must not include {absent:?}: {listing:?}"
);
}
assert_listing_matches_qpdf(&with_attachment, &["--list-attachments"]);
assert_listing_matches_qpdf(&with_attachment, &["--list-attachments", "--verbose"]);
}
#[test]
fn copy_attachments_conflict_error_names_the_target_not_the_donor() {
let temp = tempfile::tempdir().unwrap();
let target_input = minimal_pdf_temp();
let donor_input = minimal_pdf_temp();
let target_attachment = temp.path().join("target-attachment.txt");
let donor_attachment = temp.path().join("donor-attachment.txt");
std::fs::write(&target_attachment, b"target payload").unwrap();
std::fs::write(&donor_attachment, b"donor payload").unwrap();
let target_pdf = temp.path().join("target.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--add-attachment",
target_attachment.to_str().unwrap(),
"--key=dupkey",
"--",
target_input.path().to_str().unwrap(),
])
.arg(&target_pdf)
.assert()
.success();
let donor_pdf = temp.path().join("donor.pdf");
CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
"--add-attachment",
donor_attachment.to_str().unwrap(),
"--key=dupkey",
"--",
donor_input.path().to_str().unwrap(),
])
.arg(&donor_pdf)
.assert()
.success();
let output = CargoCommand::cargo_bin("flpdf")
.unwrap()
.args([
target_pdf.to_str().unwrap(),
"--copy-attachments-from",
donor_pdf.to_str().unwrap(),
"--",
])
.arg(temp.path().join("output.pdf"))
.output()
.unwrap();
assert_eq!(output.status.code(), Some(2));
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(
stderr.contains("target.pdf already has attachments with keys that conflict"),
"the conflict error must name the target file, not the donor: {stderr:?}"
);
}