merge ws/arch2: cpio ar/deb rpm .Z lz4 iso lha arj zoo cab, uu/yenc/binhex/macbinary/applesingle, d64 adf fat, bdf pcf psf fon type1 eot hershey
This commit is contained in:
commit
b5cdd77a5b
115 files changed
+17373
-29
No files matched your search
@@ -0,0 +1,78 @@
|
||||
-- shared by the archive tests: check(t, archive, want, label) compares an Archive's entries with the reference listing
|
||||
-- want = { { name, size?, crc?, mode?, mtime?, dir?, link? } } (only the fields present are compared; every wanted
|
||||
-- entry must exist; crc is the CRC-32 of the data read back), and the files written by _arch2_ours.luau.
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local E = {}
|
||||
|
||||
function E.check(t: any, a: any, want: { any }, label: string, exact: boolean?)
|
||||
local list = a:list()
|
||||
if exact ~= false then
|
||||
t:eq(#list, #want, label .. ": entry count")
|
||||
end
|
||||
local by = {}
|
||||
for _, e in list do
|
||||
by[e.name] = e
|
||||
end
|
||||
for _, w in want do
|
||||
local e = by[w.name]
|
||||
t:check(e, label .. ": has " .. tostring(w.name))
|
||||
if e then
|
||||
for _, k in { "size", "mode", "mtime", "dir", "link" } do
|
||||
if w[k] ~= nil then
|
||||
t:eq(e[k], w[k], label .. ": " .. w.name .. " " .. k)
|
||||
end
|
||||
end
|
||||
if w.crc then
|
||||
local b, err = a:read(w.name)
|
||||
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
|
||||
if b then
|
||||
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the standard file set of tools/fixtures/_arch2_ours.luau / _arch2.py
|
||||
local function content(n: number, seed: number): buffer
|
||||
local b = buffer.create(n)
|
||||
local s = seed
|
||||
for i = 0, n - 1 do
|
||||
s = (s * 75 + 74) % 65537
|
||||
buffer.writeu8(b, i, if (i // 64) % 3 == 0 then (i // 7) % 256 else s % 256)
|
||||
end
|
||||
return b
|
||||
end
|
||||
E.content = content
|
||||
E.FILES = {
|
||||
{ name = "hello.txt", data = buffer.fromstring("hello, archive\n") },
|
||||
{ name = "data.bin", data = content(5000, 7) },
|
||||
{ name = "empty.txt", data = buffer.create(0) },
|
||||
}
|
||||
|
||||
-- roundTrip(t, mod, opts, label): add the standard files to mod.new(), write with opts, decode, compare
|
||||
function E.roundTrip(t: any, mod: any, opts: any?, label: string): buffer?
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local a = mod.new()
|
||||
for _, f in E.FILES do
|
||||
a:add(f.name, f.data, { mtime = 1700000000, mode = 0x1A4 })
|
||||
end
|
||||
local out, err = a:write(opts)
|
||||
t:check(out, label .. ": write: " .. tostring(err))
|
||||
if not out then
|
||||
return nil
|
||||
end
|
||||
local back, berr = mod.decode(Bytes.reader(out), {})
|
||||
t:check(back, label .. ": decode: " .. tostring(berr))
|
||||
if back then
|
||||
local want = {}
|
||||
for _, f in E.FILES do
|
||||
table.insert(want, { name = f.name, size = buffer.len(f.data), crc = Crc32.of(f.data) })
|
||||
end
|
||||
E.check(t, back, want, label, false)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return E
|
||||
@@ -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
|
||||
@@ -0,0 +1,75 @@
|
||||
-- archive/adf tests: DD / HD images formatted OFS, FFS, FFS international, OFS / FFS with directory cache by amitools'
|
||||
-- xdftool, holding nested directories, an empty file, a file with extension blocks and an ISO-8859-1 name, list the
|
||||
-- files xdftool reads back (names, sizes, data CRCs, directories, a set date), volume name and file system; errors
|
||||
-- (root block checksum, bad hash chains); the fuzz pass. Fixtures: tools/fixtures/adf.py (images gzip-compressed).
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Adf = require("@formats/archive/adf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Deflate = require("@formats/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
|
||||
|
||||
-- errors
|
||||
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()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,156 @@
|
||||
-- archive/ar tests: GNU ar (deterministic and with times, a library with its "/" symbol table), llvm-ar BSD (#1/ names,
|
||||
-- __.SYMDEF) and bsdtar arbsd archives list the input files (names, sizes, mtimes, data CRCs) and symbols; the GNU writer
|
||||
-- gives GNU ar rcSD's bytes for the same files; bsdtar and dpkg-deb archives re-encode to their own bytes; .deb packages
|
||||
-- from dpkg-deb (xz / gzip / zstd / none) and older bzip2 / lzma members give meta.control and load through the api as
|
||||
-- their data tar; errors; the fuzz pass. Fixtures: tools/fixtures/ar.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Ar = require("@formats/archive/ar")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/ar")
|
||||
|
||||
local t = T.suite("ar")
|
||||
t:eq(Ar.name, "ar", "name")
|
||||
|
||||
local function decode(s: string): (any, any)
|
||||
return Ar.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
end
|
||||
|
||||
local function check(a: any, want: any, label: string)
|
||||
local list = a:list()
|
||||
t:eq(#list, #want, label .. ": entry count")
|
||||
for i, w in want do
|
||||
local e = list[i]
|
||||
t:eq(e.name, w.name, label .. ": entry " .. i .. " name")
|
||||
t:eq(e.size, w.size, label .. ": " .. w.name .. " size")
|
||||
if w.mtime then
|
||||
t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime")
|
||||
end
|
||||
if w.crc then
|
||||
local b = a:read(w.name)
|
||||
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
|
||||
end
|
||||
if w.type then
|
||||
t:eq(e.type, w.type, label .. ": " .. w.name .. " type")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function sortedSymbols(list: { any }): string
|
||||
local s = {}
|
||||
for _, x in list do
|
||||
table.insert(s, x.name .. "@" .. tostring(x.member))
|
||||
end
|
||||
table.sort(s)
|
||||
return table.concat(s, ",")
|
||||
end
|
||||
|
||||
-- 1. reading
|
||||
for _, c in Fx.archives do
|
||||
local a, err = decode(c.data)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
check(a, c.entries, c.name)
|
||||
t:eq(a.meta.source.variant, c.variant, c.name .. ": variant")
|
||||
t:eq(F.detect(c.data), "ar", c.name .. ": F.detect")
|
||||
if c.symbols then
|
||||
t:check(a.meta.symbols, c.name .. ": symbols")
|
||||
t:eq(sortedSymbols(a.meta.symbols), sortedSymbols(c.symbols), c.name .. ": symbol names and members")
|
||||
end
|
||||
if c.roundtrip then
|
||||
local out = assert(a:write())
|
||||
local o = buffer.tostring(out)
|
||||
t:check(string.sub(c.data, 1, #o) == o and not string.find(string.sub(c.data, #o + 1), "[^%z]"), c.name .. ": re-encode gives the same bytes (bsdtar pads with zeros)")
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. writing: GNU ar rcSD's bytes; BSD names read back; any Archive to ar
|
||||
do
|
||||
local a = Ar.new()
|
||||
for _, f in Fx.files do
|
||||
a:add(f.name, f.data)
|
||||
end
|
||||
local out = assert(a:write())
|
||||
t:check(buffer.tostring(out) == Fx.gnuD, "gnu: bytes identical to GNU ar rcSD")
|
||||
local bsd = assert(a:write({ variant = "bsd" }))
|
||||
local back = Ar.decode(Bytes.reader(bsd))
|
||||
t:eq(back.meta.source.variant, "bsd", "bsd: variant")
|
||||
t:eq(#back:list(), #Fx.files, "bsd: count")
|
||||
for _, f in Fx.files do
|
||||
t:eq(buffer.tostring(back:read(f.name)), f.data, "bsd: round trip " .. f.name)
|
||||
end
|
||||
local tar = F.format("tar").new()
|
||||
tar:add("dir/", "", { dir = true })
|
||||
tar:add("dir/file.txt", "in a dir", { mtime = 5 })
|
||||
local conv = F.load(assert(F.save(tar, nil, { format = "ar" })))
|
||||
t:eq(#conv:list(), 1, "tar -> ar: directories dropped")
|
||||
t:eq(conv:list()[1].name, "dir/file.txt", "tar -> ar: path name in the long-name table")
|
||||
t:eq(conv:list()[1].mtime, 5, "tar -> ar: mtime")
|
||||
end
|
||||
|
||||
-- 3. Debian packages
|
||||
for _, d in Fx.debs do
|
||||
local a, err = decode(d.data)
|
||||
t:check(a, d.name .. ": " .. tostring(err))
|
||||
t:eq(a.meta.source.variant, "deb", d.name .. ": variant")
|
||||
t:eq(a.meta.debian, "2.0", d.name .. ": debian-binary")
|
||||
t:eq(a.meta.payload, d.payload, d.name .. ": payload")
|
||||
t:check(a.meta.control, d.name .. ": control fields")
|
||||
for k, v in d.control do
|
||||
t:eq(a.meta.control[k], v, d.name .. ": control " .. k)
|
||||
end
|
||||
local out = assert(a:write())
|
||||
t:check(buffer.tostring(out) == d.data, d.name .. ": re-encode gives the same bytes")
|
||||
local data, lerr = F.load(d.data, { name = "hello.deb" })
|
||||
t:check(data, d.name .. ": F.load: " .. tostring(lerr))
|
||||
t:eq(data.format, "tar", d.name .. ": loads as the data tar")
|
||||
t:eq(data.meta.container, "ar", d.name .. ": meta.container")
|
||||
t:eq(data.meta.outer.meta.control.Package, "hello", d.name .. ": meta.outer control")
|
||||
check(data, d.entries, d.name .. " data")
|
||||
local outer = F.load(d.data, { unwrap = false })
|
||||
t:eq(outer.format, "ar", d.name .. ": unwrap = false keeps the ar")
|
||||
end
|
||||
|
||||
-- 4. errors
|
||||
do
|
||||
local _, e1 = decode("!<thin>\n")
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "thin archive: " .. tostring(e1))
|
||||
local good = Fx.archives[1].data
|
||||
local b = buffer.fromstring(good)
|
||||
buffer.writeu8(b, 8 + 58, 0x41)
|
||||
local _, e2 = Ar.decode(Bytes.reader(b))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad member header: " .. tostring(e2))
|
||||
local _, e3 = decode(string.sub(good, 1, 100))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
||||
local _, e4 = decode("!<arch>\n" .. string.format("%-16s%-12d%-6d%-6d%-8s%-10d`\n", "/99", 0, 0, 0, "644", 0))
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "long name without a table: " .. tostring(e4))
|
||||
local _, e5 = Ar.encode({ kind = "image" })
|
||||
t:check(e5 and e5.code == Err.ARG, "encode a non-archive")
|
||||
local _, e6 = decode("not an archive")
|
||||
t:check(e6 and e6.code == Err.CORRUPT, "bad magic")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.archives do
|
||||
if #c.data < 8000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
for _, d in Fx.debs do
|
||||
if #d.data < 8000 then
|
||||
table.insert(samples, d.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Ar.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
a:write()
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 30, keepHeader = 8 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,58 @@
|
||||
-- archive/arj tests: archives by ARJ 3.10 with methods 0 .. 4 (stored, the LHA-style Huffman methods 1 .. 3, method
|
||||
-- 4) give the files `arj x` extracts (names, sizes, data CRCs); header and data CRC errors, truncation; the fuzz
|
||||
-- pass. Fixtures: tools/fixtures/arj.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Arj = require("@formats/archive/arj")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/arj")
|
||||
|
||||
local t = T.suite("arj")
|
||||
t:eq(Arj.name, "arj", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Arj.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, false)
|
||||
end
|
||||
t:eq(F.detect(c.data), "arj", c.name .. ": detect")
|
||||
table.insert(samples, string.sub(c.data, 1, 3000))
|
||||
end
|
||||
|
||||
-- errors
|
||||
local data = Fx.cases[2].data -- -m1
|
||||
local function code(s: string): string?
|
||||
local _, e = Arj.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
local hdr = string.sub(data, 1, 10) .. string.char((string.byte(data, 11) + 1) % 256) .. string.sub(data, 12)
|
||||
t:eq(code(hdr), Err.CHECKSUM, "main header CRC")
|
||||
t:eq(code(string.sub(data, 1, #data // 2)), Err.TRUNCATED, "cut")
|
||||
t:eq(code("\96\234\30\0"), Err.TRUNCATED, "short header")
|
||||
t:eq(code("PK\3\4xxxx"), Err.CORRUPT, "not arj")
|
||||
local mid = #data // 2 + 1000
|
||||
local bad = Arj.decode(Bytes.reader(buffer.fromstring(string.sub(data, 1, mid - 1) .. string.char(bit32.bxor(string.byte(data, mid), 0x10)) .. 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, "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 = Arj.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()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,166 @@
|
||||
-- archive/cab tests: cabinets by gcab (stored / MSZIP), lcab, a builder of MSZIP blocks that use the previous block as
|
||||
-- their dictionary (several folders, reserve areas, attributes, UTF-8 names) and libmspack's test cabinets give the
|
||||
-- files cabextract extracts (names, sizes, data CRCs, DOS times read as UTC); cabextract / gcab / 7z extract our
|
||||
-- writer's output (fixture step) and it round-trips; spanned files are UNSUPPORTED; checksum, truncation and
|
||||
-- signature errors, libmspack's broken cabinets; LZX (codecs/compress/Lzx) from a fixture compressor whose cabinets
|
||||
-- cabextract extracts (windows 2^15 .. 2^21, verbatim / aligned / uncompressed blocks, E8); Quantum
|
||||
-- (codecs/compress/Quantum) the same way (windows 2^10 .. 2^21, model rescaling); Deflate's preset
|
||||
-- dictionary; the fuzz pass. Fixtures: tools/fixtures/cab.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Cab = require("@formats/archive/cab")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/cab")
|
||||
|
||||
local t = T.suite("cab")
|
||||
t:eq(Cab.name, "cab", "name")
|
||||
|
||||
local function load(s: string, opts: any?): (any, Err.Err?)
|
||||
return Cab.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
local methods = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = load(c.data)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
if a then
|
||||
E.check(t, a, c.entries, c.name)
|
||||
for _, e in a:list() do
|
||||
methods[e.method] = true
|
||||
end
|
||||
end
|
||||
t:eq(F.detect(c.data), "cab", c.name .. ": detect")
|
||||
table.insert(samples, string.sub(c.data, 1, 4000))
|
||||
end
|
||||
t:check(methods.store and methods.mszip and methods.lzx and methods.quantum, "covers store, mszip, lzx and quantum")
|
||||
|
||||
-- attributes, UTF-8 names, reserve areas (the builder's two-folder cabinet)
|
||||
local two = assert(load(Fx.cases[5].data))
|
||||
local naive = two._byName["na\u{ef}ve \u{2603}.txt"]
|
||||
t:check(naive and naive.utf8 and naive.attributes == 0x20, "utf-8 name and attributes")
|
||||
t:eq(two.meta.source.reserve.data, 3, "data reserve")
|
||||
t:eq(buffer.tostring(two.meta.reserve), "HEADER-RESERVE", "header reserve")
|
||||
t:eq(#two.meta.folders, 2, "two folders")
|
||||
t:check(assert(load(Fx.cases[4].data))._byName["b.bin"].readonly, "read-only attribute")
|
||||
|
||||
-- our writer
|
||||
t:check(Fx.ours.ok, "reference tools extract our cabinets: " .. Fx.ours.log)
|
||||
E.roundTrip(t, Cab, nil, "round trip mszip")
|
||||
E.roundTrip(t, Cab, { compression = "none" }, "round trip store")
|
||||
local big = Cab.new()
|
||||
big:add("dir/big.bin", E.content(200000, 3), { mtime = 1700000000 })
|
||||
big:add("x.txt", "x")
|
||||
local bigData = assert(big:write())
|
||||
local back = assert(Cab.decode(Bytes.reader(bigData)))
|
||||
t:eq(buffer.tostring(assert(back:read("dir/big.bin"))), buffer.tostring(E.content(200000, 3)), "200 KB through 7 blocks")
|
||||
t:eq(back:list()[1].mtime, 1700000000, "mtime")
|
||||
t:eq(select(2, Cab.encode(big, { compression = "lzx" :: any })).code, Err.ARG, "bad compression")
|
||||
|
||||
-- spanned cabinets: files continued from / to another cabinet are UNSUPPORTED, the others read
|
||||
for _, c in Fx.spanned do
|
||||
local a, err = load(c.data)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
if a then
|
||||
local unsupported = 0
|
||||
for _, e in a:list() do
|
||||
local b, rerr = a:read(e.name)
|
||||
if not b then
|
||||
t:eq(rerr.code, Err.UNSUPPORTED, c.name .. ": " .. e.name)
|
||||
unsupported += 1
|
||||
end
|
||||
end
|
||||
t:check(unsupported > 0, c.name .. ": has spanned files")
|
||||
end
|
||||
end
|
||||
|
||||
-- libmspack's broken cabinets (cabextract fails on every one): an error (never a throw), not a full read
|
||||
for _, c in Fx.bad do
|
||||
local a = load(c.data)
|
||||
local ok = a ~= nil
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
if not a:read(e.name) then
|
||||
ok = false
|
||||
end
|
||||
end
|
||||
end
|
||||
-- a 263-byte file name: over the format's 256-byte limit, which libmspack enforces and we do not
|
||||
t:check(not ok or c.name == "cve-2017-11423-fname-overread.cab", c.name .. ": fails")
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
|
||||
-- LZX directly
|
||||
local Lzx = require("@formats/codecs/compress/Lzx")
|
||||
t:check(not Lzx.decode(buffer.create(10), 0, 10, 100, 14), "LZX window 2^14 unsupported")
|
||||
t:check(not Lzx.decode(buffer.create(10), 0, 10, 100, 16), "LZX of zeros")
|
||||
t:check(not Lzx.decode(buffer.create(10), 4, 10, 100, 16), "LZX range past the end")
|
||||
|
||||
-- Quantum directly
|
||||
local Quantum = require("@formats/codecs/compress/Quantum")
|
||||
t:check(not Quantum.decode(buffer.create(10), {}, 0, 9), "Quantum window 2^9 unsupported")
|
||||
t:check(not Quantum.decode(buffer.create(10), { { at = 0, len = 10, size = 5 } }, 6, 16), "Quantum sizes must add up")
|
||||
t:check(not Quantum.decode(buffer.create(10), { { at = 4, len = 10, size = 5 } }, 5, 16), "Quantum range past the end")
|
||||
|
||||
-- Deflate's preset dictionary and MSZIP blocks
|
||||
local block1 = assert(Deflate.compress(string.rep("ab", 50)))
|
||||
local mszip = buffer.create(buffer.len(block1) + 2)
|
||||
buffer.writestring(mszip, 0, "CK")
|
||||
buffer.copy(mszip, 2, block1)
|
||||
t:eq(buffer.tostring(assert(Deflate.mszip(mszip, { { offset = 0, length = buffer.len(mszip), size = 100 } }))), string.rep("ab", 50), "Deflate.mszip")
|
||||
t:eq(select(2, Deflate.mszip(mszip, { { offset = 1, length = 4, size = 100 } })).code, Err.CORRUPT, "Deflate.mszip without CK")
|
||||
local dictText = string.rep("abcdefgh", 100)
|
||||
local z = assert(Deflate.compress(dictText .. dictText))
|
||||
local plain = assert(Deflate.inflate(z, nil, nil, 1600, buffer.fromstring("unused prefix")))
|
||||
t:eq(buffer.tostring(plain), dictText .. dictText, "inflate with an unused dictionary")
|
||||
|
||||
-- errors
|
||||
local data = Fx.cases[2].data -- gcab -z
|
||||
local function code(s: string): string?
|
||||
local a, e = load(s)
|
||||
if not a then
|
||||
return e and e.code
|
||||
end
|
||||
for _, en in a:list() do
|
||||
local b, rerr = a:read(en.name)
|
||||
if not b then
|
||||
return rerr and rerr.code
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
t:eq(code(data), nil, "intact")
|
||||
t:eq(code("MSCF\0\0\0\0" .. string.rep("\0", 10)), Err.TRUNCATED, "short header")
|
||||
t:eq(code("PK\3\4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), Err.CORRUPT, "not a cabinet")
|
||||
t:eq(code(string.sub(data, 1, #data - 100)), Err.TRUNCATED, "cut")
|
||||
local mid = #data - 200
|
||||
local flipped = string.sub(data, 1, mid - 1) .. string.char(bit32.bxor(string.byte(data, mid), 0x08)) .. string.sub(data, mid + 1)
|
||||
t:eq(code(flipped), Err.CHECKSUM, "flipped data byte")
|
||||
local a2 = assert(load(flipped, { verify = false }))
|
||||
local failed = 0
|
||||
for _, e in a2:list() do
|
||||
if not a2:read(e.name) then
|
||||
failed += 1
|
||||
end
|
||||
end
|
||||
t:note("flipped byte without verify: %d entries fail", failed)
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local r = Cab.decode(Bytes.reader(b))
|
||||
if r then
|
||||
for _, e in r:list() do
|
||||
r:read(e.name)
|
||||
end
|
||||
end
|
||||
local r2 = Cab.decode(Bytes.reader(b), { verify = false })
|
||||
if r2 then
|
||||
for _, e in r2:list() do
|
||||
r2:read(e.name)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end, samples, { mutations = 6 })
|
||||
t:done()
|
||||
@@ -0,0 +1,164 @@
|
||||
-- archive/cpio tests (and containers/Cpio): GNU cpio archives of one tree in newc / crc / odc / bin (and the bin one
|
||||
-- byte-swapped to big endian) and bsdtar newc / odc archives list the entries os.lstat sees (names, types, modes, sizes,
|
||||
-- mtimes, link counts, symlink targets, data CRCs, hard links reading the shared data); re-encoding the GNU newc / crc /
|
||||
-- odc archives gives GNU's bytes; devices (rdev fields), crc sums, a .cpio.gz through the api, conversions to and from
|
||||
-- tar (hard links share an inode), errors, the fuzz pass. Fixtures: tools/fixtures/cpio.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local CpioF = require("@formats/archive/cpio")
|
||||
local Cpio = require("@formats/containers/Cpio")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/cpio")
|
||||
|
||||
local t = T.suite("cpio")
|
||||
t:eq(CpioF.name, "cpio", "name")
|
||||
|
||||
local function check(a: any, want: any, label: string)
|
||||
local list = a:list()
|
||||
t:eq(#list, #want, label .. ": entry count")
|
||||
local by = {}
|
||||
for _, e in list do
|
||||
by[e.name] = e
|
||||
end
|
||||
for _, w in want do
|
||||
local e = by[w.name]
|
||||
t:check(e, label .. ": has " .. w.name)
|
||||
t:eq(e.type, w.type, label .. ": " .. w.name .. " type")
|
||||
t:eq(e.mode, w.mode, label .. ": " .. w.name .. " mode")
|
||||
t:eq(e.size, w.size, label .. ": " .. w.name .. " size")
|
||||
t:eq(e.mtime, w.mtime, label .. ": " .. w.name .. " mtime")
|
||||
if w.type ~= "5" then
|
||||
t:eq(e.nlink, w.nlink, label .. ": " .. w.name .. " nlink")
|
||||
end
|
||||
t:eq(e.link, w.link, label .. ": " .. w.name .. " link")
|
||||
t:eq(e.dir, w.type == "5", label .. ": " .. w.name .. " dir")
|
||||
if w.crc then
|
||||
local b, err = a:read(w.name)
|
||||
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
|
||||
t:eq(Crc32.of(b), w.crc, label .. ": " .. w.name .. " data")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 1. reading
|
||||
for _, c in Fx.archives do
|
||||
local a, err = CpioF.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
do
|
||||
check(a, c.entries, c.name)
|
||||
t:eq(a.meta.source.variant, c.format, c.name .. ": variant")
|
||||
t:eq(F.detect(c.data), "cpio", c.name .. ": F.detect")
|
||||
-- hard links: the later links of the hello.txt group name the first
|
||||
local hl = 0
|
||||
for _, e in a:list() do
|
||||
if e.hardlink then
|
||||
hl += 1
|
||||
end
|
||||
end
|
||||
t:eq(hl, 2, c.name .. ": two later hard links")
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. writing: GNU's bytes back from the decoded archive (newc / crc / odc)
|
||||
for _, c in Fx.archives do
|
||||
if c.gnu and c.format ~= "bin" then
|
||||
local a = CpioF.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
local out, err = a:write({ format = c.format })
|
||||
t:check(out, c.name .. ": write: " .. tostring(err))
|
||||
t:check(out and buffer.tostring(out) == c.data, c.name .. ": bytes identical to GNU cpio")
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. devices, sums, the container directly
|
||||
do
|
||||
local list, info = Cpio.read(buffer.fromstring(Fx.devices))
|
||||
t:check(list and info.format == "newc", "devices: read")
|
||||
t:eq(#list, 4, "devices: entries")
|
||||
t:eq(list[2].type, "3", "char device")
|
||||
t:eq(list[2].rdevmajor * 256 + list[2].rdevminor, 259, "null: rdev 1:3")
|
||||
t:eq(list[3].type, "4", "block device")
|
||||
t:eq(list[3].rdevmajor, 8, "sda: rdev major")
|
||||
t:eq(list[4].type, "s", "socket")
|
||||
local _, e1 = Cpio.read(buffer.fromstring(Fx.badcrc))
|
||||
t:check(e1 and e1.code == Err.CHECKSUM, "crc: bad sum: " .. tostring(e1))
|
||||
t:check(Cpio.read(buffer.fromstring(Fx.badcrc), 0, nil, { verify = false }), "crc: verify = false")
|
||||
-- devices survive a newc and an odc write
|
||||
for _, fmt in { "newc", "odc" } do
|
||||
local a = CpioF.decode(Bytes.reader(buffer.fromstring(Fx.devices)))
|
||||
local back = CpioF.decode(Bytes.reader(assert(a:write({ format = fmt }))))
|
||||
local d = back:list()[3]
|
||||
t:check(d.type == "4" and d.rdevmajor == 8 and d.rdevminor == 0, fmt .. ": block device round trip")
|
||||
end
|
||||
end
|
||||
|
||||
-- 4. the api: .cpio.gz unwraps, Archive values, conversions
|
||||
do
|
||||
local a, err = F.load(Fx.gz, { name = "tree.cpio.gz" })
|
||||
t:check(a and a.format == "cpio" and a.meta.compression == "gzip", "cpio.gz unwraps: " .. tostring(err))
|
||||
local c = CpioF.new()
|
||||
c:add("bin/", "", { dir = true })
|
||||
c:add("bin/run.sh", "#!/bin/sh\n", { mode = 493, mtime = 1767225600 })
|
||||
local back = F.load(assert(F.save(c)))
|
||||
t:eq(back.format, "cpio", "F.save(cpio) round trip")
|
||||
t:eq(back:list()[2].mode, 493, "mode round trip")
|
||||
t:eq(back:list()[2].mtime, 1767225600, "mtime round trip")
|
||||
t:eq(buffer.tostring(back:read("bin/run.sh")), "#!/bin/sh\n", "data round trip")
|
||||
-- cpio -> tar keeps symlinks; tar -> cpio turns tar hard links into a shared inode
|
||||
local gnu = CpioF.decode(Bytes.reader(buffer.fromstring(Fx.archives[1].data)))
|
||||
local tar = F.load(assert(F.save(gnu, nil, { format = "tar" })))
|
||||
local sym
|
||||
for _, e in tar:list() do
|
||||
if e.name == "sym" then
|
||||
sym = e
|
||||
end
|
||||
end
|
||||
t:check(sym and sym.type == "2" and sym.link == "hello.txt", "cpio -> tar: symlink")
|
||||
local tf = F.format("tar").new()
|
||||
tf:add("a.txt", "shared")
|
||||
tf:add("b.txt", "")
|
||||
tf._byName["b.txt"].type, tf._byName["b.txt"].link = "1", "a.txt"
|
||||
local cp = F.load(assert(F.save(tf, nil, { format = "cpio" })))
|
||||
t:eq(buffer.tostring(cp:read("b.txt")), "shared", "tar hard link -> cpio: data")
|
||||
t:eq(cp:list()[2].hardlink, "a.txt", "tar hard link -> cpio: hardlink")
|
||||
t:eq(cp:list()[1].nlink, 2, "tar hard link -> cpio: nlink")
|
||||
end
|
||||
|
||||
-- 5. errors
|
||||
do
|
||||
local good = Fx.archives[1].data
|
||||
local _, e1 = CpioF.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 300))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local b = buffer.fromstring(good)
|
||||
buffer.writeu8(b, 10, string.byte("z"))
|
||||
local _, e2 = CpioF.decode(Bytes.reader(b))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad hex field: " .. tostring(e2))
|
||||
local _, e3 = CpioF.encode({ kind = "image" })
|
||||
t:check(e3 and e3.code == Err.ARG, "encode a non-archive")
|
||||
local _, e4 = Cpio.write({ { name = "x", data = buffer.create(1), mtime = 2 ^ 40 } }, { format = "odc" })
|
||||
t:check(e4 and e4.code == Err.ARG, "odc: mtime too large")
|
||||
local _, e5 = CpioF.decode(Bytes.reader(buffer.fromstring("070701")))
|
||||
t:check(e5 and e5.code == Err.TRUNCATED, "header past the end")
|
||||
t:eq(CpioF.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 64)))), 0, "zeros are not cpio")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.archives do
|
||||
if #c.data <= 20000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
table.insert(samples, Fx.devices)
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = CpioF.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
a:write()
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 30, keepHeader = 0 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,75 @@
|
||||
-- archive/d64 tests: D64 / D71 / D81 images made by VICE's c1541 (PRG / SEQ / USR, multi-sector, a deleted file, an
|
||||
-- error-info block, 30 files over several directory sectors) list the files c1541 reads back (names, sizes, types,
|
||||
-- data CRCs), disk name and id; our D64 / D71 / D81 output is read by c1541 (fixture step) and round-trips; PETSCII
|
||||
-- names; errors; the fuzz pass. Fixtures: tools/fixtures/d64.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local D64 = require("@formats/archive/d64")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/d64")
|
||||
|
||||
local t = T.suite("d64")
|
||||
t:eq(D64.name, "d64", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = D64.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.meta.disk, c.disk, c.name .. ": disk name")
|
||||
t:eq(a.meta.id, c.id, c.name .. ": disk id")
|
||||
for _, w in c.entries do
|
||||
local e = a._byName[w.name]
|
||||
t:eq(e and e.type, w.type, c.name .. ": " .. w.name .. " type")
|
||||
end
|
||||
t:check(a._byName.gone == nil, c.name .. ": deleted file skipped")
|
||||
end
|
||||
t:eq(F.detect(c.data), "d64", c.name .. ": detect")
|
||||
if c.name == "d64" then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
for _, o in Fx.ours do
|
||||
t:check(o.ok, "c1541 reads our " .. o.variant .. ": " .. o.listing)
|
||||
end
|
||||
for _, v in { "d64", "d71", "d81" } do
|
||||
local out = E.roundTrip(t, D64, { variant = v, disk = "round trip", id = "rt" }, "round trip " .. v)
|
||||
if out then
|
||||
local a = D64.decode(Bytes.reader(out)) :: any
|
||||
t:eq(a.meta.variant, v, v .. ": variant")
|
||||
t:eq(a.meta.disk, "round trip", v .. ": disk name")
|
||||
end
|
||||
end
|
||||
-- PETSCII names: lower / upper case and other bytes survive a round trip
|
||||
local a = D64.new()
|
||||
a:add("Mixed Case 1", "x")
|
||||
a:add("\u{E0D3}hift", "y")
|
||||
local back = D64.decode(Bytes.reader(a:write() :: buffer)) :: any
|
||||
t:check(back._byName["Mixed Case 1"] and back._byName["\u{E0D3}hift"] == nil and back._byName["Shift"], "petscii names")
|
||||
|
||||
-- errors
|
||||
local function code(b: buffer): string?
|
||||
local _, e = D64.decode(Bytes.reader(b))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code(buffer.create(1000)), Err.CORRUPT, "bad size")
|
||||
local img = buffer.fromstring(Fx.cases[1].data)
|
||||
local dir = 357 * 256 -- track 18 sector 0
|
||||
buffer.writeu8(img, dir, 99)
|
||||
t:eq(code(img), Err.CORRUPT, "directory link off the disk")
|
||||
local many = D64.new()
|
||||
for i = 1, 145 do
|
||||
many:add("f" .. i, "x")
|
||||
end
|
||||
t:eq((select(2, many:write()) :: any).code, Err.LIMIT, "directory full")
|
||||
local big = D64.new()
|
||||
big:add("big", buffer.create(200000))
|
||||
t:eq((select(2, big:write()) :: any).code, Err.LIMIT, "disk full")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return D64.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 60, garbage = 20 })
|
||||
t:done()
|
||||
@@ -0,0 +1,84 @@
|
||||
-- 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("@formats")
|
||||
local FatF = require("@formats/archive/fat")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Deflate = require("@formats/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()
|
||||
@@ -0,0 +1,134 @@
|
||||
-- archive/iso tests (and containers/Iso9660): xorriso images (plain names, Joliet, Rock Ridge, Joliet + Rock Ridge with
|
||||
-- deep directories / a 200-byte name / a long symlink target) and a genisoimage -J -R image (rr_moved relocation, CE
|
||||
-- areas) list what the source tree holds (Rock Ridge: names, modes, symlink targets, mtimes, data CRCs) or what pycdlib
|
||||
-- reads (plain and Joliet names, sizes, data CRCs); prefer = "joliet" on a -J -R image; an El Torito boot catalog as
|
||||
-- xorriso reports it; a crafted multi-extent file reads whole; an interleaved file is UNSUPPORTED; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/iso.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Iso = require("@formats/archive/iso")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/iso")
|
||||
|
||||
local t = T.suite("iso")
|
||||
t:eq(Iso.name, "iso", "name")
|
||||
|
||||
local function dataOf(c: any): string
|
||||
return if c.same then Fx.images[c.same].data else c.data
|
||||
end
|
||||
|
||||
for _, c in Fx.images do
|
||||
local a, err = Iso.decode(Bytes.reader(buffer.fromstring(dataOf(c))), { prefer = c.tree :: any })
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:eq(a.meta.iso.tree, c.tree, c.name .. ": name set")
|
||||
t:eq(F.detect(dataOf(c)), "iso", c.name .. ": F.detect")
|
||||
local by = {}
|
||||
for _, e in a:list() do
|
||||
by[e.name] = e
|
||||
end
|
||||
t:eq(#a:list(), #c.entries, c.name .. ": entry count")
|
||||
for _, w in c.entries do
|
||||
local e = by[w.name]
|
||||
t:check(e, c.name .. ": has " .. w.name)
|
||||
t:eq(e.dir, w.dir, c.name .. ": " .. w.name .. " dir")
|
||||
if w.mode then
|
||||
t:eq(e.mode, w.mode, c.name .. ": " .. w.name .. " mode")
|
||||
end
|
||||
if w.link then
|
||||
t:eq(e.type, "2", c.name .. ": " .. w.name .. " symlink")
|
||||
t:eq(e.link, w.link, c.name .. ": " .. w.name .. " target")
|
||||
end
|
||||
if w.mtime and not w.dir then
|
||||
t:eq(e.mtime, w.mtime, c.name .. ": " .. w.name .. " mtime")
|
||||
end
|
||||
if w.crc then
|
||||
t:eq(e.size, w.size, c.name .. ": " .. w.name .. " size")
|
||||
local b, rerr = a:read(w.name)
|
||||
t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr))
|
||||
t:eq(Crc32.of(b), w.crc, c.name .. ": " .. w.name .. " data")
|
||||
end
|
||||
end
|
||||
t:note("%-24s %-9s %3d entries", c.name, c.tree, #c.entries)
|
||||
end
|
||||
|
||||
-- the default name set: Rock Ridge over Joliet; Joliet over plain
|
||||
do
|
||||
local jr = Iso.decode(Bytes.reader(buffer.fromstring(Fx.images[5].data)))
|
||||
t:eq(jr.meta.iso.tree, "rockridge", "-J -R: Rock Ridge by default")
|
||||
t:eq(jr.meta.iso.joliet, 3, "-J -R: Joliet level 3 seen")
|
||||
t:check(jr.meta.iso.rockRidge, "-J -R: rockRidge")
|
||||
local j = Iso.decode(Bytes.reader(buffer.fromstring(Fx.images[2].data)))
|
||||
t:eq(j.meta.iso.tree, "joliet", "-J: Joliet by default")
|
||||
local p = F.load(Fx.images[1].data)
|
||||
t:eq(p.format, "iso", "F.load")
|
||||
t:eq(p.meta.iso.tree, "iso9660", "plain")
|
||||
t:check(p.meta.iso.blockSize == 2048 and p.meta.iso.blocks > 0, "volume facts")
|
||||
-- any Archive converts: iso -> tar
|
||||
local tar = F.load(assert(F.save(jr, nil, { format = "tar" })))
|
||||
t:eq(buffer.tostring(tar:read("hello.txt")), "hello, iso\n", "iso -> tar")
|
||||
end
|
||||
|
||||
-- El Torito
|
||||
do
|
||||
local a = Iso.decode(Bytes.reader(buffer.fromstring(Fx.boot.data)))
|
||||
local bt = a.meta.boot
|
||||
t:check(bt, "boot catalog")
|
||||
t:eq(bt.catalog, Fx.boot.catalog, "boot: catalog LBA")
|
||||
t:eq(bt.lba, Fx.boot.lba, "boot: image LBA")
|
||||
t:eq(bt.sectors, Fx.boot.sectors, "boot: load size")
|
||||
t:eq(bt.bootable, Fx.boot.bootable, "boot: bootable")
|
||||
t:eq(bt.media, 0, "boot: no emulation")
|
||||
t:check(bt.valid, "boot: validation entry checksum")
|
||||
end
|
||||
|
||||
-- multi-extent and interleaved records
|
||||
do
|
||||
local a = Iso.decode(Bytes.reader(buffer.fromstring(Fx.multi.data)))
|
||||
local n = 0
|
||||
for _, e in a:list() do
|
||||
if e.name == Fx.multi.name then
|
||||
n += 1
|
||||
t:eq(e.size, Fx.multi.size, "multi-extent: size")
|
||||
end
|
||||
end
|
||||
t:eq(n, 1, "multi-extent: one entry")
|
||||
t:eq(Crc32.of(a:read(Fx.multi.name)), Fx.multi.crc, "multi-extent: data")
|
||||
local il = Iso.decode(Bytes.reader(buffer.fromstring(Fx.interleaved)))
|
||||
local _, e = il:read("HELLO.TXT")
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "interleaved: " .. tostring(e))
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local good = Fx.images[1].data
|
||||
local _, e1 = Iso.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 20000))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short: " .. tostring(e1))
|
||||
local b = buffer.fromstring(good)
|
||||
buffer.writeu8(b, 16 * 2048 + 1, 0x58)
|
||||
local _, e2 = Iso.decode(Bytes.reader(b))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad descriptor: " .. tostring(e2))
|
||||
local cut = Iso.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 20 * 2048))))
|
||||
t:check(cut == nil or cut, "a cut image: no throw")
|
||||
t:eq(Iso.sniff(Bytes.reader(buffer.fromstring(string.rep("\0", 40000)))), 0, "zeros are not an iso")
|
||||
end
|
||||
|
||||
-- fuzz: the volume descriptors and directories (the first 64 KB of a few images: their data lies further out)
|
||||
local samples = {}
|
||||
for i, c in Fx.images do
|
||||
if c.data and (i == 1 or i == 3 or i == 5) then
|
||||
table.insert(samples, string.sub(c.data, 1, 26 * 2048))
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Iso.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 40, keepHeader = 16 * 2048 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,86 @@
|
||||
-- archive/lha tests (and codecs/compress/Lzh): lhasa's test archives (LHarc lh1, LArc lzs / lz5 / lz4, LHA lh0 / lh5 /
|
||||
-- lh6 / lh7 by many DOS / Amiga / OS-9 / Unix / Windows tools, header levels 0 / 1 / 2, subdirectories, symlinks, lh1 and
|
||||
-- lz5 length tests up to 2 MB) and LHa for UNIX archives (-o5 / -o6 / -o7 / -z at levels 0 / 1 / 2) give the files lhasa
|
||||
-- extracts (names, sizes, data CRCs) and its symlinks; our level-2 lh0 output extracts with lhasa (fixture step) and
|
||||
-- round-trips; CRC and truncation errors; the fuzz pass. Fixtures: tools/fixtures/lha.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Lha = require("@formats/archive/lha")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/lha")
|
||||
|
||||
local t = T.suite("lha")
|
||||
t:eq(Lha.name, "lha", "name")
|
||||
|
||||
local samples = {}
|
||||
local methods = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Lha.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, false)
|
||||
for _, l in c.links do
|
||||
local e = a._byName[l.name]
|
||||
t:check(e and e.link == l.link, c.name .. ": symlink " .. l.name)
|
||||
end
|
||||
for _, e in a:list() do
|
||||
methods[e.method] = (methods[e.method] or 0) + 1
|
||||
end
|
||||
end
|
||||
if string.sub(c.data, 1, 2) ~= "MZ" then
|
||||
t:eq(F.detect(c.data), "lha", c.name .. ": detect")
|
||||
end
|
||||
if #c.data < 6000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
local seen = {}
|
||||
for m in methods do
|
||||
table.insert(seen, m)
|
||||
end
|
||||
table.sort(seen)
|
||||
t:note("methods: %s", table.concat(seen, " "))
|
||||
for _, m in { "lh0", "lh1", "lh5", "lh6", "lh7", "lzs", "lz5", "lz4", "lhd" } do
|
||||
t:check(methods[m], "covers " .. m)
|
||||
end
|
||||
t:check(Fx.ours.ok, "lhasa extracts our archive: " .. Fx.ours.listing)
|
||||
E.roundTrip(t, Lha, nil, "round trip")
|
||||
local a = Lha.new()
|
||||
a:add("dir/sub/file.txt", "nested", { mode = 0x1A4, mtime = 1700000000 })
|
||||
a:add("dir/", "", { dir = true, mode = 0x1ED })
|
||||
local back = Lha.decode(Bytes.reader(a:write() :: buffer)) :: any
|
||||
t:check(back._byName["dir/sub/file.txt"] and back._byName["dir/sub/file.txt"].mode == 0x81A4 and back._byName["dir/"], "directories and modes round trip")
|
||||
|
||||
-- errors: a flipped data byte in an lh5 entry fails its CRC (or decodes to a bad code), a cut archive is truncated
|
||||
local function first(pattern: string): any
|
||||
for _, c in Fx.cases do
|
||||
if string.find(c.name, pattern) then
|
||||
return c
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local c5 = first("lha114i %-h2 %-5")
|
||||
local bad = buffer.fromstring(c5.data)
|
||||
local ar = Lha.decode(Bytes.reader(bad)) :: any
|
||||
local ent = ar._byName["data.bin"]
|
||||
buffer.writeu8(bad, buffer.len(bad) // 2, bit32.bxor(buffer.readu8(bad, buffer.len(bad) // 2), 0x40)) -- inside dir/rand.bin
|
||||
local mut = Lha.decode(Bytes.reader(bad)) :: any
|
||||
local _, rerr = mut:read("dir/rand.bin")
|
||||
t:check(ent and rerr and (rerr.code == Err.CHECKSUM or rerr.code == Err.CORRUPT), "corrupt data: " .. tostring(rerr))
|
||||
local _, terr = Lha.decode(Bytes.reader(buffer.fromstring(string.sub(c5.data, 1, 200))))
|
||||
t:eq(terr and terr.code, Err.TRUNCATED, "cut archive")
|
||||
t:eq((select(2, Lha.decode(Bytes.reader(buffer.fromstring(string.rep("x", 100))))) :: any).code, Err.CORRUPT, "no header")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local r = Lha.decode(Bytes.reader(b))
|
||||
if r then
|
||||
for _, e in r:list() do
|
||||
r:read(e.name)
|
||||
end
|
||||
end
|
||||
return r
|
||||
end, samples, { mutations = 4, garbage = 50 })
|
||||
t:done()
|
||||
@@ -0,0 +1,119 @@
|
||||
-- archive/lz4 tests (and codecs/compress/Xxh32): frames from the lz4 tool (default, linked blocks, 64 KB blocks, block
|
||||
-- checksums, content size, no content checksum, HC, legacy), concatenated and skippable frames decode to their corpora;
|
||||
-- our frames (every option set) decode with `lz4 -d` (verdicts from the fixture step) and round-trip; XXH32 test
|
||||
-- vectors; checksum / size / truncation errors; the api (.tar.lz4 unwrap, naming); the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/lz4frame.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Lz4F = require("@formats/archive/lz4")
|
||||
local Xxh32 = require("@formats/codecs/compress/Xxh32")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/lz4frame")
|
||||
local C = require("@tests/gen/lz4frame_corpora")
|
||||
|
||||
local t = T.suite("lz4")
|
||||
t:eq(Lz4F.name, "lz4", "name")
|
||||
|
||||
-- XXH32 vectors (xxHash's sanity checks)
|
||||
t:eq(Xxh32.hash(buffer.fromstring("")), 0x02CC5D05, "xxh32 empty")
|
||||
t:eq(Xxh32.hash(buffer.fromstring(""), 0, 0, 0x9E3779B1), 0x36B78AE7, "xxh32 empty, seed prime")
|
||||
t:eq(Xxh32.hash(buffer.fromstring("a")), 0x550D7456, "xxh32 a")
|
||||
t:eq(Xxh32.hash(buffer.fromstring("abc")), 0x32D153FF, "xxh32 abc")
|
||||
t:eq(Xxh32.hash(buffer.fromstring("Nobody inspects the spammish repetition")), 0xE2293B2F, "xxh32 sentence")
|
||||
|
||||
local function want(c: any): string
|
||||
if c.corpus == "short+text" then
|
||||
return C.short .. C.text
|
||||
end
|
||||
return C[c.corpus]
|
||||
end
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local out, err = Lz4F.decompress(c.data)
|
||||
t:check(out, c.name .. ": " .. tostring(err))
|
||||
local w = want(c)
|
||||
t:eq(buffer.len(out), #w, c.name .. ": size")
|
||||
t:eq(Crc32.of(out), Crc32.of(buffer.fromstring(w)), c.name .. ": data")
|
||||
t:eq(F.detect(c.data), "lz4", c.name .. ": F.detect")
|
||||
end
|
||||
|
||||
-- our frames, read by lz4 -d
|
||||
t:check(#Fx.ours >= 15, "verdicts present")
|
||||
for _, v in Fx.ours do
|
||||
t:check(v.ok, "lz4 -d reads our " .. v.name)
|
||||
end
|
||||
for name, data in C do
|
||||
for _, o in { {}, { blockSize = 65536, blockChecksum = true, contentSize = true }, { contentChecksum = false } } do
|
||||
local z = assert(Lz4F.compress(data, o :: any))
|
||||
t:eq(buffer.tostring(assert(Lz4F.decompress(z))), data, "round trip " .. name)
|
||||
end
|
||||
end
|
||||
|
||||
-- the api
|
||||
do
|
||||
local a = assert(F.load(Fx.cases[1].data, { name = "notes.txt.lz4" }))
|
||||
t:check(a.meta.stream and a.format == "lz4", "single stream")
|
||||
t:eq(a:list()[1].name, "notes.txt", "entry named after the file")
|
||||
local tar = F.format("tar").new()
|
||||
tar:add("a.txt", "alpha")
|
||||
tar:add("b.txt", "beta")
|
||||
local tl = assert(F.save(tar, nil, { format = "lz4" }))
|
||||
local back = F.load(tl, { name = "x.tlz4" })
|
||||
t:eq(back.format, "tar", "several entries: a .tar.lz4 that unwraps")
|
||||
t:eq(back.meta.compression, "lz4", "meta.compression")
|
||||
t:eq(F.load(tl, { name = "x.tlz4", unwrap = false }):list()[1].name, "x.tar", ".tlz4 -> .tar")
|
||||
local s = assert(F.save("some text", nil, { format = "lz4" }))
|
||||
t:eq(buffer.tostring(F.load(s):read("data")), "some text", "a string")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local good
|
||||
for _, c in Fx.cases do
|
||||
if c.name == "text/BX" then
|
||||
good = c.data
|
||||
end
|
||||
end
|
||||
local function flip(at: number): buffer
|
||||
local b = buffer.fromstring(good)
|
||||
buffer.writeu8(b, at, bit32.bxor(buffer.readu8(b, at), 1))
|
||||
return b
|
||||
end
|
||||
local _, e1 = Lz4F.decompress(flip(6))
|
||||
t:check(e1 and e1.code == Err.CHECKSUM, "header checksum: " .. tostring(e1))
|
||||
local _, e2 = Lz4F.decompress(flip(20))
|
||||
t:check(e2 and e2.code == Err.CHECKSUM, "block checksum: " .. tostring(e2))
|
||||
local _, e3 = Lz4F.decompress(flip(#good - 2))
|
||||
t:check(e3 and e3.code == Err.CHECKSUM, "content checksum: " .. tostring(e3))
|
||||
t:check(Lz4F.decompress(flip(#good - 2), nil, nil, { verify = false }), "verify = false")
|
||||
local _, e4 = Lz4F.decompress(string.sub(good, 1, 1000))
|
||||
t:check(e4 and e4.code == Err.TRUNCATED, "truncated: " .. tostring(e4))
|
||||
local _, e5 = Lz4F.decompress(Fx.cases[1].data, nil, nil, { maxBytes = 10 })
|
||||
t:check(e5 == nil or e5.code == Err.LIMIT, "maxBytes")
|
||||
local big
|
||||
for _, c in Fx.cases do
|
||||
if c.name == "zeros/default" then
|
||||
big = c.data
|
||||
end
|
||||
end
|
||||
local _, e6 = Lz4F.decompress(big, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e6 and e6.code == Err.LIMIT, "maxBytes: " .. tostring(e6))
|
||||
local _, e7 = Lz4F.compress("x", { blockSize = 1000 })
|
||||
t:check(e7 and e7.code == Err.ARG, "bad blockSize")
|
||||
local _, e8 = Lz4F.decompress("not lz4 at all")
|
||||
t:check(e8 and e8.code == Err.CORRUPT, "bad magic")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.data < 6000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Lz4F.decode(Bytes.reader(b), { maxBytes = 2 ^ 22 })
|
||||
end, samples, { mutations = 40, keepHeader = 4 })
|
||||
|
||||
t:done()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,101 @@
|
||||
-- archive/rpm tests: one package from rpmbuild with each payload compressor (gzip, bzip2, xz, lzma, zstd, none) gives
|
||||
-- the header fields `rpm -qp --qf` prints, its file list (names, sizes, modes, mtimes, symlink targets; %ghost files
|
||||
-- skipped), the file data rpm2cpio's stream holds (CRCs, hard links), and Rpm.cpio = rpm2cpio's bytes; errors; the
|
||||
-- fuzz pass. Fixtures: tools/fixtures/rpm.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Rpm = require("@formats/archive/rpm")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/rpm")
|
||||
|
||||
local t = T.suite("rpm")
|
||||
t:eq(Rpm.name, "rpm", "name")
|
||||
|
||||
local TYPES = { [0x8000] = "0", [0x4000] = "5", [0xA000] = "2" }
|
||||
|
||||
for _, c in Fx.rpms do
|
||||
local a, err = Rpm.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "rpm", c.name .. ": F.detect")
|
||||
for k, v in c.fields do
|
||||
t:eq(a.meta.rpm[k], v, c.name .. ": " .. k)
|
||||
end
|
||||
t:eq(a.meta.rpm.type, "binary", c.name .. ": binary package")
|
||||
local by = {}
|
||||
for _, e in a:list() do
|
||||
by[e.name] = e
|
||||
end
|
||||
local n = 0
|
||||
for _, w in c.files do
|
||||
local e = by[w.name]
|
||||
if w.flags % 128 >= 64 then
|
||||
t:check(e == nil, c.name .. ": ghost " .. w.name .. " not listed")
|
||||
continue
|
||||
end
|
||||
n += 1
|
||||
t:check(e, c.name .. ": has " .. w.name)
|
||||
t:eq(e.type, TYPES[w.type], c.name .. ": " .. w.name .. " type")
|
||||
t:eq(e.mode, w.mode, c.name .. ": " .. w.name .. " mode")
|
||||
t:eq(e.mtime, w.mtime, c.name .. ": " .. w.name .. " mtime")
|
||||
t:eq(e.link, w.link, c.name .. ": " .. w.name .. " link")
|
||||
if w.crc then
|
||||
t:eq(e.size, w.size, c.name .. ": " .. w.name .. " size")
|
||||
local b, rerr = a:read(w.name)
|
||||
t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr))
|
||||
t:eq(Crc32.of(b), w.crc, c.name .. ": " .. w.name .. " data")
|
||||
end
|
||||
end
|
||||
t:eq(#a:list(), n, c.name .. ": entry count")
|
||||
-- hard links: both read the same bytes
|
||||
t:eq(buffer.tostring(a:read("usr/bin/hello")), "#!/bin/sh\necho hello\n", c.name .. ": hard link (first)")
|
||||
t:eq(buffer.tostring(a:read("usr/bin/hello2")), "#!/bin/sh\necho hello\n", c.name .. ": hard link (second)")
|
||||
local raw = Rpm.cpio(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(raw and buffer.len(raw) == c.cpioSize and Crc32.of(raw) == c.cpioCrc, c.name .. ": Rpm.cpio = rpm2cpio")
|
||||
t:note("%-16s %s %d files", c.name, tostring(a.meta.rpm.payloadCompressor), n)
|
||||
end
|
||||
|
||||
-- the api: an rpm loads as itself (no payload unwrap), converts to tar
|
||||
do
|
||||
local c = Fx.rpms[1]
|
||||
local a = assert(F.load(c.data))
|
||||
t:eq(a.format, "rpm", "F.load")
|
||||
local tar = F.load(assert(F.save(a, nil, { format = "tar" })))
|
||||
t:eq(buffer.tostring(tar:read("usr/bin/hello")), "#!/bin/sh\necho hello\n", "rpm -> tar")
|
||||
local _, e = F.save(a)
|
||||
t:check(e, "rpm is read-only")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local good = Fx.rpms[1].data
|
||||
local _, e1 = Rpm.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, 50))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short lead: " .. tostring(e1))
|
||||
local b = buffer.fromstring(good)
|
||||
buffer.writeu8(b, 96, 0)
|
||||
local _, e2 = Rpm.decode(Bytes.reader(b))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad signature magic: " .. tostring(e2))
|
||||
local cut = Rpm.decode(Bytes.reader(buffer.fromstring(string.sub(good, 1, #good - 200))))
|
||||
t:check(cut, "a cut payload still lists")
|
||||
local _, e3 = cut:read("usr/share/doc/hello/README")
|
||||
t:check(e3 ~= nil, "a cut payload fails on read: " .. tostring(e3))
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.rpms do
|
||||
if #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Rpm.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 25, keepHeader = 4 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,70 @@
|
||||
-- archive/uu tests: blocks by GNU uuencode (classic / -m) and uuenview (-u / -x) decode to the right names, sizes,
|
||||
-- modes and data; several blocks in a mail; CRLF lines; our uu / xx / base64 output decodes with uudecode / uudeview
|
||||
-- (fixture step) and is byte-identical to GNU uuencode for one file; round trips; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/uu.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Uu = require("@formats/archive/uu")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/uu")
|
||||
|
||||
local t = T.suite("uu")
|
||||
t:eq(Uu.name, "uu", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Uu.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)
|
||||
end
|
||||
t:eq(F.detect(c.data), "uu", c.name .. ": detect")
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
|
||||
-- byte-identical to GNU uuencode (mode 644) for each single file
|
||||
for _, g in Fx.gnu do
|
||||
local data = buffer.create(0)
|
||||
for _, f in E.FILES do
|
||||
if f.name == g.name then
|
||||
data = f.data
|
||||
end
|
||||
end
|
||||
local a = Uu.new()
|
||||
a:add(g.name, data, { mode = 0x1A4 })
|
||||
t:eq(buffer.tostring(a:write() :: buffer), g.uu, g.name .. ": same bytes as uuencode")
|
||||
t:eq(buffer.tostring(a:write({ variant = "base64" }) :: buffer), g.base64, g.name .. ": same bytes as uuencode -m")
|
||||
end
|
||||
for _, o in Fx.ours do
|
||||
t:check(o.ok, "our " .. o.variant .. " output decodes with the reference tool")
|
||||
end
|
||||
for _, v in { "uu", "xx", "base64" } do
|
||||
local out = E.roundTrip(t, Uu, { variant = v }, "round trip " .. v)
|
||||
if out then
|
||||
local back = Uu.decode(Bytes.reader(out)) :: any
|
||||
t:eq(back:list()[1].variant, v, v .. ": variant detected")
|
||||
end
|
||||
end
|
||||
-- a single block is a stream: the api unwraps what is inside (here: a uuencoded uuencoded file)
|
||||
local inner = Uu.encode("abc", { name = "x.uu" }) :: buffer
|
||||
local outer = Uu.encode(inner, { name = "x.uu" }) :: buffer
|
||||
local v = F.load(outer) :: any
|
||||
t:check(v and v.format == "uu" and v.meta.compression == "uu" and buffer.tostring((v:read("x.uu"))) == "abc", "api unwraps a single block")
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Uu.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("no blocks here\n"), Err.CORRUPT, "no begin line")
|
||||
t:eq(code("begin 644 x\n#86)C\n"), Err.TRUNCATED, "no end line")
|
||||
t:eq(code("begin 644 x\n#8~~~\n`\nend\n"), Err.CORRUPT, "bad characters")
|
||||
t:eq(select(2, Uu.encode({})) and (select(2, Uu.encode({})) :: any).code, Err.ARG, "encode a non-archive")
|
||||
t:eq((select(2, Uu.encode("x", { variant = "zz" :: any })) :: any).code, Err.ARG, "bad variant")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Uu.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,53 @@
|
||||
-- archive/yenc tests: single-part files by uuenview -y (including every byte value and the critical characters), a
|
||||
-- multi-part posting in order and reversed, two files in one text; part / file CRC and size checks; an incomplete
|
||||
-- posting; our output decodes with uudeview (fixture step) and round-trips; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/yenc.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Yenc = require("@formats/archive/yenc")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local E = require("@tests/archive/_entries")
|
||||
local Fx = require("@tests/gen/yenc")
|
||||
|
||||
local t = T.suite("yenc")
|
||||
t:eq(Yenc.name, "yenc", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Yenc.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.meta.incomplete, nil, c.name .. ": complete")
|
||||
end
|
||||
t:eq(F.detect(c.data), "yenc", c.name .. ": detect")
|
||||
if #c.data < 20000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:check(Fx.ours.ok, "our output decodes with uudeview")
|
||||
E.roundTrip(t, Yenc, nil, "round trip")
|
||||
E.roundTrip(t, Yenc, { line = 7 }, "round trip, 7-character lines")
|
||||
|
||||
-- only the first part of a posting: a short file flagged incomplete
|
||||
local p = Yenc.decode(Bytes.reader(buffer.fromstring(Fx.partial))) :: any
|
||||
t:check(p and p.meta.incomplete and p.meta.incomplete[1] == "big.bin" and p:list()[1].complete == false, "incomplete posting")
|
||||
|
||||
-- errors
|
||||
local good = buffer.tostring(Yenc.encode("hello", { name = "h" }) :: buffer)
|
||||
local function code(s: string): string?
|
||||
local _, e = Yenc.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code((string.gsub(good, "crc32=%x+", "crc32=00000000"))), Err.CHECKSUM, "bad crc32")
|
||||
t:eq(code((string.gsub(good, "=yend size=5", "=yend size=6"))), Err.CORRUPT, "size mismatch")
|
||||
t:eq(code((string.gsub(good, "=yend[^\n]*\n", ""))), Err.TRUNCATED, "no =yend")
|
||||
t:eq(code("nothing\n"), Err.CORRUPT, "no =ybegin")
|
||||
t:eq(code("=ybegin line=128 size=99999999999 name=x\n=yend size=99999999999\n"), Err.LIMIT, "huge size")
|
||||
t:eq((select(2, Yenc.encode("x", { line = 1 })) :: any).code, Err.ARG, "bad line length")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Yenc.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,70 @@
|
||||
-- archive/z tests: .Z files from ncompress load as single-stream archives (entry names from the file name, .taz -> .tar),
|
||||
-- a .tar.Z unwraps to the tar through the api, writing gives ncompress's bytes (a buffer, a one-entry Archive) and a
|
||||
-- tar inside for several entries, conversions, errors, the fuzz pass. Fixtures: tools/fixtures/z.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Z = require("@formats/archive/z")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/z")
|
||||
|
||||
local t = T.suite("z")
|
||||
t:eq(Z.name, "z", "name")
|
||||
|
||||
local text = Fx.cases[3]
|
||||
do
|
||||
local a, err = Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "notes.txt.Z" })
|
||||
t:check(a, "decode: " .. tostring(err))
|
||||
t:check(a.meta.stream, "single stream")
|
||||
t:eq(a:list()[1].name, "notes.txt", "entry named after the file")
|
||||
t:eq(a:list()[1].method, "lzw", "method")
|
||||
t:eq(buffer.tostring(a:read("notes.txt")), text.input, "data")
|
||||
t:eq(a.meta.source.maxBits, 16, "maxBits")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z))):list()[1].name, "data", "no name: data")
|
||||
t:eq(F.detect(text.z), "z", "F.detect")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "x.taz" }):list()[1].name, "x.tar", ".taz -> .tar")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "x.tZ" }):list()[1].name, "x.tar", ".tZ -> .tar")
|
||||
end
|
||||
|
||||
-- the api: a .tar.Z is the tar inside
|
||||
do
|
||||
local a, err = F.load(Fx.tarZ, { name = "src.tar.Z" })
|
||||
t:check(a, "tar.Z: " .. tostring(err))
|
||||
t:eq(a.format, "tar", "tar.Z unwraps")
|
||||
t:eq(a.meta.compression, "z", "meta.compression")
|
||||
t:eq(#a:list(), #Fx.tarNames, "tar entries")
|
||||
local outer = F.load(Fx.tarZ, { name = "src.tar.Z", unwrap = false })
|
||||
t:eq(outer:list()[1].name, "src.tar", "unwrap = false")
|
||||
end
|
||||
|
||||
-- writing
|
||||
do
|
||||
local out = assert(F.save(text.input, nil, { format = "z" }))
|
||||
t:check(buffer.tostring(out) == text.z, "a string: ncompress's bytes")
|
||||
local one = Z.new(text.input, "t.txt")
|
||||
t:check(buffer.tostring(assert(one:write())) == text.z, "a one-entry archive: ncompress's bytes")
|
||||
local tar = F.format("tar").new()
|
||||
tar:add("a.txt", "alpha")
|
||||
tar:add("b.txt", "beta")
|
||||
local tz = assert(F.save(tar, nil, { format = "z" }))
|
||||
local back = F.load(tz)
|
||||
t:eq(back.format, "tar", "several entries: a tar inside")
|
||||
t:eq(buffer.tostring(back:read("b.txt")), "beta", "tar inside: data")
|
||||
local b10 = assert(Z.encode(Fx.cases[6].input, { maxBits = Fx.cases[6].bits }))
|
||||
t:check(buffer.tostring(b10) == Fx.cases[6].z, "maxBits option")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Z.decode(Bytes.reader(buffer.fromstring("\31\157")))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short")
|
||||
local _, e2 = Z.encode({ kind = "image" })
|
||||
t:check(e2 and e2.code == Err.ARG, "encode a non-archive")
|
||||
t:eq(Z.sniff(Bytes.reader(buffer.fromstring("\31\139\8"))), 0, "gzip is not .Z")
|
||||
end
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Z.decode(Bytes.reader(b), { maxBytes = 2 ^ 22 })
|
||||
end, { text.z, Fx.tarZ }, { mutations = 40, keepHeader = 3 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,58 @@
|
||||
-- archive/zoo tests: zoo archives assembled by tools/fixtures/zoo.py (type 1 / type 2 entries, long and directory
|
||||
-- names, a deleted entry, methods stored / LZW / lzh, an LZW stream with table resets) give the files The Unarchiver
|
||||
-- extracts (names, sizes, data CRCs); CRC errors; the fuzz pass. Fixtures: tools/fixtures/zoo.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Zoo = require("@formats/archive/zoo")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/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()
|
||||
@@ -0,0 +1,73 @@
|
||||
-- codecs/font/Bitmap tests: 1-bit row pack / unpack round trips (MSB- and LSB-first, padded strides), the Font over a
|
||||
-- strike (glyphIndex / advance / rasterize / default glyph), Bitmap.strike of a bitmap font and of the first outline
|
||||
-- font from tools/fixtures/fonts.py (thresholded rasterization), and argument errors.
|
||||
local T = require("@tests/T")
|
||||
local Bitmap = require("@formats/codecs/font/Bitmap")
|
||||
local Ttf = require("@formats/font/ttf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/fonts")
|
||||
|
||||
local t = T.suite("bitmap")
|
||||
|
||||
local rand = T.rng(3)
|
||||
for k = 1, 30 do
|
||||
local w, h = rand(40), rand(20)
|
||||
local px = buffer.create(w * h)
|
||||
for i = 0, w * h - 1 do
|
||||
if rand(2) == 1 then
|
||||
buffer.writeu8(px, i, 255)
|
||||
end
|
||||
end
|
||||
local stride = math.ceil(w / 8) + (k % 3)
|
||||
local lsb = k % 2 == 0
|
||||
local packed = Bitmap.pack(px, w, h, stride, lsb)
|
||||
t:eq(buffer.len(packed), stride * h, "pack size " .. k)
|
||||
t:eq(buffer.tostring(Bitmap.unpack(packed, 0, w, h, stride, lsb)), buffer.tostring(px), "round trip " .. k)
|
||||
end
|
||||
-- 0x81 = left and right pixel on (MSB first)
|
||||
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0x81 }), 0, 8, 1, 1)), "\255\0\0\0\0\0\0\255", "msb order")
|
||||
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0x01 }), 0, 2, 1, 1, true)), "\255\0", "lsb order")
|
||||
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0xFF }), 0, 8, 2, 1)), string.rep("\255", 8) .. string.rep("\0", 8), "past the end reads 0")
|
||||
|
||||
local A = { codepoint = 65, width = 2, height = 2, left = 1, top = 7, advance = 4, pixels = T.bytes({ 255, 0, 0, 255 }) }
|
||||
local strike = { size = 8, ascent = 7, descent = 1, glyphs = { { width = 0, height = 0, left = 0, top = 0, advance = 3, pixels = buffer.create(0) }, A }, default = 0 }
|
||||
local font = Bitmap.font(strike, { family = "Test" })
|
||||
t:eq(font.kind, "font", "kind")
|
||||
t:eq(font.unitsPerEm, 8, "unitsPerEm")
|
||||
t:eq(font.descent, -1, "descent")
|
||||
t:eq(font:glyphIndex(65), 1, "glyphIndex")
|
||||
t:eq(font:glyphIndex(66), 0, "default glyph")
|
||||
t:eq(font:advance(1), 4, "advance")
|
||||
t:eq(font:outline(1), nil, "no outline")
|
||||
local img = font:rasterize(1, 16)
|
||||
t:check(img and img.format == "alpha8" and img.width == 2 and buffer.tostring(img.pixels) == "\255\0\0\255", "rasterize")
|
||||
t:eq(img and img.meta.top, 7, "rasterize top")
|
||||
t:eq(img and img.meta.scale, 2, "rasterize scale")
|
||||
t:eq(font:rasterize(9, 8), nil, "rasterize out of range")
|
||||
t:eq(Bitmap.strike(font), strike, "strike of a bitmap font")
|
||||
|
||||
local s1, e1 = Bitmap.strike({})
|
||||
t:check(s1 == nil and e1 and e1.code == Err.ARG, "strike of a non-font")
|
||||
if Fx.fonts[1] then
|
||||
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fx.fonts[1].data)))
|
||||
local s, err = Bitmap.strike(ttf, { px = 16, chars = "AB" })
|
||||
t:check(s, "strike of " .. Fx.fonts[1].name .. ": " .. tostring(err))
|
||||
if s then
|
||||
t:eq(#s.glyphs, 3, "strike glyphs")
|
||||
local g = s.glyphs[2]
|
||||
t:eq(g.codepoint, 65, "strike codepoint")
|
||||
t:check(g.width > 4 and g.height > 6 and g.top > 6 and g.advance > 4, "strike A metrics")
|
||||
local on = 0
|
||||
for i = 0, buffer.len(g.pixels) - 1 do
|
||||
local v = buffer.readu8(g.pixels, i)
|
||||
t:check(v == 0 or v == 255, "binary pixels")
|
||||
if v == 255 then
|
||||
on += 1
|
||||
end
|
||||
end
|
||||
t:check(on > 10, "A has ink")
|
||||
end
|
||||
t:check(select(2, Bitmap.strike(ttf, { px = 0 })) ~= nil, "px 0 refused")
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,75 @@
|
||||
-- codecs/compress/UnixCompress tests: ncompress 5.0 files (widths 10..16, block mode, CLEAR codes after the ratio
|
||||
-- check, empty / one-byte / random inputs) decode to their inputs; compress() gives ncompress's bytes for the same input
|
||||
-- and width; non-block-mode streams; errors; the fuzz pass. Fixtures: tools/fixtures/z.py.
|
||||
local T = require("@tests/T")
|
||||
local UnixCompress = require("@formats/codecs/compress/UnixCompress")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/z")
|
||||
|
||||
local t = T.suite("unixcompress")
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local out, err = UnixCompress.decompress(c.z)
|
||||
t:check(out, c.name .. ": decompress: " .. tostring(err))
|
||||
t:eq(buffer.len(out), #c.input, c.name .. ": size")
|
||||
t:eq(Crc32.of(out), Crc32.of(buffer.fromstring(c.input)), c.name .. ": data")
|
||||
local z, zerr = UnixCompress.compress(c.input, nil, nil, { maxBits = c.bits })
|
||||
t:check(z, c.name .. ": compress: " .. tostring(zerr))
|
||||
t:eq(buffer.len(z), #c.z, c.name .. ": compressed size = ncompress")
|
||||
t:check(buffer.tostring(z) == c.z, c.name .. ": bytes identical to ncompress -b " .. c.bits)
|
||||
t:note("%-12s %7d -> %7d bytes (-b %d)", c.name, #c.input, #c.z, c.bits)
|
||||
end
|
||||
|
||||
-- 9-bit codes, LSB first, after a .Z header with the given flags byte
|
||||
local function pack(flags: number, codes: { number }): buffer
|
||||
local acc, nb, bytes = 0, 0, { 0x1F, 0x9D, flags }
|
||||
for _, c in codes do
|
||||
acc += c * 2 ^ nb
|
||||
nb += 9
|
||||
while nb >= 8 do
|
||||
table.insert(bytes, acc % 256)
|
||||
acc = acc // 256
|
||||
nb -= 8
|
||||
end
|
||||
end
|
||||
if nb > 0 then
|
||||
table.insert(bytes, acc)
|
||||
end
|
||||
return T.bytes(bytes)
|
||||
end
|
||||
|
||||
-- a non-block-mode stream (no CLEAR code; 256 is the first table entry): "ABABABA"
|
||||
do
|
||||
local out = UnixCompress.decompress(pack(0x10, { 65, 66, 256, 258 }))
|
||||
t:eq(out and buffer.tostring(out), "ABABABA", "non-block mode")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = UnixCompress.decompress("\31\157")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short header")
|
||||
local _, e2 = UnixCompress.decompress("\31\158\144")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad magic")
|
||||
local _, e3 = UnixCompress.decompress("\31\157\145")
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "17-bit codes")
|
||||
local _, e4 = UnixCompress.decompress(pack(0x90, { 65, 400 }))
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "a code past the table: " .. tostring(e4))
|
||||
local _, e5 = UnixCompress.decompress(Fx.cases[5].z, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e5 and e5.code == Err.LIMIT, "maxBytes")
|
||||
local _, e6 = UnixCompress.compress("x", nil, nil, { maxBits = 17 })
|
||||
t:check(e6 and e6.code == Err.ARG, "maxBits 17")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.z < 4000 and #c.z > 3 then
|
||||
table.insert(samples, c.z)
|
||||
end
|
||||
end
|
||||
table.insert(samples, string.sub(Fx.cases[#Fx.cases].z, 1, 3000))
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return UnixCompress.decompress(b, nil, nil, { maxBytes = 2 ^ 22 })
|
||||
end, samples, { mutations = 40, keepHeader = 3 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,32 @@
|
||||
-- shared by the bitmap font tests: compare(t, font, ref, label) checks every glyph FreeType rendered (code point,
|
||||
-- bitmap size, left / top offsets, advance, pixels) against font:rasterize(font:glyphIndex(cp)) (by the font's own
|
||||
-- codes when FreeType had no Unicode charmap).
|
||||
local B = {}
|
||||
|
||||
function B.compare(t: any, font: any, ref: any, label: string)
|
||||
for _, g in ref.glyphs do
|
||||
local gi = font:glyphIndex(g.cp)
|
||||
if ref.unicode == false and font.meta.encodings then
|
||||
-- FreeType used the font's own codes (not Unicode): find the glyph by its code
|
||||
for k, enc in font.meta.encodings do
|
||||
if enc == g.cp then
|
||||
gi = k - 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
local img = font:rasterize(gi, 16)
|
||||
local tag = string.format("%s U+%04X", label, g.cp)
|
||||
t:check(img, tag .. ": rasterize")
|
||||
if img then
|
||||
t:eq(img.width, g.width, tag .. ": width")
|
||||
t:eq(img.height, g.height, tag .. ": height")
|
||||
t:eq(img.meta.left, g.left, tag .. ": left")
|
||||
t:eq(img.meta.top, g.top, tag .. ": top")
|
||||
t:eq(img.meta.advance, g.advance, tag .. ": advance")
|
||||
t:check(buffer.tostring(img.pixels) == g.pixels, tag .. ": pixels")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return B
|
||||
@@ -0,0 +1,61 @@
|
||||
-- font/bdf tests: Spleen BDF fonts against FreeType's renders (bitmaps, offsets, advances), properties and metrics; our
|
||||
-- BDF output (of BDF, PCF and outline fonts) read back by FreeType identically (fixture step) and round-tripping; an
|
||||
-- outline font written as BDF; errors; the fuzz pass. Fixtures: tools/fixtures/bitmapfonts.py, tools/fixtures/fonts.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Bdf = require("@formats/font/bdf")
|
||||
local Ttf = require("@formats/font/ttf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local B = require("@tests/font/_bitmap")
|
||||
local Fx = require("@tests/gen/bitmapfonts")
|
||||
local Fonts = require("@tests/gen/fonts")
|
||||
|
||||
local t = T.suite("bdf")
|
||||
t:eq(Bdf.name, "bdf", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.fonts do
|
||||
t:check(c.ours.ok, c.name .. ": FreeType reads our BDF of it the same: " .. c.ours.detail)
|
||||
if c.format == "bdf" then
|
||||
local font, err = Bdf.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
B.compare(t, font, c.ref, c.name)
|
||||
t:eq(font.ascent, c.ref.ascent, c.name .. ": ascent")
|
||||
t:eq(-font.descent, c.ref.descent, c.name .. ": descent")
|
||||
t:eq(font.info.family, "Spleen", c.name .. ": family")
|
||||
-- round trip through our writer
|
||||
local out = Bdf.encode(font) :: buffer
|
||||
local back = Bdf.decode(Bytes.reader(out)) :: any
|
||||
t:check(back and #back.bitmap.glyphs == #font.bitmap.glyphs, c.name .. ": round trip glyph count")
|
||||
if back then
|
||||
B.compare(t, back, c.ref, c.name .. " (round trip)")
|
||||
end
|
||||
end
|
||||
t:eq(F.detect(c.data), "bdf", c.name .. ": detect")
|
||||
table.insert(samples, string.sub(c.data, 1, 6000) .. "\nENDFONT\n")
|
||||
end
|
||||
end
|
||||
|
||||
-- an outline font as BDF
|
||||
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fonts.fonts[1].data)))
|
||||
local out, err = Bdf.encode(ttf, { px = 14, chars = "Hello" })
|
||||
t:check(out, "ttf -> bdf: " .. tostring(err))
|
||||
local back = Bdf.decode(Bytes.reader(out :: buffer)) :: any
|
||||
t:check(back and back:glyphIndex(72) > 0 and back.unitsPerEm == 14 and back:rasterize(back:glyphIndex(72), 14).width > 3, "ttf -> bdf glyphs")
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Bdf.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("hello"), Err.CORRUPT, "not BDF")
|
||||
t:eq(code("STARTFONT 2.1\nFONT x\n"), Err.TRUNCATED, "no ENDFONT")
|
||||
t:eq(code("STARTFONT 2.1\nSTARTCHAR a\nBBX 99999 2 0 0\nBITMAP\nENDCHAR\nENDFONT\n"), Err.CORRUPT, "bad BBX")
|
||||
t:eq((select(2, Bdf.encode({}))) and (select(2, Bdf.encode({})) :: any).code, Err.ARG, "encode a non-font")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Bdf.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,55 @@
|
||||
-- font/eot tests: EOT 1.0 / 2.1 / 2.1 XOR / 2.2 files around DejaVu Sans Mono (TrueType) and Source Sans 3 (CFF) give
|
||||
-- the header fields and the inner font (units per em, glyph count, cmap glyph ids and advances from fontTools); our
|
||||
-- EOT writer round-trips (same font bytes, header from the font's tables), XOR too; MTX is unsupported; errors; the fuzz
|
||||
-- pass. Fixtures: tools/fixtures/eot.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Eot = require("@formats/font/eot")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/eot")
|
||||
|
||||
local t = T.suite("eot")
|
||||
t:eq(Eot.name, "eot", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local font, err = Eot.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
local h = font.meta.eot
|
||||
t:eq(h.version, c.version, c.name .. ": version")
|
||||
t:eq(h.familyName, c.family, c.name .. ": family")
|
||||
t:eq(h.styleName, c.style, c.name .. ": style")
|
||||
t:eq(h.fullName, c.fullName, c.name .. ": full name")
|
||||
t:eq(h.weight, c.weight, c.name .. ": weight")
|
||||
t:eq(h.fsType, c.fsType, c.name .. ": fsType")
|
||||
t:eq(font.unitsPerEm, c.upem, c.name .. ": units per em")
|
||||
t:eq(font.numGlyphs, c.numGlyphs, c.name .. ": glyph count")
|
||||
t:eq(Crc32.of(font.meta.fontData), c.fontCrc, c.name .. ": font data")
|
||||
for _, g in c.glyphs do
|
||||
t:eq(font:glyphIndex(g.cp), g.gid, c.name .. ": cmap " .. g.cp)
|
||||
t:eq(font:advance(g.gid), g.advance, c.name .. ": advance " .. g.cp)
|
||||
end
|
||||
-- our writer
|
||||
for _, xor in { false, true } do
|
||||
local out, werr = Eot.encode(font, { xor = xor })
|
||||
t:check(out, c.name .. ": encode: " .. tostring(werr))
|
||||
local back = out and Eot.decode(Bytes.reader(out)) :: any
|
||||
t:check(back and Crc32.of(back.meta.fontData) == c.fontCrc and back.meta.eot.weight == c.weight and back.meta.eot.familyName == font.info.family, c.name .. ": round trip (xor " .. tostring(xor) .. ")")
|
||||
end
|
||||
end
|
||||
t:eq(F.detect(c.data), "eot", c.name .. ": detect")
|
||||
table.insert(samples, string.sub(c.data, 1, 3000))
|
||||
end
|
||||
local _, merr = Eot.decode(Bytes.reader(buffer.fromstring(Fx.mtx)))
|
||||
t:eq(merr and merr.code, Err.UNSUPPORTED, "MTX compressed")
|
||||
local _, serr = Eot.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.cases[1].data, 1, 50))))
|
||||
t:eq(serr and serr.code, Err.TRUNCATED, "cut header")
|
||||
t:eq((select(2, Eot.encode({}))) and (select(2, Eot.encode({})) :: any).code, Err.ARG, "encode a non-font")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Eot.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 20 })
|
||||
t:done()
|
||||
@@ -0,0 +1,50 @@
|
||||
-- font/fon tests: Windows .fnt and NE .fon fonts (one and two strikes) made by FontForge from the Spleen BDFs against
|
||||
-- FreeType's renders (ASCII: FreeType maps FNT byte codes), strikes and size selection; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/bitmapfonts.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Fon = require("@formats/font/fon")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local B = require("@tests/font/_bitmap")
|
||||
local Fx = require("@tests/gen/bitmapfonts")
|
||||
|
||||
local t = T.suite("fon")
|
||||
t:eq(Fon.name, "fon", "name")
|
||||
|
||||
local samples = {}
|
||||
local n = 0
|
||||
for _, c in Fx.fonts do
|
||||
if c.format == "fon" then
|
||||
n += 1
|
||||
local font, err = Fon.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
B.compare(t, font, c.ref, c.name)
|
||||
t:eq(font.ascent, c.ref.ascent, c.name .. ": ascent")
|
||||
if string.find(c.name, "two", 1, true) then
|
||||
t:eq(#font.meta.strikes, 2, c.name .. ": two strikes")
|
||||
local big = Fon.decode(Bytes.reader(buffer.fromstring(c.data)), { size = 24 }) :: any
|
||||
t:eq(big.unitsPerEm, 24, c.name .. ": size selection")
|
||||
end
|
||||
end
|
||||
t:eq(F.detect(c.data), "fon", c.name .. ": detect")
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:check(n >= 3, "has cases")
|
||||
|
||||
local function code(s: string): string?
|
||||
local _, e = Fon.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("\0\2\0\0"), Err.TRUNCATED, "short fnt")
|
||||
local vec = string.rep("\0", 200)
|
||||
vec = "\0\2" .. string.sub(vec, 3, 66) .. "\1\0" .. string.sub(vec, 69)
|
||||
t:eq(code(vec), Err.UNSUPPORTED, "vector font")
|
||||
t:eq(code("MZ" .. string.rep("\0", 100)), Err.CORRUPT, "MZ without NE")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Fon.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
t:done()
|
||||
@@ -0,0 +1,62 @@
|
||||
-- font/hershey tests: JHF text with Roman-style glyphs and random stroke glyphs (records wrapped over 72-column lines)
|
||||
-- against an independent parser (ids, advances, every moveTo / lineTo), ASCII and id mappings, stroked rasterization;
|
||||
-- errors; the fuzz pass. Fixtures: tools/fixtures/hershey.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Hershey = require("@formats/font/hershey")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/hershey")
|
||||
|
||||
local t = T.suite("hershey")
|
||||
t:eq(Hershey.name, "hershey", "name")
|
||||
|
||||
local font, err = Hershey.decode(Bytes.reader(buffer.fromstring(Fx.text)))
|
||||
t:check(font, "decode: " .. tostring(err))
|
||||
if font then
|
||||
t:eq(font.numGlyphs, #Fx.glyphs, "glyph count")
|
||||
for k, g in Fx.glyphs do
|
||||
local gi = k - 1
|
||||
t:eq(font.meta.ids[k], g.id, "id " .. k)
|
||||
t:eq(font:advance(gi), g.advance, "advance " .. g.id)
|
||||
local cmds = font:outline(gi) :: any
|
||||
t:eq(#cmds, #g.cmds, "commands " .. g.id)
|
||||
local same = true
|
||||
for i, c in g.cmds do
|
||||
local m = cmds[i]
|
||||
if not m or m.op ~= c.op or m[1] ~= c.x or m[2] ~= c.y then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
t:check(same, "outline " .. g.id)
|
||||
end
|
||||
t:eq(font:glyphIndex(33), 1, "ascii mapping: the second glyph is \"!\"")
|
||||
local img = font:rasterize(1, 64) :: any
|
||||
t:check(img and img.format == "alpha8" and img.width > 20 and img.height > 30 and img.meta.top > 30, "rasterize A")
|
||||
local ink = 0
|
||||
for i = 0, buffer.len(img.pixels) - 1 do
|
||||
ink += buffer.readu8(img.pixels, i)
|
||||
end
|
||||
t:check(ink > 255 * 50, "A has ink")
|
||||
t:eq(font:rasterize(0, 32).width, 0, "space is empty")
|
||||
end
|
||||
local byId = Hershey.decode(Bytes.reader(buffer.fromstring(Fx.text)), { map = "id" }) :: any
|
||||
t:eq(byId:glyphIndex(508), 2, "id mapping")
|
||||
t:eq(F.detect(Fx.text), "hershey", "detect")
|
||||
|
||||
local function code(s: string): string?
|
||||
local _, e = Hershey.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code(" 501 9MWRFJ[ RRFZ["), Err.TRUNCATED, "short record")
|
||||
t:eq(code("hello world\n"), Err.CORRUPT, "not jhf")
|
||||
t:eq(code("\n\n"), Err.CORRUPT, "empty")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local f = Hershey.decode(Bytes.reader(b))
|
||||
if f then
|
||||
f:rasterize(1, 24)
|
||||
end
|
||||
return f
|
||||
end, { string.sub(Fx.text, 1, 1500) })
|
||||
t:done()
|
||||
@@ -0,0 +1,62 @@
|
||||
-- font/pcf tests: PCF fonts compiled by bdftopcf with every glyph pad / scan unit / bit and byte order and -t, Tamzen
|
||||
-- PCFs and X11 misc-misc .pcf.gz fonts against FreeType's renders (bitmaps, offsets, advances) and metrics; charsets
|
||||
-- other than Unicode / Latin-1 stay unmapped; .pcf.gz through the api; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/bitmapfonts.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Pcf = require("@formats/font/pcf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local B = require("@tests/font/_bitmap")
|
||||
local Fx = require("@tests/gen/bitmapfonts")
|
||||
|
||||
local t = T.suite("pcf")
|
||||
t:eq(Pcf.name, "pcf", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.fonts do
|
||||
if c.format == "pcf" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
if c.gz then
|
||||
data = Deflate.gunzip(data, 0, buffer.len(data)) :: buffer
|
||||
end
|
||||
local font, err = Pcf.decode(Bytes.reader(data))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
B.compare(t, font, c.ref, c.name)
|
||||
t:eq(font.ascent, c.ref.ascent, c.name .. ": ascent")
|
||||
t:eq(-font.descent, c.ref.descent, c.name .. ": descent")
|
||||
t:eq(#font.bitmap.glyphs, c.ref.numGlyphs - 1, c.name .. ": glyph count (FreeType counts one more)")
|
||||
if string.find(c.name, "ISO8859-5", 1, true) then
|
||||
t:check(font.meta.unicode == false and font:glyphIndex(65) == (font.bitmap.default or 0), c.name .. ": not mapped to Unicode")
|
||||
end
|
||||
end
|
||||
t:eq(F.detect(data), "pcf", c.name .. ": detect")
|
||||
if #samples < 3 then
|
||||
table.insert(samples, data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- a .pcf.gz through the api: the gzip unwraps to the font (meta.compression = "gzip")
|
||||
for _, c in Fx.fonts do
|
||||
if c.gz then
|
||||
local v = F.load(c.data, { name = c.name }) :: any
|
||||
t:check(v and v.kind == "font" and v.meta.compression == "gzip" and #v.bitmap.glyphs > 100, c.name .. ": F.load unwraps the gzip")
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Pcf.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("\1fc"), Err.TRUNCATED, "short")
|
||||
t:eq(code("xxxx\0\0\0\0"), Err.CORRUPT, "bad magic")
|
||||
t:eq(code("\1fcp\0\0\0\0"), Err.CORRUPT, "no tables")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Pcf.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 40 })
|
||||
t:done()
|
||||
@@ -0,0 +1,82 @@
|
||||
-- font/psf tests: kbd / Spleen / Tamzen / Terminus / Cozette console fonts (PSF 1 and 2, with and without Unicode
|
||||
-- tables) against an independent parser (sizes, glyph bitmaps) and kbd's psfgettable (every code point -> glyph
|
||||
-- pair); our PSF 2 / PSF 1 output read back by psfgettable and the parser (fixture step) and round-tripping; an
|
||||
-- outline font written as PSF; .psfu.gz through the api; errors; the fuzz pass. Fixtures: tools/fixtures/psf.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Psf = require("@formats/font/psf")
|
||||
local Ttf = require("@formats/font/ttf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Fx = require("@tests/gen/psf")
|
||||
local Fonts = require("@tests/gen/fonts")
|
||||
|
||||
local t = T.suite("psf")
|
||||
t:eq(Psf.name, "psf", "name")
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.fonts do
|
||||
local data = buffer.fromstring(c.data)
|
||||
if c.gz then
|
||||
data = Deflate.gunzip(data, 0, buffer.len(data)) :: buffer
|
||||
end
|
||||
local font, err = Psf.decode(Bytes.reader(data))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
t:eq(font.meta.source.version, c.version, c.name .. ": version")
|
||||
t:eq(#font.bitmap.glyphs, c.count, c.name .. ": glyph count")
|
||||
t:eq(font.meta.width, c.width, c.name .. ": width")
|
||||
t:eq(font.meta.height, c.height, c.name .. ": height")
|
||||
for _, g in c.glyphs do
|
||||
local img = font:rasterize(g.index, c.height)
|
||||
t:check(img and buffer.tostring(img.pixels) == g.pixels, c.name .. ": glyph " .. g.index)
|
||||
end
|
||||
local mismatched = 0
|
||||
for _, p in c.table do
|
||||
local gi = font:glyphIndex(p.cp)
|
||||
-- a code point on several glyphs maps to the first: the glyph images are equal or the pair is an alias
|
||||
if gi ~= p.glyph and font.meta.unicodeTable[gi + 1] == nil then
|
||||
mismatched += 1
|
||||
end
|
||||
end
|
||||
t:eq(mismatched, 0, c.name .. ": Unicode table (" .. #c.table .. " pairs)")
|
||||
t:check(c.ours2.ok, c.name .. ": our PSF 2 reads back: " .. c.ours2.detail)
|
||||
if c.ours1 then
|
||||
t:check(c.ours1.ok, c.name .. ": our PSF 1 reads back: " .. c.ours1.detail)
|
||||
end
|
||||
local back = Psf.decode(Bytes.reader(Psf.encode(font) :: buffer)) :: any
|
||||
t:check(back and #back.bitmap.glyphs == c.count and back:glyphIndex(65) == font:glyphIndex(65), c.name .. ": round trip")
|
||||
end
|
||||
t:eq(F.detect(data), "psf", c.name .. ": detect")
|
||||
if c.gz then
|
||||
local v = F.load(c.data, { name = c.name }) :: any
|
||||
t:check(v and v.kind == "font" and v.meta.compression == "gzip", c.name .. ": F.load unwraps the gzip")
|
||||
end
|
||||
if #samples < 3 then
|
||||
table.insert(samples, data)
|
||||
end
|
||||
end
|
||||
|
||||
-- an outline font as PSF 2
|
||||
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fonts.fonts[1].data)))
|
||||
local out, err = Psf.encode(ttf, { px = 16, chars = "AB" })
|
||||
t:check(out, "ttf -> psf: " .. tostring(err))
|
||||
local back = Psf.decode(Bytes.reader(out :: buffer)) :: any
|
||||
t:check(back and back:glyphIndex(66) == 2 and back.meta.source.version == 2, "ttf -> psf glyphs")
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = Psf.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("\54\4\0\16"), Err.TRUNCATED, "psf1 without glyphs")
|
||||
t:eq(code("\114\181\74\134"), Err.TRUNCATED, "psf2 header cut")
|
||||
t:eq(code("\114\181\74\134\0\0\0\0\32\0\0\0\0\0\0\0\1\0\0\0\99\0\0\0\8\0\0\0\8\0\0\0"), Err.CORRUPT, "bad charsize")
|
||||
t:eq(code("hello"), Err.CORRUPT, "not psf")
|
||||
t:eq((select(2, Psf.encode(back, { version = 3 })) :: any).code, Err.ARG, "bad version")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Psf.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
t:done()
|
||||
@@ -0,0 +1,141 @@
|
||||
-- font/type1 tests (and codecs/font/GlyphNames): Latin Modern PFB fonts and a PFA (t1ascii) against fontTools
|
||||
-- (outline control boxes, areas, advances of sample glyphs including flex, hint replacement, seac accents and
|
||||
-- ligatures; code points from glyph names) and FreeType's unhinted renders (PSNR); errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/type1.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Type1 = require("@formats/font/type1")
|
||||
local GlyphNames = require("@formats/codecs/font/GlyphNames")
|
||||
local Raster = require("@formats/codecs/image/Raster")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/type1")
|
||||
|
||||
local t = T.suite("type1")
|
||||
t:eq(Type1.name, "type1", "name")
|
||||
t:eq(GlyphNames.unicode("Aacute"), 0xC1, "agl")
|
||||
t:eq(GlyphNames.unicode("uni20AC"), 0x20AC, "uniXXXX")
|
||||
t:eq(GlyphNames.unicode("u1F600"), 0x1F600, "uXXXXX")
|
||||
t:eq(GlyphNames.unicode("a.sc"), 0x61, "suffix")
|
||||
t:eq(GlyphNames.unicode("f_f_i"), 0x66, "ligature name")
|
||||
t:eq(GlyphNames.unicode("nosuchname"), nil, "unknown")
|
||||
t:eq(GlyphNames.STANDARD[65], "A", "StandardEncoding")
|
||||
|
||||
-- shoelace area of the flattened outline (font units)
|
||||
local function area(cmds: any): number
|
||||
local polys = Raster.flatten(cmds, nil, 0.02)
|
||||
local a = 0
|
||||
for _, p in polys do
|
||||
local n = #p
|
||||
local x0, y0 = p[n - 1], p[n]
|
||||
for i = 1, n - 1, 2 do
|
||||
a += x0 * p[i + 1] - p[i] * y0
|
||||
x0, y0 = p[i], p[i + 1]
|
||||
end
|
||||
end
|
||||
return a / 2
|
||||
end
|
||||
|
||||
local function psnr(img: any, r: any): (number, number)
|
||||
-- union of both bitmaps, aligned on the pen position
|
||||
local ax0, ay0 = img.meta.left, -img.meta.top
|
||||
local bx0, by0 = r.left, -r.top
|
||||
local x0, y0 = math.min(ax0, bx0), math.min(ay0, by0)
|
||||
local x1, y1 = math.max(ax0 + img.width, bx0 + r.width), math.max(ay0 + img.height, by0 + r.height)
|
||||
local se, n, ink = 0, 0, 0
|
||||
for y = y0, y1 - 1 do
|
||||
for x = x0, x1 - 1 do
|
||||
local a, b = 0, 0
|
||||
local ix, iy = x - ax0, y - ay0
|
||||
if ix >= 0 and iy >= 0 and ix < img.width and iy < img.height then
|
||||
a = buffer.readu8(img.pixels, iy * img.width + ix)
|
||||
end
|
||||
local jx, jy = x - bx0, y - by0
|
||||
if jx >= 0 and jy >= 0 and jx < r.width and jy < r.height then
|
||||
b = string.byte(r.pixels, jy * r.width + jx + 1)
|
||||
end
|
||||
se += (a - b) * (a - b)
|
||||
ink += b
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
if n == 0 or se == 0 then
|
||||
return 99, ink
|
||||
end
|
||||
return 10 * math.log10(255 * 255 / (se / n)), ink
|
||||
end
|
||||
|
||||
|
||||
local worst = 99
|
||||
local samples = {}
|
||||
for _, c in Fx.fonts do
|
||||
local font, err = Type1.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(font, c.name .. ": " .. tostring(err))
|
||||
if font then
|
||||
t:eq(font.unitsPerEm, c.upem, c.name .. ": units per em")
|
||||
t:eq(font.numGlyphs, c.numGlyphs, c.name .. ": glyph count")
|
||||
t:eq(font.info.family, c.family, c.name .. ": family")
|
||||
local byName = {}
|
||||
for k, n in font.meta.glyphNames do
|
||||
byName[n] = k - 1
|
||||
end
|
||||
local maxErr = 0
|
||||
for _, g in c.glyphs do
|
||||
local gi = byName[g.name]
|
||||
t:check(gi, c.name .. ": has " .. g.name)
|
||||
if gi then
|
||||
if g.cp then
|
||||
t:eq(font:glyphIndex(g.cp), gi, c.name .. ": cmap " .. g.name)
|
||||
end
|
||||
t:eq(font:advance(gi), g.advance, c.name .. ": advance " .. g.name)
|
||||
local cmds = font:outline(gi) :: any
|
||||
t:check(cmds, c.name .. ": outline " .. g.name)
|
||||
if g.bounds then
|
||||
local x0, y0, x1, y1 = math.huge, math.huge, -math.huge, -math.huge
|
||||
for _, cmd in cmds do
|
||||
for i = 1, #cmd, 2 do
|
||||
x0, x1 = math.min(x0, cmd[i]), math.max(x1, cmd[i])
|
||||
y0, y1 = math.min(y0, cmd[i + 1]), math.max(y1, cmd[i + 1])
|
||||
end
|
||||
end
|
||||
local ok = math.abs(x0 - g.bounds[1]) < 0.01 and math.abs(y0 - g.bounds[2]) < 0.01 and math.abs(x1 - g.bounds[3]) < 0.01 and math.abs(y1 - g.bounds[4]) < 0.01
|
||||
t:check(ok, string.format("%s: %s control box %s %s %s %s vs %s %s %s %s", c.name, g.name, x0, y0, x1, y1, g.bounds[1], g.bounds[2], g.bounds[3], g.bounds[4]))
|
||||
end
|
||||
local a = math.abs(area(cmds))
|
||||
local want = math.abs(g.area)
|
||||
local e = if want > 0 then math.abs(a - want) / want else a
|
||||
maxErr = math.max(maxErr, e)
|
||||
t:check(e < 0.002, string.format("%s: %s area %.1f vs %.1f", c.name, g.name, a, want))
|
||||
end
|
||||
end
|
||||
for _, r in c.renders do
|
||||
local img = font:rasterize(font:glyphIndex(r.cp), r.px) :: any
|
||||
local p = psnr(img, r)
|
||||
worst = math.min(worst, p)
|
||||
t:check(p > 25, string.format("%s: U+%04X at %d px: PSNR %.1f dB vs FreeType", c.name, r.cp, r.px, p))
|
||||
end
|
||||
t:note("%-22s %3d glyphs, area err %.5f", c.name, font.numGlyphs, maxErr)
|
||||
end
|
||||
t:eq(F.detect(c.data), "type1", c.name .. ": detect")
|
||||
table.insert(samples, string.sub(c.data, 1, 4000))
|
||||
end
|
||||
t:note("worst PSNR vs FreeType %.1f dB", worst)
|
||||
|
||||
local function code(s: string): string?
|
||||
local _, e = Type1.decode(Bytes.reader(buffer.fromstring(s)))
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("%!PS-AdobeFont-1.0: X\n/FontName /X def\n"), Err.CORRUPT, "no eexec")
|
||||
t:eq(code("\128\1\255\255\0\0"), Err.TRUNCATED, "cut PFB segment")
|
||||
t:eq(code("%!PS-AdobeFont-1.0\ncurrentfile eexec\n00"), Err.TRUNCATED, "short eexec")
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local f = Type1.decode(Bytes.reader(b))
|
||||
if f then
|
||||
for g = 0, math.min(f.numGlyphs - 1, 20) do
|
||||
f:outline(g)
|
||||
end
|
||||
end
|
||||
return f
|
||||
end, samples, { mutations = 20 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user