76 lines
2.9 KiB
Luau
76 lines
2.9 KiB
Luau
-- codecs/compress/UnixCompress tests: ncompress 5.0 files (widths 10..16, block mode, CLEAR codes after the ratio
|
|
-- check, empty / one-byte / random inputs) decode to their inputs; compress() gives ncompress's bytes for the same input
|
|
-- and width; non-block-mode streams; errors; the fuzz pass. Fixtures: tools/fixtures/z.py.
|
|
local T = require("@tests/T")
|
|
local UnixCompress = require("@rosetta/codecs/compress/UnixCompress")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/z")
|
|
|
|
local t = T.suite("unixcompress")
|
|
|
|
for _, c in Fx.cases do
|
|
local out, err = UnixCompress.decompress(c.z)
|
|
t:check(out, c.name .. ": decompress: " .. tostring(err))
|
|
t:eq(buffer.len(out), #c.input, c.name .. ": size")
|
|
t:eq(Crc32.of(out), Crc32.of(buffer.fromstring(c.input)), c.name .. ": data")
|
|
local z, zerr = UnixCompress.compress(c.input, nil, nil, { maxBits = c.bits })
|
|
t:check(z, c.name .. ": compress: " .. tostring(zerr))
|
|
t:eq(buffer.len(z), #c.z, c.name .. ": compressed size = ncompress")
|
|
t:check(buffer.tostring(z) == c.z, c.name .. ": bytes identical to ncompress -b " .. c.bits)
|
|
t:note("%-12s %7d -> %7d bytes (-b %d)", c.name, #c.input, #c.z, c.bits)
|
|
end
|
|
|
|
-- 9-bit codes, LSB first, after a .Z header with the given flags byte
|
|
local function pack(flags: number, codes: { number }): buffer
|
|
local acc, nb, bytes = 0, 0, { 0x1F, 0x9D, flags }
|
|
for _, c in codes do
|
|
acc += c * 2 ^ nb
|
|
nb += 9
|
|
while nb >= 8 do
|
|
table.insert(bytes, acc % 256)
|
|
acc = acc // 256
|
|
nb -= 8
|
|
end
|
|
end
|
|
if nb > 0 then
|
|
table.insert(bytes, acc)
|
|
end
|
|
return T.bytes(bytes)
|
|
end
|
|
|
|
-- a non-block-mode stream (no CLEAR code; 256 is the first table entry): "ABABABA"
|
|
do
|
|
local out = UnixCompress.decompress(pack(0x10, { 65, 66, 256, 258 }))
|
|
t:eq(out and buffer.tostring(out), "ABABABA", "non-block mode")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local _, e1 = UnixCompress.decompress("\31\157")
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "short header")
|
|
local _, e2 = UnixCompress.decompress("\31\158\144")
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad magic")
|
|
local _, e3 = UnixCompress.decompress("\31\157\145")
|
|
t:check(e3 and e3.code == Err.UNSUPPORTED, "17-bit codes")
|
|
local _, e4 = UnixCompress.decompress(pack(0x90, { 65, 400 }))
|
|
t:check(e4 and e4.code == Err.CORRUPT, "a code past the table: " .. tostring(e4))
|
|
local _, e5 = UnixCompress.decompress(Fx.cases[5].z, nil, nil, { maxBytes = 1000 })
|
|
t:check(e5 and e5.code == Err.LIMIT, "maxBytes")
|
|
local _, e6 = UnixCompress.compress("x", nil, nil, { maxBits = 17 })
|
|
t:check(e6 and e6.code == Err.ARG, "maxBits 17")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
if #c.z < 4000 and #c.z > 3 then
|
|
table.insert(samples, c.z)
|
|
end
|
|
end
|
|
table.insert(samples, string.sub(Fx.cases[#Fx.cases].z, 1, 3000))
|
|
t:fuzz("decompress", function(b: buffer)
|
|
return UnixCompress.decompress(b, nil, nil, { maxBytes = 2 ^ 22 })
|
|
end, samples, { mutations = 40, keepHeader = 3 })
|
|
|
|
t:done()
|