27 lines
1.3 KiB
Luau
27 lines
1.3 KiB
Luau
-- archive/binhex tests: the binhex cases of tools/fixtures/mac.py against The Unarchiver's listings, our output read by unar,
|
|
-- round trips with a resource fork and Finder info (tests/archive/_mac.luau), errors, the fuzz pass.
|
|
local Mod = require("@rosetta/archive/binhex")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local run = require("@tests/archive/_mac")
|
|
|
|
local t, samples = run("binhex", Mod, { {} })
|
|
local function code(s: string): string?
|
|
local _, e = Mod.decode(Bytes.reader(buffer.fromstring(s)))
|
|
return e and e.code
|
|
end
|
|
local good = buffer.tostring(Mod.encode("hello binhex", { name = "h" }) :: buffer)
|
|
-- flip one data character (keeps the 6-bit alphabet): a CRC mismatch somewhere
|
|
local i = #good - 10
|
|
local c = string.sub(good, i, i)
|
|
local bad = string.sub(good, 1, i - 1) .. (if c == "!" then "\"" else "!") .. string.sub(good, i + 1)
|
|
t:eq(code(bad), Err.CHECKSUM, "flipped character")
|
|
t:eq(code(string.sub(good, 1, #good - 3)), Err.TRUNCATED, "no closing colon")
|
|
t:eq(code("nothing"), Err.CORRUPT, "no banner")
|
|
t:eq(code("(This file must be converted with BinHex 4.0)\n:~~~:"), Err.CORRUPT, "bad character")
|
|
t:eq((select(2, Mod.encode({})) :: any).code, Err.ARG, "encode a non-archive")
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Mod.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:done()
|