Conn now carries what DESIGN 6/6.4/6.5 need per connection, all under the one registry lock: the outgoing n counter (starts at 1, server frames only), the intent token bucket, the malformed-flood window, the player's named flag and the last Pong. None of it is shared, so the n counter is not a global side channel (H25). activate/revokePlayer now hand back the doomed Conn values instead of raw sockets: the caller still has the frame count, so the final closing frame carries the right n before the close. Tests cover per-connection counters, the bucket and the flood window in isolation, and that a snapshot survives the lock.
176 lines
6.6 KiB
Nim
176 lines
6.6 KiB
Nim
## Connection caps (#17, DESIGN 4.3/6.5): 4 per player (5th evicts the oldest), 16 total (17th refused).
|
|
## Per-connection wire state (#18, DESIGN 6/6.4/6.5 row 8): the `n` counter, the intent bucket and
|
|
## the malformed-flood window are per connection and none of them is global.
|
|
import std/[unittest, options]
|
|
import mummy
|
|
import conns, auth, protocol
|
|
|
|
proc dummyWs(): WebSocket =
|
|
# Mummy's fields are private; an all-zero value is enough for registry bookkeeping tests.
|
|
var ws: WebSocket
|
|
ws
|
|
|
|
suite "total cap":
|
|
test "16 reservations succeed, the 17th is refused":
|
|
let c = newConns()
|
|
for _ in 0 ..< MaxConnsTotal: check c.reserve()
|
|
check c.pendingCount == 16
|
|
check not c.reserve()
|
|
check c.pendingCount == 16
|
|
|
|
test "a cancelled reservation gives the slot back":
|
|
let c = newConns()
|
|
for _ in 0 ..< MaxConnsTotal: check c.reserve()
|
|
check not c.reserve()
|
|
c.cancelReserve()
|
|
check c.pendingCount == 15
|
|
check c.reserve()
|
|
check not c.reserve()
|
|
|
|
test "activated connections keep the total cap; removing one frees a slot":
|
|
let c = newConns()
|
|
for i in 0 ..< MaxConnsTotal:
|
|
check c.reserve()
|
|
discard c.activate(i mod 4, prPlayer, dummyWs())
|
|
check c.activeCount == 16
|
|
check c.pendingCount == 0
|
|
check not c.reserve()
|
|
c.remove(1)
|
|
check c.activeCount == 15
|
|
check c.reserve()
|
|
|
|
suite "per-player cap":
|
|
test "the 5th connection of one player evicts that player's oldest":
|
|
let c = newConns()
|
|
var ids: seq[int]
|
|
for _ in 0 ..< MaxConnsPerPlayer:
|
|
check c.reserve()
|
|
let a = c.activate(7, prPlayer, dummyWs())
|
|
ids.add a.id
|
|
check a.evictedId.isNone
|
|
check c.activeCountFor(7) == 4
|
|
check c.reserve()
|
|
let fifth = c.activate(7, prPlayer, dummyWs())
|
|
check fifth.evictedId == some(ids[0])
|
|
check fifth.evicted.get.playerId == 7
|
|
check fifth.evicted.get.frameNo == 1 # a fresh connection: its closing frame is n = 1
|
|
check c.activeCountFor(7) == 4
|
|
check c.connById(ids[0]).isNone
|
|
check c.connById(fifth.id).isSome
|
|
# the next one evicts the second-oldest, not a new victim at random
|
|
check c.reserve()
|
|
let sixth = c.activate(7, prPlayer, dummyWs())
|
|
check sixth.evictedId == some(ids[1])
|
|
|
|
test "another player's connections are never evicted":
|
|
let c = newConns()
|
|
for _ in 0 ..< MaxConnsPerPlayer:
|
|
check c.reserve()
|
|
discard c.activate(1, prPlayer, dummyWs())
|
|
let other = c.activate(2, prPlayer, dummyWs()) # player 2 has none: no eviction
|
|
check other.evictedId.isNone
|
|
# player 1 is at the cap: its next one evicts player 1's oldest, player 2's stays
|
|
discard c.activate(1, prPlayer, dummyWs())
|
|
check c.activeCountFor(1) == 4
|
|
check c.activeCountFor(2) == 1
|
|
|
|
test "the GM is capped like anyone else (caps are per player, not per role)":
|
|
let c = newConns()
|
|
var gmIds: seq[int]
|
|
for _ in 0 ..< MaxConnsPerPlayer:
|
|
discard c.activate(1, prGm, dummyWs())
|
|
for id in 1 .. MaxConnsPerPlayer: gmIds.add id
|
|
let fifth = c.activate(1, prGm, dummyWs())
|
|
check fifth.evictedId == some(gmIds[0])
|
|
|
|
suite "revocation":
|
|
test "revokePlayer returns every live connection of that player and nothing else":
|
|
let c = newConns()
|
|
discard c.activate(1, prPlayer, dummyWs())
|
|
discard c.activate(2, prPlayer, dummyWs())
|
|
discard c.activate(1, prPlayer, dummyWs())
|
|
let revoked = c.revokePlayer(1)
|
|
check revoked.len == 2
|
|
check revoked[0].playerId == 1
|
|
for conn in revoked: check c.connById(conn.id).isNone
|
|
check c.activeCountFor(1) == 0
|
|
check c.activeCountFor(2) == 1
|
|
check c.activeCount == 1
|
|
check c.revokePlayer(1).len == 0
|
|
|
|
test "remove works by conn id; an unknown id is a no-op":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
c.remove(a.id)
|
|
c.remove(a.id)
|
|
check c.activeCount == 0
|
|
check c.connById(a.id).isNone
|
|
|
|
test "connForWs finds the registered socket (mummy hands the same value back)":
|
|
let c = newConns()
|
|
var ws: WebSocket
|
|
let a = c.activate(1, prPlayer, ws)
|
|
check c.connForWs(ws).get.id == a.id
|
|
var other: WebSocket # another all-zero value still equals the first one structurally
|
|
check c.connForWs(other).isSome
|
|
|
|
suite "per-connection wire state (#18)":
|
|
test "the frame counter starts at 1 on each connection and is never global":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
let b = c.activate(1, prPlayer, dummyWs())
|
|
check c.nextFrameNo(a.id) == 1
|
|
check c.nextFrameNo(a.id) == 2
|
|
check c.nextFrameNo(b.id) == 1 # per connection, not a server-wide sequence (H25)
|
|
check c.nextFrameNo(a.id) == 3
|
|
check c.connById(b.id).get.frames == 1
|
|
|
|
test "a removed connection has no next frame number; the caller sends closing with frameNo":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
discard c.nextFrameNo(a.id)
|
|
discard c.nextFrameNo(a.id)
|
|
let doomed = c.connById(a.id).get
|
|
check doomed.frameNo == 3
|
|
c.remove(a.id)
|
|
check c.nextFrameNo(a.id) == 0 # gone: drop the frame
|
|
check doomed.frameNo == 3 # the snapshot still says which number closes it
|
|
|
|
test "the intent bucket is per connection: draining one leaves the other full":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
let b = c.activate(2, prPlayer, dummyWs())
|
|
for _ in 0 ..< RateBurst: check c.takeIntentToken(a.id, 0)
|
|
check not c.takeIntentToken(a.id, 0)
|
|
check c.takeIntentToken(b.id, 0)
|
|
check not c.takeIntentToken(999, 0) # unknown connection: nothing to spend it on
|
|
|
|
test "the malformed window is per connection and fires on the tenth frame":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
let b = c.activate(2, prPlayer, dummyWs())
|
|
for i in 0 ..< MalformedFloodLimit - 1:
|
|
check not c.recordMalformed(a.id, int64(i))
|
|
check c.recordMalformed(a.id, int64(MalformedFloodLimit - 1))
|
|
check not c.recordMalformed(b.id, 0)
|
|
|
|
test "a Pong is recorded on the right connection":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
let b = c.activate(2, prPlayer, dummyWs())
|
|
c.notePong(a.id, 5, 4242)
|
|
check c.connById(a.id).get.pongSeq == 5
|
|
check c.connById(a.id).get.lastPongMs == 4242
|
|
check c.connById(b.id).get.pongSeq == 0
|
|
|
|
test "a snapshot hands out every live connection without holding the lock afterwards":
|
|
let c = newConns()
|
|
let a = c.activate(1, prPlayer, dummyWs())
|
|
discard c.activate(2, prGm, dummyWs())
|
|
let snap = c.snapshot()
|
|
check snap.len == 2
|
|
check snap[0].id == a.id
|
|
c.remove(a.id)
|
|
check snap.len == 2
|
|
check c.activeCount == 1
|