From badd07703cdc03ca5ac003761a6f0dcc19cac7b4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 9 Mar 2015 23:44:19 +0000 Subject: [PATCH] Add fuzz testing for base32. --- test/lua/unit/base32.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test/lua/unit/base32.lua b/test/lua/unit/base32.lua index d0a40cdd8..07fe14129 100644 --- a/test/lua/unit/base32.lua +++ b/test/lua/unit/base32.lua @@ -8,17 +8,18 @@ context("Base32 encodning", function() 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); + int memcmp(const void *a1, const void *a2, size_t len); ]] local function random_buf(max_size) - local l = ffi.C.ottery_rand_unsigned() + local l = ffi.C.ottery_rand_unsigned() % max_size + 1 local buf = ffi.new("unsigned char[?]", l) ffi.C.ottery_rand_bytes(buf, l) return buf, l end - test("Base32 exact test", function() + test("Base32 encode test", function() local cases = { {'test123', 'wm3g84fg13cy'}, {'hello', 'em3ags7p'} @@ -31,4 +32,19 @@ context("Base32 encodning", function() assert_equal(s, c[2], s .. " not equal " .. c[2]) end end) + + 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) end) \ No newline at end of file -- 2.39.5