164 lines
7.0 KiB
JavaScript
164 lines
7.0 KiB
JavaScript
// M3 slice 5 (#20), test group "probing" (DESIGN 3.4 T8, 10.3, 5.5; H26): the in-process T8 test of
|
|
// M2, re-run over the real wire with a hostile client.
|
|
//
|
|
// Attacker model: a player who controls one token and may send `token.move` with any destination.
|
|
// The only thing they learn is the shape of the reply: `{ok:true, pos}` or the uniform nack
|
|
// `{ok:false, pos:<old>}` (5.5, T9). Claims, over WS:
|
|
// P1 a probe longer than the step cap always gets the same nack, whatever the walls are;
|
|
// P2 two scenes that differ only in walls farther than cap + token radius from every position the
|
|
// token ever occupied produce IDENTICAL reply sequences, however many probes;
|
|
// P3 negative control: a wall INSIDE that bound does change the replies (the harness can fail);
|
|
// P4 nacks mutate nothing: the token never moves when refused.
|
|
// The rate limit (30/s, burst 100) is a transport bound of 6.4, not the oracle: the run stays inside
|
|
// the burst so every probe is answered as an intent.
|
|
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { createPlayer, namePlayer, openWs, sleep } from './lib.mjs'
|
|
import { gmKeyOf, makeStateDir, signIn, startServer, stopServer } from './withserver.mjs'
|
|
import { projectRoot } from './lib.mjs'
|
|
|
|
const CAP = 400 // the scene's vision_radius_default, also the step cap (5.5)
|
|
const RADIUS = 20 // the scene's token radius
|
|
const BOUND = CAP + RADIUS // no wall beyond this distance from any visited position is observable
|
|
const START = [1000, 2000]
|
|
const PROBES = 90 // inside the 100-intent burst of 6.4
|
|
|
|
// Deterministic xorshift, so both runs send exactly the same probe sequence when the replies agree.
|
|
function makeRng(seed) {
|
|
let s = BigInt(seed)
|
|
return {
|
|
next() {
|
|
s ^= (s << 13n) & 0xffffffffffffffffn
|
|
s ^= s >> 7n
|
|
s ^= (s << 17n) & 0xffffffffffffffffn
|
|
s &= 0xffffffffffffffffn
|
|
return Number(s >> 11n) / Number(1n << 53n)
|
|
},
|
|
}
|
|
}
|
|
|
|
async function setup(farWalls, nearWalls = []) {
|
|
const stateDir = makeStateDir()
|
|
const gmKey = gmKeyOf(stateDir)
|
|
const s = await startServer({ stateDir })
|
|
const gm = await openWs(s.origin, (await signIn(s.origin, gmKey)).cookie)
|
|
await gm.waitText('hello')
|
|
const scene = (await gm.waitText('walls')).scene
|
|
const addWall = async fields => {
|
|
const ack = await gm.request({ t: 'wall.add', scene, ...fields })
|
|
assert.equal(ack.ok, true, JSON.stringify(ack))
|
|
}
|
|
for (const w of nearWalls) await addWall(w)
|
|
for (const w of farWalls) await addWall(w)
|
|
const player = await createPlayer(gm)
|
|
await namePlayer(s.origin, player.key, 'Prober')
|
|
const tok = await gm.request({ t: 'token.create', scene, kind: 'pc', pos: START,
|
|
descriptive_name: 'prober', actual_name: 'Prober',
|
|
controller: player.id, flags: { vision: true } })
|
|
const a = await openWs(s.origin, (await signIn(s.origin, player.key)).cookie)
|
|
await a.waitText('hello')
|
|
await sleep(100)
|
|
return { s, gm, a, stateDir, tokenId: tok.id, playerId: player.id }
|
|
}
|
|
|
|
async function teardown(ctx) {
|
|
try { ctx.gm.close(); ctx.a.close() } catch {}
|
|
await stopServer(ctx.s)
|
|
fs.rmSync(ctx.stateDir, { recursive: true, force: true })
|
|
}
|
|
|
|
// Runs the probe sequence and returns the replies plus what else the client saw.
|
|
async function probe(ctx) {
|
|
const rng = makeRng(20261115)
|
|
const replies = []
|
|
let pos = START
|
|
let overCap = 0
|
|
let acks = 0
|
|
let nacks = 0
|
|
const badNacks = []
|
|
for (let i = 0; i < PROBES; i++) {
|
|
const over = i % 3 !== 0 // two over-cap probes for every one inside the cap
|
|
const angle = rng.next() * 2 * Math.PI
|
|
const len = over ? CAP + 1 + rng.next() * 400 : rng.next() * CAP
|
|
const dest = [
|
|
Math.min(4095, Math.max(0, pos[0] + len * Math.cos(angle))),
|
|
Math.min(4095, Math.max(0, pos[1] + len * Math.sin(angle))),
|
|
]
|
|
const distance = Math.hypot(dest[0] - pos[0], dest[1] - pos[1])
|
|
const ack = await ctx.a.request({ t: 'token.move', id: ctx.tokenId, pos: dest })
|
|
replies.push({ ok: ack.ok, pos: ack.pos, code: ack.code })
|
|
if (ack.ok) {
|
|
acks++
|
|
pos = ack.pos
|
|
} else {
|
|
nacks++
|
|
if (distance > CAP + 1e-9) {
|
|
overCap++
|
|
// P1: one shape, the old position, no reason (5.5, T9). A code or a moved token would leak.
|
|
if (ack.code !== undefined || ack.pos[0] !== pos[0] || ack.pos[1] !== pos[1]) {
|
|
badNacks.push({ i, distance, ack })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { replies, pos, overCap, acks, nacks, badNacks }
|
|
}
|
|
|
|
const farA = [
|
|
{ a: [3000, 1000], b: [3000, 3000], kind: 'wall' },
|
|
{ a: [3200, 1200], b: [3400, 2600], kind: 'door', open: false },
|
|
]
|
|
const farB = [
|
|
{ a: [3050, 2400], b: [3600, 2400], kind: 'wall' },
|
|
{ a: [3100, 1500], b: [3100, 1900], kind: 'door', open: true },
|
|
{ a: [3800, 1000], b: [3900, 3900], kind: 'wall' },
|
|
]
|
|
// Inside the bound relative to START: 150 px east.
|
|
const nearExtra = [{ a: [1150, 1900], b: [1150, 2100], kind: 'wall' }]
|
|
|
|
test('probing over WS: walls beyond cap + radius change no reply', async () => {
|
|
fs.mkdirSync(path.join(projectRoot, 'results'), { recursive: true })
|
|
const a = await setup(farA, nearExtra)
|
|
const b = await setup(farB, nearExtra)
|
|
try {
|
|
const ra = await probe(a)
|
|
const rb = await probe(b)
|
|
assert.ok(ra.acks > 5 && ra.nacks > 5, `the run must both move and be refused: ${JSON.stringify(ra)}`)
|
|
assert.ok(ra.overCap > 40, `the run must be mostly over-cap probes: ${ra.overCap}`)
|
|
assert.deepEqual(ra.badNacks, [], 'P1: an over-cap probe got state (a code or a moved token)')
|
|
const shape = r => r.replies.map(x => `${x.ok}|${x.pos[0]}|${x.pos[1]}`)
|
|
assert.deepEqual(shape(ra), shape(rb),
|
|
'P2: the reply sequence depends on walls beyond the bound')
|
|
assert.deepEqual(ra.replies.filter(r => r.code !== undefined), [],
|
|
'no probe was refused by the rate limit: the burst covers the run')
|
|
// P4: nacks never moved the token, and the server's own view agrees with the last ack
|
|
await sleep(150)
|
|
const gmTokens = b.gm.messages.filter(m => m.t === 'token' && m.id === b.tokenId)
|
|
const last = gmTokens[gmTokens.length - 1]
|
|
assert.deepEqual(last.pos, rb.pos, 'P4: the token ended where the last ack said')
|
|
fs.appendFileSync(path.join(projectRoot, 'results', 'probing.log'),
|
|
`probes=${PROBES} acks=${ra.acks} nacks=${ra.nacks} overCap=${ra.overCap} ` +
|
|
`identical=true\n`)
|
|
} finally {
|
|
await teardown(a)
|
|
await teardown(b)
|
|
}
|
|
})
|
|
|
|
test('probing over WS: a wall inside the bound does change the replies (the harness can fail)', async () => {
|
|
const plain = await setup(farA)
|
|
const extra = await setup(farA, nearExtra)
|
|
try {
|
|
const rp = await probe(plain)
|
|
const re = await probe(extra)
|
|
const shape = r => r.replies.map(x => `${x.ok}|${x.pos[0]}|${x.pos[1]}`)
|
|
assert.notDeepEqual(shape(rp), shape(re),
|
|
'P3: a wall within the bound must be observable through the replies')
|
|
} finally {
|
|
await teardown(plain)
|
|
await teardown(extra)
|
|
}
|
|
})
|