ws-arch: archive/xz: .xz / .lzma read as one-entry stream archives (codecs/Lzma)
This commit is contained in:
1 parent
b306a3bf5e
commit
0a56389b09
4 files changed
+193
No files matched your search
@@ -0,0 +1,78 @@
|
||||
-- archive/xz tests: the contract, sniffing (.xz magic, conservative .lzma), every xz / .lzma fixture of
|
||||
-- tools/fixtures/lzma.py read as a one-entry archive (entry names from opts.name), F.load through the registry, errors,
|
||||
-- and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local Xz = require("@formats/archive/xz")
|
||||
local F = require("@formats")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/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)
|
||||
|
||||
-- sniff: not xz
|
||||
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")
|
||||
|
||||
-- through the api
|
||||
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
|
||||
|
||||
-- fuzz
|
||||
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()
|
||||
Reference in new issue
Block a user