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()
|
||||
Reference in new issue
Block a user