87 lines
3.5 KiB
Luau
87 lines
3.5 KiB
Luau
-- data/plist tests: values written by Python's plistlib as XML and binary decode to plistlib's values; our XML writer
|
|
-- reproduces plistlib's XML byte for byte; binary-only values (UIDs, 64-bit integers); a hand-written XML plist;
|
|
-- error codes; the fuzz pass (XML and binary). Fixtures: tools/fixtures/plist.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Plist = require("@formats/data/plist")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Types = require("@formats/Types")
|
|
local Canon = require("@tests/data/Canon")
|
|
local Fx = require("@tests/gen/plist")
|
|
|
|
local t = T.suite("data/plist")
|
|
|
|
t:eq(Plist.name, "plist", "name")
|
|
t:check(Plist.caps.decode and Plist.caps.encode, "caps")
|
|
|
|
local function dec(s: string, opts: any?): (any, any)
|
|
return Plist.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
|
end
|
|
local function plain(v: any): any
|
|
if Plist.isUid(v) then
|
|
return { "uid", v.uid }
|
|
elseif type(v) == "table" and not Types.Date.is(v) 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.cases do
|
|
local v, err = dec(c.xml)
|
|
t:check(v ~= nil, "xml " .. i .. ": " .. tostring(err))
|
|
t:eq(Canon.of(v), c.canon, "xml " .. i .. ": value")
|
|
local vb, berr = dec(c.binary)
|
|
t:check(vb ~= nil, "binary " .. i .. ": " .. tostring(berr))
|
|
t:eq(Canon.of(vb), c.canon, "binary " .. i .. ": value")
|
|
t:eq(buffer.tostring(Plist.encode(v) :: buffer), c.xml, "xml " .. i .. ": same text as plistlib")
|
|
end
|
|
for i, c in Fx.wide do
|
|
local v, err = dec(c.binary)
|
|
t:check(v ~= nil, "wide " .. i .. ": " .. tostring(err))
|
|
t:eq(Canon.of(plain(v)), c.canon, "wide " .. i .. ": value")
|
|
end
|
|
do
|
|
local v = dec(Fx.extra)
|
|
t:eq(Canon.of(v), Fx.extraCanon, "hand-written plist")
|
|
t:eq(v.hex, 31, "hex integer")
|
|
local f = F.load(buffer.fromstring(Fx.cases[1].binary))
|
|
t:check(f ~= nil, "F.load sniffs binary plists")
|
|
local fx = F.load(buffer.fromstring(Fx.cases[1].xml))
|
|
t:eq(Canon.of(fx), Fx.cases[1].canon, "F.load sniffs XML plists over plain XML")
|
|
t:eq(dec("<plist><string>x</string></plist>"), "x", "bare root value")
|
|
local function code(s: string): string?
|
|
local _, e = dec(s)
|
|
return e and e.code
|
|
end
|
|
t:eq(code("<plist><dict><key>a</key></dict></plist>"), Err.CORRUPT, "key without value")
|
|
t:eq(code("<plist><integer>x</integer></plist>"), Err.CORRUPT, "bad integer")
|
|
t:eq(code("<plist><data>!!</data></plist>"), Err.CORRUPT, "bad base64")
|
|
t:eq(code("<plist><foo/></plist>"), Err.CORRUPT, "unknown element")
|
|
t:eq(code("bplist00"), Err.TRUNCATED, "short binary")
|
|
local _, ne = Plist.encode({ a = F.null })
|
|
t:eq(ne and ne.code, Err.ARG, "null cannot be written")
|
|
local _, ke = Plist.encode({ [1] = 1, x = 2 })
|
|
t:eq(ke and ke.code, Err.ARG, "non-string key")
|
|
-- a binary plist with a cycle: array 0 -> [array 0]
|
|
local cyc = "bplist00" .. "\xa1\x00" .. "\x08" .. string.rep("\0", 6) .. "\1\1" .. "\0\0\0\0\0\0\0\1" .. "\0\0\0\0\0\0\0\0" .. "\0\0\0\0\0\0\0\10"
|
|
t:eq(code(cyc), Err.CORRUPT, "reference cycle")
|
|
end
|
|
|
|
local samples = {}
|
|
for i = 1, 25 do
|
|
table.insert(samples, Fx.cases[i].xml)
|
|
table.insert(samples, Fx.cases[i].binary)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Plist.decode(Bytes.reader(b))
|
|
end, samples, { keepHeader = 0 })
|
|
t:fuzz("decode binary (header kept)", function(b: buffer)
|
|
return Plist.decode(Bytes.reader(b))
|
|
end, { Fx.cases[3].binary, Fx.cases[4].binary, Fx.wide[1].binary }, { keepHeader = 8, mutations = 200, garbage = 0 })
|
|
t:done()
|