ws-arch: containers/Tar + archive/tar (ustar / pax / GNU read + write, byte-identical to Python's tarfile) and archive/gzip (read + write, .tar.gz writing); tests incl. .tar.gz/.tgz/.tar.bz2/.tar.xz/.tar.zst/.tar.br unwrap through the api

This commit is contained in:
4DBug committed 2026-10-31 08:54:37 -05:00
1 parent 945abe6d94
commit a9110bdab2
9 files changed
+1298

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
-- archive/gzip tests: members from Python's gzip and the gzip CLI (FNAME, MTIME, levels, concatenated members) decode
-- to one entry with the right name / mtime / data; the writer round-trips (buffer, string, one-entry archive, several
-- entries as a .tar.gz) and keeps FNAME / MTIME; the api; errors; the fuzz pass. Fixtures: tools/fixtures/tar.py.
local T = require("@tests/T")
local F = require("@formats")
local Gzip = require("@formats/archive/gzip")
local Deflate = require("@formats/codecs/Deflate")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/tar")
local t = T.suite("gzip")
t:eq(Gzip.name, "gzip", "name")
for _, c in Fx.gzips do
local a, err = Gzip.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
t:check(a.meta.stream, c.name .. ": single stream")
local l = a:list()
t:eq(#l, 1, c.name .. ": one entry")
t:eq(l[1].name, c.entry, c.name .. ": entry name")
t:eq(l[1].mtime, if c.mtime > 0 then c.mtime else nil, c.name .. ": mtime")
t:eq(l[1].size, c.size, c.name .. ": size")
t:eq(Crc32.of((a:read(l[1].name))), c.crc, c.name .. ": data")
t:eq(F.detect(c.data), "gzip", c.name .. ": F.detect")
end
-- the file name names the entry when there is no FNAME
do
local a = F.load(Fx.gzips[2].data, { name = "dir/notes.txt.gz" })
t:eq(a:list()[1].name, "notes.txt", "entry named after the file")
local b = F.load(Fx.gzips[2].data, { name = "x.tgz" })
t:eq(b:list()[1].name, "x.tar", ".tgz entry is a .tar")
end
-- writing
do
local text = string.rep("gzip round trip ", 500)
for _, level in { 0, 1, 6, 9 } do
local z = Gzip.encode(text, { level = level }) :: buffer
t:eq(buffer.tostring((Deflate.gunzip(z))), text, "string level " .. level)
end
local one = Gzip.new(text, "notes.txt")
one._byName["notes.txt"].mtime = 1600000000
local z = one:write()
local back = Gzip.decode(Bytes.reader(z :: buffer))
t:eq(back:list()[1].name, "notes.txt", "FNAME written")
t:eq(back:list()[1].mtime, 1600000000, "MTIME written")
t:eq(buffer.tostring((back:read("notes.txt"))), text, "one-entry archive round trip")
-- a decoded gzip saves back as gzip (its source format)
local g = F.load(Fx.gzips[1].data)
local again = F.load(F.save(g))
t:eq(again:list()[1].name, "story.txt", "F.save(gzip) keeps the name")
-- several entries: a .tar.gz, which the api unwraps
local multi = Gzip.new()
multi:add("a.txt", "A")
multi:add("b.txt", "BB")
local tgz = multi:write()
local tar = F.load(tgz)
t:eq(tar.format, "tar", "several entries write a .tar.gz")
t:eq(tar.meta.compression, "gzip", "unwrapped")
t:eq(buffer.tostring((tar:read("b.txt"))), "BB", ".tar.gz entry")
-- any archive converts: tar -> gzip -> tar
local tb = F.convert(Fx.tars[1].data, "gzip")
local t2 = F.load(tb)
t:eq(#t2:list(), #Fx.tars[1].entries, "tar -> tar.gz -> tar")
local _, e = Gzip.encode(text, { level = 12 })
t:check(e and e.code == Err.ARG, "bad level")
local _, e2 = Gzip.encode({ kind = "image" })
t:check(e2 and e2.code == Err.ARG, "not an archive")
end
-- errors
do
local d = Fx.gzips[1].data
local _, e1 = Gzip.decode(Bytes.reader(buffer.fromstring(string.sub(d, 1, 20))))
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
local b = buffer.fromstring(d)
buffer.writeu8(b, #d - 6, bit32.bxor(buffer.readu8(b, #d - 6), 1))
local _, e2 = Gzip.decode(Bytes.reader(b))
t:check(e2 and e2.code == Err.CHECKSUM, "crc: " .. tostring(e2))
end
local samples = {}
for _, c in Fx.gzips do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
return Gzip.decode(Bytes.reader(b))
end, samples, { mutations = 20 })
t:done()
+156
View File
@@ -0,0 +1,156 @@
-- archive/tar tests: archives from Python's tarfile (ustar / GNU / pax) and GNU tar (ustar / gnu / posix / v7) list
-- the same entries as tarfile reads (names, types, sizes, links, mtimes, modes, data CRCs); the writer produces the same
-- bytes as tarfile for the same entries in all three formats and round-trips; Archive values (add / read / write,
-- conversion of a gzip / any archive to tar); .tar.gz / .tgz / .tar.bz2 / .tar.xz / .tar.zst / .tar.br load through the
-- api as the tar inside; errors; the fuzz pass. Fixtures: tools/fixtures/tar.py.
local T = require("@tests/T")
local F = require("@formats")
local TarF = require("@formats/archive/tar")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/tar")
local t = T.suite("tar")
t:eq(TarF.name, "tar", "name")
local function check(a: any, want: any, label: string)
local list = a:list()
t:eq(#list, #want, label .. ": entry count")
local by = {}
for _, e in list do
by[e.name] = e
end
for _, w in want do
local e = by[w.name]
t:check(e, label .. ": has " .. w.name)
t:eq(e.type, w.type, label .. ": " .. w.name .. " type")
t:eq(e.size, w.size, label .. ": " .. w.name .. " size")
t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime")
t:eq(e.mode, w.mode, label .. ": " .. w.name .. " mode")
t:eq(e.link, w.link, label .. ": " .. w.name .. " link")
t:eq(e.dir, w.type == "5", label .. ": " .. w.name .. " dir")
if w.crc then
local b, err = a:read(w.name)
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
end
end
end
-- 1. reading
for _, c in Fx.tars do
local a, err = TarF.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
check(a, c.entries, c.name)
t:eq(F.detect(c.data), "tar", c.name .. ": F.detect")
t:note("%-18s %2d entries", c.name, #c.entries)
end
-- 2. writing: byte-identical to Python's tarfile (which pads to 10240-byte records)
for _, w in Fx.writes do
local list = {}
for _, e in w.entries do
table.insert(list, {
name = e.name,
data = if e.data then buffer.fromstring(e.data) else nil,
dir = e.dir,
type = e.type,
link = e.link,
mode = e.mode or (if e.dir then 0x1ED else 0x1A4),
mtime = 1700000000,
uname = "user",
gname = "group",
})
end
local a = TarF.new()
for _, e in list do
a:add(e.name, e.data or "", { dir = e.dir, mtime = e.mtime, mode = e.mode })
local ent = a._byName[e.name]
ent.type, ent.link, ent.uname, ent.gname = e.type, e.link, e.uname, e.gname
end
local out, err = a:write({ format = w.format })
t:check(out, w.format .. ": write: " .. tostring(err))
local s = buffer.tostring(out :: buffer)
local ref = w.data
t:check(#s <= #ref and string.sub(ref, 1, #s) == s and not string.find(string.sub(ref, #s + 1), "[^%z]"), w.format .. ": bytes identical to tarfile")
local back = TarF.decode(Bytes.reader(out :: buffer))
t:eq(#back:list(), #list, w.format .. ": round trip count")
for _, e in w.entries do
if e.data then
t:eq(buffer.tostring((back:read(e.name))), e.data, w.format .. ": round trip " .. e.name)
end
end
end
-- 3. Archive values: add / read / write, other archives to tar
do
local a = TarF.new()
a:add("a.txt", "alpha", { mtime = 5 })
a:add("b/", "", { dir = true })
a:add("b/c.bin", buffer.fromstring("\0\1\2"))
a:add("a.txt", "replaced")
t:eq(#a:list(), 3, "add replaces by name")
local buf = F.save(a)
t:check(buf, "F.save(tar archive) uses the source format")
local back = F.load(buf)
t:eq(buffer.tostring(back:read("a.txt")), "replaced", "read back")
t:check(back:list()[2].dir, "directory entry")
local _, e = back:read("missing")
t:check(e and e.code == Err.ARG, "missing entry")
-- a gzip (single-stream archive) converts to a tar holding its one entry
local g = F.load(Fx.gzips[1].data)
local tb = F.save(g, nil, { format = "tar" })
local ta = F.load(tb)
t:eq(ta:list()[1].name, "story.txt", "gzip -> tar entry name")
t:eq(ta:list()[1].size, Fx.gzips[1].size, "gzip -> tar entry size")
end
-- 4. compressed tars through the api
for _, w in Fx.wrapped do
local a, err = F.load(w.data, { name = w.name })
t:check(a, w.name .. ": " .. tostring(err))
t:eq(a.format, "tar", w.name .. ": unwrapped to the tar")
t:eq(a.meta.compression, w.outer, w.name .. ": meta.compression")
check(a, Fx.innerEntries, w.name)
local outer = F.load(w.data, { name = w.name, unwrap = false })
t:eq(outer.format, w.outer, w.name .. ": unwrap = false keeps the outer archive")
t:eq(outer:list()[1].name, "a.tar", w.name .. ": inner entry named after the file")
end
-- 5. errors
do
local good = Fx.tars[1].data
local b = buffer.fromstring(good)
buffer.writeu8(b, 10, buffer.readu8(b, 10) + 1)
local _, e1 = TarF.decode(Bytes.reader(b))
t:check(e1 and e1.code == Err.CORRUPT, "first header checksum: " .. tostring(e1))
local _, e2 = TarF.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 700))))
t:check(e2 and e2.code == Err.TRUNCATED, "truncated data: " .. tostring(e2))
local _, e3 = TarF.encode({ kind = "image" })
t:check(e3 and e3.code == Err.ARG, "encode a non-archive")
local u = TarF.new()
u:add(string.rep("y", 120), "x")
local _, e4 = u:write({ format = "ustar" })
t:check(e4 and e4.code == Err.ARG, "ustar cannot hold a 120-byte name without a slash")
t:eq(TarF.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 1024)))), 0, "zeros are not a tar")
end
local samples = {}
for _, c in Fx.tars do
if #c.data <= 30000 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
local a = TarF.decode(Bytes.reader(b))
if a then
for _, e in a:list() do
a:read(e.name)
end
a:write()
end
return a
end, samples, { mutations = 30, keepHeader = 0 })
t:done()