Files
rosetta/tests/archive/cpio.luau
T

155 lines
5.8 KiB
Luau

-- archive/cpio tests (and containers/Cpio) against GNU cpio and bsdtar (tools/fixtures/cpio.py).
local T = require("@tests/T")
local F = require("@rosetta")
local CpioF = require("@rosetta/archive/cpio")
local Cpio = require("@rosetta/containers/Cpio")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/cpio")
local t = T.suite("cpio")
t:eq(CpioF.name, "cpio", "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.mode, w.mode, label .. ": " .. w.name .. " mode")
t:eq(e.size, w.size, label .. ": " .. w.name .. " size")
t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime")
if w.type ~= "5" then
t:eq(e.nlink, w.nlink, label .. ": " .. w.name .. " nlink")
end
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
for _, c in Fx.archives do
local a, err = CpioF.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(a, c.name .. ": " .. tostring(err))
do
check(a, c.entries, c.name)
t:eq(a.meta.source.variant, c.format, c.name .. ": variant")
t:eq(F.detect(c.data), "cpio", c.name .. ": F.detect")
-- hard links: the later links of the hello.txt group name the first
local hl = 0
for _, e in a:list() do
if e.hardlink then
hl += 1
end
end
t:eq(hl, 2, c.name .. ": two later hard links")
end
end
for _, c in Fx.archives do
if c.gnu and c.format ~= "bin" then
local a = CpioF.decode(Bytes.reader(buffer.fromstring(c.data)))
local out, err = a:write({ format = c.format })
t:check(out, c.name .. ": write: " .. tostring(err))
t:check(out and buffer.tostring(out) == c.data, c.name .. ": bytes identical to GNU cpio")
end
end
do
local list, info = Cpio.read(buffer.fromstring(Fx.devices))
t:check(list and info.format == "newc", "devices: read")
t:eq(#list, 4, "devices: entries")
t:eq(list[2].type, "3", "char device")
t:eq(list[2].rdevmajor * 256 + list[2].rdevminor, 259, "null: rdev 1:3")
t:eq(list[3].type, "4", "block device")
t:eq(list[3].rdevmajor, 8, "sda: rdev major")
t:eq(list[4].type, "s", "socket")
local _, e1 = Cpio.read(buffer.fromstring(Fx.badcrc))
t:check(e1 and e1.code == Err.CHECKSUM, "crc: bad sum: " .. tostring(e1))
t:check(Cpio.read(buffer.fromstring(Fx.badcrc), 0, nil, { verify = false }), "crc: verify = false")
for _, fmt in { "newc", "odc" } do
local a = CpioF.decode(Bytes.reader(buffer.fromstring(Fx.devices)))
local back = CpioF.decode(Bytes.reader(assert(a:write({ format = fmt }))))
local d = back:list()[3]
t:check(d.type == "4" and d.rdevmajor == 8 and d.rdevminor == 0, fmt .. ": block device round trip")
end
end
do
local a, err = F.load(Fx.gz, { name = "tree.cpio.gz" })
t:check(a and a.format == "cpio" and a.meta.compression == "gzip", "cpio.gz unwraps: " .. tostring(err))
local c = CpioF.new()
c:add("bin/", "", { dir = true })
c:add("bin/run.sh", "#!/bin/sh\n", { mode = 493, mtime = 1767225600 })
local back = F.load(assert(F.save(c)))
t:eq(back.format, "cpio", "F.save(cpio) round trip")
t:eq(back:list()[2].mode, 493, "mode round trip")
t:eq(back:list()[2].mtime, 1767225600, "mtime round trip")
t:eq(buffer.tostring(back:read("bin/run.sh")), "#!/bin/sh\n", "data round trip")
-- cpio -> tar keeps symlinks; tar -> cpio turns tar hard links into a shared inode
local gnu = CpioF.decode(Bytes.reader(buffer.fromstring(Fx.archives[1].data)))
local tar = F.load(assert(F.save(gnu, nil, { format = "tar" })))
local sym
for _, e in tar:list() do
if e.name == "sym" then
sym = e
end
end
t:check(sym and sym.type == "2" and sym.link == "hello.txt", "cpio -> tar: symlink")
local tf = F.format("tar").new()
tf:add("a.txt", "shared")
tf:add("b.txt", "")
tf._byName["b.txt"].type, tf._byName["b.txt"].link = "1", "a.txt"
local cp = F.load(assert(F.save(tf, nil, { format = "cpio" })))
t:eq(buffer.tostring(cp:read("b.txt")), "shared", "tar hard link -> cpio: data")
t:eq(cp:list()[2].hardlink, "a.txt", "tar hard link -> cpio: hardlink")
t:eq(cp:list()[1].nlink, 2, "tar hard link -> cpio: nlink")
end
do
local good = Fx.archives[1].data
local _, e1 = CpioF.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 300))))
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
local b = buffer.fromstring(good)
buffer.writeu8(b, 10, string.byte("z"))
local _, e2 = CpioF.decode(Bytes.reader(b))
t:check(e2 and e2.code == Err.CORRUPT, "bad hex field: " .. tostring(e2))
local _, e3 = CpioF.encode({ kind = "image" })
t:check(e3 and e3.code == Err.ARG, "encode a non-archive")
local _, e4 = Cpio.write({ { name = "x", data = buffer.create(1), mtime = 2 ^ 40 } }, { format = "odc" })
t:check(e4 and e4.code == Err.ARG, "odc: mtime too large")
local _, e5 = CpioF.decode(Bytes.reader(buffer.fromstring("070701")))
t:check(e5 and e5.code == Err.TRUNCATED, "header past the end")
t:eq(CpioF.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 64)))), 0, "zeros are not cpio")
end
local samples = {}
for _, c in Fx.archives do
if #c.data <= 20000 then
table.insert(samples, c.data)
end
end
table.insert(samples, Fx.devices)
t:fuzz("decode", function(b: buffer)
local a = CpioF.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()