Files

110 lines
4.3 KiB
Luau

-- codecs/compress/Zstd tests: zstd CLI frames (levels 1..19, --ultra -22, --fast, --long) decode, XXH64, errors.
-- Fixtures: tools/fixtures/zstd.py.
local T = require("@tests/T")
local Zstd = require("@rosetta/codecs/compress/Zstd")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/zstd")
local t = T.suite("zstd")
-- XXH64 reference values (xxhsum -H64)
do
local function hex(s: string): string
local hi, lo = Zstd.xxh64(buffer.fromstring(s))
return string.format("%08x%08x", hi, lo)
end
t:eq(hex(""), "ef46db3751d8e999", "xxh64 empty")
t:eq(hex("a"), "d24ec4f1a98c6e5b", "xxh64 a")
t:eq(hex("abc"), "44bc2cf5ad770999", "xxh64 abc")
t:eq(hex("Nobody inspects the spammish repetition"), "fbcea83c8a378bf1", "xxh64 sentence")
t:eq(hex(string.rep("0123456789", 10)), "f80e7b96315afffa", "xxh64 100 bytes")
end
local nbytes = 0
for _, c in Fx.cases do
local want = Fx.corpora[c.corpus]
local b = buffer.fromstring(c.data)
local out, consumed = Zstd.decompress(b)
t:check(out ~= nil, c.name .. ": " .. tostring(consumed))
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
t:eq(consumed, #c.data, c.name .. ": consumed")
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/l3")
local big = buffer.create(#c.data + 20)
buffer.fill(big, 0, 0xAB)
buffer.writestring(big, 7, c.data)
local out, consumed = Zstd.decompress(big, 7, #c.data)
t:check(out and buffer.tostring(out) == Fx.corpora.text, "at an offset")
t:eq(consumed, 7 + #c.data, "consumed at an offset")
t:check(buffer.tostring((Zstd.decompress(c.data))) == Fx.corpora.text, "string input")
t:check(Zstd.isFrame(buffer.fromstring(c.data)), "isFrame")
t:check(not Zstd.isFrame(buffer.fromstring("abcd")), "isFrame on other data")
-- trailing garbage is an error, as zstd -d says
local _, e = Zstd.decompress(c.data .. "junk")
t:check(e and e.code == Err.CORRUPT, "trailing garbage: " .. tostring(e))
end
-- checksum mismatch (the last 4 bytes), and verify = false skips it
do
local c = case("text/l9")
local b = buffer.fromstring(c.data)
local n = buffer.len(b)
buffer.writeu8(b, n - 1, bit32.bxor(buffer.readu8(b, n - 1), 0x10))
local out, e = Zstd.decompress(b)
t:check(out == nil and Err.is(e) and e.code == Err.CHECKSUM, "checksum mismatch: " .. tostring(e))
local out2 = Zstd.decompress(b, nil, nil, { verify = false })
t:check(out2 and buffer.tostring(out2) == Fx.corpora.text, "verify = false")
end
do
local c = case("text/l3")
local _, e = Zstd.decompress(c.data, nil, nil, { maxBytes = 1000 })
t:check(e and e.code == Err.LIMIT, "maxBytes with a content size: " .. tostring(e))
local s = case("text/stdin")
local _, e2 = Zstd.decompress(s.data, nil, nil, { maxBytes = 1000 })
t:check(e2 and e2.code == Err.LIMIT, "maxBytes without a content size: " .. tostring(e2))
local ok, r1, r2 = pcall(Zstd.decompress, 5 :: any)
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
local _, e3 = Zstd.decompress("")
t:check(e3 and e3.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e3))
local _, e4 = Zstd.decompress("\40\181\47\253\0")
t:check(e4 and e4.code == Err.TRUNCATED, "header only: " .. tostring(e4))
-- a dictionary ID: FHD with Dictionary_ID_flag = 1, single segment, then a 1-byte dictionary id and 1-byte size
local _, e5 = Zstd.decompress("\40\181\47\253\33\7\0\1\0\0")
t:check(e5 and e5.code == Err.UNSUPPORTED, "dictionary: " .. tostring(e5))
-- reserved block type
local _, e6 = Zstd.decompress("\40\181\47\253\32\0\7\0\0")
t:check(e6 and e6.code == Err.CORRUPT, "reserved block type: " .. tostring(e6))
end
do
local samples = {}
for i, c in Fx.cases do
if i % 5 == 0 and #c.data > 8 and #c.data < 40000 then
table.insert(samples, c.data)
end
end
t:fuzz("decompress", function(b: buffer)
return Zstd.decompress(b)
end, samples, { mutations = 12, garbage = 2000 })
-- keep the frame header intact so the mutations reach the blocks
t:fuzz("decompress (blocks)", function(b: buffer)
return Zstd.decompress(b, nil, nil, { verify = false, maxBytes = 1e6 })
end, samples, { mutations = 40, garbage = 0, keepHeader = 12, seed = 99 })
end
t:done()