merge master into ws/arch2
This commit is contained in:
commit
0ee0859298
64 files changed
+13113
-30
No files matched your search
@@ -169,4 +169,23 @@ do
|
||||
end, samples, { mutations = 3, garbage = 300 })
|
||||
end
|
||||
|
||||
-- peek: the first bytes of a gzip / zlib / raw stream without inflating the rest
|
||||
do
|
||||
local src = buffer.create(100000)
|
||||
for i = 0, 99999 do
|
||||
buffer.writeu8(src, i, (i * 7 + (i // 97)) % 251)
|
||||
end
|
||||
local want = buffer.readstring(src, 0, 1000)
|
||||
for _, kind in { "gzip", "zlibCompress", "compress" } do
|
||||
local z = assert((Deflate :: any)[kind](src)) :: buffer
|
||||
local head = assert(Deflate.peek(z, nil, nil, 1000))
|
||||
t:check(buffer.len(head) <= 1000 and buffer.len(head) >= 1000 - 258, "peek " .. kind .. " size")
|
||||
t:eq(buffer.tostring(head), string.sub(want, 1, buffer.len(head)), "peek " .. kind .. " bytes")
|
||||
end
|
||||
local small = assert(Deflate.peek(assert(Deflate.gzip(buffer.fromstring("hello"))), nil, nil, 1000))
|
||||
t:eq(buffer.tostring(small), "hello", "peek a short stream")
|
||||
local _, e = Deflate.peek(buffer.fromstring("\31\139\8\0\0\0\0\0\0\3\255\255\255\255\255\255\255\255"), nil, nil, 10)
|
||||
t:check(e ~= nil, "peek bad data")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,155 @@
|
||||
-- codecs/model/Minecraft tests: packed index arrays (spanning and not, 1..16 bits, against a bit-by-bit reference),
|
||||
-- Sponge varints, nibble arrays, YZX reordering, block-state strings and their NBT form, every legacy id / data pair
|
||||
-- through legacyState / legacyId, colours (air, families, fallbacks), voxels / blocksOf and nearest-colour blocks.
|
||||
-- The formats using it: tests/model/schematic.luau, tests/model/mca.luau.
|
||||
local T = require("@tests/T")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local t = T.suite("minecraft")
|
||||
local P = Mc.Packed
|
||||
local rand = T.rng(3)
|
||||
|
||||
-- 1. packed arrays against a reference that walks the bits one at a time
|
||||
local function refBit(longs: buffer, p: number): number
|
||||
return bit32.extract(buffer.readu8(longs, p // 8), p % 8, 1)
|
||||
end
|
||||
for bits = 1, 16 do
|
||||
for _, spanning in { true, false } do
|
||||
local count = 100 + rand(300)
|
||||
local max = math.min(2 ^ bits, 70000)
|
||||
local idx = buffer.create(count * 2)
|
||||
for i = 0, count - 1 do
|
||||
buffer.writeu16(idx, i * 2, rand(max))
|
||||
end
|
||||
local longs = P.pack(idx, count, bits, spanning)
|
||||
t:eq(buffer.len(longs), P.longsNeeded(count, bits, spanning) * 8, bits .. " bits: size")
|
||||
local per = 64 // bits
|
||||
local bad = 0
|
||||
for i = 0, count - 1 do
|
||||
local p = if spanning then i * bits else (i // per) * 64 + (i % per) * bits
|
||||
local v = 0
|
||||
for k = 0, bits - 1 do
|
||||
v += refBit(longs, p + k) * 2 ^ k
|
||||
end
|
||||
if v ~= buffer.readu16(idx, i * 2) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, bits .. " bits " .. tostring(spanning) .. ": layout")
|
||||
local back = buffer.create(count * 2)
|
||||
local ok, e = P.unpack(longs, bits, count, spanning, back, max)
|
||||
t:check(ok, tostring(e))
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), bits .. " bits " .. tostring(spanning) .. ": round trip")
|
||||
end
|
||||
end
|
||||
do
|
||||
local out = buffer.create(40)
|
||||
t:check(not P.unpack(buffer.create(8), 4, 20, false, out, 16), "too few longs")
|
||||
local l = P.pack(buffer.fromstring("\5\0"), 1, 4, false)
|
||||
t:check(not P.unpack(l, 4, 1, false, out, 3), "index past the palette")
|
||||
t:eq(P.bitsFor(17, 4), 5, "bitsFor")
|
||||
t:eq(P.bitsFor(1, 2), 2, "bitsFor minimum")
|
||||
end
|
||||
|
||||
-- 2. varints, nibbles, YZX
|
||||
do
|
||||
local idx = buffer.create(400)
|
||||
for i = 0, 199 do
|
||||
buffer.writeu16(idx, i * 2, (i * 997) % 20000)
|
||||
end
|
||||
local v = P.toVarints(idx, 200)
|
||||
local back = buffer.create(400)
|
||||
t:check(P.varints(v, 0, buffer.len(v), 200, back, 20000), "varints")
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), "varints round trip")
|
||||
t:check(not P.varints(v, 0, buffer.len(v) - 1, 200, back, 20000), "varints truncated")
|
||||
local nib = buffer.create(2)
|
||||
P.setNibble(nib, 0, 0, 0xA, true)
|
||||
P.setNibble(nib, 0, 1, 0x3, true)
|
||||
P.setNibble(nib, 0, 2, 0x7, false)
|
||||
t:eq(buffer.readu8(nib, 0), 0xA3, "high nibble first")
|
||||
t:eq(buffer.readu8(nib, 1), 0x07, "low nibble first")
|
||||
t:eq(P.nibble(nib, 0, 1, true), 3, "read nibble")
|
||||
local sx, sy, sz = 3, 4, 5
|
||||
local yzx = buffer.create(sx * sy * sz * 2)
|
||||
for y = 0, sy - 1 do
|
||||
for z = 0, sz - 1 do
|
||||
for x = 0, sx - 1 do
|
||||
buffer.writeu16(yzx, (x + z * sx + y * sx * sz) * 2, x * 100 + y * 10 + z)
|
||||
end
|
||||
end
|
||||
end
|
||||
local vox = P.fromYzx(yzx, sx, sy, sz)
|
||||
t:eq(buffer.readu16(vox, (2 + sx * (3 + sy * 4)) * 2), 234, "fromYzx")
|
||||
t:eq(buffer.tostring(P.toYzx(vox, sx, sy, sz)), buffer.tostring(yzx), "toYzx")
|
||||
end
|
||||
|
||||
-- 3. states
|
||||
do
|
||||
local name, props = Mc.parseState("oak_log[axis=y, foo = bar]")
|
||||
t:eq(name, "minecraft:oak_log", "namespace added")
|
||||
t:eq(#props, 2, "props")
|
||||
t:eq(Mc.stateString(name, props), "minecraft:oak_log[axis=y,foo=bar]", "stateString")
|
||||
local c = Mc.stateToNbt("minecraft:chest[facing=north,type=single]")
|
||||
t:eq(table.concat(Types.keys(c.Properties), ","), "facing,type", "NBT property order")
|
||||
t:eq(Mc.stateFromNbt(c), "minecraft:chest[facing=north,type=single]", "stateFromNbt")
|
||||
t:eq(Mc.baseName("minecraft:stone"), "stone", "baseName")
|
||||
t:check(Mc.isAir("minecraft:cave_air") and not Mc.isAir("minecraft:stone"), "isAir")
|
||||
end
|
||||
|
||||
-- 4. legacy ids: every id / data pair reads back exactly
|
||||
do
|
||||
local bad = 0
|
||||
for id = 0, 255 do
|
||||
for d = 0, 15 do
|
||||
local s = Mc.legacyState(id, d)
|
||||
local i2, d2 = Mc.legacyId(s)
|
||||
if i2 ~= id or d2 ~= d then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "legacy round trip")
|
||||
t:eq(Mc.legacyState(35, 14), "minecraft:red_wool", "wool 14")
|
||||
t:eq(Mc.legacyState(5, 2), "minecraft:birch_planks", "planks 2")
|
||||
t:eq(Mc.legacyState(17, 5), "minecraft:spruce_log[data=5]", "log axis bits kept")
|
||||
t:eq(select(1, Mc.legacyId("minecraft:white_concrete")), 251, "modern name -> legacy id")
|
||||
end
|
||||
|
||||
-- 5. colours
|
||||
do
|
||||
t:eq(Mc.color("minecraft:air"), nil, "air")
|
||||
local function hex(s: string): string
|
||||
local c = Mc.color(s) :: { number }
|
||||
return string.format("%02x%02x%02x%02x", c[1], c[2], c[3], c[4])
|
||||
end
|
||||
t:eq(hex("minecraft:stone"), "7d7d7dff", "exact")
|
||||
t:eq(hex("minecraft:stone_brick_stairs[facing=east]"), hex("minecraft:stone_bricks"), "stairs -> base")
|
||||
t:eq(hex("minecraft:red_concrete"), "8e2020ff", "dye family")
|
||||
t:eq(hex("minecraft:dark_oak_log"), "3e2f17ff", "wood bark")
|
||||
t:eq(string.sub(hex("minecraft:red_stained_glass"), 7, 8), "80", "stained glass translucent")
|
||||
t:eq(hex("minecraft:wool[data=14]"), hex("minecraft:red_wool"), "legacy data colour")
|
||||
t:eq(hex("mod:something_odd"), "909090ff", "fallback")
|
||||
end
|
||||
|
||||
-- 6. voxels / blocksOf
|
||||
do
|
||||
local states = { "minecraft:air", "minecraft:stone", "minecraft:red_wool", "minecraft:stone" }
|
||||
local idx = buffer.create(8 * 2)
|
||||
for i = 0, 7 do
|
||||
buffer.writeu16(idx, i * 2, i % 4)
|
||||
end
|
||||
local v = Mc.voxels(2, 2, 2, states, idx)
|
||||
t:eq(buffer.readu8(v.data, 0), 0, "air -> 0")
|
||||
t:eq(buffer.readu8(v.data, 1), buffer.readu8(v.data, 3), "same colour, same index")
|
||||
t:check(buffer.readu8(v.data, 2) ~= buffer.readu8(v.data, 1), "different colours")
|
||||
local s2, i2 = Mc.blocksOf(v)
|
||||
t:check(s2 == states and i2 == idx, "blocksOf keeps the blocks")
|
||||
local g = Types.Voxels.new(2, 1, 1)
|
||||
g.palette[7] = { 240, 118, 19, 255 }
|
||||
buffer.writeu8(g.data, 1, 7)
|
||||
local s3, i3 = Mc.blocksOf(g)
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 2) + 1], "minecraft:orange_wool", "nearest block by colour")
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 0) + 1], "minecraft:air", "empty -> air")
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,256 @@
|
||||
-- codecs/text/Nbt tests: random trees written by Python's nbtlib (Java big-endian, Bedrock little-endian, gzip, zlib,
|
||||
-- the level.dat header) decode to nbtlib's typed values (a canonical dump), re-encode to nbtlib's exact bytes in both
|
||||
-- byte orders, nbtlib's SNBT (indented and compact) parses to the same values and our SNBT reads back exactly;
|
||||
-- Bedrock network varint NBT; modified UTF-8; long extremes; SNBT grammar details; peek; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Types = require("@formats/Types")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("nbt")
|
||||
|
||||
local function hexOf(b: buffer, o: number, n: number, rev: boolean): string
|
||||
local parts = {}
|
||||
for i = 0, n - 1 do
|
||||
parts[i + 1] = string.format("%02x", buffer.readu8(b, if rev then o + n - 1 - i else o + i))
|
||||
end
|
||||
return table.concat(parts)
|
||||
end
|
||||
|
||||
-- the canonical dump tools/fixtures/nbt.py writes
|
||||
local function canon(v: any): string
|
||||
local ty = Nbt.typeOf(v)
|
||||
local x = if Nbt.isTag(v) then v.value else v
|
||||
if ty == "byte" or ty == "short" or ty == "int" or ty == "long" then
|
||||
local n = if type(x) == "boolean" then (if x then 1 else 0) else x
|
||||
return string.sub(ty :: string, 1, 1) .. (if type(n) == "string" then n else string.format("%d", n))
|
||||
elseif ty == "float" then
|
||||
local b = buffer.create(4)
|
||||
buffer.writef32(b, 0, x)
|
||||
return "f" .. hexOf(b, 0, 4, true)
|
||||
elseif ty == "double" then
|
||||
local b = buffer.create(8)
|
||||
buffer.writef64(b, 0, x)
|
||||
return "d" .. hexOf(b, 0, 8, true)
|
||||
elseif ty == "string" then
|
||||
return "S" .. #x .. ":" .. x
|
||||
elseif ty == "byteArray" then
|
||||
return "B" .. hexOf(x, 0, buffer.len(x), false)
|
||||
elseif ty == "intArray" then
|
||||
local p = {}
|
||||
for i, n in x do
|
||||
p[i] = string.format("%d", n)
|
||||
end
|
||||
return "I[" .. table.concat(p, ",") .. "]"
|
||||
elseif ty == "longArray" then
|
||||
local p = {}
|
||||
for i, n in Nbt.longs(v) do
|
||||
p[i] = if type(n) == "string" then n else string.format("%d", n)
|
||||
end
|
||||
return "L[" .. table.concat(p, ",") .. "]"
|
||||
elseif ty == "list" then
|
||||
local p = {}
|
||||
for i, e in v do
|
||||
p[i] = canon(e)
|
||||
end
|
||||
return "[" .. (Nbt.listType(v) or "?") .. "|" .. table.concat(p, ",") .. "]"
|
||||
end
|
||||
local p = {}
|
||||
for _, k in Types.keys(v) do
|
||||
table.insert(p, #k .. ":" .. k .. "=" .. canon(v[k]))
|
||||
end
|
||||
return "{" .. table.concat(p, ";") .. "}"
|
||||
end
|
||||
|
||||
local function same(a: buffer, b: string): boolean
|
||||
return buffer.tostring(a) == b
|
||||
end
|
||||
|
||||
-- 1. nbtlib trees
|
||||
for _, c in Fx.cases do
|
||||
local v, info = Nbt.decode(c.raw)
|
||||
t:check(v ~= nil, c.name .. ": decode " .. tostring(info))
|
||||
t:eq(canon(v), c.canon, c.name .. ": value")
|
||||
t:eq(info.name, c.root, c.name .. ": root name")
|
||||
t:check(info.endian == "big" and info.compression == "none", c.name .. ": info")
|
||||
local out = Nbt.encode(v) :: buffer
|
||||
t:check(out and same(out, c.raw), c.name .. ": same bytes (big-endian)")
|
||||
local le = Nbt.encode(v, { endian = "little" }) :: buffer
|
||||
t:check(le and same(le, c.little), c.name .. ": same bytes (little-endian)")
|
||||
for _, k in { "little", "gzip", "zlib", "header" } do
|
||||
local w, wi = Nbt.decode(c[k])
|
||||
t:check(w ~= nil, c.name .. ": decode " .. k .. " " .. tostring(wi))
|
||||
t:eq(canon(w), c.canon, c.name .. ": " .. k .. " value")
|
||||
if #c.raw > 4 then -- an empty compound with an empty name reads the same either way
|
||||
t:eq(wi.endian, if k == "little" or k == "header" then "little" else "big", c.name .. ": " .. k .. " endian")
|
||||
end
|
||||
t:eq(wi.compression, if k == "gzip" or k == "zlib" then k else "none", c.name .. ": " .. k .. " compression")
|
||||
-- written back the same way it was read
|
||||
local again = Nbt.encode(w) :: buffer
|
||||
local w2, wi2 = Nbt.decode(again)
|
||||
t:eq(canon(w2), c.canon, c.name .. ": " .. k .. " round trip")
|
||||
t:eq(wi2.compression, wi.compression, c.name .. ": " .. k .. " keeps compression")
|
||||
t:eq(wi2.header, wi.header, c.name .. ": " .. k .. " keeps the header")
|
||||
if k == "header" then
|
||||
t:eq(wi.header, c.version, c.name .. ": header version")
|
||||
t:check(same(again, c.header), c.name .. ": header same bytes")
|
||||
end
|
||||
end
|
||||
-- SNBT: nbtlib's text parses to the same values (an empty list's type is not in the text); ours reads back
|
||||
-- exactly (both forms)
|
||||
local function untyped(s: string): string
|
||||
return (string.gsub(s, "%[%a+|%]", "[|]"))
|
||||
end
|
||||
for _, k in { "snbt", "compact" } do
|
||||
local s, e = Nbt.parse(c[k])
|
||||
t:check(s ~= nil, c.name .. ": parse " .. k .. " " .. tostring(e))
|
||||
t:eq(untyped(canon(s)), untyped(c.canon), c.name .. ": " .. k .. " value")
|
||||
end
|
||||
for _, o in { {}, { indent = 2 }, { quote = "'" } } do
|
||||
local text = Nbt.print(v, o) :: string
|
||||
local back, e = Nbt.parse(text)
|
||||
t:check(back ~= nil, c.name .. ": our SNBT parses " .. tostring(e))
|
||||
t:eq(untyped(canon(back)), untyped(c.canon), c.name .. ": our SNBT round trip")
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. Bedrock network NBT
|
||||
for i, c in Fx.net do
|
||||
local v, info = Nbt.decode(c.data, { varint = true })
|
||||
t:check(v ~= nil, "net " .. i .. ": " .. tostring(info))
|
||||
t:eq(canon(v), c.canon, "net " .. i .. ": value")
|
||||
t:check(same(Nbt.encode(v, { varint = true }) :: buffer, c.data), "net " .. i .. ": same bytes")
|
||||
end
|
||||
|
||||
-- 3. details
|
||||
do
|
||||
-- longs: exact past 2^53, both directions
|
||||
for _, s in { "9223372036854775807", "-9223372036854775808", "9007199254740993", "-9007199254740993", "4294967296", "-1" } do
|
||||
local hi, lo = Nbt.toHiLo(s)
|
||||
t:check(hi ~= nil, "toHiLo " .. s)
|
||||
t:eq(tostring(Nbt.fromHiLo(hi :: number, lo :: number)), s, "long text " .. s)
|
||||
end
|
||||
t:eq(Nbt.toHiLo("9223372036854775808"), nil, "long overflow")
|
||||
t:eq(Nbt.long("12").value, 12, "small long text becomes a number")
|
||||
-- modified UTF-8: NUL as C0 80 and U+1F600 as a surrogate pair in Java files, plain UTF-8 in Bedrock ones
|
||||
local v = Nbt.compound({ s = "a\0b\u{1F600}" }, { "s" })
|
||||
local big = Nbt.write(v) :: buffer
|
||||
t:check(string.find(buffer.tostring(big), "a\192\128b\237\160\189\237\184\128", 1, true) ~= nil, "modified UTF-8 out")
|
||||
t:eq((Nbt.read(big)).s, "a\0b\u{1F600}", "modified UTF-8 in")
|
||||
local little = Nbt.write(v, { endian = "little" }) :: buffer
|
||||
t:check(string.find(buffer.tostring(little), "a\0b\u{1F600}", 1, true) ~= nil, "Bedrock strings are UTF-8")
|
||||
-- lenient inputs: plain numbers, booleans, plain tables
|
||||
local p = Nbt.read(Nbt.write({ a = 1.5, b = true, c = { 1, 2 }, d = {}, e = 2 ^ 40 }) :: buffer)
|
||||
t:check(p.a.type == "double" and p.b.type == "byte" and p.b.value == 1, "plain number / boolean")
|
||||
t:check(Nbt.listType(p.c) == "int" and Nbt.listType(p.d) == "end" and p.e.type == "double", "plain lists")
|
||||
-- mixed lists are wrapped in compounds, as Minecraft 1.21.5 does
|
||||
local m = Nbt.read(Nbt.write({ l = { 1, "x" } }) :: buffer)
|
||||
t:check(Nbt.listType(m.l) == "compound" and m.l[1][""] == 1 and m.l[2][""] == "x", "heterogeneous list")
|
||||
-- empty typed list keeps its type
|
||||
local et = Nbt.read(Nbt.write({ l = Nbt.list("compound") }) :: buffer)
|
||||
t:eq(Nbt.listType(et.l), "compound", "empty typed list")
|
||||
-- tostring of tags is SNBT; == compares type and value
|
||||
t:eq(tostring(Nbt.byte(3)), "3b", "tostring byte")
|
||||
t:eq(tostring(Nbt.float(0.1)), "0.1f", "tostring float (shortest)")
|
||||
t:check(Nbt.short(5) == Nbt.short(5) and Nbt.short(5) ~= Nbt.int(5) :: any, "tag equality")
|
||||
-- get
|
||||
local g = assert(Nbt.parse("{Data:{Pos:[1.0d,2.5d,3.0d],Inv:[{id:\"x\"}]}}"))
|
||||
t:eq(Nbt.get(g, "Data.Pos[1]").value, 2.5, "get list element")
|
||||
t:eq(Nbt.get(g, "Data.Inv[0].id"), "x", "get nested")
|
||||
t:eq(Nbt.get(g, "Data.Missing.x"), nil, "get missing")
|
||||
end
|
||||
|
||||
-- 4. SNBT grammar
|
||||
do
|
||||
local cases: { { string } } = {
|
||||
{ "1b", "byte:1" }, { "-5S", "short:-5" }, { "7", "int:7" }, { "7i", "int:7" }, { "1_000", "int:1000" },
|
||||
{ "123L", "long:123" }, { "0x1F", "int:31" }, { "0xFFub", "byte:-1" }, { "0x7Fsb", "byte:127" },
|
||||
{ "0b101", "int:5" }, { "0b", "byte:0" }, { "200ub", "byte:-56" }, { "1.5", "double:1.5" }, { ".5f", "float:0.5" },
|
||||
{ "2e3", "double:2000" }, { "3f", "float:3" }, { "4D", "double:4" }, { "true", "byte:1" }, { "false", "byte:0" },
|
||||
{ "abc", "string:abc" }, { "1.2.3", "string:1.2.3" }, { "'it''", "" }, { "'a\\'b'", "string:a'b" },
|
||||
{ '"\\u00e9\\x41\\n"', "string:\u{e9}A\n" }, { "-Infinityd", "double:-inf" },
|
||||
}
|
||||
for _, c in cases do
|
||||
local v = Nbt.parse(c[1])
|
||||
if c[2] == "" then
|
||||
t:eq(v, nil, "snbt " .. c[1] .. " fails")
|
||||
else
|
||||
local ty = Nbt.typeOf(v) :: string
|
||||
local x = Nbt.number(v)
|
||||
local got = ty .. ":" .. (if ty == "string" then v elseif x == -math.huge then "-inf" else string.format("%.17g", x :: number))
|
||||
t:eq(got, c[2], "snbt " .. c[1])
|
||||
end
|
||||
end
|
||||
local a = assert(Nbt.parse("[B; 1b, -2b, 3]"))
|
||||
t:check(typeof(a) == "buffer" and buffer.len(a) == 3 and buffer.readi8(a, 1) == -2, "byte array")
|
||||
local ia = assert(Nbt.parse("[I;]"))
|
||||
t:check(ia.type == "intArray" and #ia.value == 0, "empty int array")
|
||||
local la = assert(Nbt.parse("[L; -9223372036854775808L, 5L]"))
|
||||
t:eq(Nbt.longs(la)[1], "-9223372036854775808", "long array extreme")
|
||||
local tc = assert(Nbt.parse("{a:1,b:[1,2,],}"))
|
||||
t:check(tc.a == 1 and #tc.b == 2, "trailing commas")
|
||||
t:eq(Nbt.print(assert(Nbt.parse('{"a b":1,c:"say \\"hi\\""}'))), "{\"a b\":1,c:'say \"hi\"'}", "quoting")
|
||||
local _, e1 = Nbt.parse("{a:1")
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "unterminated compound")
|
||||
local _, e2 = Nbt.parse("{a:300b}")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "byte out of range")
|
||||
local _, e3 = Nbt.parse(string.rep("[", 600) .. string.rep("]", 600))
|
||||
t:check(e3 and e3.code == Err.LIMIT, "depth limit")
|
||||
end
|
||||
|
||||
-- 5. peek
|
||||
do
|
||||
local c = Fx.cases[2]
|
||||
for _, k in { "raw", "gzip", "zlib", "header" } do
|
||||
local p = Nbt.peek(buffer.fromstring(c[k]))
|
||||
t:check(p ~= nil and p.name == c.root and p.rootType == "compound", "peek " .. k)
|
||||
end
|
||||
t:eq(Nbt.peek(buffer.fromstring("hello world")), nil, "peek text")
|
||||
end
|
||||
|
||||
-- 6. errors
|
||||
do
|
||||
local _, e = Nbt.decode("\10\0\0\1\0\1a")
|
||||
t:check(e and e.code == Err.TRUNCATED, "truncated")
|
||||
local _, e2 = Nbt.decode("\13\0\0")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad root type")
|
||||
local _, e3 = Nbt.decode("\10\0\0\9\0\1l\1\127\255\255\255")
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "huge list length")
|
||||
local deep = {}
|
||||
local cur = deep
|
||||
for _ = 1, 600 do
|
||||
local n = {}
|
||||
cur.x = n
|
||||
cur = n
|
||||
end
|
||||
local _, e4 = Nbt.encode(deep)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode depth")
|
||||
local _, e5 = Nbt.encode({ a = Nbt.byte(300) })
|
||||
t:check(e5 and e5.code == Err.ARG, "encode byte range")
|
||||
local _, e6 = Nbt.encode({ a = function() end })
|
||||
t:check(e6 and e6.code == Err.ARG, "encode function")
|
||||
end
|
||||
|
||||
-- 7. fuzz
|
||||
local samples = {}
|
||||
for i = 1, 8 do
|
||||
table.insert(samples, Fx.cases[i].raw)
|
||||
table.insert(samples, Fx.cases[i].little)
|
||||
end
|
||||
table.insert(samples, Fx.cases[3].gzip)
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Nbt.decode(b)
|
||||
end, samples)
|
||||
t:fuzz("decode varint", function(b: buffer)
|
||||
return Nbt.decode(b, { varint = true })
|
||||
end, { Fx.net[1].data, Fx.net[2].data })
|
||||
local texts = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(texts, Fx.cases[i].compact)
|
||||
end
|
||||
t:fuzz("parse", function(b: buffer)
|
||||
return Nbt.parse(buffer.tostring(b))
|
||||
end, texts)
|
||||
t:done()
|
||||
Reference in new issue
Block a user