ws-arch2: archive/binhex, archive/macbinary (I / II / III), archive/applesingle (AppleSingle / AppleDouble v1 / v2), read + write, over the new containers/MacFile (fork convention, Mac OS Roman names, 1904 dates)

This commit is contained in:
4DBug committed 2026-10-31 14:11:03 -05:00
1 parent fe44292f58
commit 3243291877
13 files changed
+1152 -4

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
-- shared by tests/archive/{binhex,macbinary,applesingle}.luau: run(name, module) checks the format's cases of
-- tools/fixtures/mac.py against The Unarchiver's listings (fork names, sizes, data CRCs, type / creator), our writer's
-- output read by unar (fixture step), round trips with a resource fork and Finder info, and the fuzz pass.
local T = require("@tests/T")
local F = require("@formats")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local E = require("@tests/archive/_entries")
local Fx = require("@tests/gen/mac")
return function(name: string, mod: any, encodeVariants: { any }): (any, { string })
local t = T.suite(name)
t:eq(mod.name, name, "name")
local samples = {}
local n = 0
for _, c in Fx.cases do
if c.format == name then
n += 1
local a, err = mod.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "x" })
t:check(a, c.name .. ": " .. tostring(err))
if a then
E.check(t, a, c.entries, c.name)
local by = {}
for _, e in a:list() do
by[e.name] = e
end
for _, w in c.entries do
local e = by[w.name]
if e and w.macType then
t:eq(e.macType, w.macType, c.name .. ": type")
t:eq(e.macCreator, w.macCreator, c.name .. ": creator")
end
end
end
t:eq(F.detect(c.data), name, c.name .. ": detect")
table.insert(samples, c.data)
end
end
t:check(n > 0, "has cases")
for _, o in Fx.ours do
if o.format == name then
t:check(o.ok, "unar reads our output (" .. o.variant .. "): " .. o.detail)
end
end
-- round trips: Finder info, dates, a resource fork
for _, opts in encodeVariants do
local a = mod.new()
a:add("Caf\u{e9}", "data fork", { mtime = 1700000000 })
a:add("Caf\u{e9}.rsrc", string.rep("R", 300), {})
local d = a._byName["Caf\u{e9}"]
d.macType, d.macCreator, d.finderFlags = "TEXT", "ttxt", 0x0100
a._byName["Caf\u{e9}.rsrc"].fork = "resource"
local out, err = a:write(opts)
t:check(out, "write: " .. tostring(err))
local back = out and mod.decode(Bytes.reader(out))
t:check(back, "round trip decodes")
if back then
local list = back:list()
local by = {}
for _, e in list do
by[e.name] = e
end
local rs = by["Caf\u{e9}.rsrc"]
t:check(rs and rs.size == 300 and rs.fork == "resource", "round trip resource fork")
t:eq(Crc32.of((back:read("Caf\u{e9}.rsrc"))), Crc32.string(string.rep("R", 300)), "round trip resource data")
local df = by["Caf\u{e9}"]
local fi = df or rs
t:check(fi and fi.macType == "TEXT" and fi.macCreator == "ttxt" and fi.finderFlags == 0x0100, "round trip Finder info")
if not (opts and opts.double) then
t:eq(buffer.tostring((back:read("Caf\u{e9}"))), "data fork", "round trip data fork")
if name ~= "binhex" then
t:eq(df and df.mtime, 1700000000, "round trip mtime")
end
end
end
end
return t, samples
end
+23
View File
@@ -0,0 +1,23 @@
-- archive/applesingle tests: the applesingle 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("@formats/archive/applesingle")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local run = require("@tests/archive/_mac")
local t, samples = run("applesingle", Mod, { {}, { double = true } })
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", { name = "h" }) :: buffer)
t:eq(code(string.sub(good, 1, 30)), Err.TRUNCATED, "entry table cut")
t:eq(code(string.sub(good, 1, #good - 2)), Err.TRUNCATED, "entry past the end")
t:eq(code("\0\5\22\0\0\3\0\0" .. string.rep("\0", 20)), Err.CORRUPT, "bad version")
-- the AppleDouble name comes from the file name without "._"
local d = Mod.decode(Bytes.reader(Mod.encode("x", { double = true, name = "" }) :: buffer), { name = "dir/._Report" }) :: any
t:check(d and d.meta.double, "appledouble flag")
t:fuzz("decode", function(b: buffer)
return Mod.decode(Bytes.reader(b))
end, samples)
t:done()
+26
View File
@@ -0,0 +1,26 @@
-- 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("@formats/archive/binhex")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/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()
+24
View File
@@ -0,0 +1,24 @@
-- archive/macbinary tests: the macbinary 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("@formats/archive/macbinary")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local run = require("@tests/archive/_mac")
local t, samples = run("macbinary", Mod, { {} })
local function code(s: string): string?
local _, e = Mod.decode(Bytes.reader(buffer.fromstring(s)))
return e and e.code
end
local good = Mod.encode("hello macbinary", { name = "h" }) :: buffer
local bad = buffer.create(buffer.len(good))
buffer.copy(bad, 0, good)
buffer.writeu8(bad, 70, 1)
t:eq(code(buffer.tostring(bad)), Err.CHECKSUM, "header CRC")
t:eq(code(string.sub(buffer.tostring(good), 1, 140)), Err.TRUNCATED, "short data fork")
t:eq(code(string.rep("\1", 200)), Err.CORRUPT, "not MacBinary")
t:eq(code("short"), Err.TRUNCATED, "short header")
t:fuzz("decode", function(b: buffer)
return Mod.decode(Bytes.reader(b))
end, samples, { keepHeader = 0 })
t:done()