88 lines
3.3 KiB
Luau
88 lines
3.3 KiB
Luau
-- archive/pak tests: Quake ("PACK") and SiN ("SPAK") archives written by tools/fixtures/pak.py list the entries an
|
|
-- independent Python reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer
|
|
-- for the same files and round-trips; conversion from a tar; errors; the fuzz pass. Fixtures: tools/fixtures/pak.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Pak = require("@rosetta/archive/pak")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/pak")
|
|
|
|
local t = T.suite("pak")
|
|
t:eq(Pak.name, "pak", "name")
|
|
t:check(Pak.caps.decode and Pak.caps.encode, "caps")
|
|
|
|
for _, c in Fx do
|
|
local a, err = Pak.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
local list = a:list()
|
|
t:eq(#list, #c.entries, c.name .. ": entry count")
|
|
for i, w in c.entries do
|
|
t:eq(list[i].name, w.name, c.name .. ": name " .. i)
|
|
t:eq(list[i].size, w.size, c.name .. ": size " .. w.name)
|
|
t:eq(Crc32.of((a:read(w.name))), w.crc, c.name .. ": data " .. w.name)
|
|
end
|
|
t:eq(a.meta.source.variant, c.variant, c.name .. ": variant")
|
|
if #c.data > 12 then
|
|
t:eq(F.detect(c.data), "pak", c.name .. ": F.detect")
|
|
end
|
|
-- the writer: same layout as the Python writer
|
|
local n = Pak.new(c.variant)
|
|
for _, f in c.files do
|
|
n:add(f.name, f.data)
|
|
end
|
|
local out = n:write() :: buffer
|
|
if c.layoutDefault then
|
|
t:check(buffer.tostring(out) == c.data, c.name .. ": bytes identical to the Python writer")
|
|
end
|
|
local back = Pak.decode(Bytes.reader(out))
|
|
t:eq(#back:list(), #c.files, c.name .. ": round trip count")
|
|
-- rewriting a decoded archive keeps its variant
|
|
local again = F.save(a) :: buffer
|
|
t:eq(buffer.readstring(again, 0, 4), if c.variant == "sin" then "SPAK" else "PACK", c.name .. ": F.save keeps the variant")
|
|
end
|
|
|
|
-- a sub-range reader, conversion from another archive, errors
|
|
do
|
|
local c = Fx[1]
|
|
local big = buffer.create(#c.data + 7)
|
|
buffer.writestring(big, 7, c.data)
|
|
local a = Pak.decode(Bytes.reader(big, 7, #c.data))
|
|
t:check(a and Crc32.of((a:read(c.entries[2].name))) == c.entries[2].crc, "decode from a sub-range")
|
|
local tar = F.format("tar").new()
|
|
tar:add("dir/", "", { dir = true })
|
|
tar:add("dir/x.txt", "x")
|
|
local p = F.load(F.save(tar, nil, { format = "pak" }) :: buffer)
|
|
t:check(p.format == "pak" and #p:list() == 1 and buffer.tostring(p:read("dir/x.txt")) == "x", "tar -> pak (directories dropped)")
|
|
local _, e1 = Pak.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 10))))
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
|
local bad = buffer.fromstring(c.data)
|
|
buffer.writeu32(bad, 8, 65)
|
|
local _, e2 = Pak.decode(Bytes.reader(bad))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "directory size: " .. tostring(e2))
|
|
local long = Pak.new()
|
|
long:add(string.rep("n", 56), "x")
|
|
local _, e3 = long:write()
|
|
t:check(e3 and e3.code == Err.ARG, "a 56-byte name does not fit")
|
|
local _, e4 = Pak.encode({ kind = "image" })
|
|
t:check(e4 and e4.code == Err.ARG, "encode a non-archive")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local a = Pak.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 = 40 })
|
|
|
|
t:done()
|