Files
rosetta/tests/archive/fat.luau
T

85 lines
3.5 KiB
Luau

-- archive/fat tests (and containers/Fat): FAT12 (1.44 MB, 720 KB) / FAT16 / FAT32 images by mkfs.fat filled by mtools
-- (long names over several entries, lower-case 8.3, non-ASCII names, nested directories, empty and many-cluster files)
-- and a hard-disk image with an MBR partition list what mtools extracts (names, sizes, data CRCs, dates as UTC,
-- directories), FAT width and label; our floppies pass fsck.fat -n and extract with mtools (fixture step) and
-- round-trip; errors; the fuzz pass. Fixtures: tools/fixtures/fat.py (images gzip-compressed).
local T = require("@tests/T")
local F = require("@rosetta")
local FatF = require("@rosetta/archive/fat")
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/fat")
local t = T.suite("fat")
t:eq(FatF.name, "fat", "name")
local samples = {}
for _, c in Fx.cases do
local gz = buffer.fromstring(c.gz)
local data = Deflate.gunzip(gz, 0, buffer.len(gz)) :: buffer
local a, err = FatF.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.fat, c.bits, c.name .. ": bits")
t:eq(a.meta.label, c.label, c.name .. ": label")
end
t:eq(F.detect(data), "fat", c.name .. ": detect")
if c.name == "fat12 720K" then
table.insert(samples, data)
end
end
for _, o in Fx.ours do
t:check(o.ok, "mtools extracts our " .. o.size .. " KB floppy")
t:check(not string.find(o.fsck, "rror") and string.find(o.fsck, " files, ", 1, true) ~= nil and select(2, string.gsub(o.fsck, "\n", "")) == 2, "fsck.fat -n is clean: " .. o.fsck)
end
for _, size in { 1440, 720, 360, 2880 } do
E.roundTrip(t, FatF, { size = size }, "round trip " .. size)
end
-- long and short names, directories, dates
local a = FatF.new()
a:add("Some Long Name.html", "a", { mtime = 1700000000 })
a:add("dir/UPPER.TXT", "b", { mtime = 1700000002 })
a:add("dir/lower.txt", "c")
a:add("dir/Same Prefix 1.txt", "d")
a:add("dir/Same Prefix 2.txt", "e")
a:add("empty dir/", "", { dir = true })
local back = FatF.decode(Bytes.reader(a:write({ label = "Test" }) :: buffer)) :: any
t:check(back, "names round trip decodes")
for _, n in { "Some Long Name.html", "dir/UPPER.TXT", "dir/lower.txt", "dir/Same Prefix 1.txt", "dir/Same Prefix 2.txt", "empty dir/", "dir/" } do
t:check(back._byName[n], "names round trip: " .. n)
end
t:eq(back._byName["dir/UPPER.TXT"].mtime, 1700000002, "dos time")
t:eq(back._byName["dir/Same Prefix 2.txt"].shortName, "SAMEPR~2.TXT", "numeric tail")
t:eq(back.meta.label, "TEST", "label")
-- errors
local function code(b: buffer): string?
local _, e = FatF.decode(Bytes.reader(b))
return e and e.code
end
t:eq(code(buffer.create(100)), Err.TRUNCATED, "short")
t:eq(code(buffer.create(4096)), Err.CORRUPT, "no boot sector")
local big = FatF.new()
big:add("big", buffer.create(2000000))
t:eq((select(2, big:write()) :: any).code, Err.LIMIT, "disk full")
local clash = FatF.new()
clash:add("x", "file")
clash:add("x/y", "under a file")
t:eq((select(2, clash:write()) :: any).code, Err.ARG, "file and directory clash")
t:eq((select(2, FatF.encode(FatF.new(), { size = 999 })) :: any).code, Err.ARG, "bad size")
-- fuzz: the boot sector, FAT and directories are in the first 16 KB
t:fuzz("decode", function(b: buffer)
local r = FatF.decode(Bytes.reader(b))
if r then
for _, e in r:list() do
r:read(e.name)
end
end
return r
end, samples, { mutations = 200, garbage = 20, seed = 5 })
t:done()