#![cfg(feature = "qpdf-zlib-compat")]
use flpdf::{CompressStreams, EncryptParams, NewlineBeforeEndstream, ObjectStreamMode, Pdf};
use std::path::Path;
use std::process::Command;
fn flpdf_linearized(fixture: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
flpdf_linearized_at(&path)
}
fn flpdf_linearized_at(path: &Path) -> Vec<u8> {
let file = std::fs::File::open(path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open(std::io::BufReader::new(file)).unwrap();
let opts = WriterTestSettings {
deterministic_id: true,
newline_before_endstream: NewlineBeforeEndstream::Never,
..WriterTestSettings::default()
};
write_linearized_with_settings(&mut pdf, &opts).unwrap()
}
fn flpdf_linearized_repair(fixture: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open_with_repair(std::io::BufReader::new(file)).unwrap();
let opts = WriterTestSettings {
deterministic_id: true,
newline_before_endstream: NewlineBeforeEndstream::Never,
..WriterTestSettings::default()
};
write_linearized_with_settings(&mut pdf, &opts).unwrap()
}
fn golden(fixture_stem: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/golden/references")
.join(fixture_stem)
.join("linearize.pdf");
std::fs::read(&path).unwrap_or_else(|e| panic!("read golden {path:?}: {e}"))
}
fn golden_classic(fixture_stem: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/golden/references")
.join(fixture_stem)
.join("linearize-classic.pdf");
std::fs::read(&path).unwrap_or_else(|e| panic!("read golden {path:?}: {e}"))
}
fn first_diff(a: &[u8], b: &[u8]) -> Option<usize> {
if a == b {
return None;
}
let common = a.len().min(b.len());
for i in 0..common {
if a[i] != b[i] {
return Some(i);
}
}
Some(common)
}
fn assert_linearize_byte_identical(fixture: &str, stem: &str) {
let actual = flpdf_linearized(fixture);
let expected = golden(stem);
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: not byte-identical to qpdf --linearize --deterministic-id golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
fn assert_linearize_byte_identical_repair(fixture: &str, stem: &str) {
let actual = flpdf_linearized_repair(fixture);
let expected = golden(stem);
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: not byte-identical to qpdf --linearize --deterministic-id golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
fn assert_classic_byte_identical(fixture: &str, stem: &str) {
let actual = flpdf_linearized(fixture);
let expected = golden_classic(stem);
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: not byte-identical to qpdf --linearize --deterministic-id golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
fn assert_classic_structurally_byte_identical(fixture: &str, stem: &str) {
let actual = mask_id1(&flpdf_linearized(fixture));
let expected = mask_id1(&golden_classic(stem));
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: structural layout diverged from qpdf classic golden (ignoring /ID[1]) \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
fn pinned_qpdf() -> Option<&'static str> {
let output = Command::new("qpdf").arg("--version").output();
output.ok().and_then(|output| {
String::from_utf8_lossy(&output.stdout)
.lines()
.next()
.is_some_and(|line| line == "qpdf version 11.9.0")
.then_some("qpdf")
})
}
#[test]
fn part9_pages_precede_lower_numbered_preserved_objstm() {
let Some(oracle) = pinned_qpdf() else {
eprintln!("[SKIP cmp_linearize_tests] qpdf 11.9.0 is unavailable");
return;
};
let fixture = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join("preserve-pages-source-order-conflict.pdf");
let directory = tempfile::tempdir().expect("tempdir");
let rewritten = directory.path().join("rewritten.pdf");
let status = Command::new(oracle)
.args(["--static-id", "--warning-exit-0"])
.arg(&fixture)
.arg(&rewritten)
.status()
.expect("qpdf runs");
assert_eq!(status.code(), Some(0), "qpdf rewrite must succeed");
let expected_path = directory.path().join("qpdf.pdf");
let status = Command::new(oracle)
.args(["--linearize", "--deterministic-id", "--warning-exit-0"])
.arg(&rewritten)
.arg(&expected_path)
.status()
.expect("qpdf runs");
assert_eq!(status.code(), Some(0), "qpdf linearization must succeed");
let actual = flpdf_linearized_at(&rewritten);
let expected = std::fs::read(&expected_path).expect("qpdf output");
if let Some(off) = first_diff(&actual, &expected) {
panic!(
"the page tree must precede the lower-numbered preserved object \
stream (flpdf={} bytes, qpdf={} bytes, first diff at byte {off})",
actual.len(),
expected.len(),
);
}
}
#[test]
fn preserved_container_anchors_after_lc_other_not_the_promoted_page_tree() {
assert_linearize_byte_identical(
"objstm-lin-part9-head-anchor.pdf",
"objstm-lin-part9-head-anchor",
);
}
#[test]
fn part9_head_group_keeps_source_order_across_a_preserved_container() {
assert_linearize_byte_identical(
"objstm-lin-part9-head-tiebreak.pdf",
"objstm-lin-part9-head-tiebreak",
);
}
#[test]
fn part9_head_group_places_a_preserved_container_by_its_source_number() {
assert_linearize_byte_identical(
"objstm-lin-part9-head-container-order.pdf",
"objstm-lin-part9-head-container-order",
);
}
#[test]
fn one_page_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("one-page.pdf", "one-page");
}
#[test]
fn two_page_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("two-page.pdf", "two-page");
}
#[test]
fn three_page_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("three-page.pdf", "three-page");
}
#[test]
fn deep_nesting_257_resources_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"deep-nesting-257-resources.pdf",
"deep-nesting-257-resources",
);
}
#[test]
fn no_stream_one_page_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("no-stream-one-page.pdf", "no-stream-one-page");
}
#[test]
fn nonid_id0_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("nonid-id0.pdf", "nonid-id0");
}
#[test]
fn indirect_extensions_linearized_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"linearize-indirect-extensions.pdf",
"linearize-indirect-extensions",
);
}
#[test]
fn inherited_resources_one_page_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"inherited-resources-one-page.pdf",
"inherited-resources-one-page",
);
}
#[test]
fn inherited_rotate_one_page_byte_identical_to_qpdf() {
assert_linearize_byte_identical("inherited-rotate-one-page.pdf", "inherited-rotate-one-page");
}
#[test]
fn shared_page_two_parents_byte_identical_to_qpdf() {
assert_linearize_byte_identical("shared-page-two-parents.pdf", "shared-page-two-parents");
}
#[test]
fn shared_page_two_parents_pushmint_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"shared-page-two-parents-pushmint.pdf",
"shared-page-two-parents-pushmint",
);
}
#[test]
fn shared_page_two_parents_reconstructed_byte_identical_to_qpdf() {
assert_linearize_byte_identical_repair(
"shared-page-two-parents-reconstructed.pdf",
"shared-page-two-parents-reconstructed",
);
}
#[test]
fn mistyped_page_tree_byte_identical_to_qpdf() {
assert_linearize_byte_identical("mistyped-page-tree.pdf", "mistyped-page-tree");
}
#[test]
fn missing_mediabox_leaf_byte_identical_to_qpdf() {
assert_linearize_byte_identical("missing-mediabox-leaf.pdf", "missing-mediabox-leaf");
}
#[test]
fn shared_leaf_mediabox_default_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"shared-leaf-mediabox-default.pdf",
"shared-leaf-mediabox-default",
);
}
#[test]
fn indirect_mediabox_element_byte_identical_to_qpdf() {
assert_linearize_byte_identical("indirect-mediabox-element.pdf", "indirect-mediabox-element");
}
#[test]
fn direct_leaf_kid_byte_identical_to_qpdf() {
assert_linearize_byte_identical("direct-leaf-kid.pdf", "direct-leaf-kid");
}
#[test]
fn classic_thumbnail_part9_order_matches_qpdf() {
let Some(oracle) = pinned_qpdf() else {
eprintln!("[SKIP cmp_linearize_tests] qpdf 11.9.0 is unavailable");
return;
};
for fixture in [
"objstm-lin-part9-categories-thumb-74-225.pdf",
"objstm-lin-part9-pages-shared-thumb-74-225.pdf",
"objstm-lin-thumbnail-private-shared.pdf",
] {
let input = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let directory = tempfile::tempdir().expect("tempdir");
let expected_path = directory.path().join("qpdf.pdf");
let status = Command::new(oracle)
.args(["--linearize", "--deterministic-id", "--warning-exit-0"])
.arg(&input)
.arg(&expected_path)
.status()
.expect("qpdf runs");
assert_eq!(status.code(), Some(0), "qpdf linearization must succeed");
let actual = flpdf_linearized(fixture);
let expected = std::fs::read(&expected_path).expect("qpdf output");
if let Some(off) = first_diff(&actual, &expected) {
panic!(
"{fixture}: classic thumbnail order differs from qpdf \
(flpdf={} bytes, qpdf={} bytes, first diff at byte {off})",
actual.len(),
expected.len(),
);
}
}
}
#[test]
fn root_pages_points_into_tree_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"root-pages-points-into-tree.pdf",
"root-pages-points-into-tree",
);
}
#[test]
fn relinearize_one_page_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("linearized-one-page.pdf", "linearized-one-page");
}
#[test]
fn relinearize_two_page_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical("linearized-two-page.pdf", "linearized-two-page");
}
#[test]
fn relinearize_two_stream_overflow_source_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"linearized-two-stream-overflow.pdf",
"linearized-two-stream-overflow",
);
}
#[test]
fn dangling_body_refs_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical("dangling-body-one-page.pdf", "dangling-body-one-page");
}
#[test]
fn resurrect_free_array_ref_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical("resurrect-free-one-page.pdf", "resurrect-free-one-page");
}
#[test]
fn resurrect_missing_array_ref_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"resurrect-missing-one-page.pdf",
"resurrect-missing-one-page",
);
}
#[test]
fn catalog_firstpage_shared_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"catalog-firstpage-shared-one-page.pdf",
"catalog-firstpage-shared-one-page",
);
}
#[test]
fn catalog_firstpage_shared_two_page_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"catalog-firstpage-shared-two-page.pdf",
"catalog-firstpage-shared-two-page",
);
}
#[test]
fn catalog_otherpage_other_two_page_classic_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"catalog-otherpage-other-two-page.pdf",
"catalog-otherpage-other-two-page",
);
}
#[test]
fn catalog_otherpage_other_info_two_page_classic_is_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"catalog-otherpage-other-info-two-page.pdf",
"catalog-otherpage-other-info-two-page",
);
}
#[test]
fn pages_ext_firstpage_shared_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"pages-ext-firstpage-shared-one-page.pdf",
"pages-ext-firstpage-shared-one-page",
);
}
#[test]
fn resurrect_missing_page_arr_classic_byte_identical_to_qpdf() {
assert_linearize_byte_identical(
"resurrect-missing-page-arr.pdf",
"resurrect-missing-page-arr",
);
}
#[test]
fn resurrect_page2_arr_page1_dictval_not_in_first_page_section() {
assert_linearize_byte_identical(
"resurrect-missing-page1-dictval-page2-arr.pdf",
"resurrect-missing-page1-dictval-page2-arr",
);
}
#[test]
fn resurrect_both_edges_same_page_null_in_first_page_section() {
assert_linearize_byte_identical(
"resurrect-both-edges-same-page.pdf",
"resurrect-both-edges-same-page",
);
}
#[test]
fn resurrect_crossobj_arr_via_live_desc_null_in_first_page_section() {
assert_linearize_byte_identical(
"resurrect-crossobj-arr-via-live-desc.pdf",
"resurrect-crossobj-arr-via-live-desc",
);
}
#[test]
fn else_branch_children_ordered_by_original_object_number() {
assert_linearize_byte_identical(
"else-branch-obj-number-order.pdf",
"else-branch-obj-number-order",
);
}
#[test]
fn revorder_resurrect_null_in_first_page_section() {
assert_linearize_byte_identical("revorder-resurrect.pdf", "revorder-resurrect");
}
#[test]
fn page_highnum_content_lownum_page_before_content() {
assert_linearize_byte_identical(
"page-highnum-content-lownum.pdf",
"page-highnum-content-lownum",
);
}
#[test]
fn od_null_also_in_first_page_arr_byte_identical_to_qpdf() {
assert_linearize_byte_identical("od-null-page-arr.pdf", "od-null-page-arr");
}
#[test]
fn od_catalog_arr_null_byte_identical_to_qpdf() {
assert_linearize_byte_identical("od-arr-null.pdf", "od-arr-null");
}
fn mask_id1(buf: &[u8]) -> Vec<u8> {
let mut out = buf.to_vec();
let needle = b" /ID [<";
let mut i = 0usize;
while let Some(rel) = find(&out[i..], needle) {
let arr = i + rel + needle.len();
let id0_end = arr + 32;
if id0_end + 2 + 32 <= out.len() && out[id0_end] == b'>' && out[id0_end + 1] == b'<' {
let id1 = id0_end + 2;
for b in &mut out[id1..id1 + 32] {
*b = b'0';
}
i = id1 + 32;
} else {
i = arr;
}
}
out
}
fn assert_linearize_structurally_byte_identical(fixture: &str, stem: &str) {
let actual = mask_id1(&flpdf_linearized(fixture));
let expected = mask_id1(&golden(stem));
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: structural layout diverged from qpdf golden (ignoring /ID[1]) (flpdf={} bytes, golden={} bytes, first diff at byte {off})\n flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
#[test]
fn one_page_linearized_structurally_byte_identical_to_qpdf() {
assert_linearize_structurally_byte_identical("one-page.pdf", "one-page");
}
#[test]
fn two_page_linearized_structurally_byte_identical_to_qpdf() {
assert_linearize_structurally_byte_identical("two-page.pdf", "two-page");
}
#[test]
fn three_page_linearized_structurally_byte_identical_to_qpdf() {
assert_linearize_structurally_byte_identical("three-page.pdf", "three-page");
}
#[test]
fn lone_flate_l9_linearized_structurally_byte_identical_to_qpdf() {
assert_linearize_structurally_byte_identical("lone-flate-l9.pdf", "lone-flate-l9");
}
fn plain_rewrite_one_page() -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join("one-page.pdf");
let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open(std::io::BufReader::new(file)).unwrap();
let opts = WriterTestSettings {
deterministic_id: true,
newline_before_endstream: NewlineBeforeEndstream::Never,
..WriterTestSettings::default()
};
let mut out = Vec::new();
write_with_settings(&mut pdf, &mut out, &opts).unwrap();
out
}
mod common;
#[allow(unused_imports)]
use common::{write_linearized_with_settings, write_with_settings, WriterTestSettings};
#[test]
fn encrypted_linearized_v4_aes_state_matches_qpdf() {
let Some(oracle) = pinned_qpdf() else {
eprintln!("[SKIP cmp_linearize_tests] qpdf 11.9.0 is unavailable");
return;
};
let fixture = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join("one-page.pdf");
let directory = tempfile::tempdir().expect("tempdir");
let qpdf_output = directory.path().join("qpdf-encrypted-linearized.pdf");
let flpdf_output = directory.path().join("flpdf-encrypted-linearized.pdf");
let status = Command::new(oracle)
.args([
"--linearize",
"--static-id",
"--static-aes-iv",
"--object-streams=disable",
"--stream-data=uncompress",
"--encrypt",
"user",
"owner",
"128",
"--use-aes=y",
"--",
])
.arg(&fixture)
.arg(&qpdf_output)
.status()
.expect("qpdf writes encrypted linearized output");
assert_eq!(
status.code(),
Some(0),
"qpdf encryption and linearization succeed"
);
let file = std::fs::File::open(&fixture).expect("open one-page fixture");
let mut pdf = Pdf::open(std::io::BufReader::new(file)).expect("open fixture as PDF");
let settings = WriterTestSettings {
static_id: true,
static_aes_iv: true,
object_streams: ObjectStreamMode::Disable,
compress_streams: CompressStreams::No,
encrypt: Some(EncryptParams::v4_aes128(b"user", b"owner")),
..WriterTestSettings::default()
};
let actual = write_linearized_with_settings(&mut pdf, &settings)
.expect("flpdf writes encrypted linearized output");
std::fs::write(&flpdf_output, &actual).expect("write flpdf output for qpdf check");
let expected = std::fs::read(&qpdf_output).expect("read qpdf output");
assert_eq!(
actual, expected,
"encrypted linearized output must match qpdf"
);
let check_status = Command::new(oracle)
.args(["--password=user", "--check-linearization"])
.arg(&flpdf_output)
.status()
.expect("qpdf checks the encrypted flpdf output");
assert_eq!(
check_status.code(),
Some(0),
"qpdf must accept the encrypted flpdf linearization layout"
);
}
#[test]
fn linearized_extra_header_starts_after_qpdf_separator_newline() {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join("one-page.pdf");
let file = std::fs::File::open(&path).expect("open one-page fixture");
let mut pdf = Pdf::open(std::io::BufReader::new(file)).expect("open fixture as PDF");
let settings = WriterTestSettings {
static_id: true,
newline_before_endstream: NewlineBeforeEndstream::Never,
extra_header_text: "%% Comment\n% No newline".to_owned(),
..WriterTestSettings::default()
};
let output = write_linearized_with_settings(&mut pdf, &settings)
.expect("write linearized output with extra header");
let marker = b"%% Comment\n% No newline\n";
let marker_start = output
.windows(marker.len())
.position(|window| window == marker)
.expect("extra header should be present");
assert!(marker_start > 0, "extra header must not start at byte zero");
assert_eq!(
output[marker_start - 1],
b'\n',
"qpdf writes a separator newline before linearized extra header text"
);
assert_eq!(
&output[marker_start + marker.len()..marker_start + marker.len() + 5],
b"xref\n",
"qpdf writes xref immediately after extra header's terminating newline"
);
}
#[test]
fn trailer_external_file_keys_linearized_match_qpdf_11_9() {
let fixture = "trailer-external-file-keys.pdf";
let input = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let Some(oracle) = pinned_qpdf() else {
eprintln!("[SKIP cmp_linearize_tests] qpdf 11.9.0 is unavailable");
return;
};
let directory = tempfile::tempdir().expect("tempdir");
let expected_path = directory.path().join("qpdf.pdf");
let status = Command::new(oracle)
.args(["--linearize", "--deterministic-id"])
.arg(&input)
.arg(&expected_path)
.status()
.expect("qpdf runs");
assert_eq!(status.code(), Some(0), "qpdf linearization must succeed");
let actual = flpdf_linearized(fixture);
let expected = std::fs::read(&expected_path).expect("qpdf output");
assert_eq!(actual, expected, "linearized trailer keys must match qpdf");
}
fn content_stream_object(data: &[u8]) -> (Vec<u8>, Vec<u8>) {
let needle = b"stream\n";
let mut found: Option<(Vec<u8>, Vec<u8>)> = None;
let mut i = 0usize;
while let Some(rel) = find(&data[i..], needle) {
let stream_kw = i + rel; let dict_open = rfind(&data[..stream_kw], b"<<").expect("stream must follow a dict");
let dict_close = stream_kw - 1; let dict = &data[dict_open..dict_close];
let payload_start = stream_kw + needle.len();
let es =
find(&data[payload_start..], b"endstream").expect("endstream must follow a stream");
let payload_end = payload_start + es;
let payload = &data[payload_start..payload_end];
let is_lone_flate = find(dict, b"/Filter /FlateDecode").is_some();
let has_type = find(dict, b"/Type").is_some();
let is_hint = find(dict, b"/S ").is_some();
if is_lone_flate && !has_type && !is_hint {
assert!(
found.is_none(),
"expected exactly one page content stream in one-page output"
);
found = Some((dict.to_vec(), payload.to_vec()));
}
i = payload_end + b"endstream".len();
}
found.expect("a single-FlateDecode page content stream must be present")
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|w| w == needle)
}
fn rfind(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).rposition(|w| w == needle)
}
#[test]
fn one_page_linearized_content_stream_is_recompressed_single_flate() {
let lin = flpdf_linearized("one-page.pdf");
let (dict, payload) = content_stream_object(&lin);
assert_eq!(
dict,
b"<< /Length 82 /Filter /FlateDecode >>",
"linearized page content stream must be re-encoded to a single \
/FlateDecode in qpdf key order; got {:?}",
String::from_utf8_lossy(&dict)
);
assert!(
find(&dict, b"ASCII85").is_none(),
"the ASCII85 source filter must be dropped on recompression"
);
assert_eq!(payload.len(), 82, "/Length must equal the on-disk payload");
}
#[test]
fn one_page_linearized_content_stream_equals_plain_and_qpdf_golden() {
let lin = flpdf_linearized("one-page.pdf");
let plain = plain_rewrite_one_page();
let qpdf = golden("one-page");
let (lin_dict, lin_payload) = content_stream_object(&lin);
let (plain_dict, plain_payload) = content_stream_object(&plain);
let (qpdf_dict, qpdf_payload) = content_stream_object(&qpdf);
assert_eq!(
(&lin_dict, &lin_payload),
(&plain_dict, &plain_payload),
"linearized content stream must equal the plain-path content stream"
);
assert_eq!(
(&lin_dict, &lin_payload),
(&qpdf_dict, &qpdf_payload),
"linearized content stream must equal qpdf golden obj9"
);
}
#[test]
fn outlines_classic_structurally_byte_identical_to_qpdf() {
assert_classic_structurally_byte_identical(
"objstm-lin-outlines-80-80.pdf",
"objstm-lin-outlines-80-80",
);
}
#[test]
fn outlines_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical("objstm-lin-outlines-80-80.pdf", "objstm-lin-outlines-80-80");
}
#[test]
fn useoutlines_classic_structurally_byte_identical_to_qpdf() {
assert_classic_structurally_byte_identical(
"objstm-lin-useoutlines-80-80.pdf",
"objstm-lin-useoutlines-80-80",
);
}
#[test]
fn useoutlines_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-useoutlines-80-80.pdf",
"objstm-lin-useoutlines-80-80",
);
}
#[test]
fn useoutlines_shared_page_classic_structurally_byte_identical_to_qpdf() {
assert_classic_structurally_byte_identical(
"objstm-lin-useoutlines-shared-page-80-80.pdf",
"objstm-lin-useoutlines-shared-page-80-80",
);
}
#[test]
fn useoutlines_shared_page_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-useoutlines-shared-page-80-80.pdf",
"objstm-lin-useoutlines-shared-page-80-80",
);
}
#[test]
fn outlines_shared_page_classic_structurally_byte_identical_to_qpdf() {
assert_classic_structurally_byte_identical(
"objstm-lin-outlines-shared-page-80-80.pdf",
"objstm-lin-outlines-shared-page-80-80",
);
}
#[test]
fn outlines_shared_page_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-outlines-shared-page-80-80.pdf",
"objstm-lin-outlines-shared-page-80-80",
);
}
#[test]
fn od_indirect_length_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-od-indirect-length.pdf",
"objstm-lin-od-indirect-length",
);
}
#[test]
fn openaction_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-openaction-80-80.pdf",
"objstm-lin-openaction-80-80",
);
}
#[test]
fn acroform_widget_page1_page2_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-acroform-widget-page1-page2.pdf",
"objstm-lin-acroform-widget-page1-page2",
);
}
#[test]
fn acroform_widget_page1_only_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-acroform-widget-page1-only.pdf",
"objstm-lin-acroform-widget-page1-only",
);
}
#[test]
fn acroform_widget_page0_5_10_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-acroform-widget-page0-5-10.pdf",
"objstm-lin-acroform-widget-page0-5-10",
);
}
#[test]
fn acroform_widget_ap_stream_page0_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-acroform-widget-ap-stream-page0.pdf",
"objstm-lin-acroform-widget-ap-stream-page0",
);
}
#[test]
fn useoutline_od_shared_stream_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-useoutline-od-shared-stream.pdf",
"objstm-lin-useoutline-od-shared-stream",
);
}
#[test]
fn od_indirect_length_flate_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-od-indirect-length-flate.pdf",
"objstm-lin-od-indirect-length-flate",
);
}
#[test]
fn openaction_multi_od_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-openaction-multi-od.pdf",
"objstm-lin-openaction-multi-od",
);
}
#[test]
fn outline_od_shared_stream_classic_byte_identical_to_qpdf() {
assert_classic_byte_identical(
"objstm-lin-outline-od-shared-stream.pdf",
"objstm-lin-outline-od-shared-stream",
);
}