Files
faunosteo/packages/pdf/tests/r4.inflate-oracles.test.ts
T
bchanot 4439fd2420 test(tests): R4.1e browser harness and TS baselines (Chromium, Firefox, WebKit)
Playwright harness under scripts/browser: 127.0.0.1 server with the DEPLOY
CSP, export and import baselines of the TypeScript engine under a 4 GiB RSS
watchdog (not a guaranteed memory limit), O9 browser columns in
inflate.browsers.json (26 rows agree with Node in all three engines).
Results of 2026-10-18 committed; the O9 Node test no longer expects browser
columns in inflate.json.
2026-10-18 15:05:28 +02:00

334 lines
9.3 KiB
TypeScript

/**
* R4.1 freeze O9, Node part (plan 2026-10-17 §B.1.2 "Inflation", §B.5.1):
* the verdict of `inflateFlate(bytes, limit)` (DecompressionStream('deflate')
* fed in 16 KiB awaited slices, null on any failure or past the limit) on
* the zlib framing cases, inputs under `apps/app/tests/fixtures/pdf/r4/
* inflate/`, rows in `crates/core/tests/oracles/r4/inflate.json`. Each row
* holds the Node verdict; the browser harness (`scripts/browser/
* inflate-verdicts.mjs`) runs the same function on the same files in each
* engine and writes its columns next to Node's in `inflate.browsers.json`.
* `MAKE_R4_ORACLES=1` writes the inputs and this file.
*/
import { resolve } from 'node:path'
import {
constants,
createDeflate,
deflateRawSync,
deflateSync,
gzipSync,
} from 'node:zlib'
import { describe, it } from 'vitest'
import { MAX_ATTACHMENT_BYTES } from '../src'
import { ascii, concat, deflate, KiB, noise } from './helpers/r4-reader-craft'
import {
expectOnlyFiles,
inflateVerdict,
R4_INPUTS_DIR,
R4_ORACLES_DIR,
sha256,
storeFile,
storeJson,
} from './helpers/r4-reader-io'
const INPUTS_DIR = resolve(R4_INPUTS_DIR, 'inflate')
/** The engine column this generator owns: recorded, never compared. */
const nodeEngine = () => ({
version: process.version,
zlib: process.versions.zlib,
})
const OUT = resolve(R4_ORACLES_DIR, 'inflate.json')
const CAP = MAX_ATTACHMENT_BYTES
const TEXT = ascii('{"version":3,"note":"inflate oracle"}')
const STREAM = deflate(TEXT)
const EXACT = 64 * KiB
function adler32(data: Uint8Array): number {
let a = 1
let b = 0
for (const byte of data) {
a = (a + byte) % 65521
b = (b + a) % 65521
}
return ((b << 16) | a) >>> 0
}
/** A stored deflate block; `nlen` defaults to the one's complement of the length. */
function storedBlock(
data: Uint8Array,
final: boolean,
nlen = ~data.length,
): Uint8Array {
const len = data.length
return concat([
Uint8Array.from([
final ? 1 : 0,
len & 0xff,
len >> 8,
nlen & 0xff,
(nlen >> 8) & 0xff,
]),
data,
])
}
/** zlib framing (CMF 0x78, FLG 0x01) around raw deflate blocks. */
function zlibOf(blocks: Uint8Array[], payload: Uint8Array): Uint8Array {
const check = adler32(payload)
const trailer = [
check >>> 24,
(check >> 16) & 0xff,
(check >> 8) & 0xff,
check & 0xff,
]
return concat([
Uint8Array.from([0x78, 0x01]),
...blocks,
Uint8Array.from(trailer),
])
}
function storedBlocks(): Uint8Array {
const parts = [ascii('abc'), ascii(''), ascii('defgh')]
const blocks = parts.map((part, i) =>
storedBlock(part, i === parts.length - 1),
)
return zlibOf(blocks, concat(parts))
}
function storedBadNlen(): Uint8Array {
const data = ascii('abcdefgh')
return zlibOf([storedBlock(data, true, ~data.length ^ 1)], data)
}
/** One zlib stream, a full flush after each chunk: several compressed blocks. */
async function flushedBlocks(chunks: Uint8Array[]): Promise<Uint8Array> {
const deflater = createDeflate()
const out: Buffer[] = []
deflater.on('data', (chunk: Buffer) => out.push(chunk))
const ended = new Promise((done) => deflater.on('end', done))
for (const chunk of chunks) {
deflater.write(chunk)
await new Promise<void>((done) =>
deflater.flush(constants.Z_FULL_FLUSH, () => done()),
)
}
deflater.end()
await ended
return new Uint8Array(Buffer.concat(out))
}
const MULTI_BLOCK_TEXT = [
'{"version":3,',
`"note":"${'abc'.repeat(100)}",`,
'"end":true}',
].map(ascii)
const flipped = (bytes: Uint8Array, index: number, mask: number) =>
Uint8Array.from(bytes, (b, i) => (i === index ? b ^ mask : b))
interface InflateCase {
file: string
title: string
build: () => Uint8Array | Promise<Uint8Array>
/** [limit, row title]; absent = the default 16 MiB cap only. */
limits?: Array<[number, string]>
}
const CASES: InflateCase[] = [
{
file: 'valid-noise-64kib.bin',
title: 'valid zlib stream, 64 KiB of noise over several 16 KiB slices',
build: () => deflate(noise(EXACT)),
limits: [
[CAP, 'valid zlib stream, default cap'],
[EXACT, 'output exactly at the limit'],
[EXACT - 1, 'output one byte over the limit'],
],
},
{
file: 'valid-json.bin',
title: 'valid zlib stream, small JSON',
build: () => STREAM,
},
{
file: 'empty-input.bin',
title: 'empty input (0 bytes)',
build: () => new Uint8Array(0),
},
{
file: 'empty-zlib-stream.bin',
title: 'empty zlib stream (no output)',
build: () => deflate(new Uint8Array(0)),
},
{
file: 'truncated.bin',
title: 'truncated stream (first 2 KiB of a 4 KiB noise stream)',
build: () => deflate(noise(4 * KiB)).subarray(0, 2 * KiB),
},
{
file: 'truncated-header-only.bin',
title: 'the 2-byte zlib header alone',
build: () => STREAM.subarray(0, 2),
},
{
file: 'missing-adler.bin',
title: 'complete deflate data, Adler-32 missing',
build: () => STREAM.subarray(0, -4),
},
{
file: 'partial-adler.bin',
title: 'complete deflate data, 2 of the 4 Adler-32 bytes',
build: () => STREAM.subarray(0, -2),
},
{
file: 'bad-adler.bin',
title: 'bad Adler-32 (last byte flipped)',
build: () => flipped(STREAM, STREAM.length - 1, 0xff),
},
{
file: 'trailing-bytes.bin',
title: 'trailing bytes after the stream end',
build: () => Uint8Array.from([...deflate(noise(KiB)), 1, 2, 3]),
},
{
file: 'trailing-zero-byte.bin',
title: 'one 0x00 byte after the stream end',
build: () => concat([STREAM, new Uint8Array(1)]),
},
{
file: 'two-streams.bin',
title: 'two complete zlib streams back to back',
build: () => concat([STREAM, STREAM]),
},
{
file: 'raw-deflate.bin',
title: 'raw deflate without zlib header',
build: () => new Uint8Array(deflateRawSync(TEXT)),
},
{
file: 'gzip.bin',
title: 'gzip header and trailer',
build: () => new Uint8Array(gzipSync(TEXT)),
},
{
file: 'preset-dictionary.bin',
title: 'preset dictionary flag (FDICT) set',
build: () =>
new Uint8Array(deflateSync(TEXT, { dictionary: ascii('{"version":') })),
},
{
file: 'bad-header-check.bin',
title: 'zlib header whose FCHECK fails',
build: () => flipped(STREAM, 1, 0x01),
},
{
file: 'small-window.bin',
title: 'window size below 32 KiB (CINFO 1, windowBits 9)',
build: () => new Uint8Array(deflateSync(TEXT, { windowBits: 9 })),
},
{
file: 'stored-blocks.bin',
title: 'stored blocks: two non-final ones (one empty), then a final one',
build: storedBlocks,
},
{
file: 'stored-bad-nlen.bin',
title: 'stored block whose NLEN is not the complement of LEN',
build: storedBadNlen,
},
{
file: 'multiple-blocks.bin',
title: 'several compressed blocks separated by full flushes',
build: () => flushedBlocks(MULTI_BLOCK_TEXT),
},
{
file: 'corrupt-body.bin',
title: 'corrupt body (every byte after the ninth flipped)',
build: () =>
Uint8Array.from(deflate(noise(KiB)), (b, i) => (i > 8 ? b ^ 0xff : b)),
},
{
file: 'not-deflate.bin',
title: 'not deflate at all (JSON text)',
build: () => ascii('{"version":3}'),
},
{
file: 'output-exactly-16mib.bin',
title: 'output of exactly 16 MiB (the default cap)',
build: () => deflate(new Uint8Array(CAP)),
},
{
file: 'bomb-16mib-plus-1.bin',
title: 'output of 16 MiB + 1 (one byte over the default cap)',
build: () => deflate(new Uint8Array(CAP + 1)),
},
]
interface Row {
file: string
case: string
limit: number
verdicts: Record<string, unknown>
}
interface InflateOracle {
engines: Record<string, unknown>
inputs: Array<{ file: string; length: number; sha256: string }>
rows: Row[]
}
async function build() {
const inputs: InflateOracle['inputs'] = []
const rows: Row[] = []
for (const spec of CASES) {
const bytes = await spec.build()
storeFile(resolve(INPUTS_DIR, spec.file), bytes)
inputs.push({
file: spec.file,
length: bytes.byteLength,
sha256: sha256(bytes),
})
for (const [limit, title] of spec.limits ?? [[CAP, spec.title]]) {
const node = await inflateVerdict(bytes, limit)
rows.push({ file: spec.file, case: title, limit, verdicts: { node } })
}
}
return {
oracle: 'O9',
plan: '.claude/tasks/plans/2026-10-17-rust-r4-pdf-core.md §B.1.2, §B.5.1',
generator: 'packages/pdf/tests/r4.inflate-oracles.test.ts',
function:
'inflateFlate(bytes, limit) of packages/pdf/src/attachment/inflate.ts, run in each engine on the file bytes',
verdict: 'null, or { length, sha256 } of the output',
inputsDir: 'apps/app/tests/fixtures/pdf/r4/inflate',
engines: { node: nodeEngine() },
inputs,
rows,
}
}
/** The committed file restricted to what this generator owns, with the running Node recorded. */
function nodeColumn(committed: unknown): unknown {
const oracle = committed as InflateOracle
return {
...oracle,
engines: { node: nodeEngine() },
rows: oracle.rows.map((row) => ({
...row,
verdicts: { node: row.verdicts.node },
})),
}
}
describe('R4 inflate oracles O9 (TypeScript freeze, Node)', () => {
it('matches the frozen inputs and Node verdicts', async () => {
const oracle = await build()
storeJson(OUT, oracle, nodeColumn)
expectOnlyFiles(
INPUTS_DIR,
CASES.map((c) => c.file),
)
}, 120_000)
})