ws-arch: merge ws/arch-lzma
This commit is contained in:
commit
f6ce1da736
16 files changed
+2912
No files matched your search
@@ -0,0 +1,115 @@
|
||||
-- archive/7z tests: the contract and sniff; every archive 7-Zip wrote in tools/fixtures/7z.py (LZMA / LZMA2 / copy /
|
||||
-- Deflate, solid and not, BCJ / ARM64 / ARM / PPC + SPARC / delta chains, plain and encoded headers) lists exactly the
|
||||
-- source tree (names incl. UTF-16 surrogates, directories, empty files, mtimes) and reads every file back; unsupported
|
||||
-- methods (BZip2, PPMd, BCJ2, AES) list but fail on read with Err.UNSUPPORTED naming the method; corrupt CRCs; F.load;
|
||||
-- the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local SevenZip = require("@formats/archive/7z")
|
||||
local F = require("@formats")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/7z")
|
||||
|
||||
local t = T.suite("7z")
|
||||
|
||||
t:eq(SevenZip.name, "7z", "name")
|
||||
t:eq(SevenZip.category, "archive", "category")
|
||||
t:check(SevenZip.caps.decode and not SevenZip.caps.encode, "caps")
|
||||
|
||||
local function decode(data: string, opts: any?): (any, any)
|
||||
return SevenZip.decode(Bytes.reader(buffer.fromstring(data)), opts)
|
||||
end
|
||||
|
||||
local want: { [string]: any } = {}
|
||||
for _, f in Fx.files do
|
||||
want[f.name] = f
|
||||
end
|
||||
|
||||
local nfiles = 0
|
||||
for _, c in Fx.cases do
|
||||
t:eq(SevenZip.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
|
||||
local a, err = decode(c.data)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
local list = a:list()
|
||||
if c.empty then
|
||||
t:eq(#list, 0, c.name .. ": no entries")
|
||||
continue
|
||||
end
|
||||
local seen = {}
|
||||
for _, e in list do
|
||||
local w = want[e.name]
|
||||
t:check(w ~= nil, c.name .. ": unexpected entry " .. e.name)
|
||||
seen[e.name] = true
|
||||
t:eq(e.dir, w.dir, c.name .. ": " .. e.name .. ": dir")
|
||||
t:eq(e.mtime, w.mtime, c.name .. ": " .. e.name .. ": mtime")
|
||||
if not w.dir then
|
||||
t:eq(e.size, #w.data, c.name .. ": " .. e.name .. ": size")
|
||||
local b, rerr = a:read(e.name)
|
||||
if c.unsupported and #w.data > 0 then
|
||||
t:check(b == nil and rerr and rerr.code == Err.UNSUPPORTED and string.find(rerr.message, c.unsupported, 1, true) ~= nil,
|
||||
c.name .. ": " .. e.name .. ": unsupported: " .. tostring(rerr))
|
||||
else
|
||||
t:check(b and buffer.tostring(b) == w.data, c.name .. ": " .. e.name .. ": data: " .. tostring(rerr))
|
||||
nfiles += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
for name in want do
|
||||
t:check(seen[name], c.name .. ": missing " .. name)
|
||||
end
|
||||
end
|
||||
t:note("%d archives, %d files read", #Fx.cases, nfiles)
|
||||
|
||||
-- corrupt data: a flipped byte in a stored file is a CRC error on read; a flipped header byte a header CRC error
|
||||
do
|
||||
local c
|
||||
for _, x in Fx.cases do
|
||||
if x.name == "copy" then
|
||||
c = x
|
||||
end
|
||||
end
|
||||
local s = c.data
|
||||
local at = string.find(s, "deep\ndeep", 1, true) :: number
|
||||
local bad = string.sub(s, 1, at - 1) .. "D" .. string.sub(s, at + 1)
|
||||
local a = decode(bad)
|
||||
t:check(a, "copy archive with a flipped data byte still lists")
|
||||
local b, e = a:read("a/b/c/deep.txt")
|
||||
t:check(b == nil and e and e.code == Err.CHECKSUM, "data CRC: " .. tostring(e))
|
||||
local _, e2 = decode(string.sub(s, 1, #s - 3) .. "\0\0\0")
|
||||
t:check(e2 and (e2.code == Err.CHECKSUM or e2.code == Err.CORRUPT), "header CRC: " .. tostring(e2))
|
||||
local _, e3 = decode(string.sub(s, 1, 20))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
||||
local a4 = decode(bad, { checkCrc = false })
|
||||
t:check(a4 and a4:read("a/b/c/deep.txt"), "checkCrc = false")
|
||||
end
|
||||
|
||||
-- through the api
|
||||
do
|
||||
local c = Fx.cases[2]
|
||||
local a, err = F.load(c.data)
|
||||
t:check(a and a.format == "7z", "F.load: " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "7z", "F.detect")
|
||||
local r = a:read("readme.txt")
|
||||
t:check(r and buffer.tostring(r) == want["readme.txt"].data, "F.load read")
|
||||
end
|
||||
|
||||
-- fuzz: decode + read every entry
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.data < 60000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = SevenZip.decode(Bytes.reader(b), { maxBytes = 2 ^ 21 })
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 12, garbage = 300 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,142 @@
|
||||
-- codecs/Lzma tests: every fixture (liblzma through Python's lzma module and the xz CLI: presets 0-9 and -e, lc / lp /
|
||||
-- pb variants, raw LZMA1 / LZMA2, .lzma files, xz with every check type, BCJ and delta filters, multiple blocks,
|
||||
-- multiple streams and stream padding) decodes to its corpus; consumed offsets; checksum / size / truncation errors;
|
||||
-- maxBytes; the fuzz no-throw pass. Fixtures: tools/fixtures/lzma.py.
|
||||
local T = require("@tests/T")
|
||||
local Lzma = require("@formats/codecs/Lzma")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/lzma")
|
||||
|
||||
local t = T.suite("lzma")
|
||||
|
||||
local function decode(c: any, b: buffer, opts: any?): (buffer?, any)
|
||||
if c.kind == "raw" then
|
||||
return Lzma.lzma(b, 0, nil, c.props, c.size, opts)
|
||||
elseif c.kind == "raw2" then
|
||||
return Lzma.lzma2(b, 0, nil, nil, nil, opts)
|
||||
elseif c.kind == "alone" then
|
||||
return Lzma.alone(b, 0, nil, opts)
|
||||
end
|
||||
return Lzma.xz(b, 0, nil, opts)
|
||||
end
|
||||
|
||||
local nbytes = 0
|
||||
for _, c in Fx.cases do
|
||||
local want = Fx.corpora[c.corpus]
|
||||
local b = buffer.fromstring(c.data)
|
||||
local out, res = decode(c, b)
|
||||
t:check(out ~= nil, c.name .. ": " .. tostring(res))
|
||||
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
|
||||
if c.kind ~= "raw" then
|
||||
t:eq(res, #c.data, c.name .. ": consumed")
|
||||
end
|
||||
nbytes += #want
|
||||
end
|
||||
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
|
||||
|
||||
-- at an offset inside a bigger buffer, with trailing bytes
|
||||
do
|
||||
for _, c in Fx.cases do
|
||||
if c.kind == "xz" and c.name == "text/xz-6" then
|
||||
local big = buffer.create(#c.data + 11)
|
||||
buffer.fill(big, 0, 0xAB)
|
||||
buffer.writestring(big, 3, c.data)
|
||||
local out, res = Lzma.xz(big, 3, #c.data)
|
||||
t:check(out and buffer.tostring(out) == Fx.corpora.text and res == 3 + #c.data, "xz at an offset")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local xz, xzc
|
||||
for _, c in Fx.cases do
|
||||
if c.name == "text/xz-check-crc32" then
|
||||
xz = c
|
||||
elseif c.name == "text/xz-6" then
|
||||
xzc = c
|
||||
end
|
||||
end
|
||||
-- the check field: the last bytes before the index; flip a data bit near the end of the block instead
|
||||
local b = buffer.fromstring(xzc.data)
|
||||
local n = buffer.len(b)
|
||||
-- footer magic
|
||||
buffer.writeu8(b, n - 1, 0)
|
||||
local _, e1 = Lzma.xz(b)
|
||||
t:check(e1 and (e1.code == Err.CORRUPT or e1.code == Err.CHECKSUM), "bad footer: " .. tostring(e1))
|
||||
-- the stream header CRC
|
||||
local b2 = buffer.fromstring(xz.data)
|
||||
buffer.writeu8(b2, 8, bit32.bxor(buffer.readu8(b2, 8), 1))
|
||||
local _, e2 = Lzma.xz(b2)
|
||||
t:check(e2 and e2.code == Err.CHECKSUM, "header crc: " .. tostring(e2))
|
||||
-- the block check (CRC-32 / CRC-64): flip a byte of the check field (found from the index: the 4 / 8 bytes before
|
||||
-- the index indicator, which follows the block padding)
|
||||
for _, c in { xz, xzc } do
|
||||
local b3 = buffer.fromstring(c.data)
|
||||
local n3 = buffer.len(b3)
|
||||
local backward = (buffer.readu32(b3, n3 - 8) + 1) * 4
|
||||
local indexAt = n3 - 12 - backward
|
||||
buffer.writeu8(b3, indexAt - 1, bit32.bxor(buffer.readu8(b3, indexAt - 1), 0x10))
|
||||
local _, e3 = Lzma.xz(b3)
|
||||
t:check(e3 and e3.code == Err.CHECKSUM, c.name .. ": block check: " .. tostring(e3))
|
||||
end
|
||||
-- truncations are TRUNCATED or CORRUPT, never a throw
|
||||
local _, e4 = Lzma.xz(buffer.fromstring(string.sub(xzc.data, 1, #xzc.data // 2)))
|
||||
t:check(e4 and e4.code == Err.TRUNCATED, "truncated xz: " .. tostring(e4))
|
||||
-- maxBytes
|
||||
local _, e5 = Lzma.xz(xzc.data, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e5 and e5.code == Err.LIMIT, "maxBytes: " .. tostring(e5))
|
||||
local ok, r1, r2 = pcall(Lzma.xz, 5 :: any)
|
||||
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
||||
local _, e6 = Lzma.lzma("\0\0\0\0\0", 0, nil, "\255")
|
||||
t:check(e6 and e6.code == Err.CORRUPT, "bad props byte: " .. tostring(e6))
|
||||
end
|
||||
|
||||
-- raw LZMA with the size known: decoding stops at the size even without an end marker
|
||||
do
|
||||
for _, c in Fx.cases do
|
||||
if c.kind == "raw" and #Fx.corpora[c.corpus] > 1000 then
|
||||
local want = Fx.corpora[c.corpus]
|
||||
local out = Lzma.lzma(c.data, 0, nil, { lc = 3, lp = 0, pb = 2 }, nil)
|
||||
if c.name == c.corpus .. "/raw-lc3-lp0-pb2" then
|
||||
t:check(out and buffer.tostring(out) == want, c.name .. ": table props, end marker")
|
||||
end
|
||||
local part = Lzma.lzma(c.data, 0, nil, c.props, 500)
|
||||
t:check(part and buffer.tostring(part) == string.sub(want, 1, 500), c.name .. ": a prefix by size")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 7 == 0 and #c.data > 16 and #c.data < 40000 then
|
||||
table.insert(samples, c)
|
||||
end
|
||||
end
|
||||
local xs, as, rs = {}, {}, {}
|
||||
for _, c in samples do
|
||||
if c.kind == "xz" then
|
||||
table.insert(xs, c.data)
|
||||
elseif c.kind == "alone" then
|
||||
table.insert(as, c.data)
|
||||
else
|
||||
table.insert(rs, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("xz", function(b: buffer)
|
||||
return Lzma.xz(b, 0, nil, { maxBytes = 1 * 2 ^ 20 })
|
||||
end, xs, { mutations = 6, garbage = 300 })
|
||||
t:fuzz("alone", function(b: buffer)
|
||||
return Lzma.alone(b, 0, nil, { maxBytes = 1 * 2 ^ 20 })
|
||||
end, as, { mutations = 6, garbage = 300 })
|
||||
t:fuzz("lzma2", function(b: buffer)
|
||||
return Lzma.lzma2(b, 0, nil, nil, nil, { maxBytes = 1 * 2 ^ 20 })
|
||||
end, rs, { mutations = 4, garbage = 1000 })
|
||||
t:fuzz("lzma", function(b: buffer)
|
||||
return Lzma.lzma(b, 0, nil, "\93", nil, { maxBytes = 1 * 2 ^ 20 })
|
||||
end, rs, { mutations = 2, garbage = 1000 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user