Files
rosetta/tests/archive/cab.luau
T

167 lines
6.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, 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()