130 lines
4.7 KiB
Luau
130 lines
4.7 KiB
Luau
-- 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; 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, "covers store and mszip")
|
|
|
|
-- 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
|
|
|
|
-- Deflate's preset dictionary
|
|
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()
|