Files

98 lines
3.2 KiB
Luau

-- codecs/compress/Bzip2 tests: Python bz2 (libbzip2) streams decode exactly, CRC mismatches, truncations.
-- Fixtures: tools/fixtures/bzip2.py.
local T = require("@tests/T")
local Bzip2 = require("@rosetta/codecs/compress/Bzip2")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/bzip2")
local t = T.suite("bzip2")
t:eq(Bzip2.crc(buffer.fromstring("123456789")), 0xFC891918, "CRC-32/BZIP2 check value")
local nbytes = 0
for _, c in Fx.cases do
local want = Fx.corpora[c.corpus]
local out, consumed = Bzip2.decompress(c.data)
t:check(out ~= nil, c.name .. ": " .. tostring(consumed))
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
if c.name ~= "multi/trailing" then
t:eq(consumed, #c.data, c.name .. ": consumedEnd")
end
nbytes += #want
end
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
local function case(name: string): any
for _, c in Fx.cases do
if c.name == name then
return c
end
end
error("no case " .. name)
end
do
local c = case("text/l9")
local big = buffer.create(#c.data + 20)
buffer.fill(big, 0, 0xAB)
buffer.writestring(big, 7, c.data)
local out, consumed = Bzip2.decompress(big, 7, #c.data + 5)
t:check(out and buffer.tostring(out) == Fx.corpora.text, "at an offset")
t:eq(consumed, 7 + #c.data, "consumedEnd at an offset")
end
do
local c = case("zeros/l9")
local _, e = Bzip2.decompress(c.data, nil, nil, { maxBytes = 1000 })
t:check(e and e.code == Err.LIMIT, "maxBytes: " .. tostring(e))
local ok = Bzip2.decompress(c.data, nil, nil, { maxBytes = 250000 })
t:check(ok ~= nil, "maxBytes = exact size")
end
do
local c = case("text/l9")
local b = buffer.fromstring(c.data)
-- block CRC sits at bytes 10..13 (after "BZh9" + 6 magic bytes)
buffer.writeu8(b, 11, bit32.bxor(buffer.readu8(b, 11), 0x10))
local _, e = Bzip2.decompress(b)
t:check(e and e.code == Err.CHECKSUM, "block CRC mismatch: " .. tostring(e))
local s = case("one/l9")
local b2 = buffer.fromstring(s.data)
local n = buffer.len(b2)
buffer.writeu8(b2, n - 2, bit32.bxor(buffer.readu8(b2, n - 2), 0x01))
local _, e2 = Bzip2.decompress(b2)
t:check(e2 and (e2.code == Err.CHECKSUM or e2.code == Err.CORRUPT), "stream CRC mismatch: " .. tostring(e2))
end
-- truncation -> TRUNCATED, bad magic -> CORRUPT, bad args -> ARG
do
local c = case("text/l1")
for _, cut in { 3, 10, 40, #c.data // 2, #c.data - 3 } do
local _, e = Bzip2.decompress(string.sub(c.data, 1, cut))
t:check(e and e.code == Err.TRUNCATED, "truncated at " .. cut .. ": " .. tostring(e))
end
local _, e = Bzip2.decompress("BZx9abcdefgh")
t:check(e and e.code == Err.CORRUPT, "bad magic")
local _, e2 = Bzip2.decompress(5 :: any)
t:check(e2 and e2.code == Err.ARG, "non-buffer")
local _, e3 = Bzip2.decompress("BZh9", -1)
t:check(e3 and e3.code == Err.ARG, "bad offset")
end
do
local samples = {}
for i, c in Fx.cases do
if i % 4 == 0 and #c.data < 40000 then
table.insert(samples, c.data)
end
end
t:fuzz("decompress", function(b: buffer)
return Bzip2.decompress(b)
end, samples, { mutations = 12, garbage = 1000, keepHeader = 4 })
t:fuzz("decompress (no header kept)", function(b: buffer)
return Bzip2.decompress(b)
end, samples, { mutations = 4, garbage = 200 })
end
t:done()