One shared validator next to the property caps: 8 KB serialized, 64 keys per object, depth 8 (top-level object = 1). At a cap is accepted; over is invalid and applies nothing. Both call sites use it, and a refusal is logged once with the field and limit, never the value. Token flags and props keep their existing rules; stored over-cap rows still load as stored.
180 lines
7.6 KiB
Nim
180 lines
7.6 KiB
Nim
## Property buckets, caps and the schema registry (#22, DESIGN 6.1 prop payload rules, 8, 9). Pure
|
|
## unit tests: no DB, no socket. The wire behaviour (the ack codes, the bucket filtering a player
|
|
## sees) lives in tools/details.test.mjs and tests/tviews.nim.
|
|
import std/[algorithm, json, options, sequtils, strutils, tables, unittest]
|
|
import props
|
|
|
|
proc bucketed(): Props =
|
|
result = newProps()
|
|
result[pbPublic] = %*{"aura": "cold"}
|
|
result[pbOwner] = %*{"hp": 7}
|
|
result[pbGm] = %*{"secret": "yes"}
|
|
|
|
suite "caps (6.1, numbers FIXED owner-delegated by #22)":
|
|
test "64 keys per bucket, the 65th is refused and nothing changes":
|
|
var p = newProps()
|
|
for i in 0 ..< MaxPropKeys:
|
|
check p.setProp(pbPublic, "k" & $i, %i)
|
|
check p[pbPublic].len == MaxPropKeys
|
|
check not p.setProp(pbPublic, "one-too-many", %1)
|
|
check p[pbPublic].len == MaxPropKeys
|
|
check not p.contains(pbPublic, "one-too-many")
|
|
# a bucket has its own budget: the owner bucket is still empty
|
|
check p.setProp(pbOwner, "k0", %1)
|
|
|
|
test "an existing key can still be overwritten at the cap":
|
|
var p = newProps()
|
|
for i in 0 ..< MaxPropKeys: check p.setProp(pbPublic, "k" & $i, %i)
|
|
check p.setProp(pbPublic, "k0", %"replaced")
|
|
check p[pbPublic]["k0"].getStr == "replaced"
|
|
|
|
test "a key is 1..64 code points, trimmed, with no control characters":
|
|
check validPropKey("hp")
|
|
check validPropKey(repeat("x", MaxPropKeyLen))
|
|
check validPropKey("café") # code points, not bytes
|
|
check not validPropKey("")
|
|
check not validPropKey(" padded")
|
|
check not validPropKey("padded ")
|
|
check not validPropKey(repeat("x", MaxPropKeyLen + 1))
|
|
check not validPropKey("tab\there")
|
|
|
|
test "a value is at most 8 KB of serialized JSON":
|
|
var p = newProps()
|
|
let small = %repeat("x", 8000) # ~8 KB with the quotes
|
|
check valueBytes(small) <= MaxPropValueBytes
|
|
check p.setProp(pbPublic, "small", small)
|
|
let big = %repeat("x", MaxPropValueBytes + 1)
|
|
check valueBytes(big) > MaxPropValueBytes
|
|
|
|
suite "opaque object caps (6.1, FIXED owner-delegated by #35)":
|
|
proc objectWith(keys: int): JsonNode =
|
|
result = newJObject()
|
|
for i in 0 ..< keys: result["k" & $i] = %i
|
|
|
|
proc nestedObjects(depth: int): JsonNode =
|
|
result = newJObject()
|
|
var cur = result
|
|
for _ in 1 ..< depth:
|
|
let child = newJObject()
|
|
cur["a"] = child
|
|
cur = child
|
|
|
|
test "size: exactly 8 KB is accepted, one byte over is refused":
|
|
let atCap = %*{"blob": repeat("x", MaxOpaqueObjectBytes - 11)}
|
|
check ($atCap).len == MaxOpaqueObjectBytes
|
|
check checkOpaqueObject(atCap) == olOk
|
|
let over = %*{"blob": repeat("x", MaxOpaqueObjectBytes - 10)}
|
|
check ($over).len == MaxOpaqueObjectBytes + 1
|
|
check checkOpaqueObject(over) == olSize
|
|
|
|
test "keys: 64 per object is accepted, 65 is refused, and it is per object":
|
|
check checkOpaqueObject(objectWith(MaxOpaqueObjectKeys)) == olOk
|
|
check checkOpaqueObject(objectWith(MaxOpaqueObjectKeys + 1)) == olKeys
|
|
# a nested object has its own budget, not a shared one
|
|
var nested = objectWith(MaxOpaqueObjectKeys - 1)
|
|
nested["inner"] = objectWith(MaxOpaqueObjectKeys)
|
|
check checkOpaqueObject(nested) == olOk
|
|
nested["inner"] = objectWith(MaxOpaqueObjectKeys + 1)
|
|
check checkOpaqueObject(nested) == olKeys
|
|
|
|
test "depth: 8 containers is accepted, 9 is refused":
|
|
check checkOpaqueObject(nestedObjects(MaxOpaqueObjectDepth)) == olOk
|
|
check checkOpaqueObject(nestedObjects(MaxOpaqueObjectDepth + 1)) == olDepth
|
|
# arrays are containers too
|
|
var deep = newJArray()
|
|
let root = newJObject()
|
|
root["a"] = deep
|
|
for _ in 1 ..< MaxOpaqueObjectDepth - 1:
|
|
let next = newJArray()
|
|
deep.add next
|
|
deep = next
|
|
check checkOpaqueObject(root) == olOk
|
|
|
|
suite "parseProps (the `props` field of token.create)":
|
|
test "absent and null mean three empty buckets":
|
|
for node in [JsonNode(nil), newJNull()]:
|
|
let parsed = parseProps(defaultRegistry(), node)
|
|
check parsed.isSome
|
|
for b in PropBucket: check parsed.get[b].kind == JObject and parsed.get[b].len == 0
|
|
|
|
test "the three bucket names are the only ones accepted":
|
|
let reg = defaultRegistry()
|
|
check parseProps(reg, %*{"public": {"a": 1}}).isSome
|
|
check parseProps(reg, %*{"public": {}, "owner": {}, "gm": {}}).isSome
|
|
check parseProps(reg, %*{"secret": {}}).isNone
|
|
check parseProps(reg, %*{"public": 3}).isNone
|
|
check parseProps(reg, %*{"public": {"k": newJNull()}}).isNone
|
|
check parseProps(reg, %*[1, 2]).isNone
|
|
check parseProps(reg, %*{"public": {"": 1}}).isNone
|
|
|
|
test "the caps are enforced while parsing":
|
|
let many = newJObject()
|
|
for i in 0 ..< MaxPropKeys + 1: many["k" & $i] = %i
|
|
check parseProps(defaultRegistry(), %*{"public": many}).isNone
|
|
let tooBig = newJObject()
|
|
tooBig["k"] = %repeat("x", MaxPropValueBytes + 1)
|
|
check parseProps(defaultRegistry(), %*{"public": tooBig}).isNone
|
|
|
|
suite "the registry (DESIGN 8/9: v1 is empty and permissive)":
|
|
test "with no entries every key is accepted in every bucket":
|
|
let reg = defaultRegistry()
|
|
for b in PropBucket:
|
|
check reg.checkWrite(b, "anything", %*{"nested": [1, 2, 3]})
|
|
check reg.checkWrite(b, "anything", %"text")
|
|
check reg.checkWrite(b, "anything", %12.5)
|
|
check reg.checkWrite(b, "anything", %true)
|
|
|
|
test "a registered key is enforced: kind and bucket":
|
|
var reg = defaultRegistry()
|
|
reg.schemas["hp"] = PropSchema(kind: pkNumber, bucket: some(pbOwner))
|
|
check reg.checkWrite(pbOwner, "hp", %7)
|
|
check not reg.checkWrite(pbOwner, "hp", %"seven") # wrong kind
|
|
check not reg.checkWrite(pbPublic, "hp", %7) # wrong bucket
|
|
check reg.checkWrite(pbPublic, "other", %"anything") # unregistered keys still use the fallback
|
|
check parseProps(reg, %*{"owner": {"hp": 3}}).isSome
|
|
check parseProps(reg, %*{"owner": {"hp": "three"}}).isNone
|
|
check parseProps(reg, %*{"public": {"hp": 3}}).isNone
|
|
|
|
test "a schema with no bucket accepts the key anywhere":
|
|
var reg = defaultRegistry()
|
|
reg.schemas["note"] = PropSchema(kind: pkString, bucket: none(PropBucket))
|
|
for b in PropBucket: check reg.checkWrite(b, "note", %"hi")
|
|
for b in PropBucket: check not reg.checkWrite(b, "note", %1)
|
|
|
|
suite "set / unset":
|
|
test "unsetting an absent key is allowed and reports it did nothing":
|
|
var p = bucketed()
|
|
check p.unsetProp(pbPublic, "aura")
|
|
check not p.contains(pbPublic, "aura")
|
|
check not p.unsetProp(pbPublic, "aura")
|
|
|
|
test "buckets are independent: the same key may live in all three":
|
|
var p = newProps()
|
|
check p.setProp(pbPublic, "hp", %1)
|
|
check p.setProp(pbOwner, "hp", %2)
|
|
check p.setProp(pbGm, "hp", %3)
|
|
check p[pbPublic]["hp"].getInt == 1
|
|
check p[pbOwner]["hp"].getInt == 2
|
|
check p[pbGm]["hp"].getInt == 3
|
|
|
|
test "propsJson carries all three buckets, always as objects":
|
|
let node = propsJson(bucketed())
|
|
check node.kind == JObject
|
|
check node.keys.toSeq.sorted == @["gm", "owner", "public"]
|
|
check node["public"]["aura"].getStr == "cold"
|
|
check node["gm"]["secret"].getStr == "yes"
|
|
|
|
test "clone gives fresh buckets: editing the copy cannot reach the original":
|
|
var original = bucketed()
|
|
var copy = original.clone()
|
|
check copy.setProp(pbPublic, "new", %1)
|
|
check copy.unsetProp(pbPublic, "aura")
|
|
check not original.contains(pbPublic, "new")
|
|
check original.contains(pbPublic, "aura")
|
|
check copy[pbOwner]["hp"].getInt == 7 # untouched values are shared, not lost
|
|
|
|
test "normalize repairs nil buckets":
|
|
var p: Props
|
|
let fixed = normalize(p)
|
|
for b in PropBucket: check fixed[b].kind == JObject
|