mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 07:05:44 +01:00
Answer architecture round 1: the reader returns its refusals and owns its stack, a decision and a test keep JSON.parse out, item 83 filed, the scoring run recorded
This commit is contained in:
+25
-12
@@ -1,5 +1,8 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { dirname, join } from 'node:path'
|
||||
import fc from 'fast-check'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { readFileSync, readdirSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { JsonSpelling } from './canonical-json.ts'
|
||||
@@ -13,48 +16,50 @@ test('reads every JSON value kind', () => {
|
||||
assert.deepEqual(parseJsonText('[ ]'), { value: [] })
|
||||
assert.deepEqual(parseJsonText('{ }'), { value: {} })
|
||||
assert.deepEqual(parseJsonText('[[],{}]'), { value: [[], {}] })
|
||||
assert.ok(Object.is(parseJsonText('-0')?.value, -0))
|
||||
assert.ok(Object.is(parseJsonText('-0').value, -0))
|
||||
})
|
||||
|
||||
test('decodes every string escape', () => {
|
||||
assert.deepEqual(parseJsonText('"\\"\\\\\\/\\b\\f\\n\\r\\t\\u00e9\\uD83C\\uDF89\\ud800"'), { value: '"\\/\b\f\n\r\té\u{1f389}\ud800' })
|
||||
})
|
||||
|
||||
test('keys stay the keys the text spells after V8 has read a colliding key (https://issues.chromium.org/issues/521080746)', () => {
|
||||
test('keys stay the keys the text spells after JSON.parse has read a key holding a backslash (https://issues.chromium.org/issues/521080746)', () => {
|
||||
const canary = '{"\\r":1,"\\f":2}'
|
||||
JSON.parse(canary)
|
||||
JSON.parse('{"\\r":3,"\\\\":4}')
|
||||
assert.deepEqual(Object.keys(parseJsonText(canary)?.value ?? {}), ['\r', '\f'])
|
||||
assert.deepEqual(Object.keys(parseJsonText(canary).value ?? {}), ['\r', '\f'])
|
||||
})
|
||||
|
||||
test('a __proto__ key is an own key', () => {
|
||||
const read = parseJsonText('{"__proto__":{"a":1}}')?.value
|
||||
const read = parseJsonText('{"__proto__":{"a":1}}').value
|
||||
assert.deepEqual(Object.keys(read ?? {}), ['__proto__'])
|
||||
assert.equal(Object.getPrototypeOf(read), Object.prototype)
|
||||
})
|
||||
|
||||
test('a repeated key keeps its first position and its last value, as JSON.parse does', () => {
|
||||
const read = parseJsonText('{"a":1,"b":2,"a":3}')?.value
|
||||
const read = parseJsonText('{"a":1,"b":2,"a":3}').value
|
||||
assert.deepEqual(read, { a: 3, b: 2 })
|
||||
assert.deepEqual(Object.keys(read ?? {}), ['a', 'b'])
|
||||
})
|
||||
|
||||
test('a number past the double range reads as JSON.parse reads it', () => {
|
||||
assert.deepEqual(parseJsonText('1e999'), { value: Infinity })
|
||||
test('refuses a number past the double range, which JSON.parse reads as Infinity', () => {
|
||||
assert.deepEqual(parseJsonText('1e999'), { refusal: 'non-finite' })
|
||||
assert.deepEqual(parseJsonText('[-1e999]'), { refusal: 'non-finite' })
|
||||
assert.deepEqual(parseJsonText('[1e999,'), { refusal: 'syntax' })
|
||||
})
|
||||
|
||||
test('refuses text JSON.parse refuses', () => {
|
||||
const refused = ['', ' ', '{', '[', '[1', '[1,', '[1,]', '{"a"}', '{"a":}', '{"a":1,}', '{a:1}', '{"a" 1}', '{,}', '01', '1.', '.5', '-', '1e', '+1', '"a', '"\u0001"', '"\\x"', '"\\u12"', '"\\u12g4"', 'tru', 'nul', '1 2', '[1]]', '}', "'a'", 'NaN', ' 1']
|
||||
const refused = ['', ' ', '{', '[', '[1', '[1,', '[1,]', '{"a"}', '{"a":}', '{"a":1,}', '{a:1}', '{"a" 1}', '{,}', '01', '1.', '.5', '-', '1e', '+1', '"a', '"\u0001"', '"\\x"', '"\\u12"', '"\\u12g4"', 'tru', 'nul', '1 2', '[1]]', '}', "'a'", 'NaN', '\u00a01']
|
||||
for (const text of refused) {
|
||||
assert.throws(() => JSON.parse(text), SyntaxError, text)
|
||||
assert.equal(parseJsonText(text), undefined, text)
|
||||
assert.deepEqual(parseJsonText(text), { refusal: 'syntax' }, text)
|
||||
}
|
||||
})
|
||||
|
||||
test('reads a value nested far past the call stack without recursing', () => {
|
||||
const depth = 100_000
|
||||
assert.ok(parseJsonText(`${'['.repeat(depth)}${']'.repeat(depth)}`) !== undefined)
|
||||
assert.ok(parseJsonText(`${'{"a":'.repeat(depth)}1${'}'.repeat(depth)}`) !== undefined)
|
||||
assert.ok(parseJsonText(`${'['.repeat(depth)}${']'.repeat(depth)}`).value !== undefined)
|
||||
assert.ok(parseJsonText(`${'{"a":'.repeat(depth)}1${'}'.repeat(depth)}`).value !== undefined)
|
||||
})
|
||||
|
||||
test('reads back every value the canonical serializer writes', () => {
|
||||
@@ -76,8 +81,16 @@ test('accepts exactly the text JSON.parse accepts', () => {
|
||||
} catch {
|
||||
accepted = false
|
||||
}
|
||||
assert.equal(parseJsonText(text) !== undefined, accepted)
|
||||
assert.equal(parseJsonText(text).refusal !== 'syntax', accepted)
|
||||
}),
|
||||
propertyRuns(2000),
|
||||
)
|
||||
})
|
||||
|
||||
test('no shipped file reads JSON with JSON.parse, which parseJsonText stands in for', () => {
|
||||
const sourceRoot = dirname(fileURLToPath(import.meta.url))
|
||||
const readers = readdirSync(sourceRoot, { encoding: 'utf8', recursive: true }).filter(
|
||||
(name) => name.endsWith('.ts') && !name.endsWith('.test.ts') && !name.startsWith('conformance') && readFileSync(join(sourceRoot, name), 'utf8').includes('JSON.parse('),
|
||||
)
|
||||
assert.deepEqual(readers, [])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user