66 lines
2.6 KiB
Luau
66 lines
2.6 KiB
Luau
-- archive/bz2 tests: the contract, sniff, a .bz2 as a one-entry single-stream archive (entry named after the file),
|
|
-- the api path (F.load by sniff, .tar.bz2 unwrap is the api's: covered once tar exists), errors, the fuzz pass.
|
|
-- Fixtures: tools/fixtures/bzip2.py.
|
|
local T = require("@tests/T")
|
|
local Bz2 = require("@formats/archive/bz2")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local F = require("@formats")
|
|
local Fx = require("@tests/gen/bzip2")
|
|
|
|
local t = T.suite("bz2")
|
|
|
|
t:eq(Bz2.name, "bz2", "name")
|
|
t:eq(Bz2.category, "archive", "category")
|
|
t:check(Bz2.caps.decode and not Bz2.caps.encode, "caps")
|
|
|
|
for _, c in Fx.cases do
|
|
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
|
|
end
|
|
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring("BZh9hello"))), 0, "sniff: no block magic")
|
|
t:eq(Bz2.sniff(Bytes.reader(buffer.fromstring("\x1f\x8b\x08\x00"))), 0, "sniff: gzip")
|
|
|
|
do
|
|
local c = Fx.cases[1]
|
|
for _, x in Fx.cases do
|
|
if x.name == "text/l9" then
|
|
c = x
|
|
end
|
|
end
|
|
local a, err = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "dir/notes.txt.bz2" })
|
|
t:check(a, "decode: " .. tostring(err))
|
|
t:check(a.kind == "archive" and a.format == "bz2" and a.meta.stream, "a single-stream archive")
|
|
local list = a:list()
|
|
t:eq(#list, 1, "one entry")
|
|
t:eq(list[1].name, "notes.txt", "entry named after the file")
|
|
t:eq(list[1].size, #Fx.corpora.text, "entry size")
|
|
t:eq(list[1].compressedSize, #c.data, "compressed size")
|
|
t:check(buffer.tostring((a:read("notes.txt"))) == Fx.corpora.text, "read")
|
|
local _, re = a:read("nope")
|
|
t:check(re and re.code == Err.ARG, "read of a missing entry")
|
|
local _, we = a:write()
|
|
t:check(we and we.code == Err.UNSUPPORTED, "read-only")
|
|
local a2 = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "x.tbz2" })
|
|
t:eq(a2:list()[1].name, "x.tar", ".tbz2 -> .tar")
|
|
local a3 = Bz2.decode(Bytes.reader(buffer.fromstring(c.data)))
|
|
t:eq(a3:list()[1].name, "data", "no name -> data")
|
|
-- through the api
|
|
local v, ferr = F.load(c.data)
|
|
t:check(v and v.kind == "archive" and v.format == "bz2", "F.load sniffs bz2: " .. tostring(ferr))
|
|
t:eq(F.detect(c.data), "bz2", "F.detect")
|
|
local _, de = Bz2.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 100))))
|
|
t:check(de and de.code == Err.TRUNCATED, "truncated: " .. tostring(de))
|
|
end
|
|
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if i % 6 == 0 and #c.data < 30000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Bz2.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 6, garbage = 300 })
|
|
|
|
t:done()
|