Browse Source

[Test] Hide expensive unit tests with RSPAMD_LUA_EXPENSIVE_TESTS env variable

pull/4367/head
Vsevolod Stakhov 3 years ago
parent
commit
51903dd4b6
No known key found for this signature in database GPG Key ID: 7647B6790081437
  1. 34
      test/lua/unit/base32.lua
  2. 198
      test/lua/unit/base64.lua
  3. 24
      test/lua/unit/compression.lua
  4. 39
      test/lua/unit/fpconv.lua
  5. 42
      test/lua/unit/quoted_printable.lua
  6. 46
      test/lua/unit/rfc2047.lua
  7. 136
      test/lua/unit/utf.lua

34
test/lua/unit/base32.lua

@ -33,21 +33,23 @@ context("Base32 encodning", function()
end
end)
test("Base32 fuzz test: zbase32", function()
for i = 1,1000 do
local b, l = random_buf(4096)
local how = math.floor(math.random(3) - 1)
local ben = ffi.C.rspamd_encode_base32(b, l, how)
local bs = ffi.string(ben)
local nl = ffi.new("size_t [1]")
local nb = ffi.C.rspamd_decode_base32(bs, #bs, nl, how)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
test("Base32 fuzz test: zbase32", function()
for i = 1,1000 do
local b, l = random_buf(4096)
local how = math.floor(math.random(3) - 1)
local ben = ffi.C.rspamd_encode_base32(b, l, how)
local bs = ffi.string(ben)
local nl = ffi.new("size_t [1]")
local nb = ffi.C.rspamd_decode_base32(bs, #bs, nl, how)
assert_equal(tonumber(nl[0]), l,
string.format("invalid size reported: %d reported vs %d expected", tonumber(nl[0]), l))
local cmp = ffi.C.memcmp(b, nb, l)
ffi.C.g_free(ben)
ffi.C.g_free(nb)
assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
end
end)
assert_equal(tonumber(nl[0]), l,
string.format("invalid size reported: %d reported vs %d expected", tonumber(nl[0]), l))
local cmp = ffi.C.memcmp(b, nb, l)
ffi.C.g_free(ben)
ffi.C.g_free(nb)
assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
end
end)
end
end)

198
test/lua/unit/base64.lua

@ -89,104 +89,106 @@ vehemence of any carnal pleasure.]]
assert_equal(cmp, 0)
end)
test("Base64 fuzz test", function()
for i = 1,1000 do
local b, l = random_safe_buf(4096)
local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
local orig = ffi.string(b)
local ben = util.encode_base64(orig, lim)
local dec = util.decode_base64(ben)
assert_equal(orig, tostring(dec), "fuzz test failed for length: " .. #orig)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
test("Base64 fuzz test", function()
for i = 1,1000 do
local b, l = random_safe_buf(4096)
local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
local orig = ffi.string(b)
local ben = util.encode_base64(orig, lim)
local dec = util.decode_base64(ben)
assert_equal(orig, tostring(dec), "fuzz test failed for length: " .. #orig)
end
end)
test("Base64 fuzz test (ffi)", function()
for i = 1,1000 do
local b, l = random_buf(4096)
local nl = ffi.new("size_t [1]")
local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
local ben = ffi.C.rspamd_encode_base64(b, l, lim, nl)
local bs = ffi.string(ben)
local ol = ffi.new("size_t [1]")
local nb = ffi.C.g_base64_decode(ben, ol)
local cmp = ffi.C.memcmp(b, nb, l)
ffi.C.g_free(ben)
ffi.C.g_free(nb)
assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
end
end)
local speed_iters = 10000
local function perform_base64_speed_test(chunk, is_reference, line_len)
local ticks = ffi.C.base64_test(is_reference, speed_iters, chunk, line_len)
local what = 'Optimized'
if is_reference then
what = 'Reference'
end
logger.messagex("%s base64 %s chunk (%s line len): %s ticks per iter, %s ticks per byte",
what, chunk, line_len,
ticks / speed_iters, ticks / speed_iters / chunk)
return 1
end
end)
test("Base64 fuzz test (ffi)", function()
for i = 1,1000 do
local b, l = random_buf(4096)
local nl = ffi.new("size_t [1]")
local lim = ffi.C.ottery_rand_unsigned() % 64 + 10
local ben = ffi.C.rspamd_encode_base64(b, l, lim, nl)
local bs = ffi.string(ben)
local ol = ffi.new("size_t [1]")
local nb = ffi.C.g_base64_decode(ben, ol)
local cmp = ffi.C.memcmp(b, nb, l)
ffi.C.g_free(ben)
ffi.C.g_free(nb)
assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
end
end)
local speed_iters = 10000
local function perform_base64_speed_test(chunk, is_reference, line_len)
local ticks = ffi.C.base64_test(is_reference, speed_iters, chunk, line_len)
local what = 'Optimized'
if is_reference then
what = 'Reference'
end
logger.messagex("%s base64 %s chunk (%s line len): %s ticks per iter, %s ticks per byte",
what, chunk, line_len,
ticks / speed_iters, ticks / speed_iters / chunk)
return 1
test("Base64 test reference vectors 78", function()
local res = perform_base64_speed_test(78, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 78", function()
local res = perform_base64_speed_test(78, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 512", function()
local res = perform_base64_speed_test(512, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 512", function()
local res = perform_base64_speed_test(512, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 512 (78 line len)", function()
local res = perform_base64_speed_test(512, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 512 (78 line len)", function()
local res = perform_base64_speed_test(512, false, 78)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 1K", function()
local res = perform_base64_speed_test(1024, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 1K", function()
local res = perform_base64_speed_test(1024, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 1K (78 line len)", function()
local res = perform_base64_speed_test(1024, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 1K (78 line len)", function()
local res = perform_base64_speed_test(1024, false, 78)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 10K", function()
local res = perform_base64_speed_test(10 * 1024, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 10K", function()
local res = perform_base64_speed_test(10 * 1024, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 10K (78 line len)", function()
local res = perform_base64_speed_test(10 * 1024, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 10K (78 line len)", function()
local res = perform_base64_speed_test(10 * 1024, false, 78)
assert_not_equal(res, 0)
end)
end
test("Base64 test reference vectors 78", function()
local res = perform_base64_speed_test(78, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 78", function()
local res = perform_base64_speed_test(78, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 512", function()
local res = perform_base64_speed_test(512, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 512", function()
local res = perform_base64_speed_test(512, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 512 (78 line len)", function()
local res = perform_base64_speed_test(512, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 512 (78 line len)", function()
local res = perform_base64_speed_test(512, false, 78)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 1K", function()
local res = perform_base64_speed_test(1024, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 1K", function()
local res = perform_base64_speed_test(1024, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 1K (78 line len)", function()
local res = perform_base64_speed_test(1024, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 1K (78 line len)", function()
local res = perform_base64_speed_test(1024, false, 78)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 10K", function()
local res = perform_base64_speed_test(10 * 1024, true, 0)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 10K", function()
local res = perform_base64_speed_test(10 * 1024, false, 0)
assert_not_equal(res, 0)
end)
test("Base64 test reference vectors 10K (78 line len)", function()
local res = perform_base64_speed_test(10 * 1024, true, 78)
assert_not_equal(res, 0)
end)
test("Base64 test optimized vectors 10K (78 line len)", function()
local res = perform_base64_speed_test(10 * 1024, false, 78)
assert_not_equal(res, 0)
end)
end)

24
test/lua/unit/compression.lua

@ -19,17 +19,19 @@ context("Rspamd compression", function()
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)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
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
end
test("Compressed chunks", function()

39
test/lua/unit/fpconv.lua

@ -2,7 +2,6 @@
context("Fpconv printf functions", function()
local ffi = require("ffi")
local u = require "rspamd_util"
local niter_fuzz = 100000
local function small_double()
return math.random()
@ -72,25 +71,27 @@ long rspamd_printf(const char *format, ...);
assert_equal(sbuf, c[3], c[3] .. " but test returned " .. sbuf)
end)
end
for i,c in ipairs(benchmarks) do
test("Fuzz fp test " .. c[1], function()
for _=1,niter_fuzz do
local sign = 1
if math.random() > 0.5 then
sign = -1
end
local d = c[2]() * sign
ffi.C.snprintf(buf, 64, c[3], d)
ffi.C.rspamd_snprintf(buf2, 64, c[3], d)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
for _,c in ipairs(benchmarks) do
test("Fuzz fp test " .. c[1], function()
for _=1,niter_fuzz do
local sign = 1
if math.random() > 0.5 then
sign = -1
end
local d = c[2]() * sign
ffi.C.snprintf(buf, 64, c[3], d)
ffi.C.rspamd_snprintf(buf2, 64, c[3], d)
local sbuf = ffi.string(buf)
local sbuf2 = ffi.string(buf2)
local sbuf = ffi.string(buf)
local sbuf2 = ffi.string(buf2)
assert_less_than(math.abs(d - tonumber(sbuf2))/math.abs(d),
0.00001,
string.format('rspamd emitted: %s, libc emitted: %s, original number: %g',
sbuf2, sbuf, d))
end
end)
assert_less_than(math.abs(d - tonumber(sbuf2))/math.abs(d),
0.00001,
string.format('rspamd emitted: %s, libc emitted: %s, original number: %g',
sbuf2, sbuf, d))
end
end)
end
end
end)

42
test/lua/unit/quoted_printable.lua

@ -134,29 +134,31 @@ context("Quoted-Printable encoding", function()
end)
end
-- Fuzz testing
local charset = {}
for i = 0, 255 do table.insert(charset, string.char(i)) end
local function random_string(length)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
-- Fuzz testing
local charset = {}
for i = 0, 255 do table.insert(charset, string.char(i)) end
if length > 0 then
return random_string(length - 1) .. charset[math.random(1, #charset)]
else
return ""
end
end
local function random_string(length)
for _,l in ipairs({10, 100, 1000, 10000}) do
test("QP fuzz test max length " .. tostring(l), function()
for _=1,100 do
local inp = random_string(math.random() * l + 1)
local res = {
expect = inp,
actual = tostring(rspamd_util.decode_qp((rspamd_util.encode_qp(inp, 0))))
}
assert_rspamd_eq(res)
if length > 0 then
return random_string(length - 1) .. charset[math.random(1, #charset)]
else
return ""
end
end)
end
for _,l in ipairs({10, 100, 1000, 10000}) do
test("QP fuzz test max length " .. tostring(l), function()
for _=1,100 do
local inp = random_string(math.random() * l + 1)
local res = {
expect = inp,
actual = tostring(rspamd_util.decode_qp((rspamd_util.encode_qp(inp, 0))))
}
assert_rspamd_eq(res)
end
end)
end
end
end)

46
test/lua/unit/rfc2047.lua

@ -60,31 +60,33 @@ context("RFC2047 decoding", function()
ffi.C.rspamd_mempool_delete(pool)
end)
test("Fuzz test for rfc2047 tokens", function()
local util = require("rspamd_util")
local pool = ffi.C.rspamd_mempool_new_(4096, "lua", 0, "rfc2047.lua:63")
local str = "Тест Тест Тест Тест Тест"
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
test("Fuzz test for rfc2047 tokens", function()
local util = require("rspamd_util")
local pool = ffi.C.rspamd_mempool_new_(4096, "lua", 0, "rfc2047.lua:63")
local str = "Тест Тест Тест Тест Тест"
for i = 0,1000 do
local r1 = math.random()
local r2 = math.random()
local sl1 = #str / 2.0 * r1
local sl2 = #str / 2.0 * r2
for _ = 0,1000 do
local r1 = math.random()
local r2 = math.random()
local sl1 = #str / 2.0 * r1
local sl2 = #str / 2.0 * r2
local s1 = tostring(util.encode_base64(string.sub(str, 1, sl1)))
local s2 = tostring(util.encode_base64(string.sub(str, sl1 + 1, sl2)))
local s3 = tostring(util.encode_base64(string.sub(str, sl2 + 1)))
local s1 = tostring(util.encode_base64(string.sub(str, 1, sl1)))
local s2 = tostring(util.encode_base64(string.sub(str, sl1 + 1, sl2)))
local s3 = tostring(util.encode_base64(string.sub(str, sl2 + 1)))
if #s1 > 0 and #s2 > 0 and #s3 > 0 then
local s = string.format('=?UTF-8?B?%s?= =?UTF-8?B?%s?= =?UTF-8?B?%s?=',
s1, s2, s3)
local res = ffi.C.rspamd_mime_header_decode(pool, s, #s)
res = ffi.string(res)
assert_not_nil(res, "cannot decode " .. s)
assert_rspamd_eq({actual = res, expect = str})
if #s1 > 0 and #s2 > 0 and #s3 > 0 then
local s = string.format('=?UTF-8?B?%s?= =?UTF-8?B?%s?= =?UTF-8?B?%s?=',
s1, s2, s3)
local res = ffi.C.rspamd_mime_header_decode(pool, s, #s)
res = ffi.string(res)
assert_not_nil(res, "cannot decode " .. s)
assert_rspamd_eq({actual = res, expect = str})
end
end
end
ffi.C.rspamd_mempool_delete(pool)
end)
ffi.C.rspamd_mempool_delete(pool)
end)
end
end)

136
test/lua/unit/utf.lua

@ -122,83 +122,85 @@ context("UTF8 check functions", function()
end)
end
local speed_iters = 10000
local function test_size(buflen, is_valid, impl)
local logger = require "rspamd_logger"
local test_str
if is_valid then
test_str = table.concat(valid_cases)
else
test_str = table.concat(valid_cases) .. table.concat(invalid_cases)
end
local buf = ffi.new("char[?]", buflen)
if #test_str < buflen then
local t = {}
local len = #test_str
while len < buflen do
t[#t + 1] = test_str
len = len + #test_str
end
test_str = table.concat(t)
end
ffi.copy(buf, test_str:sub(1, buflen))
local tm = 0
for _=1,speed_iters do
if impl == 'ref' then
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_ref(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
elseif impl == 'sse' then
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_sse41(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
if os.getenv("RSPAMD_LUA_EXPENSIVE_TESTS") then
local speed_iters = 10000
local function test_size(buflen, is_valid, impl)
local logger = require "rspamd_logger"
local test_str
if is_valid then
test_str = table.concat(valid_cases)
else
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_avx2(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
test_str = table.concat(valid_cases) .. table.concat(invalid_cases)
end
end
logger.messagex("%s utf8 %s check (valid = %s): %s ticks per iter, %s ticks per byte",
impl, buflen, is_valid,
tm / speed_iters, tm / speed_iters / buflen)
local buf = ffi.new("char[?]", buflen)
if #test_str < buflen then
local t = {}
local len = #test_str
while len < buflen do
t[#t + 1] = test_str
len = len + #test_str
end
test_str = table.concat(t)
end
ffi.copy(buf, test_str:sub(1, buflen))
local tm = 0
for _=1,speed_iters do
if impl == 'ref' then
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_ref(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
elseif impl == 'sse' then
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_sse41(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
else
local t1 = ffi.C.rspamd_get_ticks(1)
ffi.C.rspamd_fast_utf8_validate_avx2(buf, buflen)
local t2 = ffi.C.rspamd_get_ticks(1)
tm = tm + (t2 - t1)
end
end
return 0
end
logger.messagex("%s utf8 %s check (valid = %s): %s ticks per iter, %s ticks per byte",
impl, buflen, is_valid,
tm / speed_iters, tm / speed_iters / buflen)
for _,sz in ipairs({78, 512, 65535}) do
test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'valid'), function()
local res = test_size(sz, true, 'ref')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'invalid'), function()
local res = test_size(sz, false, 'ref')
assert_equal(res, 0)
end)
return 0
end
if jit.arch == 'x64' then
test(string.format("Utf8 test %s %d buffer, %s", 'sse', sz, 'valid'), function()
local res = test_size(sz, true, 'sse')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'sse', sz, 'invalid'), function()
local res = test_size(sz, false, 'sse')
for _,sz in ipairs({78, 512, 65535}) do
test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'valid'), function()
local res = test_size(sz, true, 'ref')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'avx2', sz, 'valid'), function()
local res = test_size(sz, true, 'avx2')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'avx2', sz, 'invalid'), function()
local res = test_size(sz, false, 'avx2')
test(string.format("Utf8 test %s %d buffer, %s", 'ref', sz, 'invalid'), function()
local res = test_size(sz, false, 'ref')
assert_equal(res, 0)
end)
if jit.arch == 'x64' then
test(string.format("Utf8 test %s %d buffer, %s", 'sse', sz, 'valid'), function()
local res = test_size(sz, true, 'sse')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'sse', sz, 'invalid'), function()
local res = test_size(sz, false, 'sse')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'avx2', sz, 'valid'), function()
local res = test_size(sz, true, 'avx2')
assert_equal(res, 0)
end)
test(string.format("Utf8 test %s %d buffer, %s", 'avx2', sz, 'invalid'), function()
local res = test_size(sz, false, 'avx2')
assert_equal(res, 0)
end)
end
end
end
Loading…
Cancel
Save