-- archive/br tests: the contract; fixture streams (tools/fixtures/brotli.py) load as a one-entry stream archive named -- after the file; detection through F.load (sniff + extension); sniff rejects garbage and other formats; errors as -- Err values; the fuzz no-throw pass. local T = require("@tests/T") local Br = require("@formats/archive/br") local F = require("@formats") local Bytes = require("@formats/core/Bytes") local Err = require("@formats/core/Err") local Fx = require("@tests/gen/brotli") local t = T.suite("br") -- 1. the contract t:eq(Br.name, "br", "name") t:eq(Br.category, "archive", "category") t:check(Br.caps.decode and not Br.caps.encode, "caps") -- 2. fixtures local n = 0 for i, c in Fx.cases do if i % 4 == 0 then local a, err = Br.decode(Bytes.reader(buffer.fromstring(c.data)), { name = "dir/file.txt.br" }) t:check(a, c.name .. ": " .. tostring(err)) t:check(a.kind == "archive" and a.format == "br" and a.meta.stream, c.name .. ": a stream archive") local list = a:list() t:eq(#list, 1, c.name .. ": one entry") t:eq(list[1].name, "file.txt", c.name .. ": entry name") t:eq(list[1].size, #Fx.corpora[c.corpus], c.name .. ": entry size") t:eq(list[1].compressedSize, #c.data, c.name .. ": compressed size") local data = a:read("file.txt") t:check(data and buffer.tostring(data) == Fx.corpora[c.corpus], c.name .. ": entry data") n += 1 end end t:note("%d streams", n) -- default entry name do local a = Br.decode(Bytes.reader(buffer.fromstring(Fx.cases[5].data))) t:eq(a:list()[1].name, "data", "default entry name") end -- 3. through the api: by sniff (with and without the extension) and by format name do local c = Fx.cases[40] local a, err = F.load(c.data, { name = "x.br" }) t:check(a and a.format == "br", "F.load by sniff + extension: " .. tostring(err)) t:eq(a:list()[1].name, "x", "F.load names the entry") local a2 = F.load(c.data) t:check(a2 and a2.format == "br", "F.load by sniff alone") local a3 = F.archive.load(c.data, { format = "br" }) t:check(a3 and a3.format == "br", "F.archive.load with format") t:eq(F.detect(c.data, { name = "x.br" }), "br", "F.detect") local _, e = F.save(a3, nil, { format = "br" }) t:check(e and e.code == Err.UNSUPPORTED, "no encoder: " .. tostring(e)) end -- 4. sniff: garbage and plain text are rejected do local rand = T.rng(99) local hits = 0 for _ = 1, 300 do local len = 16 + rand(400) local b = buffer.create(len) for k = 0, len - 1 do buffer.writeu8(b, k, rand(256)) end if Br.sniff(Bytes.reader(b)) > 0 then hits += 1 end end t:note("sniff accepted %d of 300 random buffers", hits) t:check(hits <= 3, "sniff rejects random data") t:eq(Br.sniff(Bytes.reader(buffer.fromstring("Hello, world! This is plain text.\n"))), 0, "sniff rejects text") t:eq(Br.sniff(Bytes.reader(buffer.create(0))), 0, "sniff of nothing") t:check(Br.sniff(Bytes.reader(buffer.fromstring(Fx.cases[40].data))) > 0, "sniff accepts a stream") local _, e = Br.decode(Bytes.reader(buffer.fromstring("\x1b\xff\xff"))) t:check(e and Err.is(e) and (e.code == Err.CORRUPT or e.code == Err.TRUNCATED), "bad data: " .. tostring(e)) end -- 5. fuzz do local samples = {} for i, c in Fx.cases do if i % 23 == 0 and #c.data > 8 then table.insert(samples, if #c.data > 8000 then string.sub(c.data, 1, 8000) else c.data) end end t:fuzz("decode", function(b: buffer) return Br.decode(Bytes.reader(b)) end, samples, { mutations = 4, garbage = 500 }) t:fuzz("sniff", function(b: buffer) return Br.sniff(Bytes.reader(b)) end, samples, { mutations = 2, garbage = 300 }) end t:done()