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:
1 parent
945abe6d94
commit
a9110bdab2
9 files changed
+1298
No files matched your search
@@ -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()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,165 @@
|
||||
-- containers/Tar and containers/Archive tests: hand-made headers for the edge cases the reference archives do not hit
|
||||
-- (base-256 numbers, the historical signed checksum, GNU 'K' long links, pax 'g' global records, pax size overrides,
|
||||
-- v7 headers without magic, a missing end-of-archive marker), the writer's GNU / pax extensions (long links, fractional
|
||||
-- and negative mtimes), and Archive helpers (streamName, entries of foreign archive values, failing loaders).
|
||||
local T = require("@tests/T")
|
||||
local Tar = require("@formats/containers/Tar")
|
||||
local Archive = require("@formats/containers/Archive")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("containers/tar")
|
||||
|
||||
-- a header block built by hand
|
||||
local function hdr(fields: { [string]: any }): buffer
|
||||
local h = buffer.create(512)
|
||||
buffer.writestring(h, 0, fields.name or "f")
|
||||
buffer.writestring(h, 100, fields.mode or "0000644\0")
|
||||
buffer.writestring(h, 124, fields.size or "00000000000\0")
|
||||
buffer.writestring(h, 136, fields.mtime or "00000000000\0")
|
||||
buffer.writestring(h, 156, fields.type or "0")
|
||||
if fields.link then
|
||||
buffer.writestring(h, 157, fields.link)
|
||||
end
|
||||
if fields.magic ~= false then
|
||||
buffer.writestring(h, 257, "ustar\00000")
|
||||
end
|
||||
if fields.raw then
|
||||
for at, v in fields.raw do
|
||||
buffer.writeu8(h, at, v)
|
||||
end
|
||||
end
|
||||
buffer.writestring(h, 148, " ")
|
||||
local sum, signed = 0, 0
|
||||
for i = 0, 511 do
|
||||
local v = buffer.readu8(h, i)
|
||||
sum += v
|
||||
signed += if v >= 128 then v - 256 else v
|
||||
end
|
||||
buffer.writestring(h, 148, string.format("%06o\0 ", if fields.signed then signed % 0x40000 else sum))
|
||||
return h
|
||||
end
|
||||
local function cat(parts: { any }): buffer
|
||||
local s = {}
|
||||
for _, p in parts do
|
||||
table.insert(s, if type(p) == "string" then p else buffer.tostring(p))
|
||||
end
|
||||
return buffer.fromstring(table.concat(s))
|
||||
end
|
||||
local function block(s: string): string
|
||||
return s .. string.rep("\0", (512 - #s % 512) % 512)
|
||||
end
|
||||
local END = string.rep("\0", 1024)
|
||||
|
||||
-- base-256 size (GNU), checksum with the signed sum, no end marker
|
||||
do
|
||||
local raw = { [124] = 0x80 }
|
||||
for i = 125, 134 do
|
||||
raw[i] = 0
|
||||
end
|
||||
raw[135] = 5
|
||||
local b = cat({ hdr({ name = "big\200", raw = raw, signed = true }), block("hello") })
|
||||
local list, err = Tar.read(b)
|
||||
t:check(list, "base-256 / signed checksum: " .. tostring(err))
|
||||
t:eq(list[1].size, 5, "base-256 size")
|
||||
t:check(Tar.valid(b, 0), "valid() accepts the signed sum")
|
||||
end
|
||||
|
||||
-- GNU long link ('K'), pax global + per-file records, v7 header
|
||||
do
|
||||
local rec = "21 path=from/pax.txt\n"
|
||||
local glob = "20 uname=globaluser\n"
|
||||
local b = cat({
|
||||
hdr({ name = "././@LongLink", type = "K", size = string.format("%011o\0", 131) }),
|
||||
block(string.rep("L", 130) .. "\0"),
|
||||
hdr({ name = "sym", type = "2", link = "short" }),
|
||||
hdr({ name = "pax_global", type = "g", size = string.format("%011o\0", #glob) }),
|
||||
block(glob),
|
||||
hdr({ name = "PaxHeaders/x", type = "x", size = string.format("%011o\0", #rec + 15) }),
|
||||
block(rec .. "15 size=000003\n"),
|
||||
hdr({ name = "ignored-name", size = "00000000077\0" }),
|
||||
block("abc"),
|
||||
hdr({ name = "v7file", magic = false, size = "00000000002\0", mtime = "12345670123\0" }),
|
||||
block("v7"),
|
||||
END,
|
||||
})
|
||||
local list, err = Tar.read(b)
|
||||
t:check(list, "extensions: " .. tostring(err))
|
||||
t:eq(#list, 3, "meta records are not entries")
|
||||
t:eq(list[1].link, string.rep("L", 130), "GNU long link")
|
||||
t:eq(list[2].name, "from/pax.txt", "pax path")
|
||||
t:eq(list[2].size, 3, "pax size overrides the header")
|
||||
t:eq(list[2].uname, "globaluser", "pax global record")
|
||||
t:eq(list[3].name, "v7file", "v7 header")
|
||||
t:eq(list[3].mtime, tonumber("12345670123", 8), "octal mtime")
|
||||
t:eq(list[3].uname, "globaluser", "global records apply to later entries")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Tar.read(cat({ hdr({ size = "00000002000\0" }), block("x") }))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "data past the end: " .. tostring(e1))
|
||||
local _, e2 = Tar.read(cat({ hdr({ type = "x", size = "00000000010\0" }), block("garbage!"), END }))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad pax record: " .. tostring(e2))
|
||||
local good = hdr({})
|
||||
local bad = cat({ good, hdr({ name = "two" }) })
|
||||
buffer.writeu8(bad, 513, 77)
|
||||
local _, e3 = Tar.read(bad)
|
||||
t:check(e3 and e3.code == Err.CHECKSUM, "second header checksum: " .. tostring(e3))
|
||||
local list = Tar.read(buffer.create(0))
|
||||
t:check(list and #list == 0, "empty input is an empty archive")
|
||||
end
|
||||
|
||||
-- writer extensions
|
||||
do
|
||||
local long = string.rep("n", 120)
|
||||
for _, fmt in { "gnu", "pax" } do
|
||||
local out = Tar.write({
|
||||
{ name = long, data = buffer.fromstring("x"), mtime = -5.25 },
|
||||
{ name = "l", type = "2", link = string.rep("k", 150) },
|
||||
}, { format = fmt :: any }) :: buffer
|
||||
local list = Tar.read(out) :: any
|
||||
t:eq(list[1].name, long, fmt .. ": long name")
|
||||
t:eq(list[2].link, string.rep("k", 150), fmt .. ": long link")
|
||||
if fmt == "pax" then
|
||||
t:eq(list[1].mtime, -5.25, "pax fractional negative mtime")
|
||||
end
|
||||
end
|
||||
local _, e = Tar.write({ { name = "" } })
|
||||
t:check(e and e.code == Err.ARG, "empty name")
|
||||
end
|
||||
|
||||
-- Archive helpers
|
||||
do
|
||||
t:eq(Archive.streamName({ name = "/x/y/notes.txt.gz" }, { "gz" }), "notes.txt", "streamName strips the extension")
|
||||
t:eq(Archive.streamName({ name = "a.tbz2" }, { "bz2" }), "a.tar", "streamName .tbz2")
|
||||
t:eq(Archive.streamName(nil, { "gz" }), "data", "streamName default")
|
||||
t:eq(Archive.streamName({ name = "plain" }, { "gz" }), "plain", "streamName without an extension")
|
||||
local foreign = {
|
||||
kind = "archive",
|
||||
list = function()
|
||||
return { { name = "a", size = 1, dir = false } }
|
||||
end,
|
||||
read = function()
|
||||
error("boom")
|
||||
end,
|
||||
}
|
||||
local items = Archive.entries(foreign) :: any
|
||||
t:eq(#items, 1, "entries of a foreign archive")
|
||||
local b, e = items[1].read()
|
||||
t:check(b == nil and e and e.code == Err.CORRUPT, "a throwing read() becomes an Err")
|
||||
t:check(Archive.entries({ kind = "image" }) == nil, "not an archive")
|
||||
local a = Archive.new("x")
|
||||
Archive.put(a, { name = "f", size = 1, dir = false, load = function()
|
||||
error("bad")
|
||||
end })
|
||||
local _, e2 = a:read("f")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "a throwing loader becomes an Err")
|
||||
local _, e3 = a:write()
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "read-only archives")
|
||||
end
|
||||
|
||||
t:fuzz("read", function(b: buffer)
|
||||
return Tar.read(b)
|
||||
end, { buffer.tostring(Tar.write({ { name = string.rep("p", 130), data = buffer.fromstring("abc") }, { name = "d", dir = true } }) :: buffer) }, { mutations = 60 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user