ws-arch: merge ws/arch-lzma

This commit is contained in:
4DBug committed 2026-10-31 08:38:55 -05:00
commit f6ce1da736
16 files changed
+2912

No files matched your search

+115
View File
@@ -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()
+78
View File
@@ -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()