Files
rosetta/tests/archive/lz4.luau
T

114 lines
4.2 KiB
Luau

-- archive/lz4 tests (and codecs/compress/Xxh32) against the lz4 tool (tools/fixtures/lz4frame.py).
local T = require("@tests/T")
local F = require("@rosetta")
local Lz4F = require("@rosetta/archive/lz4")
local Xxh32 = require("@rosetta/codecs/compress/Xxh32")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/lz4frame")
local C = require("@tests/gen/lz4frame_corpora")
local t = T.suite("lz4")
t:eq(Lz4F.name, "lz4", "name")
-- XXH32 vectors (xxHash's sanity checks)
t:eq(Xxh32.hash(buffer.fromstring("")), 0x02CC5D05, "xxh32 empty")
t:eq(Xxh32.hash(buffer.fromstring(""), 0, 0, 0x9E3779B1), 0x36B78AE7, "xxh32 empty, seed prime")
t:eq(Xxh32.hash(buffer.fromstring("a")), 0x550D7456, "xxh32 a")
t:eq(Xxh32.hash(buffer.fromstring("abc")), 0x32D153FF, "xxh32 abc")
t:eq(Xxh32.hash(buffer.fromstring("Nobody inspects the spammish repetition")), 0xE2293B2F, "xxh32 sentence")
local function want(c: any): string
if c.corpus == "short+text" then
return C.short .. C.text
end
return C[c.corpus]
end
for _, c in Fx.cases do
local out, err = Lz4F.decompress(c.data)
t:check(out, c.name .. ": " .. tostring(err))
local w = want(c)
t:eq(buffer.len(out), #w, c.name .. ": size")
t:eq(Crc32.of(out), Crc32.of(buffer.fromstring(w)), c.name .. ": data")
t:eq(F.detect(c.data), "lz4", c.name .. ": F.detect")
end
-- our frames, read by lz4 -d
t:check(#Fx.ours >= 15, "verdicts present")
for _, v in Fx.ours do
t:check(v.ok, "lz4 -d reads our " .. v.name)
end
for name, data in C do
for _, o in { {}, { blockSize = 65536, blockChecksum = true, contentSize = true }, { contentChecksum = false } } do
local z = assert(Lz4F.compress(data, o :: any))
t:eq(buffer.tostring(assert(Lz4F.decompress(z))), data, "round trip " .. name)
end
end
do
local a = assert(F.load(Fx.cases[1].data, { name = "notes.txt.lz4" }))
t:check(a.meta.stream and a.format == "lz4", "single stream")
t:eq(a:list()[1].name, "notes.txt", "entry named after the file")
local tar = F.format("tar").new()
tar:add("a.txt", "alpha")
tar:add("b.txt", "beta")
local tl = assert(F.save(tar, nil, { format = "lz4" }))
local back = F.load(tl, { name = "x.tlz4" })
t:eq(back.format, "tar", "several entries: a .tar.lz4 that unwraps")
t:eq(back.meta.compression, "lz4", "meta.compression")
t:eq(F.load(tl, { name = "x.tlz4", unwrap = false }):list()[1].name, "x.tar", ".tlz4 -> .tar")
local s = assert(F.save("some text", nil, { format = "lz4" }))
t:eq(buffer.tostring(F.load(s):read("data")), "some text", "a string")
end
do
local good
for _, c in Fx.cases do
if c.name == "text/BX" then
good = c.data
end
end
local function flip(at: number): buffer
local b = buffer.fromstring(good)
buffer.writeu8(b, at, bit32.bxor(buffer.readu8(b, at), 1))
return b
end
local _, e1 = Lz4F.decompress(flip(6))
t:check(e1 and e1.code == Err.CHECKSUM, "header checksum: " .. tostring(e1))
local _, e2 = Lz4F.decompress(flip(20))
t:check(e2 and e2.code == Err.CHECKSUM, "block checksum: " .. tostring(e2))
local _, e3 = Lz4F.decompress(flip(#good - 2))
t:check(e3 and e3.code == Err.CHECKSUM, "content checksum: " .. tostring(e3))
t:check(Lz4F.decompress(flip(#good - 2), nil, nil, { verify = false }), "verify = false")
local _, e4 = Lz4F.decompress(string.sub(good, 1, 1000))
t:check(e4 and e4.code == Err.TRUNCATED, "truncated: " .. tostring(e4))
local _, e5 = Lz4F.decompress(Fx.cases[1].data, nil, nil, { maxBytes = 10 })
t:check(e5 == nil or e5.code == Err.LIMIT, "maxBytes")
local big
for _, c in Fx.cases do
if c.name == "zeros/default" then
big = c.data
end
end
local _, e6 = Lz4F.decompress(big, nil, nil, { maxBytes = 1000 })
t:check(e6 and e6.code == Err.LIMIT, "maxBytes: " .. tostring(e6))
local _, e7 = Lz4F.compress("x", { blockSize = 1000 })
t:check(e7 and e7.code == Err.ARG, "bad blockSize")
local _, e8 = Lz4F.decompress("not lz4 at all")
t:check(e8 and e8.code == Err.CORRUPT, "bad magic")
end
local samples = {}
for _, c in Fx.cases do
if #c.data < 6000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Lz4F.decode(Bytes.reader(b), { maxBytes = 2 ^ 22 })
end, samples, { mutations = 40, keepHeader = 4 })
t:done()