157 lines
5.6 KiB
Luau
157 lines
5.6 KiB
Luau
-- archive/ar tests: GNU ar (deterministic and with times, a library with its "/" symbol table), llvm-ar BSD (#1/ names,
|
|
-- __.SYMDEF) and bsdtar arbsd archives list the input files (names, sizes, mtimes, data CRCs) and symbols; the GNU writer
|
|
-- gives GNU ar rcSD's bytes for the same files; bsdtar and dpkg-deb archives re-encode to their own bytes; .deb packages
|
|
-- from dpkg-deb (xz / gzip / zstd / none) and older bzip2 / lzma members give meta.control and load through the api as
|
|
-- their data tar; errors; the fuzz pass. Fixtures: tools/fixtures/ar.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Ar = require("@formats/archive/ar")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Crc32 = require("@formats/core/Crc32")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/ar")
|
|
|
|
local t = T.suite("ar")
|
|
t:eq(Ar.name, "ar", "name")
|
|
|
|
local function decode(s: string): (any, any)
|
|
return Ar.decode(Bytes.reader(buffer.fromstring(s)))
|
|
end
|
|
|
|
local function check(a: any, want: any, label: string)
|
|
local list = a:list()
|
|
t:eq(#list, #want, label .. ": entry count")
|
|
for i, w in want do
|
|
local e = list[i]
|
|
t:eq(e.name, w.name, label .. ": entry " .. i .. " name")
|
|
t:eq(e.size, w.size, label .. ": " .. w.name .. " size")
|
|
if w.mtime then
|
|
t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime")
|
|
end
|
|
if w.crc then
|
|
local b = a:read(w.name)
|
|
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
|
|
end
|
|
if w.type then
|
|
t:eq(e.type, w.type, label .. ": " .. w.name .. " type")
|
|
end
|
|
end
|
|
end
|
|
|
|
local function sortedSymbols(list: { any }): string
|
|
local s = {}
|
|
for _, x in list do
|
|
table.insert(s, x.name .. "@" .. tostring(x.member))
|
|
end
|
|
table.sort(s)
|
|
return table.concat(s, ",")
|
|
end
|
|
|
|
-- 1. reading
|
|
for _, c in Fx.archives do
|
|
local a, err = decode(c.data)
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
check(a, c.entries, c.name)
|
|
t:eq(a.meta.source.variant, c.variant, c.name .. ": variant")
|
|
t:eq(F.detect(c.data), "ar", c.name .. ": F.detect")
|
|
if c.symbols then
|
|
t:check(a.meta.symbols, c.name .. ": symbols")
|
|
t:eq(sortedSymbols(a.meta.symbols), sortedSymbols(c.symbols), c.name .. ": symbol names and members")
|
|
end
|
|
if c.roundtrip then
|
|
local out = assert(a:write())
|
|
local o = buffer.tostring(out)
|
|
t:check(string.sub(c.data, 1, #o) == o and not string.find(string.sub(c.data, #o + 1), "[^%z]"), c.name .. ": re-encode gives the same bytes (bsdtar pads with zeros)")
|
|
end
|
|
end
|
|
|
|
-- 2. writing: GNU ar rcSD's bytes; BSD names read back; any Archive to ar
|
|
do
|
|
local a = Ar.new()
|
|
for _, f in Fx.files do
|
|
a:add(f.name, f.data)
|
|
end
|
|
local out = assert(a:write())
|
|
t:check(buffer.tostring(out) == Fx.gnuD, "gnu: bytes identical to GNU ar rcSD")
|
|
local bsd = assert(a:write({ variant = "bsd" }))
|
|
local back = Ar.decode(Bytes.reader(bsd))
|
|
t:eq(back.meta.source.variant, "bsd", "bsd: variant")
|
|
t:eq(#back:list(), #Fx.files, "bsd: count")
|
|
for _, f in Fx.files do
|
|
t:eq(buffer.tostring(back:read(f.name)), f.data, "bsd: round trip " .. f.name)
|
|
end
|
|
local tar = F.format("tar").new()
|
|
tar:add("dir/", "", { dir = true })
|
|
tar:add("dir/file.txt", "in a dir", { mtime = 5 })
|
|
local conv = F.load(assert(F.save(tar, nil, { format = "ar" })))
|
|
t:eq(#conv:list(), 1, "tar -> ar: directories dropped")
|
|
t:eq(conv:list()[1].name, "dir/file.txt", "tar -> ar: path name in the long-name table")
|
|
t:eq(conv:list()[1].mtime, 5, "tar -> ar: mtime")
|
|
end
|
|
|
|
-- 3. Debian packages
|
|
for _, d in Fx.debs do
|
|
local a, err = decode(d.data)
|
|
t:check(a, d.name .. ": " .. tostring(err))
|
|
t:eq(a.meta.source.variant, "deb", d.name .. ": variant")
|
|
t:eq(a.meta.debian, "2.0", d.name .. ": debian-binary")
|
|
t:eq(a.meta.payload, d.payload, d.name .. ": payload")
|
|
t:check(a.meta.control, d.name .. ": control fields")
|
|
for k, v in d.control do
|
|
t:eq(a.meta.control[k], v, d.name .. ": control " .. k)
|
|
end
|
|
local out = assert(a:write())
|
|
t:check(buffer.tostring(out) == d.data, d.name .. ": re-encode gives the same bytes")
|
|
local data, lerr = F.load(d.data, { name = "hello.deb" })
|
|
t:check(data, d.name .. ": F.load: " .. tostring(lerr))
|
|
t:eq(data.format, "tar", d.name .. ": loads as the data tar")
|
|
t:eq(data.meta.container, "ar", d.name .. ": meta.container")
|
|
t:eq(data.meta.outer.meta.control.Package, "hello", d.name .. ": meta.outer control")
|
|
check(data, d.entries, d.name .. " data")
|
|
local outer = F.load(d.data, { unwrap = false })
|
|
t:eq(outer.format, "ar", d.name .. ": unwrap = false keeps the ar")
|
|
end
|
|
|
|
-- 4. errors
|
|
do
|
|
local _, e1 = decode("!<thin>\n")
|
|
t:check(e1 and e1.code == Err.UNSUPPORTED, "thin archive: " .. tostring(e1))
|
|
local good = Fx.archives[1].data
|
|
local b = buffer.fromstring(good)
|
|
buffer.writeu8(b, 8 + 58, 0x41)
|
|
local _, e2 = Ar.decode(Bytes.reader(b))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad member header: " .. tostring(e2))
|
|
local _, e3 = decode(string.sub(good, 1, 100))
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
|
local _, e4 = decode("!<arch>\n" .. string.format("%-16s%-12d%-6d%-6d%-8s%-10d`\n", "/99", 0, 0, 0, "644", 0))
|
|
t:check(e4 and e4.code == Err.CORRUPT, "long name without a table: " .. tostring(e4))
|
|
local _, e5 = Ar.encode({ kind = "image" })
|
|
t:check(e5 and e5.code == Err.ARG, "encode a non-archive")
|
|
local _, e6 = decode("not an archive")
|
|
t:check(e6 and e6.code == Err.CORRUPT, "bad magic")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx.archives do
|
|
if #c.data < 8000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
for _, d in Fx.debs do
|
|
if #d.data < 8000 then
|
|
table.insert(samples, d.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local a = Ar.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 = 8 })
|
|
|
|
t:done()
|