57 lines
1.8 KiB
Luau
57 lines
1.8 KiB
Luau
-- archive/zoo tests against The Unarchiver (tools/fixtures/zoo.py).
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Zoo = require("@rosetta/archive/zoo")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local E = require("@tests/archive/_entries")
|
|
local Fx = require("@tests/gen/zoo")
|
|
|
|
local t = T.suite("zoo")
|
|
t:eq(Zoo.name, "zoo", "name")
|
|
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
local a, err = Zoo.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
if a then
|
|
E.check(t, a, c.entries, c.name)
|
|
t:eq(a._byName["gone.txt"], nil, c.name .. ": deleted entry skipped")
|
|
end
|
|
t:eq(F.detect(c.data), "zoo", c.name .. ": detect")
|
|
if #c.data < 60000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
|
|
local data = Fx.cases[2].data -- lzw
|
|
local function code(s: string): string?
|
|
local _, e = Zoo.decode(Bytes.reader(buffer.fromstring(s)))
|
|
return e and e.code
|
|
end
|
|
t:eq(code(string.sub(data, 1, 20)), Err.TRUNCATED, "short header")
|
|
t:eq(code(string.rep("\0", 100)), Err.CORRUPT, "no tag")
|
|
t:eq(code(string.sub(data, 1, 200)), Err.TRUNCATED, "cut")
|
|
local mid = #data // 2
|
|
local bad = Zoo.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, mid - 1) .. string.char(bit32.bxor(string.byte(data, mid), 4)) .. string.sub(data, mid + 1)))) :: any
|
|
local failed = 0
|
|
for _, e in bad:list() do
|
|
local b, err = bad:read(e.name)
|
|
if not b then
|
|
failed += 1
|
|
t:check(err.code == Err.CHECKSUM or err.code == Err.CORRUPT or err.code == Err.TRUNCATED, "flipped data: " .. tostring(err))
|
|
end
|
|
end
|
|
t:eq(failed, 1, "one entry fails after a flipped data byte")
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
local r = Zoo.decode(Bytes.reader(b))
|
|
if r then
|
|
for _, e in r:list() do
|
|
r:read(e.name)
|
|
end
|
|
end
|
|
return r
|
|
end, samples, { mutations = 6 })
|
|
t:done()
|