Shared change: codecs/compress/Deflate gains Deflate.peek(buf, off, len, n) (the first n inflated bytes of a gzip / zlib / raw stream, for sniffing gzip-wrapped NBT) through a backward-compatible prefix flag on Inflate.raw; gunzip's header parsing moved into a local helper both use. Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
2.5 KiB
Luau
61 lines
2.5 KiB
Luau
-- data/nbt tests: the contract, nbtlib files through the api (F.load / F.save, NBT -> SNBT -> NBT), detection by
|
|
-- content and extension, exact round trips (root name, edition, compression and the Bedrock header kept), error
|
|
-- codes, and the fuzz pass. The engine itself: tests/codecs/nbt.luau; SNBT files: tests/data/snbt.luau.
|
|
-- Fixtures: tools/fixtures/nbt.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local NbtF = require("@formats/data/nbt")
|
|
local SnbtF = require("@formats/data/snbt")
|
|
local Nbt = require("@formats/codecs/text/Nbt")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/nbt")
|
|
|
|
local t = T.suite("data/nbt")
|
|
|
|
t:eq(NbtF.name, "nbt", "name")
|
|
t:eq(SnbtF.name, "snbt", "snbt name")
|
|
t:check(NbtF.caps.decode and NbtF.caps.encode and SnbtF.caps.decode and SnbtF.caps.encode, "caps")
|
|
|
|
for i = 1, 12 do
|
|
local c = Fx.cases[i]
|
|
local v = F.load(c.gzip, { name = "level.dat" })
|
|
t:check(v ~= nil, c.name .. ": F.load gzip")
|
|
local text = assert(F.saveString(v, { format = "snbt" }))
|
|
local back = assert(F.load(text, { format = "snbt" }))
|
|
t:eq(Nbt.print(back), Nbt.print(v), c.name .. ": NBT -> SNBT -> value")
|
|
local again = assert(F.save(v, nil, { format = "nbt" })) :: buffer
|
|
t:eq(buffer.readu8(again, 0), 0x1f, c.name .. ": saved gzip like the source")
|
|
t:eq(Nbt.print(F.load(again, { format = "nbt" })), Nbt.print(v), c.name .. ": saved file reads back")
|
|
t:eq(Nbt.info(F.load(again, { format = "nbt" })).name, c.root, c.name .. ": root name kept")
|
|
local raw = assert(F.save(v, nil, { format = "nbt", compression = "none" })) :: buffer
|
|
t:eq(buffer.tostring(raw), c.raw, c.name .. ": uncompressed = nbtlib's bytes")
|
|
local le = assert(F.load(c.header, { format = "nbt" }))
|
|
t:eq(buffer.tostring(assert(F.save(le, nil, { format = "nbt" })) :: buffer), c.header, c.name .. ": Bedrock level.dat same bytes")
|
|
end
|
|
|
|
-- detection
|
|
do
|
|
local c = Fx.cases[3]
|
|
t:eq(F.detect(c.gzip, { name = "level.dat" }), "nbt", "detect gzip .dat")
|
|
t:eq(F.detect(c.raw, { name = "x.nbt" }), "nbt", "detect raw .nbt")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local _, e = NbtF.decode(Bytes.reader(buffer.fromstring("\10\0\0\1")))
|
|
t:check(e and e.code == Err.TRUNCATED, "truncated")
|
|
local _, e3 = NbtF.encode(nil :: any)
|
|
t:check(e3 and e3.code == Err.ARG, "encode nil")
|
|
end
|
|
|
|
local samples = {}
|
|
for i = 1, 6 do
|
|
table.insert(samples, Fx.cases[i].raw)
|
|
table.insert(samples, Fx.cases[i].header)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return NbtF.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:done()
|