Files
pdfcraft/crates/annot/tests/support/fixture.rs
amoisttowe1 d4da4b94bb M5.1: structural round-trip, property and never-crash tests for crates/annot (tests only)
## What this adds

Structural round-trip, property and never-crash evidence for `crates/annot`, which had no `tests/` directory — every prior test checked an in-memory object and never crossed the writer. 50 new tests take the crate from 42 to **92 passing / 0 failed**, with nothing ignored and no existing test weakened:

- `tests/round_trip.rs` (25) — annotations survive save and reopen across all three writer paths: geometry, contents text, flag bits, opacity, identifying names, pop-up back-references, link targets/URIs/dash structure, per-page order and ownership.
- `tests/properties.rs` (10) — proptest invariants over generated geometry, opacity and flags.
- `tests/malformed.rs` (15) — damaged input must never panic; each case documents the *measured* behaviour as defined degradation, not endorsed output.
- `tests/support/{bytes.rs,fixture.rs}` — a raw-byte reader and PDF skeletons, so claims can be made about the saved file rather than only about parsed objects.

`proptest = { workspace = true }` as a dev-dependency (same declaration as `cos`; style differs from the dotted form in crypt/filters). proptest 1.11.0 is already locked in the workspace, so `Cargo.lock` gains one edge and no new package.

**No product code is changed.** This pins current behaviour, including three questionable behaviours it surfaced (below), and leaves every fix to a follow-up.

## Appearance is deliberately out of scope

Nothing here asserts what an appearance stream draws, so this stays clear of #2345, #2137, #2066, #1775 and #1513 (all open, all rewriting appearance generation). Structural fidelity — where a mark sits, what it says, which rules apply — is behaviour none of them intends to alter.

## Evidence quality: mutation-tested, with credit split honestly

Ten single-site guard/arithmetic/key-rename mutants were run against `crates/annot/src/lib.rs` by two independent reviewers plus the author. All ten were caught, and they split **five and five**:

- Killed by pre-existing inline tests in `crates/annot/src/tests.rs` as well: `lib.rs:857` inverted, `:1030` inverted (six inline tests), `:1930` inverted, `:2161` tie-break swapped, `:1241` `/Parent` renamed.
- Killed **only** by tests added here: `:1030` clamp deleted, `:857` clamp deleted, opacity clamp removed at `:1024`, colour clamp in `rgb()` removed at `:824`, and a clamp introduced on the read path at `:2238`.

`lib.rs:857` and `:1030` appear on both sides deliberately — inverting either trips inline tests, deleting them trips nothing outside this branch. Confusing those two mutation families is easy enough to produce a false refutation, so the distinction is recorded in `round_trip.rs` rather than left to whoever re-runs them. A build failure was never counted as a kill in any pass. The mutants were run at `crates/annot/src/lib.rs` as of base `99941daa`. Main has since split that file into modules (`5ce4d244`); the anchor statements are byte-identical at their new homes (the `/Parent` site apart, locals renamed), but main also grew the inline suite from 42 to 74 tests — so the five-and-five split describes the base tree, and the battery was not re-run at the merge head `3122ebad`.

## Three defects found while writing this — reported, not fixed

1. **Large finite numbers are written unvalidated and become unreadable downstream.** The only style guard on the create path (`lib.rs:1018`) is `!finite(&style.color) || !style.opacity.is_finite() || !style.width.is_finite()`; the `abs() < 1e7` cap inside `finite()` (`:827-829`) applies to **colour alone**, so `width` carries no magnitude limit at all. Measured: `width = 1e9` and `1e299` are accepted and written verbatim to `/BS /W`. qpdf (`pikepdf.Job(["--check", …])`, pikepdf 9.11 / libqpdf 12.2) exits **3** — its *warning* status, printed with "operation succeeded with warnings" (0 = clean and silent here) — reporting `overflow/underflow converting <301 digits> to 64-bit integer`, and `/Annots[0]` resolves to `null` (still null after a qpdf re-save) while `pikepdf.open()` reports success. Our own `repair_log()` stays empty, which conflicts with CLAUDE.md's "never silently drop or repair data".
2. **Fill colour is accepted on create and refused on edit.** Create never screens `fill` (it is clamped component-wise by `rgb()` at `:823-825`), while `set_fill` refuses the identical value with `Invalid("invalid fill colour")` at `:2010`. Pinned by `malformed::the_same_huge_fill_is_accepted_on_create_and_refused_on_edit`.
3. **One unparseable token in a page's `/Annots` loses the whole page, silently.** For `[4 0 R notanumber]`, `page_refs()` returns `Ok([])` with `/Pages` still showing the page — so the page object itself fails to parse, every annotation on it disappears, and `repair_log()` records nothing. qpdf keeps the page and warns: `(object 3 0, offset 191): unknown token while reading object; treating as string`.

Anchors in this section read against `crates/annot/src/lib.rs` at base `99941daa`; main has since split that file (`5ce4d244`). The same code at merge head `3122ebad`: `:1018` → `create.rs:416`, `:827-829` → `create.rs:17-19`, `:823-825` → `create.rs:13-15`, `:2010` → `modify.rs:626-627`. All three described behaviours re-verified unchanged at this head.

These need a policy decision rather than a patch, so they are described here and will be filed as separate issues; nothing in this PR fixes them.

## CI status and base note

This branch was opened at `afa8e18` (then current `main`) and rebased onto `99941da`. Two jobs were red on the original base — `test (ubuntu-latest)`, `test (windows-latest)` and the FreeBSD job, all failing in `crates/automation/tests/automation.rs` at `:354` (`signing_with_translated_labels_through_tools`, which passed a 7-character password against an 8-character minimum) and at `:359` (`a_save_that_does_not_reopen_leaves_the_file_as_it_was`), `173 passed; 2 failed`. Those were upstream failures reproduced on that commit's own CI run (**38703572296**, plus the FreeBSD run **38703573428**) in a crate this change never touches; they have since been fixed on `main` by #2420 (the signing password now meets the minimum) and #2484 (save/search regression expectations updated), which is why `main`'s latest CI run is green and why this branch was rebased. The rebase is a no-op for our code: `crates/annot/src/lib.rs` at both commits is byte-identical (`fb491e19…`), so every line number cited below still resolves as written.

Since then `main` was merged in (`dc943eeb`, merge commit `3122ebad`) to clear the FreeBSD job per the CI triage on this PR: the `files_and_quit_from_the_operating_system` race was fixed on main by `12fcc641` (#2812). The merge probe was clean (zero conflicts; `Cargo.lock` the only overlapping path). Main split `crates/annot/src/lib.rs` into modules and reshaped `set_style` to take a `StyleChange`; the merge commit carries three tests-only adaptations (two `SaveOptions` literals gain `drop_unlinked: false`, the writer's default; one `set_style` call re-shaped; citations re-derived to the new file layout). No crate source diverges from main's: `git diff --stat upstream/main HEAD -- crates/annot/src/` is empty. Inline-suite counts at this head: 74 (main grew it from 42); the 42 → 92 figures above describe base `99941daa`. proptest on main's lock is now 1.12.0; one edge added, no new package.

## Where the proof stops

- The first CI run on this branch was at our original base and showed the inherited failures described above; results cited here are local macOS runs (we have no Poppler locally, so qpdf cross-checks ran through pikepdf in a workspace virtualenv).
- The probe PDFs used for qpdf measurements are **not** committed, because AGENTS.md §1 requires an `ATTRIBUTION.toml` entry per sample document — so CI cannot re-measure them. Each measurement carries a regenerate recipe and sha256 in the test comments (`malformed.rs`).
- All oracle figures are tied to one machine's build: pikepdf 9.11 over libqpdf 12.2 (`__libqpdf_version__`; the Job API rejects `--version`, so no raw qpdf version string). Poppler comparisons would need CI, as `pdftoppm` is unavailable locally.
- Mutation evidence covers single-site guard/arithmetic/key-rename mutants only — no multi-site, data-flow or off-by-one classes were attempted.
- We did not measure what any viewer displays to a user after these failures; the listed-but-null entry was observed, not rendered behaviour.

## Question for maintainers

CLAUDE.md asks new tests to be registered in `ROADMAP.md` and `parity/acrobat-features.toml`, but several open PRs above are editing both files right now, so we left them untouched to avoid collisions. Tell us where you would like these registered and we will add the lines here, or open a follow-up.

Credit: `contributors/people.toml` deliberately carries no new entry in this PR — ours is already queued on #2073.
2026-11-18 16:29:46 +07:00

208 lines
9.4 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Shared helpers compile into every integration-test binary of this crate, and not every
//! binary uses every one of them; `dead_code` would then fail `clippy -D warnings` for reasons
//! that have nothing to do with the test being written.
#![allow(dead_code)]
//! Hand-written PDF skeletons and the save/reopen helpers these tests share.
//!
//! The shape follows `crates/annot/src/tests.rs::fixture`: build the bytes of a small
//! cross-referenced PDF by hand, open it with `pdfcraft_cos::Document::open`, and let
//! `pdfcraft-annot` add comments through its public API. Doing it this way (rather than
//! constructing a `Document` in memory) matters for these tests: the file has a real
//! cross-reference chain, so `write_incremental` takes its genuine incremental path instead of
//! silently falling back to a full rewrite.
//!
//! Every save here goes through one of three writer paths, and the tests replay their assertions
//! over all three:
//!
//! * `Save::Incremental` — the original bytes plus an appended revision and `/Prev`;
//! * `Save::FullTable` — a full rewrite with a classic cross-reference table;
//! * `Save::FullStreams` — a full rewrite that packs objects into compressed object streams and
//! renumbers everything from 1.
//!
//! The third path is the interesting one for annotation *identity*: object numbers change, so a
//! `/P`, `/Popup` or `/IRT` that survives it has really been re-pointed, not copied.
use std::sync::Arc;
use pdfcraft_cos::{Dict, Document, Object, SaveOptions, write_full, write_incremental};
/// Which writer path a round trip should take.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Save {
Incremental,
FullTable,
FullStreams,
}
/// Every path, in the order tests report them.
pub const ALL_SAVES: [Save; 3] = [Save::Incremental, Save::FullTable, Save::FullStreams];
/// The paths whose output holds plain (uncompressed) objects, so raw-byte assertions can look at
/// them. Object streams are Flate-compressed by `write_full`, so their bytes need decoding
/// first; those cases are checked through the reopened document instead.
pub const UNCOMPRESSED_SAVES: [Save; 2] = [Save::Incremental, Save::FullTable];
/// Objects written as indirect objects plus a classic cross-reference table and trailer. The
/// catalog is object 1, so `objs` starts with it.
pub fn pdf(objs: &[&[u8]]) -> Vec<u8> {
let mut out = b"%PDF-1.7\n".to_vec();
let mut offsets = Vec::with_capacity(objs.len());
for (i, o) in objs.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
out.extend_from_slice(o);
out.extend_from_slice(b"\nendobj\n");
}
let xref = out.len();
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
for o in offsets {
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
}
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
out
}
/// A document of `pages` blank pages (612×792, no `/Annots` anywhere).
pub fn blank(pages: usize) -> Document {
let mut kids = String::new();
for i in 0..pages {
kids.push_str(&format!("{} 0 R ", 3 + i));
}
let mut objs: Vec<Vec<u8>> =
vec![b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), format!("<</Type /Pages/Kids [{kids}]/Count {pages} >>").into_bytes()];
for _ in 0..pages {
objs.push(b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".to_vec());
}
open(&pdf_refs(&objs))
}
/// Like [`blank`], but page `page` starts with an indirect (and initially empty) `/Annots`
/// array, so writes to it go through `set_annots`' shared-array branch.
pub fn blank_with_indirect_annots(pages: usize, page: usize) -> Document {
let mut objs: Vec<Vec<u8>> = vec![
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
format!("<< /Type /Pages /Kids [{}] /Count {pages} >>", (0..pages).map(|i| format!("{} 0 R", 3 + i)).collect::<Vec<_>>().join(" "))
.into_bytes(),
];
for i in 0..pages {
objs.push(if i == page {
format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots {} 0 R >>", 3 + pages).into_bytes()
} else {
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".to_vec()
});
}
objs.push(b"[]".to_vec());
open(&pdf_refs(&objs))
}
/// [`pdf`] over already-owned object bodies.
fn pdf_refs(objs: &[Vec<u8>]) -> Vec<u8> {
let borrowed: Vec<&[u8]> = objs.iter().map(Vec::as_slice).collect();
pdf(&borrowed)
}
fn open(bytes: &[u8]) -> Document {
Document::open(Arc::new(bytes.to_vec())).expect("the skeleton parses")
}
/// Save one document, then reopen the bytes with our reader. A second, independent parser must
/// accept the same bytes first — that is what makes "it reloads" mean more than "our own writer
/// and reader agree with each other".
pub fn save_and_reopen(doc: &Document, path: Save) -> (Vec<u8>, Document) {
let bytes = save(doc, path);
hayro_syntax::Pdf::new(bytes.clone()).unwrap_or_else(|e| panic!("{path:?} output rejected by hayro-syntax: {e:?}"));
(bytes.clone(), Document::open(Arc::new(bytes)).unwrap_or_else(|e| panic!("{path:?} output rejected by our reader: {e:?}")))
}
/// Save with the path's options. `/Info /ModDate` is left unset so a full save adds nothing that
/// would make byte comparisons depend on the clock.
pub fn save(doc: &Document, path: Save) -> Vec<u8> {
let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: path == Save::FullStreams, drop_unlinked: false };
match path {
Save::Incremental => write_incremental(doc, &opts),
Save::FullTable | Save::FullStreams => write_full(doc, &opts),
}
.expect("saves")
}
/// The leaf pages, in `/Kids` order. Local so these tests do not depend on `annot::page_refs`
/// about a page tree they are only incidentally exercising.
pub fn page_object_numbers(doc: &Document) -> Vec<u32> {
let root = doc.root().expect("a catalog");
let pages = doc.get(root).as_dict().and_then(|d| d.reference(b"Pages")).expect("/Pages");
let mut out = Vec::new();
let mut seen = std::collections::HashSet::new();
collect_pages(doc, pages, &mut seen, &mut out);
out
}
fn collect_pages(doc: &Document, node: pdfcraft_cos::ObjRef, seen: &mut std::collections::HashSet<pdfcraft_cos::ObjRef>, out: &mut Vec<u32>) {
// `seen` bounds the walk, so a cyclic page tree visits each node once and cannot recurse forever.
if !seen.insert(node) || seen.len() > 10_000 {
return;
}
let obj = doc.get(node);
let Some(d) = obj.as_dict() else { return };
if d.name(b"Type") == Some(b"Page") {
out.push(node.num);
return;
}
let Some(kids) = d.get(b"Kids").map(|k| doc.resolve(k)).and_then(|k| k.as_array().cloned()) else { return };
for k in kids {
// The array element itself is the reference; resolving it would give the page dictionary.
if let Object::Ref(r) = k {
collect_pages(doc, r, seen, out);
}
}
}
/// The raw entries of page `page`'s `/Annots`, in array order (references or inline dictionaries).
pub fn annot_entries(doc: &Document, page: usize) -> Vec<Object> {
let num = *page_object_numbers(doc).get(page).expect("the page exists");
let d = doc.get(pdfcraft_cos::ObjRef::new(num, 0));
let Some(a) = d.as_dict().and_then(|d| d.get(b"Annots").map(|a| doc.resolve(a))) else { return Vec::new() };
a.as_array().cloned().unwrap_or_default()
}
/// The dictionary an `/Annots` entry names (a stream's dictionary counts as one).
pub fn dict_of(doc: &Document, entry: &Object) -> Option<Dict> {
doc.resolve(entry).as_dict().cloned()
}
/// A dictionary's number array with every element resolved, as PDF numbers. An entry that is not
/// a number (a name, a string, a dictionary) yields `None` here rather than 0.0, so tests can
/// tell "absent or wrong type" apart from "present and zero".
pub fn nums(doc: &Document, d: &Dict, key: &[u8]) -> Option<Vec<f64>> {
let a = doc.resolve(d.get(key)?).as_array()?.clone();
a.iter().map(|x| doc.resolve(x).as_f64()).collect()
}
/// A single number entry (`/CA`, `/BS /W`), resolving an indirect value.
pub fn num(doc: &Document, d: &Dict, key: &[u8]) -> Option<f64> {
doc.resolve(d.get(key)?).as_f64()
}
/// A dictionary's text entry, decoded with our reader (`None` when absent or not a string).
pub fn str_of(doc: &Document, d: &Dict, key: &[u8]) -> Option<String> {
doc.resolve(d.get(key)?).as_string().map(|s| s.to_text())
}
/// A dictionary's name entry as a `&str` where it is ASCII, for readable assertion messages.
pub fn name_of(doc: &Document, d: &Dict, key: &[u8]) -> Option<String> {
let o = doc.resolve(d.get(key)?);
Some(String::from_utf8_lossy(o.as_name()?).into_owned())
}
/// Annotation flag bit masks, ISO 32000-2 §12.5.3 Table 165 (the spec numbers bits from 1).
/// `pdfcraft-annot` keeps these private, so they are restated here from the standard: this is
/// deliberately *not* read back from the crate, otherwise a wrong bit would be tested against
/// itself.
pub const FLAG_INVALID: i64 = 1 << 0; // bit 1
pub const FLAG_HIDDEN: i64 = 1 << 1; // bit 2
pub const FLAG_PRINT: i64 = 1 << 2; // bit 3
pub const FLAG_NO_ZOOM: i64 = 1 << 3; // bit 4
pub const FLAG_NO_ROTATE: i64 = 1 << 4; // bit 5
pub const FLAG_NO_VIEW: i64 = 1 << 5; // bit 6
pub const FLAG_READONLY: i64 = 1 << 7; // bit 8 (`/ReadOnly`, PdfCraft's Locked)