| 1 |
-- Compression unit tests |
| 2 |
|
| 3 |
context("Rspamd compression", function() |
| 4 |
local rspamd_zstd = require "rspamd_zstd" |
| 5 |
local rspamd_text = require "rspamd_text" |
| 6 |
|
| 7 |
test("Compressed can be decompressed", function() |
| 8 |
local str = 'test' |
| 9 |
local cctx = rspamd_zstd.compress_ctx() |
| 10 |
local dctx = rspamd_zstd.decompress_ctx() |
| 11 |
assert_rspamd_eq({actual = dctx:stream(cctx:stream(str, 'end')), |
| 12 |
expect = rspamd_text.fromstring(str)}) |
| 13 |
end) |
| 14 |
test("Compressed concatenation can be decompressed", function() |
| 15 |
local str = 'test' |
| 16 |
local cctx = rspamd_zstd.compress_ctx() |
| 17 |
local dctx = rspamd_zstd.decompress_ctx() |
| 18 |
assert_rspamd_eq({actual = dctx:stream(cctx:stream(str) .. cctx:stream(str, 'end')), |
| 19 |
expect = rspamd_text.fromstring(str .. str)}) |
| 20 |
end) |
| 21 |
|
| 22 |
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then |
| 23 |
local sizes = {10, 100, 1000, 10000} |
| 24 |
for _,sz in ipairs(sizes) do |
| 25 |
test("Compressed fuzz size: " .. tostring(sz), function() |
| 26 |
for _=1,1000 do |
| 27 |
local rnd = rspamd_text.randombytes(sz) |
| 28 |
local cctx = rspamd_zstd.compress_ctx() |
| 29 |
local dctx = rspamd_zstd.decompress_ctx() |
| 30 |
assert_rspamd_eq({actual = dctx:stream(cctx:stream(rnd, 'end')), |
| 31 |
expect = rnd}) |
| 32 |
end |
| 33 |
end) |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
test("Compressed chunks", function() |
| 38 |
local cctx = rspamd_zstd.compress_ctx() |
| 39 |
local tin = {} |
| 40 |
local tout = {} |
| 41 |
for i=1,1000 do |
| 42 |
local rnd = rspamd_text.randombytes(i) |
| 43 |
tin[#tin + 1] = rnd |
| 44 |
end |
| 45 |
for i=1,1000 do |
| 46 |
local o |
| 47 |
if i == 1000 then |
| 48 |
o = cctx:stream(tin[i], 'end') |
| 49 |
else |
| 50 |
o = cctx:stream(tin[i]) |
| 51 |
end |
| 52 |
tout[#tout + 1] = o |
| 53 |
end |
| 54 |
local dctx = rspamd_zstd.decompress_ctx() |
| 55 |
assert_rspamd_eq({actual = dctx:stream(rspamd_text.fromtable(tout)), |
| 56 |
expect = rspamd_text.fromtable(tin)}) |
| 57 |
end) |
| 58 |
end) |