prototype-server-vision: deliberate-leak test (-d:leakfault build, 8 injected faults, exact checker class sets); checker failures carry classes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,3 +7,4 @@ tests/t[a-z]*
|
||||
server
|
||||
run/
|
||||
results/*.jsonl
|
||||
results/faults.log
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
"nimble test",
|
||||
"devbox run smoke",
|
||||
"devbox run leaktest",
|
||||
"devbox run faulttest",
|
||||
"devbox run restart",
|
||||
"DUR=15 devbox run concurrency"
|
||||
],
|
||||
"server": [
|
||||
@@ -45,6 +47,12 @@
|
||||
"concurrency": [
|
||||
"tools/withserver.sh node tools/concurrency.mjs"
|
||||
],
|
||||
"faulttest": [
|
||||
"tools/faulttest.sh"
|
||||
],
|
||||
"restart": [
|
||||
"NOSERVE=1 tools/withserver.sh node tools/restart.mjs"
|
||||
],
|
||||
"smoke": [
|
||||
"tools/withserver.sh node tools/smoke.mjs"
|
||||
],
|
||||
|
||||
@@ -50,6 +50,13 @@ var
|
||||
nMut: int
|
||||
lk.initLock
|
||||
|
||||
# TEST BUILD ONLY (-d:leakfault, tools/faulttest.sh): deliberate leaks picked at run time by env LEAK_FAULT.
|
||||
# Without the define fault() is a constant false and neither this code nor the env name is compiled in.
|
||||
when defined(leakfault):
|
||||
let faultName = getEnv("LEAK_FAULT")
|
||||
template fault(n: string): bool =
|
||||
when defined(leakfault): faultName == n else: false
|
||||
|
||||
proc add(s: var Stat, v: float) = inc s.n; s.sum += v; s.worst = max(s.worst, v)
|
||||
|
||||
proc printStats() =
|
||||
@@ -98,19 +105,24 @@ proc sync(c: Conn, ev: Ev, initial: bool) =
|
||||
var r: Role
|
||||
if player:
|
||||
r = roles[c.role]
|
||||
if c.ver != r.ver:
|
||||
if c.ver != r.ver or (fault("other-vision") and c.role == "A"):
|
||||
c.ver = r.ver
|
||||
var ps = newJArray()
|
||||
for p in r.polys:
|
||||
for p in (if fault("other-vision") and c.role == "A": r.polys & roles["B"].polys else: r.polys):
|
||||
var a = newJArray()
|
||||
for v in p: a.add pt(v)
|
||||
ps.add a
|
||||
c.send("vision", %*{"polys": ps})
|
||||
var cells = newJArray()
|
||||
let leakB = fault("other-explored") and c.role == "A"
|
||||
if initial:
|
||||
for k in r.explored: cells.add %[k mod cw, k div cw]
|
||||
if leakB:
|
||||
for k in roles["B"].explored: cells.add %[k mod cw, k div cw]
|
||||
else:
|
||||
for k in r.fresh: cells.add %[k mod cw, k div cw]
|
||||
if leakB:
|
||||
for k in roles["B"].fresh: cells.add %[k mod cw, k div cw]
|
||||
if cells.len > 0: c.send("explored", %*{"cells": cells})
|
||||
elif ev.door >= 0:
|
||||
c.send("door", %*{"id": ev.door, "open": ev.doorOpen})
|
||||
@@ -119,6 +131,8 @@ proc sync(c: Conn, ev: Ev, initial: bool) =
|
||||
if not vis:
|
||||
for p in r.polys:
|
||||
if inside(p, t.pos): vis = true; break
|
||||
if player and ((fault("hidden") and t.id == "M5") or (fault("withhold") and t.id == "M1")):
|
||||
vis = t.id == "M5"
|
||||
if vis:
|
||||
if c.known.getOrDefault(t.id, (-1.0, -1.0)) != t.pos or initial:
|
||||
c.known[t.id] = t.pos
|
||||
@@ -127,7 +141,7 @@ proc sync(c: Conn, ev: Ev, initial: bool) =
|
||||
c.send("token", j)
|
||||
elif c.known.hasKey(t.id):
|
||||
c.known.del t.id
|
||||
c.send("remove", %*{"id": t.id})
|
||||
if not (fault("noremove") and player): c.send("remove", %*{"id": t.id})
|
||||
|
||||
proc finish(ev: Ev, kind: string, visMs: float, mover: Conn = nil) =
|
||||
## send phase of a mutation (caller holds lk, DB already committed)
|
||||
@@ -196,10 +210,10 @@ proc openConn(ws: WebSocket, role: string) =
|
||||
conns[key(ws)] = c
|
||||
let ids = newJArray()
|
||||
for t in toks:
|
||||
if owns(role, t.id): ids.add %t.id
|
||||
if owns(role, t.id) or (fault("other-owner") and role == "A" and t.id == "B"): ids.add %t.id
|
||||
c.send("hello", %*{"role": (if role == "gm": "gm" else: "player"), "tokens": ids,
|
||||
"world": {"w": worldW, "h": worldH}, "tileSize": tileSize, "exploredCell": cell, "tokenRadius": tokRadius})
|
||||
if role == "gm":
|
||||
if role == "gm" or fault("walls"):
|
||||
var ws = newJArray()
|
||||
for i, w in walls: ws.add %*{"id": i, "a": pt(w.a), "b": pt(w.b), "door": w.door, "open": w.open}
|
||||
c.send("walls", %*{"walls": ws})
|
||||
@@ -258,7 +272,7 @@ proc handler(request: Request) {.gcsafe.} =
|
||||
let s = cookieSecret(request)
|
||||
if s in secrets:
|
||||
let rn = secrets[s]
|
||||
ok = rn == "gm"
|
||||
ok = rn == "gm" or fault("tile")
|
||||
if not ok:
|
||||
let per = tileSize div cell # shortcut: assumes tileSize % exploredCell == 0
|
||||
for cy in ty * per ..< (ty + 1) * per:
|
||||
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
# Deliberate-leak test: the checker must FAIL, under the right class, for every fault injected into a -d:leakfault server build.
|
||||
# usage: tools/faulttest.sh (isolated via withserver.sh; also proves the normal build has no hook, and a clean build still passes)
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
mkdir -p run/test results
|
||||
nim c -d:release --hints:off -o:run/test/leakcheck tools/leakcheck.nim >/dev/null || exit 1
|
||||
bad=0
|
||||
# 1. normal build carries no hook
|
||||
nim c -d:release --hints:off --nimblePath:"${NIMBLE_DIR:-.devbox/nimble}/pkgs2" --nimcache:run/test/nimcache-clean -o:run/test/server-clean src/server.nim >/dev/null || exit 1
|
||||
for f in run/test/server-clean; do
|
||||
[ -f $f ] || continue
|
||||
if grep -aq LEAK_FAULT $f; then echo "FAIL $f contains the fault hook"; bad=1; else echo "PASS $f has no fault hook"; fi
|
||||
done
|
||||
# 2. each fault: "fault-name class" (class must appear as FAILCLASS in checker output, exit must be nonzero)
|
||||
: > results/faults.log
|
||||
while read -r fault cls extra; do
|
||||
out=$(SERVER_FLAGS=-d:leakfault LEAK_FAULT=$fault SUBJECT=A tools/withserver.sh sh -c 'node tools/leak.mjs && run/test/leakcheck' 2>&1); rc=$?
|
||||
{ echo "== fault $fault"; echo "$out" | grep -E '^(FAIL|leakcheck)'; } >> results/faults.log
|
||||
got=$(echo "$out" | sed -n 's/^FAILCLASS \([A-Z_]*\) .*/\1/p' | sort | xargs)
|
||||
want=$(echo "$cls $extra" | tr ' ' '\n' | sort | xargs)
|
||||
# exact class set: the intended class fires and nothing unrelated does (a checker that flags everything would not prove anything)
|
||||
if [ $rc -ne 0 ] && [ "$got" = "$want" ]; then echo "PASS fault $fault caught as: $got"
|
||||
else echo "FAIL fault $fault (rc=$rc) classes [$got] expected [$want]"; echo "$out" | tail -5; bad=1; fi
|
||||
done <<'LIST'
|
||||
hidden HIDDEN_TOKEN
|
||||
walls WALLDATA
|
||||
tile TILE
|
||||
withhold WITHHELD
|
||||
other-vision OTHER_PLAYER OWN_VIEW
|
||||
other-explored OTHER_PLAYER EXPLORED
|
||||
other-owner OTHER_PLAYER
|
||||
noremove NO_REMOVE
|
||||
LIST
|
||||
# 3. clean build still passes
|
||||
if SUBJECT=A tools/withserver.sh sh -c 'node tools/leak.mjs && run/test/leakcheck' >/dev/null 2>&1; then echo "PASS clean build passes"; else echo "FAIL clean build rejected"; bad=1; fi
|
||||
exit $bad
|
||||
@@ -6,10 +6,13 @@ import geometry
|
||||
let me = getEnv("SUBJECT", "A") # subject whose view is checked; the other player is noise
|
||||
let other = if me == "A": "B" else: "A"
|
||||
type Side = enum Out, Amb, In
|
||||
# every failure carries a class (HIDDEN_TOKEN WALLDATA TILE WITHHELD OTHER_PLAYER NO_REMOVE | OWN_VIEW EXPLORED GEOMETRY PROTOCOL);
|
||||
# tools/faulttest.sh asserts each injected server fault is reported under its class
|
||||
var fails = 0
|
||||
proc fail(s: string) =
|
||||
inc fails
|
||||
if fails <= 20: echo "FAIL ", s
|
||||
var classes: CountTable[string]
|
||||
proc fail(cls, s: string) =
|
||||
inc fails; classes.inc cls
|
||||
if fails <= 20: echo "FAIL [", cls, "] ", s
|
||||
|
||||
# boundary is ambiguous in inside(): In/Out only if the point and its 0.5px neighbours agree
|
||||
proc side(poly: seq[Vec], p: Vec): Side =
|
||||
@@ -24,7 +27,7 @@ const allowed = ["hello", "vision", "explored", "token", "remove", "moveAck"]
|
||||
proc scan(j: JsonNode, t: string) =
|
||||
if j.kind == JObject:
|
||||
for k, v in j:
|
||||
if k in banned: fail "A msg " & t & " has wall-like key " & k
|
||||
if k in banned: fail "WALLDATA", "A msg " & t & " has wall-like key " & k
|
||||
scan(v, t)
|
||||
elif j.kind == JArray:
|
||||
for v in j: scan(v, t)
|
||||
@@ -45,64 +48,74 @@ var tileSize, cell, cw: int
|
||||
var firstG: Table[int, int] # explored cell -> gseq it first became explored (independent)
|
||||
var view: Table[string, Vec] # what A has been told
|
||||
var aExp: HashSet[int]
|
||||
var oFirst: HashSet[int] # cells the OTHER player has explored so far (independent), to attribute leaks
|
||||
var leaked: HashSet[string] # tokens already reported as HIDDEN_TOKEN (their stale view entry is not a missing remove)
|
||||
var ai, removes, groups, expN: int
|
||||
var seen: HashSet[string]
|
||||
|
||||
proc same(j: JsonNode, poly: seq[Vec]): bool =
|
||||
if j.len != poly.len: return false
|
||||
for i in 0 ..< poly.len:
|
||||
if abs(j[i][0].getFloat - poly[i].x) > 1e-6 or abs(j[i][1].getFloat - poly[i].y) > 1e-6: return false
|
||||
true
|
||||
|
||||
proc finalize(g: int) =
|
||||
inc groups
|
||||
let poly = visibilityPolygon(truth[me], walls, radius)
|
||||
let polyO = visibilityPolygon(truth[other], walls, radius)
|
||||
for (ix, iy) in cellsTouched(polyO, cell.float): oFirst.incl iy * cw + ix
|
||||
# independent oracle (segment tests, not the ray fan): clear straight line to a point in range => point is in the polygon
|
||||
for i in 0 ..< 64:
|
||||
for k in [0.3, 0.6, 0.95]:
|
||||
let o = truth[me]
|
||||
let p: Vec = (o.x + k * radius * cos(2 * PI * i.float / 64), o.y + k * radius * sin(2 * PI * i.float / 64))
|
||||
if not moveBlocked(walls, o, p) and side(poly, p) == Out: fail "polygon misses clear-LOS point " & $p & " at g=" & $g
|
||||
if not moveBlocked(walls, o, p) and side(poly, p) == Out: fail "GEOMETRY", "polygon misses clear-LOS point " & $p & " at g=" & $g
|
||||
for (ix, iy) in cellsTouched(poly, cell.float):
|
||||
if ix >= 0 and iy >= 0 and ix < cw and iy < cw and (iy * cw + ix) notin firstG:
|
||||
firstG[iy * cw + ix] = g; inc expN
|
||||
while ai < am.len and am[ai]["gseq"].getInt <= g:
|
||||
let m = am[ai]; inc ai
|
||||
let t = m["t"].getStr
|
||||
if t == "error": fail "A got error " & $m
|
||||
if t notin allowed: fail "A got forbidden type " & t; continue
|
||||
if t == "error": fail "PROTOCOL", "A got error " & $m
|
||||
if t notin allowed: fail (if t in ["walls", "door"]: "WALLDATA" else: "PROTOCOL"), "A got forbidden type " & t; continue
|
||||
if t == "moveAck": continue
|
||||
scan(m, t)
|
||||
if m["gseq"].getInt != g: fail "A msg gseq " & $m["gseq"] & " not in GM log (group " & $g & ")"
|
||||
if t notin ["token", "remove"] and "\"" & other & "\"" in $m: fail other & " mentioned in " & $m
|
||||
if m["gseq"].getInt != g: fail "PROTOCOL", "A msg gseq " & $m["gseq"] & " not in GM log (group " & $g & ")"
|
||||
if t notin ["token", "remove"] and "\"" & other & "\"" in $m: fail "OTHER_PLAYER", other & " mentioned in " & $m
|
||||
case t
|
||||
of "hello": (if m["tokens"] != %[me]: fail "hello tokens " & $m["tokens"])
|
||||
of "hello": (if m["tokens"] != %[me]: fail "OTHER_PLAYER", "hello tokens " & $m["tokens"])
|
||||
of "token":
|
||||
let id = m["id"].getStr
|
||||
if id notin truth: fail "unknown token " & id; continue
|
||||
if vec(m["pos"]) != truth[id]: fail "token " & id & " pos != truth at g=" & $g
|
||||
if m["mine"].getBool != (id == me): fail "mine flag " & id
|
||||
if id != other and m{"by"}.getStr == other: fail other & " leak via by on " & id
|
||||
if id != me and side(poly, truth[id]) == Out: fail "LEAK token " & id & " outside A vision at g=" & $g
|
||||
if id notin truth: fail "HIDDEN_TOKEN", "unknown token " & id; continue
|
||||
if vec(m["pos"]) != truth[id]: fail "OWN_VIEW", "token " & id & " pos != truth at g=" & $g
|
||||
if m["mine"].getBool != (id == me): fail "OTHER_PLAYER", "mine flag " & id
|
||||
if id != other and m{"by"}.getStr == other: fail "OTHER_PLAYER", other & " leak via by on " & id
|
||||
if id != me and side(poly, truth[id]) == Out: leaked.incl id; fail "HIDDEN_TOKEN", "LEAK token " & id & " outside A vision at g=" & $g
|
||||
view[id] = vec(m["pos"]); seen.incl id
|
||||
of "remove":
|
||||
let id = m["id"].getStr
|
||||
if id notin view: fail "remove of unknown " & id
|
||||
if id in truth and side(poly, truth[id]) == In: fail "remove of token still visible " & id
|
||||
if id notin view: fail "PROTOCOL", "remove of unknown " & id
|
||||
if id in truth and side(poly, truth[id]) == In: fail "WITHHELD", "remove of token still visible " & id
|
||||
view.del id; inc removes
|
||||
of "vision":
|
||||
let ps = m["polys"]
|
||||
if ps.len != 1 or ps[0].len != poly.len: fail "vision shape at g=" & $g; continue
|
||||
for i in 0 ..< poly.len:
|
||||
let p = ps[0][i]
|
||||
if abs(p[0].getFloat - poly[i].x) > 1e-6 or abs(p[1].getFloat - poly[i].y) > 1e-6: fail "vision vertex differs at g=" & $g; break
|
||||
for q in ps:
|
||||
if same(q, polyO): fail "OTHER_PLAYER", "vision carries " & other & "'s polygon at g=" & $g
|
||||
if ps.len != 1 or ps[0].len != poly.len: fail "OWN_VIEW", "vision shape at g=" & $g; continue
|
||||
if not same(ps[0], poly): fail "OWN_VIEW", "vision vertex differs at g=" & $g
|
||||
of "explored":
|
||||
for c in m["cells"]:
|
||||
let k = c[1].getInt * cw + c[0].getInt
|
||||
if firstG.getOrDefault(k, int.high) > g: fail "LEAK explored cell not yet explored " & $c
|
||||
if firstG.getOrDefault(k, int.high) > g: fail (if k in oFirst: "OTHER_PLAYER" else: "EXPLORED"), "LEAK explored cell not yet explored " & $c
|
||||
aExp.incl k
|
||||
else: discard
|
||||
if aExp.len != expN: fail "explored count at g=" & $g & ": A " & $aExp.len & " expected " & $expN
|
||||
if aExp.len != expN: fail "EXPLORED", "explored count at g=" & $g & ": A " & $aExp.len & " expected " & $expN
|
||||
for id, p in truth:
|
||||
let s = side(poly, p)
|
||||
if s == Amb and id != me: continue
|
||||
let want = id == me or s == In
|
||||
if want != (id in view): fail "view mismatch " & id & " want=" & $want & " at g=" & $g
|
||||
elif want and view[id] != p: fail "view pos stale " & id & " at g=" & $g
|
||||
if want != (id in view): fail (if want: "WITHHELD" elif id in leaked: "HIDDEN_TOKEN" else: "NO_REMOVE"), "view mismatch " & id & " want=" & $want & " at g=" & $g
|
||||
elif want and view[id] != p: fail "OWN_VIEW", "view pos stale " & id & " at g=" & $g
|
||||
|
||||
var curG = -1
|
||||
for m in gm:
|
||||
@@ -117,7 +130,7 @@ for m in gm:
|
||||
of "token": truth[m["id"].getStr] = vec(m["pos"])
|
||||
else: discard
|
||||
finalize(curG)
|
||||
if ai < am.len: fail "A messages after last GM gseq: " & $am[ai]
|
||||
if ai < am.len: fail "PROTOCOL", "A messages after last GM gseq: " & $am[ai]
|
||||
|
||||
var n200, n403, nOther: int
|
||||
for t in tiles:
|
||||
@@ -129,12 +142,13 @@ for t in tiles:
|
||||
if firstG.getOrDefault(cy * cw + cx, int.high) <= g: exp = true
|
||||
let s = t["s"].getInt
|
||||
if s == 200: inc n200 elif s == 403: inc n403 else: inc nOther
|
||||
if s != (if exp: 200 else: 403): fail "tile " & $t["x"] & "_" & $t["y"] & " g=" & $g & " (" & t["label"].getStr & ") status " & $s & " expected " & (if exp: "200" else: "403")
|
||||
if s != (if exp: 200 else: 403): fail "TILE", "tile " & $t["x"] & "_" & $t["y"] & " g=" & $g & " (" & t["label"].getStr & ") status " & $s & " expected " & (if exp: "200" else: "403")
|
||||
|
||||
# coverage: the scripted path must actually exercise enter/leave
|
||||
for id in (if me == "A": @["M1", "M2", "B", "M5"] else: @["A", "M5"]):
|
||||
if id notin seen: fail "scenario never showed A token " & id
|
||||
if removes < (if me == "A": 3 else: 1): fail "only " & $removes & " removes"
|
||||
echo "leakcheck: ", groups, " gseq groups, A msgs ", am.len, ", tokens seen ", seen.len - 1, " others, removes ", removes,
|
||||
if id notin seen: fail "WITHHELD", "scenario never showed subject token " & id
|
||||
if removes < (if me == "A": 3 else: 1): fail "NO_REMOVE", "only " & $removes & " removes"
|
||||
for c, n in classes: echo "FAILCLASS ", c, " ", n
|
||||
echo "leakcheck: ", groups, " gseq groups, subject msgs ", am.len, ", tokens seen ", seen.len - 1, " others, removes ", removes,
|
||||
", tiles ", tiles.len, " (200:", n200, " 403:", n403, " other:", nOther, "), explored cells ", expN, ", failures ", fails
|
||||
quit(if fails > 0: 1 else: 0)
|
||||
|
||||
@@ -6,10 +6,12 @@ R=$RUN_DIR
|
||||
case "$PORT:$BACKEND_PORT" in 8088:*|*:9000|*:8088) echo "withserver: refusing live port (8088/9000)" >&2; exit 1;; esac
|
||||
mkdir -p $R/temp client/dist
|
||||
rm -f $R/state.db* $R/invites.txt
|
||||
nim c -d:release --hints:off --nimblePath:"${NIMBLE_DIR:-.devbox/nimble}/pkgs2" -o:$R/server src/server.nim >/dev/null || exit 1
|
||||
nim c -d:release $SERVER_FLAGS --nimcache:$R/nimcache$(echo $SERVER_FLAGS | tr -dc a-z) --hints:off --nimblePath:"${NIMBLE_DIR:-.devbox/nimble}/pkgs2" -o:$R/server src/server.nim >/dev/null || exit 1
|
||||
sed -e "s/NGX_PORT/$PORT/" -e "s/NGX_BACKEND/$BACKEND_PORT/" -e "s#run/#$R/#g" -e "s#\.\./nginx/mime#$PWD/nginx/mime#" nginx/nginx.conf > $R/nginx.conf
|
||||
if [ -z "$NOSERVE" ]; then # NOSERVE=1: build only, the command starts/stops the server itself (tools/restart.mjs)
|
||||
./$R/server > $R/server.log 2>&1 & S=$!
|
||||
nginx -p "$PWD/" -c $R/nginx.conf -e $R/error.log > $R/nginx.out 2>&1 & N=$!
|
||||
trap 'kill -INT $S; sleep 1; kill $N 2>/dev/null' EXIT
|
||||
for i in $(seq 50); do [ -s $R/invites.txt ] && curl -s -o /dev/null "localhost:$PORT/" && break; sleep 0.2; done
|
||||
fi
|
||||
"$@"
|
||||
|
||||
Reference in New Issue
Block a user