ws-game: archive/grp (Build engine GRP read + write)
This commit is contained in:
1 parent
5af4aa361e
commit
eac069f7dc
5 files changed
+227
No files matched your search
@@ -0,0 +1,64 @@
|
||||
-- archive/grp tests: Build GRP archives written by tools/fixtures/grp.py list the entries an independent Python
|
||||
-- reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer and round-trips;
|
||||
-- conversion from a zip; errors; the fuzz pass. Fixtures: tools/fixtures/grp.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Grp = require("@formats/archive/grp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/grp")
|
||||
|
||||
local t = T.suite("grp")
|
||||
t:eq(Grp.name, "grp", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local a, err = Grp.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(F.detect(c.data), "grp", c.name .. ": F.detect")
|
||||
local n = Grp.new()
|
||||
for _, f in c.files do
|
||||
n:add(f.name, f.data)
|
||||
end
|
||||
t:check(buffer.tostring(n:write() :: buffer) == c.data, c.name .. ": bytes identical to the Python writer")
|
||||
t:check(buffer.tostring(F.save(a) :: buffer) == c.data, c.name .. ": decode + F.save is lossless")
|
||||
end
|
||||
|
||||
do
|
||||
local zip = F.format("zip").create()
|
||||
zip:add("readme.txt", "hi")
|
||||
local g = F.load(F.save(zip, nil, { format = "grp", upper = true }) :: buffer)
|
||||
t:check(g.format == "grp" and g:list()[1].name == "README.TXT", "zip -> grp, upper-cased")
|
||||
local c = Fx[1]
|
||||
local _, e1 = Grp.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 1))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Grp.decode(Bytes.reader(buffer.fromstring("KenSilverman\255\255\255\255")))
|
||||
t:check(e2 and e2.code == Err.LIMIT, "count limit: " .. tostring(e2))
|
||||
local long = Grp.new()
|
||||
long:add("THIRTEEN.CHAR", "x")
|
||||
local _, e3 = long:write()
|
||||
t:check(e3 and e3.code == Err.ARG, "a 13-byte name does not fit")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Grp.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 40 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user