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,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