Skip to content

test(kernel): pin the catalog bytes of Save and the signing path (part of #606) - #626

Merged
Tim81 merged 5 commits into
mainfrom
test/catalog-golden-hashes
Nov 20, 2026
Merged

Tim81 merged 5 commits into
mainfrom
test/catalog-golden-hashes

Conversation

@Tim81

@Tim81 Tim81 commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Part of #606

Tests only, no production change, no public API change.

Save and PrepareForSigning build the document catalog in two separate blocks of PdfDocument. They do not agree:

  • /AcroForm is written after /StructTreeRoot by Save (and only when the document has form fields) and straight after /Metadata by the signing path (always, because it holds the signature field).
  • Save reserves the output-intent objects before the outline tree; the signing path reserves them after it. A document with both an outline and a PDF/A claim therefore numbers /OutputIntents differently on each path (8 0 R on Save, 12 0 R when signing, measured).

#606 merges the two blocks into one builder. That change has to keep every byte, so this PR pins the bytes first, ahead of the refactor.

What is pinned

tests/VellumPdf.Kernel.Tests/CatalogGoldenTests.cs, with Timestamp and (for signing) SignaturePlaceholderOptions.SigningTime pinned. Both pins are needed: Timestamp alone leaves /M in the signature dictionary on the wall clock.

Path Configurations Pinned by
Save plain, outline, tagged, PDF/A-2u, PDF/UA-1 + tagged + Language "en", text field, Language "en" with no conformance level, every optional key at once (outline + text field + PDF/UA-1 + tagged + Language "en"), outline + PDF/A-2u, Linearize, UseObjectStreams SHA-256 of the normalised file (below); exact catalog object text (classic xref and linearized files)
Save, object streams the catalog sits inside an object stream read back through VellumPdf.Reader: keys in order (Type, Pages, Metadata) and every value
Save, Encrypt not hashable: the file key, /U//O salts and AES IVs come from RandomNumberGenerator with no seam catalog object text, trailer /Size//Root//Info//Encrypt, catalog read back with password u
PrepareForSigning the same configurations except Linearize and UseObjectStreams SHA-256 of the normalised file; exact catalog object text
PrepareForSigning with Linearize = true same bytes as the plain signing file

Every file also goes through qpdf --check (the encrypted one with --password=u), asserting the line "No syntax or stream encoding errors found" and not only the exit code, through ExternalTool.TryRun so OracleGate applies. veraPDF is not used here: the catalog key order is not a veraPDF rule, so it would prove nothing about order; the refactor PR will use it for its own claim.

The hash is taken over a normalised file, so it does not depend on the runtime's Deflate output. That output already differs between the two target frameworks: an empty page content stream is written as /Filter /FlateDecode /Length 0 with no data on net10.0 and as the 8 bytes of an empty zlib stream on net11.0. Normalising inflates every /FlateDecode stream (setting its /Length to the inflated length) and replaces what depends on a byte offset or a compressed size: the cross-reference entries, startxref, the trailer or cross-reference stream /Prev offset (an outline item's /Prev N 0 R is not masked), the linearization dictionary's /L, /H, /E and /T (/O and /N stay pinned), and the contents of a hint stream and of a cross-reference stream. Object order, object numbers, every dictionary key and every stream's decoded content are still hashed, and qpdf --check checks the offsets in the same file. The catalog text is compared exactly and is not normalised. One hash per configuration serves both frameworks; I measured that by running the suite on net10.0 and net11.0, where all 20 values agreed.

Not measured

Linux. The expected values were measured on Windows. This PR should not merge before CI is green on both target frameworks; if Linux hashes differ, that is a finding to report, not something to paper over with OS-keyed values.

Evidence the goldens discriminate

Mutations run against src/VellumPdf.Kernel/Document/PdfDocument.cs on the committed tests (rebuilt Kernel DLL each time, both target frameworks, same failing set on net10.0 and net11.0), restored with git checkout -- src tests. Save_matchesTheGoldenBytes and the catalog-text test are written Save_* below; the two signing tests are PrepareForSigning_*.

Mutation Failing tests
Signing path: write /AcroForm after /StructTreeRoot (the Save position) 12 cases: the hash and the catalog-text test for outline, tagged, pdfa2u, pdfua1, outline-pdfa2u and everything. plain, form and lang do not fail: nothing sits between the two positions there.
Signing path: reserve the output intents before the outline tree 4 cases: both tests for outline-pdfa2u and everything
Save: reserve the output intents after the outline tree 4 cases: both tests for outline-pdfa2u and everything
Save: write /AcroForm unconditionally 19 cases on each target framework
Both paths: DisplayDocTitle false instead of true 8 cases: the hash and catalog-text tests for pdfua1 and everything, on both paths
Save: /AcroForm straight after /Metadata (the signing order) 2 cases: Save_* for everything (hash and catalog text)
Save: /AcroForm last, after /ViewerPreferences 2 cases: Save_* for everything
Save: /AcroForm after /OutputIntents, before /Lang 2 cases: Save_* for everything
Both paths: /Lang written only under PDF/UA-1 4 cases: both tests for lang, on both paths
Save: /Pages and /Metadata swapped 23 cases, among them Save_matchesTheGoldenBytes(objstm): that catalog is inside a compressed stream, so this shows the normalised hash still covers decoded stream content
One hex digit of the plain Save expected hash changed Save_matchesTheGoldenBytes(plain), with the computed hash in the message

Without the everything configuration the three /AcroForm position mutants (straight after /Metadata, last, after /OutputIntents) survive on Save: no other configuration holds an outline or tagging or a conformance claim together with a form. Without lang, the /Lang condition survives.

Test counts

dotnet test tests/VellumPdf.Kernel.Tests -c Release with REQUIRE_ORACLES=1, REQUIRE_VERAPDF=1, veraPDF 1.30.4, qpdf 12.4.2, poppler on POPPLER_HOME, on this branch merged with main b563dae: net10.0 1,762 passed, net11.0 1,762 passed, 0 failed, 0 skipped. CatalogGoldenTests is 63 of each. Also clean: dotnet build VellumPdf.slnx -c Release (0 warnings), dotnet format --verify-no-changes, eng/clean-room-check.ps1, python eng/generate-roadmap.py --check.

Not done

Save and PrepareForSigning build the document catalog in two separate
blocks that differ in key order (/AcroForm sits after /StructTreeRoot in
one and straight after /Metadata in the other) and in when the
output-intent objects are numbered. Merging them (#606) must not move a
byte, so pin each path with the SHA-256 of the whole file and the exact
catalog object text for plain, outline, tagged, PDF/A-2u, PDF/UA-1, form
and outline + PDF/A-2u documents, plus the linearized and object-stream
saves.

Expected hashes are per target framework (net10.0 and net11.0 write an
empty content stream differently); the catalog text is the same on both.
Encrypted output cannot be hashed, so it is pinned structurally. Every
file is also checked with qpdf.
…ange

The golden hashes covered the raw bytes, which depend on the runtime's Deflate
output, and CI floats the runtime patch. The hash now covers the file with every
FlateDecode stream inflated and byte offsets masked, so one value serves both
target frameworks, and the catalog text stays exact.

Two configurations are added on both paths: one with every optional catalog key
(outline, text field, PDF/UA-1, tagged, language) and one with a language and no
conformance level, so that the position of /AcroForm and the condition on /Lang
are both pinned.
… covers

The /Prev mask also matched an outline item's /Prev N 0 R. It now skips an indirect reference. The remark and CHANGELOG name the linearization keys masked (/L /H /E /T) rather than all its numbers.
Rewrap thirteen comment lines that ran past the column limit. Wording is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant