You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base32.lua 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. -- Test zbase32 encoding/decoding
  2. context("Base32 encodning", function()
  3. local ffi = require("ffi")
  4. ffi.cdef[[
  5. void ottery_rand_bytes(void *buf, size_t n);
  6. unsigned ottery_rand_unsigned(void);
  7. unsigned char* rspamd_decode_base32 (const char *in, size_t inlen, size_t *outlen);
  8. char * rspamd_encode_base32 (const unsigned char *in, size_t inlen);
  9. void g_free(void *ptr);
  10. int memcmp(const void *a1, const void *a2, size_t len);
  11. ]]
  12. local function random_buf(max_size)
  13. local l = ffi.C.ottery_rand_unsigned() % max_size + 1
  14. local buf = ffi.new("unsigned char[?]", l)
  15. ffi.C.ottery_rand_bytes(buf, l)
  16. return buf, l
  17. end
  18. test("Base32 encode test", function()
  19. local cases = {
  20. {'test123', 'wm3g84fg13cy'},
  21. {'hello', 'em3ags7p'}
  22. }
  23. for _,c in ipairs(cases) do
  24. local b = ffi.C.rspamd_encode_base32(c[1], #c[1])
  25. local s = ffi.string(b)
  26. ffi.C.g_free(b)
  27. assert_equal(s, c[2], s .. " not equal " .. c[2])
  28. end
  29. end)
  30. test("Base32 fuzz test", function()
  31. for i = 1,1000 do
  32. local b, l = random_buf(4096)
  33. local ben = ffi.C.rspamd_encode_base32(b, l)
  34. local bs = ffi.string(ben)
  35. local nl = ffi.new("size_t [1]")
  36. local nb = ffi.C.rspamd_decode_base32(bs, #bs, nl)
  37. local cmp = ffi.C.memcmp(b, nb, l)
  38. ffi.C.g_free(ben)
  39. ffi.C.g_free(nb)
  40. assert_equal(cmp, 0, "fuzz test failed for length: " .. tostring(l))
  41. end
  42. end)
  43. end)