118 lines
4.8 KiB
Luau
118 lines
4.8 KiB
Luau
-- data/cbor tests: RFC 8949 Appendix A and Python's cbor2 (tools/fixtures/cbor.py).
|
|
local T = require("@tests/T")
|
|
local Cbor = require("@rosetta/data/cbor")
|
|
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/cbor")
|
|
|
|
local t = T.suite("data/cbor")
|
|
|
|
t:eq(Cbor.name, "cbor", "name")
|
|
t:check(Cbor.caps.decode and Cbor.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 Cbor.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
-- the fixture's view of tags / simple values / undefined
|
|
local function plain(v: any): any
|
|
if v == Cbor.undefined then
|
|
return { "undefined" }
|
|
elseif Cbor.isSimple(v) then
|
|
return { "simple", v.value }
|
|
elseif Cbor.isTagged(v) then
|
|
return { "tag", v.tag, plain(v.value) }
|
|
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[plain(k)] = plain(x)
|
|
end
|
|
return o
|
|
end
|
|
return v
|
|
end
|
|
|
|
local nApp = 0
|
|
for _, c in Fx.appendix do
|
|
local v, err = dec(c.data)
|
|
if c.error then
|
|
t:check(v == nil and err ~= nil, c.hex .. ": rejected like cbor2")
|
|
else
|
|
t:check(v ~= nil or err == nil, c.hex .. ": " .. tostring(err))
|
|
t:eq(Canon.of(plain(v)), c.canon, c.hex .. ": value")
|
|
t:eq(Canon.of(plain(dec(c.data, { bigInts = "string" }))), c.canonStr, c.hex .. ": exact")
|
|
t:eq(Canon.of(plain(dec(Cbor.encode(v) :: buffer))), c.canon, c.hex .. ": round trip")
|
|
t:eq(Canon.of(plain(dec(Cbor.encode(v, { compactFloats = true }) :: buffer))), c.canon, c.hex .. ": compact round trip")
|
|
end
|
|
nApp += 1
|
|
end
|
|
t:note("RFC 8949 appendix A: %d examples", nApp)
|
|
|
|
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 = Cbor.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")
|
|
t:eq(Canon.of(plain(dec(c.data, { bigInts = "string" }))), c.canonStr, "wide " .. i .. ": exact")
|
|
end
|
|
|
|
do
|
|
local function hex(b: buffer): string
|
|
return (string.gsub(buffer.tostring(b), ".", function(ch)
|
|
return string.format("%02x", string.byte(ch))
|
|
end))
|
|
end
|
|
t:eq(hex(Cbor.encode(1.5, { compactFloats = true }) :: buffer), "f93e00", "half float")
|
|
t:eq(hex(Cbor.encode(100000.0 + 0.5, { compactFloats = true }) :: buffer), "fa47c35040", "single float")
|
|
t:eq(hex(Cbor.encode(1.1) :: buffer), "fb3ff199999999999a", "double float")
|
|
t:eq(hex(Cbor.encode(-0.0, { compactFloats = true }) :: buffer), "f98000", "negative zero")
|
|
t:eq(hex(Cbor.encode(0 / 0, { compactFloats = true }) :: buffer), "f97e00", "nan")
|
|
t:eq(hex(Cbor.encode(-2 ^ 64) :: buffer), "3bffffffffffffffff", "-2^64")
|
|
t:eq(hex(Cbor.encode(Cbor.tagged(32, "x")) :: buffer), "d8206178", "tagged")
|
|
t:eq(hex(Cbor.encode(Cbor.simple(255)) :: buffer), "f8ff", "simple")
|
|
t:eq(hex(Cbor.encode(Cbor.undefined) :: buffer), "f7", "undefined")
|
|
t:eq(hex(Cbor.encode(Types.Date.parse("2013-03-21T20:04:00Z")) :: buffer), "c074323031332d30332d32315432303a30343a30305a", "date tag 0")
|
|
t:eq(hex(Cbor.encode(Types.Date.parse("2013-03-21T20:04:00Z"), { dates = "epoch" }) :: buffer), "c11a514b67b0", "date tag 1")
|
|
t:eq(hex(Cbor.encode({ 1 }, { selfDescribe = true }) :: buffer), "d9d9f78101", "self-describe")
|
|
t:eq(dec("\217\217\247\1"), 1, "self-describe stripped")
|
|
t:eq(Cbor.sniff(Bytes.reader(buffer.fromstring("\217\217\247\1"))), 1, "sniff self-describe")
|
|
t:eq(#dec("\1\2\3", { all = true }), 3, "sequence")
|
|
t:eq(hex(Cbor.encode({ 1, "a" }, { all = true }) :: buffer), "016161", "encode sequence")
|
|
local function code(s: string): string?
|
|
local _, e = dec(s)
|
|
return e and e.code
|
|
end
|
|
t:eq(code("\x9b\xff\xff\xff\xff\xff\xff\xff\xff"), Err.TRUNCATED, "huge array count")
|
|
t:eq(code("\x5f\x61\x61\xff"), Err.CORRUPT, "text chunk in a byte string")
|
|
t:eq(code("\xff"), Err.CORRUPT, "lone break")
|
|
t:eq(code("\x1c"), Err.CORRUPT, "reserved additional info")
|
|
t:eq(code("\x62\xc3\x28"), Err.CORRUPT, "invalid UTF-8")
|
|
t:eq(code("\x01\x02"), Err.CORRUPT, "trailing data")
|
|
t:eq(code("\xc0\x01"), Err.CORRUPT, "tag 0 on a number")
|
|
t:eq(code(string.rep("\x81", 600) .. "\x01"), Err.LIMIT, "depth")
|
|
t:eq(code("\x9f\x01"), Err.TRUNCATED, "unterminated indefinite array")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx.appendix do
|
|
table.insert(samples, c.data)
|
|
end
|
|
for i = 1, 40 do
|
|
if #Fx.safe[i].data < 3000 then
|
|
table.insert(samples, Fx.safe[i].data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Cbor.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 4 })
|
|
t:done()
|