aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-25 17:52:38 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-05-25 17:52:38 +0100
commit8ca5cb79b2ebcfbfe967a2d2c24f4599f00726fa (patch)
tree611b5ceaf4d1682b3ce77980d9aa81bed1ace3c6
parent122b896ba0b85f7d44c7417acd283f6a6ca83e4c (diff)
downloadrspamd-8ca5cb79b2ebcfbfe967a2d2c24f4599f00726fa.tar.gz
rspamd-8ca5cb79b2ebcfbfe967a2d2c24f4599f00726fa.zip
Add unit tests for b64 decoding and native encoding.
-rw-r--r--test/lua/unit/base64.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/lua/unit/base64.lua b/test/lua/unit/base64.lua
index 227b91176..98095cc88 100644
--- a/test/lua/unit/base64.lua
+++ b/test/lua/unit/base64.lua
@@ -1,5 +1,6 @@
context("Base64 encodning", function()
local ffi = require("ffi")
+ local util = require("rspamd_util")
ffi.cdef[[
void ottery_rand_bytes(void *buf, size_t n);
unsigned ottery_rand_unsigned(void);
@@ -18,6 +19,19 @@ context("Base64 encodning", function()
return buf, l
end
+ local function random_safe_buf(max_size)
+ local l = ffi.C.ottery_rand_unsigned() % max_size + 1
+ local buf = ffi.new("unsigned char[?]", l)
+
+ for i = 0,l-1 do
+ buf[i] = ffi.C.ottery_rand_unsigned() % 20 + string.byte('A')
+ end
+
+ buf[l - 1] = 0;
+
+ return buf, l
+ end
+
test("Base64 encode test", function()
local cases = {
{"", ""},
@@ -38,6 +52,23 @@ context("Base64 encodning", function()
end
end)
+ test("Base64 decode test", function()
+ local cases = {
+ {"", ""},
+ {"f", "Zg=="},
+ {"fo", "Zm8="},
+ {"foo", "Zm9v"},
+ {"foob", "Zm9vYg=="},
+ {"fooba", "Zm9vYmE="},
+ {"foobar", "Zm9vYmFy"},
+ }
+
+ for _,c in ipairs(cases) do
+ local b = tostring(util.decode_base64(c[2]))
+ assert_equal(b, c[1], b .. " not equal " .. c[1])
+ end
+ end)
+
test("Base64 line split encode test", function()
local text = [[
Man is distinguished, not only by his reason, but by this singular passion from
@@ -54,6 +85,16 @@ vehemence of any carnal pleasure.]]
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