Files
FFF_vtt_frozen/tests/tprobing.nim
T

127 lines
6.0 KiB
Nim

## T8 probing test, in-process (DESIGN 3.4 T8, 10.3, 5.5). The WebSocket / hostile-client version is M3.
##
## Attacker model: owns one token and may send any destination, any number of times (rate limit is a transport
## concern, M3). It sees only the reply (ack/nack + position), exactly what the wire carries.
##
## CONCRETE BOUND B = stepCap + tokenRadius (token vision radius, or the scene default, plus r):
## P1 a probe longer than the cap always gets the same nack, whatever the walls and doors are;
## P2 the reply to any probe from position s depends on walls only within distance B of s; so by induction two
## scenes that differ only in walls/doors farther than B from every position the token ever occupied produce
## IDENTICAL reply sequences (no wall or door state beyond B is recoverable, however many probes);
## P3 inside B, one probe yields one bit (ack/nack): it can reveal walls behind a wall within the disc, which is
## what the cap allows (cap = LOS radius); the negative control shows the harness does detect that;
## P4 nacks mutate nothing (position, blocked flag), so probing cannot be used to move or unflag a token.
import std/[unittest, math]
import geometry, moverules, testutil
const
R = 20.0
Cap = 300.0
Bound = Cap + R
WorldW = 4096.0
let me = Actor(role: rolePlayer, player: 1)
proc mkScene(walls: seq[Wall]): Scene =
Scene(w: WorldW, h: WorldW, tokenRadius: R, visionDefault: Cap, walls: walls,
tokens: @[Token(pos: (300.0, 2000.0), owner: 1, hasVision: true, visionRadius: Cap)])
type Trace = object
replies: seq[MoveResult]
visited: seq[Vec]
proc attack(sc: var Scene, seed: uint64, steps: int, xMax: float): Trace =
## random prober confined (by its own choice of destinations) to x <= xMax; half the probes are over-cap
var r = Rng(s: seed)
for i in 0 ..< steps:
let s = sc.tokens[0].pos
var d: Vec
if i mod 2 == 0:
d = (r.uni(0, xMax), r.uni(0, WorldW)) # anywhere (mostly over the cap)
else:
let a = r.uni(0, 2 * PI)
let l = r.uni(0, 1.1 * Cap) # around the cap
d = (min(xMax, max(0.0, s.x + l * cos a)), min(WorldW, max(0.0, s.y + l * sin a)))
let res = sc.moveToken(me, 0, d)
result.replies.add res
result.visited.add sc.tokens[0].pos
# P4: every nack leaves the token where it was
if res.kind == mkNack: doAssert sc.tokens[0].pos == s and res.pos == s
proc minDist(trace: Trace, walls: seq[Wall]): float =
result = Inf
for p in trace.visited:
for w in walls: result = min(result, segDist(p, w.a, w.b))
suite "probing (T8)":
test "P1: probes longer than the cap get one fixed nack regardless of the scene":
var empty = mkScene(@[])
var rnd = Rng(s: 99)
var dense = mkScene(randomWalls(rnd, 400, 3000))
dense.tokens[0].pos = (300.0, 2000.0) # may overlap a wall (unflagged, set directly): attacker cannot tell
var r = Rng(s: 5)
for i in 0 ..< 2000:
let a = r.uni(0, 2 * PI)
let l = r.uni(Cap + 0.01, 3000)
let d: Vec = (300 + l * cos a, 2000 + l * sin a)
let (x, y) = (empty.moveToken(me, 0, d), dense.moveToken(me, 0, d))
check x == y
check x == MoveResult(kind: mkNack, pos: (300.0, 2000.0))
test "P2: scenes differing only beyond the bound give identical replies over 3000 probes":
# shared near field (x < 1500) + a far field (x >= 2600) that differs: other walls, extra walls, doors toggled
var r = Rng(s: 2024)
let near = randomWalls(r, 80, 1500)
var farA = randomWalls(r, 120, 1200, 2600)
var farB = randomWalls(r, 200, 1200, 2600) # different walls
for w in farA.mitems: w.door = true; w.open = false
for w in farB.mitems: w.door = true; w.open = true # same count class, doors flipped
var a = mkScene(near & farA)
var b = mkScene(near & farB)
for seed in [1'u64, 2, 3]:
a = mkScene(near & farA); b = mkScene(near & farB)
let ta = attack(a, seed, 3000, 1500)
let tb = attack(b, seed, 3000, 1500)
check ta.replies == tb.replies
# the premise really held: the walks stayed farther than B from every differing wall
check minDist(ta, farA & farB) > Bound
var acks = 0
for x in ta.replies:
if x.kind == mkAck: inc acks
check acks > 100 and acks < 2900 # the attacker both moved and was refused: not a trivial run
test "P3 negative control: a wall inside the disc IS visible through replies (the harness can fail)":
var r = Rng(s: 2024)
let near = randomWalls(r, 80, 1500)
let a = mkScene(near)
# same scene plus one wall right in front of the start, within B
let b = mkScene(near & @[W(300 + 150, 1900, 300 + 150, 2100)])
var sa = a
var sb = b
let ta = attack(sa, 7, 400, 1500)
let tb = attack(sb, 7, 400, 1500)
check ta.replies != tb.replies
# and a single targeted probe shows the one-bit leak the cap allows: through-the-wall move nacks, free one acks
var ca = mkScene(@[])
var cb = mkScene(@[W(450, 1900, 450, 2100)])
check ca.moveToken(me, 0, (500.0, 2000.0)).kind == mkAck
check cb.moveToken(me, 0, (500.0, 2000.0)).kind == mkNack
test "P4: probing cannot unflag or move a blocked token":
var s = mkScene(@[W(310, 1900, 310, 2100)]) # overlaps the token at (300,2000)
s.tokens[0].blocked = true
var r = Rng(s: 11)
for i in 0 ..< 500:
let res = s.moveToken(me, 0, (r.uni(0, 600), r.uni(1700, 2300)))
check res == MoveResult(kind: mkNack, pos: (300.0, 2000.0))
check s.tokens[0].blocked and s.tokens[0].pos == (300.0, 2000.0)
test "doors beyond the bound: opening or closing them changes no reply":
var r = Rng(s: 31)
let near = randomWalls(r, 60, 1200)
let doors = @[W(2800, 2000, 2800, 2200, door = true), W(3000, 1800, 3000, 2100, door = true, open = true)]
var a = mkScene(near & doors)
var closedAll = doors
closedAll[1].open = false
var b = mkScene(near & closedAll)
check attack(a, 77, 2000, 1200).replies == attack(b, 77, 2000, 1200).replies