143 lines
5.2 KiB
Luau
143 lines
5.2 KiB
Luau
-- codecs/Lzma tests: every fixture (liblzma through Python's lzma module and the xz CLI: presets 0-9 and -e, lc / lp /
|
|
-- pb variants, raw LZMA1 / LZMA2, .lzma files, xz with every check type, BCJ and delta filters, multiple blocks,
|
|
-- multiple streams and stream padding) decodes to its corpus; consumed offsets; checksum / size / truncation errors;
|
|
-- maxBytes; the fuzz no-throw pass. Fixtures: tools/fixtures/lzma.py.
|
|
local T = require("@tests/T")
|
|
local Lzma = require("@formats/codecs/Lzma")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/lzma")
|
|
|
|
local t = T.suite("lzma")
|
|
|
|
local function decode(c: any, b: buffer, opts: any?): (buffer?, any)
|
|
if c.kind == "raw" then
|
|
return Lzma.lzma(b, 0, nil, c.props, c.size, opts)
|
|
elseif c.kind == "raw2" then
|
|
return Lzma.lzma2(b, 0, nil, nil, nil, opts)
|
|
elseif c.kind == "alone" then
|
|
return Lzma.alone(b, 0, nil, opts)
|
|
end
|
|
return Lzma.xz(b, 0, nil, opts)
|
|
end
|
|
|
|
local nbytes = 0
|
|
for _, c in Fx.cases do
|
|
local want = Fx.corpora[c.corpus]
|
|
local b = buffer.fromstring(c.data)
|
|
local out, res = decode(c, b)
|
|
t:check(out ~= nil, c.name .. ": " .. tostring(res))
|
|
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
|
|
if c.kind ~= "raw" then
|
|
t:eq(res, #c.data, c.name .. ": consumed")
|
|
end
|
|
nbytes += #want
|
|
end
|
|
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
|
|
|
|
-- at an offset inside a bigger buffer, with trailing bytes
|
|
do
|
|
for _, c in Fx.cases do
|
|
if c.kind == "xz" and c.name == "text/xz-6" then
|
|
local big = buffer.create(#c.data + 11)
|
|
buffer.fill(big, 0, 0xAB)
|
|
buffer.writestring(big, 3, c.data)
|
|
local out, res = Lzma.xz(big, 3, #c.data)
|
|
t:check(out and buffer.tostring(out) == Fx.corpora.text and res == 3 + #c.data, "xz at an offset")
|
|
end
|
|
end
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local xz, xzc
|
|
for _, c in Fx.cases do
|
|
if c.name == "text/xz-check-crc32" then
|
|
xz = c
|
|
elseif c.name == "text/xz-6" then
|
|
xzc = c
|
|
end
|
|
end
|
|
-- the check field: the last bytes before the index; flip a data bit near the end of the block instead
|
|
local b = buffer.fromstring(xzc.data)
|
|
local n = buffer.len(b)
|
|
-- footer magic
|
|
buffer.writeu8(b, n - 1, 0)
|
|
local _, e1 = Lzma.xz(b)
|
|
t:check(e1 and (e1.code == Err.CORRUPT or e1.code == Err.CHECKSUM), "bad footer: " .. tostring(e1))
|
|
-- the stream header CRC
|
|
local b2 = buffer.fromstring(xz.data)
|
|
buffer.writeu8(b2, 8, bit32.bxor(buffer.readu8(b2, 8), 1))
|
|
local _, e2 = Lzma.xz(b2)
|
|
t:check(e2 and e2.code == Err.CHECKSUM, "header crc: " .. tostring(e2))
|
|
-- the block check (CRC-32 / CRC-64): flip a byte of the check field (found from the index: the 4 / 8 bytes before
|
|
-- the index indicator, which follows the block padding)
|
|
for _, c in { xz, xzc } do
|
|
local b3 = buffer.fromstring(c.data)
|
|
local n3 = buffer.len(b3)
|
|
local backward = (buffer.readu32(b3, n3 - 8) + 1) * 4
|
|
local indexAt = n3 - 12 - backward
|
|
buffer.writeu8(b3, indexAt - 1, bit32.bxor(buffer.readu8(b3, indexAt - 1), 0x10))
|
|
local _, e3 = Lzma.xz(b3)
|
|
t:check(e3 and e3.code == Err.CHECKSUM, c.name .. ": block check: " .. tostring(e3))
|
|
end
|
|
-- truncations are TRUNCATED or CORRUPT, never a throw
|
|
local _, e4 = Lzma.xz(buffer.fromstring(string.sub(xzc.data, 1, #xzc.data // 2)))
|
|
t:check(e4 and e4.code == Err.TRUNCATED, "truncated xz: " .. tostring(e4))
|
|
-- maxBytes
|
|
local _, e5 = Lzma.xz(xzc.data, nil, nil, { maxBytes = 1000 })
|
|
t:check(e5 and e5.code == Err.LIMIT, "maxBytes: " .. tostring(e5))
|
|
local ok, r1, r2 = pcall(Lzma.xz, 5 :: any)
|
|
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
|
local _, e6 = Lzma.lzma("\0\0\0\0\0", 0, nil, "\255")
|
|
t:check(e6 and e6.code == Err.CORRUPT, "bad props byte: " .. tostring(e6))
|
|
end
|
|
|
|
-- raw LZMA with the size known: decoding stops at the size even without an end marker
|
|
do
|
|
for _, c in Fx.cases do
|
|
if c.kind == "raw" and #Fx.corpora[c.corpus] > 1000 then
|
|
local want = Fx.corpora[c.corpus]
|
|
local out = Lzma.lzma(c.data, 0, nil, { lc = 3, lp = 0, pb = 2 }, nil)
|
|
if c.name == c.corpus .. "/raw-lc3-lp0-pb2" then
|
|
t:check(out and buffer.tostring(out) == want, c.name .. ": table props, end marker")
|
|
end
|
|
local part = Lzma.lzma(c.data, 0, nil, c.props, 500)
|
|
t:check(part and buffer.tostring(part) == string.sub(want, 1, 500), c.name .. ": a prefix by size")
|
|
end
|
|
end
|
|
end
|
|
|
|
-- fuzz
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if i % 7 == 0 and #c.data > 16 and #c.data < 40000 then
|
|
table.insert(samples, c)
|
|
end
|
|
end
|
|
local xs, as, rs = {}, {}, {}
|
|
for _, c in samples do
|
|
if c.kind == "xz" then
|
|
table.insert(xs, c.data)
|
|
elseif c.kind == "alone" then
|
|
table.insert(as, c.data)
|
|
else
|
|
table.insert(rs, c.data)
|
|
end
|
|
end
|
|
t:fuzz("xz", function(b: buffer)
|
|
return Lzma.xz(b, 0, nil, { maxBytes = 1 * 2 ^ 20 })
|
|
end, xs, { mutations = 6, garbage = 300 })
|
|
t:fuzz("alone", function(b: buffer)
|
|
return Lzma.alone(b, 0, nil, { maxBytes = 1 * 2 ^ 20 })
|
|
end, as, { mutations = 6, garbage = 300 })
|
|
t:fuzz("lzma2", function(b: buffer)
|
|
return Lzma.lzma2(b, 0, nil, nil, nil, { maxBytes = 1 * 2 ^ 20 })
|
|
end, rs, { mutations = 4, garbage = 1000 })
|
|
t:fuzz("lzma", function(b: buffer)
|
|
return Lzma.lzma(b, 0, nil, "\93", nil, { maxBytes = 1 * 2 ^ 20 })
|
|
end, rs, { mutations = 2, garbage = 1000 })
|
|
end
|
|
|
|
t:done()
|