-- codecs/compress/Brotli tests: every fixture stream (reference encoder: qualities 0..11, windows 10..24, text / font / generic -- modes, large windows) decodes to its corpus; consumedEnd; offsets inside a bigger buffer; the static dictionary; -- hand-made streams (empty, metadata, uncompressed); maxBytes; truncated / corrupted / random input returns nil, Err -- and never throws. Fixtures: tools/fixtures/brotli.py. local T = require("@tests/T") local Brotli = require("@formats/codecs/compress/Brotli") local Err = require("@formats/core/Err") local Crc32 = require("@formats/core/Crc32") local Fx = require("@tests/gen/brotli") local t = T.suite("brotli") -- the static dictionary do local d = Brotli.dictionary() t:check(d and buffer.len(d) == 122784, "dictionary size") t:eq(buffer.readstring(d :: buffer, 0, 8), "timedown", "dictionary starts with the length-4 words") end -- every case local nbytes = 0 for _, c in Fx.cases do local want = Fx.corpora[c.corpus] local out, e = Brotli.decompress(c.data) t:check(out ~= nil, c.name .. ": " .. tostring(e)) if buffer.tostring(out :: buffer) ~= want then t:check(false, c.name .. ": output differs (" .. buffer.len(out :: buffer) .. " bytes, want " .. #want .. ")") end t:eq(e, #c.data, c.name .. ": consumed") nbytes += #want end t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6) -- at an offset inside a bigger buffer, with trailing bytes do local c = Fx.cases[30] local big = buffer.create(#c.data + 13) buffer.fill(big, 0, 0xAB) buffer.writestring(big, 5, c.data) local out, e = Brotli.decompress(big, 5, #c.data + 8) t:check(out and buffer.tostring(out) == Fx.corpora[c.corpus] and e == 5 + #c.data, "at an offset") end -- hand-made streams do local bytes = T.bytes -- WBITS 16 (bit 0), ISLAST, ISLASTEMPTY: 0b110 = 6 local o, e = Brotli.decompress(bytes({ 6 })) t:check(o and buffer.len(o) == 0 and e == 1, "empty stream: " .. tostring(e)) -- 0x3b: python brotli's empty stream (WBITS 22) local o2 = Brotli.decompress(bytes({ 0x3b })) t:check(o2 and buffer.len(o2) == 0, "empty stream (WBITS 22)") local _, e0 = Brotli.decompress(bytes({})) t:check(e0 and e0.code == Err.TRUNCATED, "empty input is TRUNCATED") -- an uncompressed meta-block "hi": WBITS 16 [0], ISLAST 0, MNIBBLES 4 [00], MLEN-1 = 1 [1000 0000 0000 0000], -- ISUNCOMPRESSED 1, pad, "hi", then ISLAST 1 ISLASTEMPTY 1 -- bits LSB first: 0 | 0 | 00 | 1000 | 0000 0000 0000 | 1 -> byte0 = 0b00010000 = 0x10 (bits 0..7: 0,0,0,0,1,0,0,0) -- byte1 = 0, byte2 = bits 16..23: 0,0,0,0 (rest of nibbles) 1 (ISUNCOMPRESSED) + pad 000 = 0x10 local o3 = Brotli.decompress(bytes({ 0x10, 0x00, 0x10, 0x68, 0x69, 0x03 })) t:check(o3 and buffer.tostring(o3) == "hi", "uncompressed meta-block") local _, e3 = Brotli.decompress(bytes({ 0x10, 0x00, 0x10, 0x68 })) t:check(e3 and e3.code == Err.TRUNCATED, "truncated uncompressed block: " .. tostring(e3)) -- nonzero padding at the end local _, e4 = Brotli.decompress(bytes({ 0x10, 0x00, 0x10, 0x68, 0x69, 0x0b })) t:check(e4 and e4.code == Err.CORRUPT, "nonzero padding: " .. tostring(e4)) -- a metadata block (MNIBBLES 3, 1 byte length, 2 bytes skipped) before the last empty one -- bits: WBITS 0 | ISLAST 0 | MNIBBLES 11 | reserved 0 | MSKIPBYTES 01 | len-1 = 1 (8 bits) | pad | 2 bytes | 11 -- 0,0,1,1,0,1,0, then 8 bits of 1: 1,0,0,0,0,0,0,0, pad to 16 -> byte0 = 0b0 1 0 1 1 0 0 = bits0..7: 0,0,1,1,0,1,0,1 local o5 = Brotli.decompress(bytes({ 0xAC, 0x00, 0xEE, 0xEE, 0x03 })) t:check(o5 and buffer.len(o5) == 0, "metadata block skipped") local ok, r1, r2 = pcall(Brotli.decompress, 5 :: any) t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG") end -- maxBytes do local c for _, x in Fx.cases do if x.corpus == "english" then c = x break end end local _, e = Brotli.decompress(c.data, nil, nil, { maxBytes = 1000 }) t:check(e and e.code == Err.LIMIT, "maxBytes: " .. tostring(e)) local big for _, x in Fx.cases do if string.find(x.name, "large", 1, true) then big = x break end end local _, e2 = Brotli.decompress(big.data, nil, nil, { largeWindow = false }) t:check(e2 and e2.code == Err.UNSUPPORTED, "largeWindow = false: " .. tostring(e2)) end -- a corrupted stream reports CORRUPT or TRUNCATED, never a wrong success with the right CRC do local bad = 0 for i, c in Fx.cases do if i % 17 == 0 and #c.data > 20 then local b = buffer.fromstring(c.data) buffer.writeu8(b, #c.data // 2, bit32.bxor(buffer.readu8(b, #c.data // 2), 0x5A)) local out = Brotli.decompress(b) if out and Crc32.of(out) == Crc32.of(buffer.fromstring(Fx.corpora[c.corpus])) then bad += 1 end end end t:eq(bad, 0, "corrupted streams never decode to the original") end -- truncation, corruption and garbage never throw do local samples = {} for i, c in Fx.cases do if i % 11 == 0 and #c.data > 8 then table.insert(samples, if #c.data > 20000 then string.sub(c.data, 1, 20000) else c.data) end end t:fuzz("decompress", function(b: buffer) return Brotli.decompress(b) end, samples, { mutations = 6, garbage = 3000 }) end t:done()