72 lines
2.3 KiB
Luau
72 lines
2.3 KiB
Luau
-- archive/adf tests against amitools' xdftool (tools/fixtures/adf.py, images gzip-compressed).
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Adf = require("@rosetta/archive/adf")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
local E = require("@tests/archive/_entries")
|
|
local Fx = require("@tests/gen/adf")
|
|
|
|
local t = T.suite("adf")
|
|
t:eq(Adf.name, "adf", "name")
|
|
|
|
local first: buffer? = nil
|
|
for _, c in Fx.cases do
|
|
local gz = buffer.fromstring(c.gz)
|
|
local data = Deflate.gunzip(gz, 0, buffer.len(gz)) :: buffer
|
|
first = first or data
|
|
local a, err = Adf.decode(Bytes.reader(data))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
if a then
|
|
E.check(t, a, c.entries, c.name)
|
|
t:eq(a.meta.volume, c.volume, c.name .. ": volume")
|
|
t:eq(a.meta.fs, c.fs, c.name .. ": fs")
|
|
t:eq(a._byName.readme.protect, 3, c.name .. ": protect rw (the D and E bits set: active low)")
|
|
end
|
|
t:eq(F.detect(data), "adf", c.name .. ": detect")
|
|
end
|
|
|
|
local img = first :: buffer
|
|
local function code(b: buffer): string?
|
|
local _, e = Adf.decode(Bytes.reader(b))
|
|
return e and e.code
|
|
end
|
|
local bad = buffer.create(buffer.len(img))
|
|
buffer.copy(bad, 0, img)
|
|
buffer.writeu8(bad, 880 * 512 + 433, 0x7A) -- the volume name: root checksum fails
|
|
t:eq(code(bad), Err.CHECKSUM, "root checksum")
|
|
t:eq(code(buffer.create(1000)), Err.CORRUPT, "not blocks")
|
|
t:eq(code(buffer.create(4096)), Err.CORRUPT, "no boot block")
|
|
local dos6 = buffer.create(4096)
|
|
buffer.writestring(dos6, 0, "DOS\6")
|
|
t:eq(code(dos6), Err.UNSUPPORTED, "long-name file system")
|
|
|
|
-- fuzz: mutations concentrated on the root and directory blocks (a whole image is mostly free blocks)
|
|
local rand = T.rng(7)
|
|
local threw = 0
|
|
for i = 1, 300 do
|
|
local m = buffer.create(buffer.len(img))
|
|
buffer.copy(m, 0, img)
|
|
for _ = 1, 1 + rand(4) do
|
|
local blk = if rand(2) == 0 then 880 else rand(1760)
|
|
buffer.writeu8(m, blk * 512 + rand(512), rand(256))
|
|
end
|
|
local ok = pcall(function()
|
|
local a = Adf.decode(Bytes.reader(m))
|
|
if a then
|
|
for _, e in a:list() do
|
|
a:read(e.name)
|
|
end
|
|
end
|
|
end)
|
|
if not ok then
|
|
threw += 1
|
|
end
|
|
end
|
|
t:eq(threw, 0, "mutated images never throw")
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Adf.decode(Bytes.reader(b))
|
|
end, { buffer.tostring(img) }, { mutations = 20, garbage = 20 })
|
|
t:done()
|