97 lines
4.1 KiB
Luau
97 lines
4.1 KiB
Luau
-- data/msgpack tests: values packed by Python's msgpack decode to msgpack's own values (canonical form), re-encode to
|
|
-- the same bytes, 64-bit integers as doubles and as exact strings, timestamps / ext, streams, options, error codes,
|
|
-- and the fuzz pass. Fixtures: tools/fixtures/msgpack.py.
|
|
local T = require("@tests/T")
|
|
local Mp = require("@rosetta/data/msgpack")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Canon = require("@tests/data/Canon")
|
|
local Fx = require("@tests/gen/msgpack")
|
|
|
|
local t = T.suite("data/msgpack")
|
|
|
|
t:eq(Mp.name, "msgpack", "name")
|
|
t:check(Mp.caps.decode and Mp.caps.encode, "caps")
|
|
|
|
local function dec(s: string | buffer, opts: any?): (any, any)
|
|
local b = if type(s) == "string" then buffer.fromstring(s) else s
|
|
return Mp.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
-- ext values canonicalise like the fixture's ["ext", code, data]
|
|
local function plain(v: any): any
|
|
if Mp.isExt(v) then
|
|
return { "ext", v.type, v.data }
|
|
elseif type(v) == "table" and not Types.Date.is(v) and v ~= Types.null then
|
|
local o = if Types.isObject(v) then Types.object() else {}
|
|
for k, x in next, v do
|
|
o[k] = plain(x)
|
|
end
|
|
return o
|
|
end
|
|
return v
|
|
end
|
|
|
|
for i, c in Fx.safe do
|
|
local v, err = dec(c.data)
|
|
t:check(v ~= nil, "safe " .. i .. ": " .. tostring(err))
|
|
t:eq(Canon.of(plain(v)), c.canon, "safe " .. i .. ": value")
|
|
local b = Mp.encode(v) :: buffer
|
|
t:check(b and buffer.tostring(b) == c.data, "safe " .. i .. ": same bytes")
|
|
end
|
|
for i, c in Fx.wide do
|
|
local v, err = dec(c.data)
|
|
t:check(v ~= nil, "wide " .. i .. ": " .. tostring(err))
|
|
t:eq(Canon.of(plain(v)), c.canon, "wide " .. i .. ": value")
|
|
local vs = dec(c.data, { bigInts = "string" })
|
|
t:eq(Canon.of(plain(vs)), c.canonStr, "wide " .. i .. ": exact big integers")
|
|
t:eq(Canon.of(plain(dec(Mp.encode(v) :: buffer))), c.canon, "wide " .. i .. ": round trip")
|
|
end
|
|
|
|
-- details
|
|
do
|
|
t:eq(Mp.isExt(dec("\xd4\x05\x07")), true, "fixext1")
|
|
local d = dec("\xd6\xff\x00\x00\x00\x00")
|
|
t:check(Types.Date.is(d) and tostring(d) == "1970-01-01T00:00:00Z", "timestamp32")
|
|
t:eq(dec("\xcf\xff\xff\xff\xff\xff\xff\xff\xff", { bigInts = "string" }), "18446744073709551615", "u64 max text")
|
|
t:eq(dec("\xd3\x80\x00\x00\x00\x00\x00\x00\x00", { bigInts = "string" }), "-9223372036854775808", "i64 min text")
|
|
t:eq(dec("\xd3\xff\xff\xff\xff\xff\xff\xff\xff"), -1, "i64 -1")
|
|
t:eq(buffer.tostring(Mp.encode(-2 ^ 40) :: buffer), "\xd3\xff\xff\xff\x00\x00\x00\x00\x00", "encode i64")
|
|
t:eq(buffer.tostring(Mp.encode(1.5, { float32 = true }) :: buffer), "\xca\x3f\xc0\x00\x00", "float32")
|
|
t:eq(buffer.tostring(Mp.encode({}) :: buffer), "\x90", "empty table = array")
|
|
t:eq(buffer.tostring(Mp.encode({}, { emptyTable = "map" }) :: buffer), "\x80", "emptyTable map")
|
|
t:eq(buffer.tostring(Mp.encode(Types.object()) :: buffer), "\x80", "Types.object = map")
|
|
t:eq(buffer.tostring(Mp.encode({ b = 1, a = 2 }) :: buffer), "\x82\xa1a\x02\xa1b\x01", "plain keys sorted")
|
|
t:eq(buffer.tostring(Mp.encode("ab", { stringsAsBin = true }) :: buffer), "\xc4\x02ab", "stringsAsBin")
|
|
t:eq(buffer.tostring(Mp.encode(Mp.ext(3, buffer.fromstring("xyz"))) :: buffer), "\xc7\x03\x03xyz", "ext8")
|
|
local all = dec("\x01\x02\xa1x", { all = true })
|
|
t:eq(#all, 3, "stream")
|
|
t:eq(buffer.tostring(Mp.encode({ 1, "x" }, { all = true }) :: buffer), "\x01\xa1x", "encode stream")
|
|
t:eq(dec("\xc0"), Types.null, "nil -> null")
|
|
local cyc = {}
|
|
cyc[1] = cyc
|
|
local _, ce = Mp.encode(cyc)
|
|
t:eq(ce and ce.code, Err.ARG, "cycle")
|
|
local function code(s: string, o: any?): string?
|
|
local _, e = dec(s, o)
|
|
return e and e.code
|
|
end
|
|
t:eq(code("\xdd\xff\xff\xff\xff"), Err.TRUNCATED, "huge array count")
|
|
t:eq(code("\xa5ab"), Err.TRUNCATED, "short string")
|
|
t:eq(code("\xc1"), Err.CORRUPT, "reserved byte")
|
|
t:eq(code("\x01\x02"), Err.CORRUPT, "trailing data")
|
|
t:eq(code(string.rep("\x91", 600) .. "\x01"), Err.LIMIT, "depth")
|
|
end
|
|
|
|
local samples = {}
|
|
for i = 5, 60 do
|
|
if #Fx.safe[i].data < 3000 then
|
|
table.insert(samples, Fx.safe[i].data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Mp.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:done()
|