-- codecs/compress/Deflate tests: every fixture case (Python zlib, levels 0-9, every strategy, small windows, flushes, gzip -- headers, multi-member gzip) decodes; exact-size mode; checksum vectors; the compressor round-trips every corpus at -- every level (and its zlib / gzip framing); truncated / corrupted / random input returns nil, Err and never throws. -- Ported from deflate-luau tests/inflate.luau. Fixtures: tools/fixtures/deflate.py. local T = require("@tests/T") local Deflate = require("@formats/codecs/compress/Deflate") local Err = require("@formats/core/Err") local Fx = require("@tests/gen/deflate") local t = T.suite("deflate") local function decode(kind: string, b: buffer, ...: any): (buffer?, any) if kind == "raw" then return Deflate.inflate(b, ...) elseif kind == "zlib" then return Deflate.zlib(b, ...) else return Deflate.gunzip(b, ...) end end -- checksum vectors t:eq(Deflate.crc32("123456789"), 0xCBF43926, "crc32 check value") t:eq(Deflate.crc32(""), 0, "crc32 empty") t:eq(Deflate.adler32("Wikipedia"), 0x11E60398, "adler32 Wikipedia") t:eq(Deflate.adler32(""), 1, "adler32 empty") do local s = string.rep("The quick brown fox jumps over the lazy dog. ", 400) local b = buffer.fromstring(s) local whole = Deflate.crc32(b) local part = Deflate.crc32(b, 0, 1001) t:eq(Deflate.crc32(b, 1001, nil, part), whole, "crc32 continuation") local a = Deflate.adler32(b, 0, 7000) t:eq(Deflate.adler32(b, 7000, nil, a), Deflate.adler32(b), "adler32 continuation") t:eq(Deflate.crc32(b, 3, 17), Deflate.crc32(string.sub(s, 4, 20)), "crc32 range") end -- every case decodes to its corpus local nbytes = 0 for _, c in Fx.cases do local want = Fx.corpora[c.corpus] local b = buffer.fromstring(c.data) local out, err = decode(c.kind, b) t:check(out ~= nil, c.name .. ": " .. tostring(err)) t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs") nbytes += #want if c.kind == "raw" then t:eq(err, #c.data, c.name .. ": consumed") -- exact size, and a wrong exact size local o2 = Deflate.inflate(b, 0, nil, #want) t:check(o2 and buffer.tostring(o2) == want, c.name .. ": exact size") if #want > 0 then t:check(Deflate.inflate(b, 0, nil, #want - 1) == nil, c.name .. ": too small exact size fails") end t:check(Deflate.inflate(b, 0, nil, #want + 1) == nil, c.name .. ": too large exact size fails") -- the same stream inside a bigger buffer, at an offset, with trailing bytes local big = buffer.create(#c.data + 13) buffer.fill(big, 0, 0xAB) buffer.copy(big, 5, b) local o3, e3 = Deflate.inflate(big, 5, #c.data + 8) t:check(o3 and buffer.tostring(o3) == want and e3 == 5 + #c.data, c.name .. ": at an offset") elseif c.kind == "zlib" then t:eq(err, #c.data, c.name .. ": zlib consumed") end end t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6) -- strings are accepted t:check(buffer.tostring((Deflate.zlib(Fx.cases[25].data))) == Fx.corpora[Fx.cases[25].corpus], "string input") -- corrupted checksums do local z for _, c in Fx.cases do if c.kind == "zlib" and #Fx.corpora[c.corpus] > 100 then z = c break end end local b = buffer.fromstring(z.data) buffer.writeu8(b, buffer.len(b) - 1, bit32.bxor(buffer.readu8(b, buffer.len(b) - 1), 1)) local out, err = Deflate.zlib(b) t:check(out == nil and Err.is(err) and err.code == Err.CHECKSUM, "zlib adler mismatch: " .. tostring(err)) for _, c in Fx.cases do if c.kind == "gzip" and #Fx.corpora[c.corpus] > 100 then local g = buffer.fromstring(c.data) local n = buffer.len(g) buffer.writeu8(g, n - 6, bit32.bxor(buffer.readu8(g, n - 6), 0x40)) local o, e = Deflate.gunzip(g) t:check(o == nil and e and e.code == Err.CHECKSUM, c.name .. ": gzip crc mismatch: " .. tostring(e)) break end end end -- hand-made streams do local bytes = T.bytes -- stored block "hi" then a fixed block with only end-of-block local o = Deflate.inflate(bytes({ 0x00, 0x02, 0x00, 0xFD, 0xFF, 0x68, 0x69, 0x03, 0x00 })) t:check(o and buffer.tostring(o) == "hi", "stored + fixed EOB") t:check(Deflate.inflate(bytes({ 0x01, 0x02, 0x00, 0xFC, 0xFF, 0x68, 0x69 })) == nil, "stored NLEN mismatch") t:check(Deflate.inflate(bytes({ 0x07 })) == nil, "block type 3") local _, e0 = Deflate.inflate(bytes({})) t:check(e0 and e0.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e0)) -- fixed block: a match before any output (distance too far back) local _, e = Deflate.inflate(bytes({ 0x03, 0x02, 0x00 })) t:check(e ~= nil and e.code == Err.CORRUPT, "distance too far back: " .. tostring(e)) t:check(Deflate.zlib(bytes({ 0x78, 0x9c })) == nil, "zlib truncated header") t:check(Deflate.zlib(bytes({ 0x78, 0x9d, 0x03, 0, 0, 0, 0, 1 })) == nil, "zlib FCHECK") t:check(Deflate.gunzip(bytes({ 0x1f, 0x8c, 8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 })) == nil, "gzip magic") local ok, r1, r2 = pcall(Deflate.inflate, 5 :: any) t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG") local ok2, _, r3 = pcall(Deflate.inflate, "abc", -1) t:check(ok2 and r3.code == Err.ARG, "a bad offset returns Err.ARG") end -- the compressor: every corpus, every level, raw / zlib / gzip; ratio against Python zlib -6 do local total, comp6, ref6 = 0, 0, 0 for name, data in Fx.corpora do local b = buffer.fromstring(data) for level = 0, 9 do local z, err = Deflate.compress(b, { level = level }) t:check(z, name .. ": compress level " .. level .. ": " .. tostring(err)) local back, berr = Deflate.inflate(z :: buffer) t:check(back and buffer.tostring(back) == data, string.format("%s: level %d round trip: %s", name, level, tostring(berr))) if level == 6 then comp6 += buffer.len(z :: buffer) total += #data end end local zl = Deflate.zlibCompress(data) :: buffer local back = Deflate.zlib(zl) t:check(back and buffer.tostring(back) == data, name .. ": zlibCompress round trip") local gz = Deflate.gzip(data, { level = 9 }) :: buffer local back2 = Deflate.gunzip(gz) t:check(back2 and buffer.tostring(back2) == data, name .. ": gzip round trip") end for _, c in Fx.cases do if c.name == c.corpus .. "/zlib6" then ref6 += #c.data - 6 end end t:note("compress -6: %d -> %d bytes (%.1f%%); Python zlib -6: %d bytes", total, comp6, 100 * comp6 / total, ref6) t:check(comp6 < ref6 * 1.25, "level 6 within 25% of zlib") local _, e = Deflate.compress("x", { level = 11 }) t:check(e and e.code == Err.ARG, "bad level returns Err.ARG") end -- truncation, corruption and garbage never throw do local samples = {} local kinds = {} for i, c in Fx.cases do if i % 9 == 0 and #c.data > 8 then table.insert(samples, c.data) kinds[c.data] = c.kind end end t:fuzz("inflate", function(b: buffer) return Deflate.inflate(b) end, samples, { mutations = 6, garbage = 3000 }) t:fuzz("zlib", function(b: buffer) return Deflate.zlib(b) end, samples, { mutations = 3, garbage = 300 }) t:fuzz("gunzip", function(b: buffer) return Deflate.gunzip(b) end, samples, { mutations = 3, garbage = 300 }) end t:done()