diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-04-26 16:48:42 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-04-26 16:48:42 +0100 |
commit | c15c3243bc0b79fc1068a146609c12c00880bf1a (patch) | |
tree | cc2bee7fe589d1532d14fccbda8c1b5bd8c05096 /test/lua | |
parent | f1dd413d7a092c432e032aac8c3dd46eee332aa1 (diff) | |
download | rspamd-c15c3243bc0b79fc1068a146609c12c00880bf1a.tar.gz rspamd-c15c3243bc0b79fc1068a146609c12c00880bf1a.zip |
[Test] Add unit tests for stream compression
Diffstat (limited to 'test/lua')
-rw-r--r-- | test/lua/unit/compression.lua | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/test/lua/unit/compression.lua b/test/lua/unit/compression.lua new file mode 100644 index 000000000..6ce5acfe8 --- /dev/null +++ b/test/lua/unit/compression.lua @@ -0,0 +1,56 @@ +-- Compression unit tests + +context("Rspamd compression", function() + local rspamd_zstd = require "rspamd_zstd" + local rspamd_text = require "rspamd_text" + + test("Compressed can be decompressed", function() + local str = 'test' + local cctx = rspamd_zstd.compress_ctx() + local dctx = rspamd_zstd.decompress_ctx() + assert_rspamd_eq({actual = dctx:stream(cctx:stream(str, 'end')), + expect = rspamd_text.fromstring(str)}) + end) + test("Compressed concatenation can be decompressed", function() + local str = 'test' + local cctx = rspamd_zstd.compress_ctx() + local dctx = rspamd_zstd.decompress_ctx() + assert_rspamd_eq({actual = dctx:stream(cctx:stream(str) .. cctx:stream(str, 'end')), + expect = rspamd_text.fromstring(str .. str)}) + end) + + local sizes = {10, 100, 1000, 10000} + for _,sz in ipairs(sizes) do + test("Compressed fuzz size: " .. tostring(sz), function() + for _=1,1000 do + local rnd = rspamd_text.randombytes(sz) + local cctx = rspamd_zstd.compress_ctx() + local dctx = rspamd_zstd.decompress_ctx() + assert_rspamd_eq({actual = dctx:stream(cctx:stream(rnd, 'end')), + expect = rnd}) + end + end) + end + + test("Compressed chunks", function() + local cctx = rspamd_zstd.compress_ctx() + local tin = {} + local tout = {} + for i=1,1000 do + local rnd = rspamd_text.randombytes(i) + tin[#tin + 1] = rnd + end + for i=1,1000 do + local o + if i == 1000 then + o = cctx:stream(tin[i], 'end') + else + o = cctx:stream(tin[i]) + end + tout[#tout + 1] = o + end + local dctx = rspamd_zstd.decompress_ctx() + assert_rspamd_eq({actual = dctx:stream(rspamd_text.fromtable(tout)), + expect = rspamd_text.fromtable(tin)}) + end) +end)
\ No newline at end of file |