]> source.dussan.org Git - rspamd.git/commitdiff
Add unit tests for b64 decoding and native encoding.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 25 May 2015 16:52:38 +0000 (17:52 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 25 May 2015 16:52:38 +0000 (17:52 +0100)
test/lua/unit/base64.lua

index 227b911761d1e32b1023ce1ea6a75ee0981cbf76..98095cc88374e70c7cf2f0a54d9319aac1b681d0 100644 (file)
@@ -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
@@ -53,6 +84,16 @@ vehemence of any carnal pleasure.]]
   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)
+    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]")