merge ws/arch2-unix (cpio, ar/deb, rpm, z, iso, lz4; containers/Cpio, Iso9660; codecs UnixCompress, Xxh32, Lz4 from ws/roblox)
This commit is contained in:
commit
242e38c4fe
36 files changed
+5171
-7
No files matched your search
@@ -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,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,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,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,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/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,101 @@
|
||||
-- codecs/compress/Lz4 tests: every reference block (python-lz4 fast at accelerations 1 / 4 / 16, HC 4 / 9 / 12)
|
||||
-- decodes to its corpus, with and without the size; our encoder's blocks decode with the reference (fixture verdicts)
|
||||
-- and round trip here; linked blocks through decodeInto's history; error codes; truncated / mutated / random blocks
|
||||
-- never throw. Fixtures: tools/fixtures/lz4.py.
|
||||
local T = require("@tests/T")
|
||||
local Lz4 = require("@formats/codecs/compress/Lz4")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/lz4")
|
||||
local C = require("@tests/gen/lz4_corpora")
|
||||
|
||||
local t = T.suite("lz4")
|
||||
|
||||
local nbytes = 0
|
||||
for _, c in Fx.cases do
|
||||
local want = C[c.corpus]
|
||||
local out, err = Lz4.decompress(c.data, nil, nil, #want)
|
||||
t:check(out ~= nil and buffer.tostring(out) == want, c.name .. ": " .. tostring(err))
|
||||
if #want > 0 then
|
||||
local grown = Lz4.decompress(c.data)
|
||||
t:check(grown ~= nil and buffer.tostring(grown) == want, c.name .. ": without the size")
|
||||
end
|
||||
nbytes += #want
|
||||
end
|
||||
t:note("%d reference blocks, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
|
||||
|
||||
for _, v in Fx.ours do
|
||||
t:check(v.ok, v.name .. ": the reference decodes our block")
|
||||
end
|
||||
for name, data in C do
|
||||
local b = Lz4.compress(data) :: buffer
|
||||
local back = Lz4.decompress(b, nil, nil, #data)
|
||||
t:check(back ~= nil and buffer.tostring(back) == data, name .. ": round trip")
|
||||
if name == "text" or name == "zeros" or name == "runs" then
|
||||
t:check(buffer.len(b) < #data / 2, name .. ": compresses")
|
||||
end
|
||||
t:check(buffer.len(b) <= Lz4.bound(#data), name .. ": within bound")
|
||||
end
|
||||
|
||||
-- at an offset, and linked blocks: the second block refers into the first one's output
|
||||
do
|
||||
local text = C.text
|
||||
local a, b2 = string.sub(text, 1, 70000), string.sub(text, 70001, 140000)
|
||||
local pa = Lz4.compress(a) :: buffer
|
||||
-- a block of b2 that copies the first 40 bytes of b2's region from a (a match reaching back across blocks)
|
||||
local big = buffer.create(buffer.len(pa) + 20)
|
||||
buffer.copy(big, 9, pa)
|
||||
local out = Lz4.decompress(big, 9, buffer.len(pa), #a)
|
||||
t:check(out ~= nil and buffer.tostring(out) == a, "at an offset")
|
||||
local hand = buffer.create(3 + 1 + 2 + 1)
|
||||
-- token: 0 literals, match length 15 + ext; offset = 100 back into the previous block
|
||||
buffer.writeu8(hand, 0, 0x0F)
|
||||
buffer.writeu16(hand, 1, 100)
|
||||
buffer.writeu8(hand, 3, 20) -- match length 4 + 15 + 20 = 39
|
||||
buffer.writeu8(hand, 4, 0x10) -- the last sequence: one literal
|
||||
buffer.writeu8(hand, 5, 33)
|
||||
local dst = buffer.create(#a + 40)
|
||||
buffer.writestring(dst, 0, a)
|
||||
local e, msg = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40, 0)
|
||||
t:check(e == #a + 40, "linked block: " .. tostring(msg))
|
||||
t:eq(buffer.readstring(dst, #a, 39), string.sub(a, #a - 99, #a - 61), "linked block: match from the history")
|
||||
local e2, msg2 = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40)
|
||||
t:check(e2 == nil and string.find(msg2 or "", "offset") ~= nil, "without history the match is corrupt")
|
||||
t:check(b2 ~= nil, "second half")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Lz4.decompress("")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "empty: " .. tostring(e1))
|
||||
local _, e2 = Lz4.decompress("\x50ab")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "literals past the end: " .. tostring(e2))
|
||||
local _, e3 = Lz4.decompress("\x14abcd\x09\x00\x10z")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "offset before the start: " .. tostring(e3))
|
||||
local _, e4 = Lz4.decompress("\x10a", nil, nil, 5)
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "shorter than outSize: " .. tostring(e4))
|
||||
local _, e5 = Lz4.decompress("\x50abcde", nil, nil, 2)
|
||||
t:check(e5 and e5.code == Err.LIMIT, "longer than outSize: " .. tostring(e5))
|
||||
local _, e6 = Lz4.decompress(C.zeros and (Lz4.compress(C.zeros) :: buffer), nil, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e6 and e6.code == Err.LIMIT, "maxBytes: " .. tostring(e6))
|
||||
local ok, r1, r2 = pcall(Lz4.decompress, 5 :: any)
|
||||
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
||||
local _, e7 = Lz4.decompress("\x10a", nil, nil, -1)
|
||||
t:check(e7 and e7.code == Err.ARG, "bad outSize")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 4 == 0 and #c.data > 8 and #c.data < 30000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return Lz4.decompress(b, nil, nil, nil, { maxBytes = 1e6 })
|
||||
end, samples, { mutations = 30, garbage = 2000 })
|
||||
t:fuzz("decompress (sized)", function(b: buffer)
|
||||
return Lz4.decompress(b, nil, nil, 70000)
|
||||
end, samples, { mutations = 30, garbage = 500, seed = 5 })
|
||||
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()
|
||||
Reference in new issue
Block a user