116 lines
3.9 KiB
Luau
116 lines
3.9 KiB
Luau
-- archive/7z tests: the contract and sniff; every archive 7-Zip wrote in tools/fixtures/7z.py (LZMA / LZMA2 / copy /
|
|
-- Deflate, solid and not, BCJ / ARM64 / ARM / PPC + SPARC / delta chains, plain and encoded headers) lists exactly the
|
|
-- source tree (names incl. UTF-16 surrogates, directories, empty files, mtimes) and reads every file back; unsupported
|
|
-- methods (PPMd, BCJ2, AES) list but fail on read with Err.UNSUPPORTED naming the method; corrupt CRCs; F.load;
|
|
-- the fuzz no-throw pass.
|
|
local T = require("@tests/T")
|
|
local SevenZip = require("@formats/archive/7z")
|
|
local F = require("@formats")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/7z")
|
|
|
|
local t = T.suite("7z")
|
|
|
|
t:eq(SevenZip.name, "7z", "name")
|
|
t:eq(SevenZip.category, "archive", "category")
|
|
t:check(SevenZip.caps.decode and not SevenZip.caps.encode, "caps")
|
|
|
|
local function decode(data: string, opts: any?): (any, any)
|
|
return SevenZip.decode(Bytes.reader(buffer.fromstring(data)), opts)
|
|
end
|
|
|
|
local want: { [string]: any } = {}
|
|
for _, f in Fx.files do
|
|
want[f.name] = f
|
|
end
|
|
|
|
local nfiles = 0
|
|
for _, c in Fx.cases do
|
|
t:eq(SevenZip.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
|
|
local a, err = decode(c.data)
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
local list = a:list()
|
|
if c.empty then
|
|
t:eq(#list, 0, c.name .. ": no entries")
|
|
continue
|
|
end
|
|
local seen = {}
|
|
for _, e in list do
|
|
local w = want[e.name]
|
|
t:check(w ~= nil, c.name .. ": unexpected entry " .. e.name)
|
|
seen[e.name] = true
|
|
t:eq(e.dir, w.dir, c.name .. ": " .. e.name .. ": dir")
|
|
t:eq(e.mtime, w.mtime, c.name .. ": " .. e.name .. ": mtime")
|
|
if not w.dir then
|
|
t:eq(e.size, #w.data, c.name .. ": " .. e.name .. ": size")
|
|
local b, rerr = a:read(e.name)
|
|
if c.unsupported and #w.data > 0 then
|
|
t:check(b == nil and rerr and rerr.code == Err.UNSUPPORTED and string.find(rerr.message, c.unsupported, 1, true) ~= nil,
|
|
c.name .. ": " .. e.name .. ": unsupported: " .. tostring(rerr))
|
|
else
|
|
t:check(b and buffer.tostring(b) == w.data, c.name .. ": " .. e.name .. ": data: " .. tostring(rerr))
|
|
nfiles += 1
|
|
end
|
|
end
|
|
end
|
|
for name in want do
|
|
t:check(seen[name], c.name .. ": missing " .. name)
|
|
end
|
|
end
|
|
t:note("%d archives, %d files read", #Fx.cases, nfiles)
|
|
|
|
-- corrupt data: a flipped byte in a stored file is a CRC error on read; a flipped header byte a header CRC error
|
|
do
|
|
local c
|
|
for _, x in Fx.cases do
|
|
if x.name == "copy" then
|
|
c = x
|
|
end
|
|
end
|
|
local s = c.data
|
|
local at = string.find(s, "deep\ndeep", 1, true) :: number
|
|
local bad = string.sub(s, 1, at - 1) .. "D" .. string.sub(s, at + 1)
|
|
local a = decode(bad)
|
|
t:check(a, "copy archive with a flipped data byte still lists")
|
|
local b, e = a:read("a/b/c/deep.txt")
|
|
t:check(b == nil and e and e.code == Err.CHECKSUM, "data CRC: " .. tostring(e))
|
|
local _, e2 = decode(string.sub(s, 1, #s - 3) .. "\0\0\0")
|
|
t:check(e2 and (e2.code == Err.CHECKSUM or e2.code == Err.CORRUPT), "header CRC: " .. tostring(e2))
|
|
local _, e3 = decode(string.sub(s, 1, 20))
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
|
local a4 = decode(bad, { checkCrc = false })
|
|
t:check(a4 and a4:read("a/b/c/deep.txt"), "checkCrc = false")
|
|
end
|
|
|
|
-- through the api
|
|
do
|
|
local c = Fx.cases[2]
|
|
local a, err = F.load(c.data)
|
|
t:check(a and a.format == "7z", "F.load: " .. tostring(err))
|
|
t:eq(F.detect(c.data), "7z", "F.detect")
|
|
local r = a:read("readme.txt")
|
|
t:check(r and buffer.tostring(r) == want["readme.txt"].data, "F.load read")
|
|
end
|
|
|
|
-- fuzz: decode + read every entry
|
|
do
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
if #c.data < 60000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local a = SevenZip.decode(Bytes.reader(b), { maxBytes = 2 ^ 21 })
|
|
if a then
|
|
for _, e in a:list() do
|
|
a:read(e.name)
|
|
end
|
|
end
|
|
return a
|
|
end, samples, { mutations = 12, garbage = 300 })
|
|
end
|
|
|
|
t:done()
|