rspamd/test/lua/unit/base32.lua

50 lines
1.5 KiB
Lua
Raw Normal View History

2015-03-10 00:01:05 +01:00
-- Test zbase32 encoding/decoding
context("Base32 encodning", function()
local ffi = require("ffi")
ffi.cdef[[
void ottery_rand_bytes(void *buf, size_t n);
unsigned ottery_rand_unsigned(void);
unsigned char* rspamd_decode_base32 (const char *in, size_t inlen, size_t *outlen);
char * rspamd_encode_base32 (const unsigned char *in, size_t inlen);
void g_free(void *ptr);
2015-03-10 00:44:19 +01:00
int memcmp(const void *a1, const void *a2, size_t len);
2015-03-10 00:01:05 +01:00
]]
local function random_buf(max_size)
2015-03-10 00:44:19 +01:00
local l = ffi.C.ottery_rand_unsigned() % max_size + 1
2015-03-10 00:01:05 +01:00
local buf = ffi.new("unsigned char[?]", l)
ffi.C.ottery_rand_bytes(buf, l)
return buf, l
end
2015-03-10 00:44:19 +01:00
test("Base32 encode test", function()
2015-03-10 00:01:05 +01:00
local cases = {
{'test123', 'wm3g84fg13cy'},
{'hello', 'em3ags7p'}
}
for _,c in ipairs(cases) do
local b = ffi.C.rspamd_encode_base32(c[1], #c[1])
local s = ffi.string(b)
ffi.C.g_free(b)
assert_equal(s, c[2], s .. " not equal " .. c[2])
end
end)
2015-03-10 00:44:19 +01:00
test("Base32 fuzz test", function()
for i = 1,1000 do
local b, l = random_buf(4096)
local ben = ffi.C.rspamd_encode_base32(b, l)
local bs = ffi.string(ben)
local nl = ffi.new("size_t [1]")
local nb = ffi.C.rspamd_decode_base32(bs, #bs, nl)
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)
2015-03-10 00:01:05 +01:00
end)