74 lines
2.4 KiB
Luau
74 lines
2.4 KiB
Luau
-- archive/xz tests (tools/fixtures/lzma.py).
|
|
local T = require("@tests/T")
|
|
local Xz = require("@rosetta/archive/xz")
|
|
local F = require("@rosetta")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/lzma")
|
|
|
|
local t = T.suite("xz")
|
|
|
|
t:eq(Xz.name, "xz", "name")
|
|
t:eq(Xz.category, "archive", "category")
|
|
t:check(Xz.caps.decode and not Xz.caps.encode, "caps")
|
|
|
|
local function reader(s: string): Bytes.Reader
|
|
return Bytes.reader(buffer.fromstring(s))
|
|
end
|
|
|
|
local n = 0
|
|
for _, c in Fx.cases do
|
|
if c.kind == "xz" or c.kind == "alone" then
|
|
local want = Fx.corpora[c.corpus]
|
|
t:check(Xz.sniff(reader(c.data)) > 0, c.name .. ": sniff")
|
|
local a, err = Xz.decode(reader(c.data), { name = "dir/file.txt." .. (if c.kind == "xz" then "xz" else "lzma") })
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
local list = a:list()
|
|
t:eq(#list, 1, c.name .. ": one entry")
|
|
t:eq(list[1].name, "file.txt", c.name .. ": entry name")
|
|
t:eq(list[1].size, #want, c.name .. ": size")
|
|
t:check(a.meta.stream == true, c.name .. ": meta.stream")
|
|
local b = a:read("file.txt")
|
|
t:check(b and buffer.tostring(b) == want, c.name .. ": data")
|
|
n += 1
|
|
end
|
|
end
|
|
t:note("%d xz / lzma files", n)
|
|
|
|
t:eq(Xz.sniff(reader("PK\3\4 hello")), 0, "zip is not xz")
|
|
t:eq(Xz.sniff(reader("")), 0, "empty")
|
|
t:eq(Xz.sniff(reader(string.rep("\0", 40))), 0, "zeros are not .lzma")
|
|
|
|
do
|
|
local c = Fx.cases[1]
|
|
for _, x in Fx.cases do
|
|
if x.name == "text/xz-6" then
|
|
c = x
|
|
end
|
|
end
|
|
local a, err = F.load(c.data, { name = "notes.txz" })
|
|
t:check(a and a.kind == "archive" and a.format == "xz", "F.load: " .. tostring(err))
|
|
t:eq(a:list()[1].name, "notes.tar", "txz -> tar entry name")
|
|
t:eq(F.detect(c.data), "xz", "F.detect")
|
|
local a2 = F.archive.load(c.data)
|
|
t:eq(a2:list()[1].name, "data", "unnamed entry")
|
|
local _, e2 = F.save(a2, nil, { format = "xz" })
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "xz is read-only: " .. tostring(e2))
|
|
local _, e3 = F.load(string.sub(c.data, 1, 100), { format = "xz" })
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if (c.kind == "xz" or c.kind == "alone") and i % 5 == 0 and #c.data < 30000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Xz.decode(Bytes.reader(b), { maxBytes = 2 ^ 20 })
|
|
end, samples, { mutations = 6, garbage = 400 })
|
|
end
|
|
|
|
t:done()
|