Files

83 lines
3.1 KiB
Luau

-- archive/zst tests (tools/fixtures/zstd.py, shared with tests/codecs/zstd.luau).
local T = require("@tests/T")
local F = require("@rosetta")
local Zst = require("@rosetta/archive/zst")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/zstd")
local t = T.suite("zst")
t:eq(Zst.name, "zst", "name")
t:eq(Zst.category, "archive", "category")
t:check(Zst.caps.decode and not Zst.caps.encode, "caps")
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
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring(case("text/l3").data))), 1, "sniff zstd")
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring(case("skipfirst/frames").data))), 0.5, "sniff skippable frame")
t:eq(Zst.sniff(Bytes.reader(buffer.fromstring("PK\3\4"))), 0, "sniff other")
t:eq(Zst.sniff(Bytes.reader(buffer.create(0))), 0, "sniff empty")
for _, name in { "text/l3", "runs/ultra22", "multi/frames", "empty/l3", "big/l19" } do
local c = case(name)
local a, err = Zst.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "dir/notes.txt.zst" })
t:check(a, name .. ": " .. tostring(err))
local list = a:list()
t:eq(#list, 1, name .. ": one entry")
t:eq(list[1].name, "notes.txt", name .. ": entry name")
t:eq(list[1].size, #Fx.corpora[c.corpus], name .. ": size")
t:eq(list[1].compressedSize, #c.data, name .. ": compressedSize")
t:check(a.meta.stream == true and a.kind == "archive" and a.format == "zst", name .. ": archive fields")
local data = a:read(list[1].name)
t:check(data and buffer.tostring(data) == Fx.corpora[c.corpus], name .. ": read")
end
do
local c = case("short/l3")
local a = Zst.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
t:eq(a:list()[1].name, "data", "no name -> data")
local a2 = Zst.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "backup.tzst" }) :: any
t:eq(a2:list()[1].name, "backup.tar", ".tzst -> .tar")
local _, e = a:read("nope")
t:check(e and e.code == Err.ARG, "missing entry")
local _, we = a:write()
t:check(we and we.code == Err.UNSUPPORTED, "read-only: " .. tostring(we))
local big = buffer.create(#c.data + 10)
buffer.writestring(big, 5, c.data)
local a3 = Zst.decode(Bytes.reader(big, 5, #c.data)) :: any
t:check(a3 and buffer.tostring(a3:read("data")) == Fx.corpora.short, "reader range")
end
do
local c = case("text/l5")
local a, err = F.load(c.data, { name = "x/readme.md.zst" })
t:check(a and a.format == "zst", "F.load detects zst: " .. tostring(err))
t:eq(a:list()[1].name, "readme.md", "F.load entry name")
t:eq(F.detect(c.data), "zst", "F.detect")
local _, e = F.load(string.sub(c.data, 1, 30), { format = "zst" })
t:check(e and e.code == Err.TRUNCATED, "truncated through the api: " .. tostring(e))
local _, se = F.save(a, nil, { format = "zst" })
t:check(se and se.code == Err.UNSUPPORTED, "no encoder")
end
do
local samples = {}
for i, c in Fx.cases do
if i % 11 == 0 and #c.data < 30000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
return Zst.decode(Bytes.reader(b), { maxBytes = 1e6 })
end, samples, { mutations = 10, garbage = 500 })
end
t:done()