97 lines
3.4 KiB
Luau
97 lines
3.4 KiB
Luau
-- archive/rpm tests against rpm -qp and rpm2cpio (tools/fixtures/rpm.py).
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Rpm = require("@rosetta/archive/rpm")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/rpm")
|
|
|
|
local t = T.suite("rpm")
|
|
t:eq(Rpm.name, "rpm", "name")
|
|
|
|
local TYPES = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2" }
|
|
|
|
for _, c in Fx.rpms do
|
|
local a, err = Rpm.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
t:eq(F.detect(c.data), "rpm", c.name .. ": F.detect")
|
|
for k, v in c.fields do
|
|
t:eq(a.meta.rpm[k], v, c.name .. ": " .. k)
|
|
end
|
|
t:eq(a.meta.rpm.type, "binary", c.name .. ": binary package")
|
|
local by = {}
|
|
for _, e in a:list() do
|
|
by[e.name] = e
|
|
end
|
|
local n = 0
|
|
for _, w in c.files do
|
|
local e = by[w.name]
|
|
if w.flags % 128 >= 64 then
|
|
t:check(e == nil, c.name .. ": ghost " .. w.name .. " not listed")
|
|
continue
|
|
end
|
|
n += 1
|
|
t:check(e, c.name .. ": has " .. w.name)
|
|
t:eq(e.type, TYPES[w.type], c.name .. ": " .. w.name .. " type")
|
|
t:eq(e.mode, w.mode, c.name .. ": " .. w.name .. " mode")
|
|
t:eq(e.mtime, w.mtime, c.name .. ": " .. w.name .. " mtime")
|
|
t:eq(e.link, w.link, c.name .. ": " .. w.name .. " link")
|
|
if w.crc then
|
|
t:eq(e.size, w.size, c.name .. ": " .. w.name .. " size")
|
|
local b, rerr = a:read(w.name)
|
|
t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr))
|
|
t:eq(Crc32.of(b), w.crc, c.name .. ": " .. w.name .. " data")
|
|
end
|
|
end
|
|
t:eq(#a:list(), n, c.name .. ": entry count")
|
|
t:eq(buffer.tostring(a:read("usr/bin/hello")), "#!/bin/sh\necho hello\n", c.name .. ": hard link (first)")
|
|
t:eq(buffer.tostring(a:read("usr/bin/hello2")), "#!/bin/sh\necho hello\n", c.name .. ": hard link (second)")
|
|
local raw = Rpm.cpio(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(raw and buffer.len(raw) == c.cpioSize and Crc32.of(raw) == c.cpioCrc, c.name .. ": Rpm.cpio = rpm2cpio")
|
|
t:note("%-16s %s %d files", c.name, tostring(a.meta.rpm.payloadCompressor), n)
|
|
end
|
|
|
|
-- the api: an rpm loads as itself (no payload unwrap), converts to tar
|
|
do
|
|
local c = Fx.rpms[1]
|
|
local a = assert(F.load(c.data))
|
|
t:eq(a.format, "rpm", "F.load")
|
|
local tar = F.load(assert(F.save(a, nil, { format = "tar" })))
|
|
t:eq(buffer.tostring(tar:read("usr/bin/hello")), "#!/bin/sh\necho hello\n", "rpm -> tar")
|
|
local _, e = F.save(a)
|
|
t:check(e, "rpm is read-only")
|
|
end
|
|
|
|
do
|
|
local good = Fx.rpms[1].data
|
|
local _, e1 = Rpm.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 50))))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "short lead: " .. tostring(e1))
|
|
local b = buffer.fromstring(good)
|
|
buffer.writeu8(b, 96, 0)
|
|
local _, e2 = Rpm.decode(Bytes.reader(b))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad signature magic: " .. tostring(e2))
|
|
local cut = Rpm.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, #good - 200))))
|
|
t:check(cut, "a cut payload still lists")
|
|
local _, e3 = cut:read("usr/share/doc/hello/README")
|
|
t:check(e3 ~= nil, "a cut payload fails on read: " .. tostring(e3))
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx.rpms do
|
|
if #c.data < 40000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local a = Rpm.decode(Bytes.reader(b))
|
|
if a then
|
|
for _, e in a:list() do
|
|
a:read(e.name)
|
|
end
|
|
end
|
|
return a
|
|
end, samples, { mutations = 25, keepHeader = 4 })
|
|
|
|
t:done()
|