275 lines
11 KiB
Luau
275 lines
11 KiB
Luau
-- codecs/compress/Deflate tests: Python zlib streams (levels 0-9, every strategy, gzip), the compressor round trips.
|
|
-- Fixtures: tools/fixtures/deflate.py.
|
|
local T = require("@tests/T")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
local Err = require("@rosetta/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
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
t:check(buffer.tostring((Deflate.zlib(Fx.cases[25].data))) == Fx.corpora[Fx.cases[25].corpus], "string input")
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
-- zlibStream: split input ranges (junk between them), a small output window, a sink that takes fixed-size records
|
|
local function stream(b: buffer, pieces: number, window: number?, record: number): (string?, any)
|
|
local n = buffer.len(b)
|
|
local big = buffer.create(n + pieces * 7)
|
|
buffer.fill(big, 0, 0xCD)
|
|
local ranges = {}
|
|
local at, w = 0, 0
|
|
for i = 1, pieces do
|
|
local len = if i == pieces then n - at else (n - at) // (pieces - i + 1)
|
|
buffer.copy(big, w, b, at, len)
|
|
table.insert(ranges, { w, len })
|
|
at += len
|
|
w += len + 7
|
|
end
|
|
local parts = {}
|
|
local size, err = Deflate.zlibStream(big, ranges, function(out: buffer, from: number, to: number): number
|
|
table.insert(parts, buffer.readstring(out, from, to - from))
|
|
return (to - from) % record
|
|
end, window)
|
|
if not size then
|
|
return nil, err
|
|
end
|
|
return table.concat(parts), size
|
|
end
|
|
|
|
do
|
|
local cases, other = 0, nil
|
|
for _, c in Fx.cases do
|
|
if c.kind == "zlib" then
|
|
local want = Fx.corpora[c.corpus]
|
|
local b = buffer.fromstring(c.data)
|
|
other = other or b
|
|
for _, v in { { 1, nil, 1 }, { 3, 1, 777 }, { 9, 5000, 4096 } } do
|
|
local got, size = stream(b, math.min(v[1], buffer.len(b)), v[2], v[3])
|
|
t:check(got == want and size == #want, string.format("%s: zlibStream %d ranges, window %s: %s", c.name, v[1], tostring(v[2]), tostring(size)))
|
|
end
|
|
cases += 1
|
|
end
|
|
end
|
|
t:note("zlibStream: %d zlib streams match, split and through small windows", cases)
|
|
-- a sink that yields while another inflate runs in between
|
|
local src = buffer.create(300000)
|
|
for i = 0, 299999 do
|
|
buffer.writeu8(src, i, (i * 13 + (i // 1000) * 7 + (i // 7) % 11) % 256)
|
|
end
|
|
local z = assert(Deflate.zlibCompress(src, { level = 6 }))
|
|
local got = {}
|
|
local co = coroutine.wrap(function(): any
|
|
return Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function(out: buffer, from: number, to: number): number
|
|
table.insert(got, buffer.readstring(out, from, to - from))
|
|
coroutine.yield()
|
|
return 0
|
|
end, 4096)
|
|
end)
|
|
local res = co()
|
|
local yields = 0
|
|
while res == nil do
|
|
yields += 1
|
|
local mid = Deflate.zlib(z)
|
|
t:check(mid and buffer.len(mid) == 300000, "inflate while a stream is suspended")
|
|
res = co()
|
|
end
|
|
t:check(res == 300000 and table.concat(got) == buffer.tostring(src), "zlibStream across yields (" .. yields .. ")")
|
|
local bad = buffer.fromstring(buffer.tostring(z))
|
|
buffer.writeu8(bad, buffer.len(bad) - 1, 0)
|
|
local _, e = Deflate.zlibStream(bad, { { 0, buffer.len(bad) } }, function()
|
|
return 0
|
|
end)
|
|
t:check(e and e.code == Err.CHECKSUM, "zlibStream Adler-32: " .. tostring(e))
|
|
local _, e2 = Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function()
|
|
return -1
|
|
end)
|
|
t:check(e2 and e2.code == Err.CORRUPT, "zlibStream sink abort: " .. tostring(e2))
|
|
local _, e3 = Deflate.zlibStream(z, { { 0, buffer.len(z) + 1 } }, function()
|
|
return 0
|
|
end)
|
|
t:check(e3 and e3.code == Err.ARG, "zlibStream range out of bounds")
|
|
local samples = {}
|
|
for i, c in Fx.cases do
|
|
if c.kind == "zlib" and i % 5 == 0 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("zlibStream", function(b: buffer)
|
|
local half = buffer.len(b) // 2
|
|
return Deflate.zlibStream(b, { { 0, half }, { half, buffer.len(b) - half } }, function(_: buffer, from: number, to: number): number
|
|
return (to - from) % 5
|
|
end, 100)
|
|
end, samples, { mutations = 3, garbage = 100 })
|
|
end
|
|
|
|
-- peek: the first bytes of a gzip / zlib / raw stream without inflating the rest
|
|
do
|
|
local src = buffer.create(100000)
|
|
for i = 0, 99999 do
|
|
buffer.writeu8(src, i, (i * 7 + (i // 97)) % 251)
|
|
end
|
|
local want = buffer.readstring(src, 0, 1000)
|
|
for _, kind in { "gzip", "zlibCompress", "compress" } do
|
|
local z = assert((Deflate :: any)[kind](src)) :: buffer
|
|
local head = assert(Deflate.peek(z, nil, nil, 1000))
|
|
t:check(buffer.len(head) <= 1000 and buffer.len(head) >= 1000 - 258, "peek " .. kind .. " size")
|
|
t:eq(buffer.tostring(head), string.sub(want, 1, buffer.len(head)), "peek " .. kind .. " bytes")
|
|
end
|
|
local small = assert(Deflate.peek(assert(Deflate.gzip(buffer.fromstring("hello"))), nil, nil, 1000))
|
|
t:eq(buffer.tostring(small), "hello", "peek a short stream")
|
|
local _, e = Deflate.peek(buffer.fromstring("\31\139\8\0\0\0\0\0\0\3\255\255\255\255\255\255\255\255"), nil, nil, 10)
|
|
t:check(e ~= nil, "peek bad data")
|
|
end
|
|
|
|
t:done()
|