summaryrefslogtreecommitdiffstats
path: root/src/test/test_base64url.lua
blob: 00aea6a10cbc3e5f9799bbc9023b57d6160a041b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
base64url = require("dnsjit.lib.base64url")
ffi = require("ffi")

-- empty string works
assert(base64url.decode(base64url.encode("")) == "")

-- regular string data
assert(base64url.encode("abcd") == "YWJjZA")
assert(base64url.decode(base64url.encode("abcd")) == "abcd")

-- invalid base64 data
base64url.decode("+")

-- check all symbols - arbitrary binary data
c_array = ffi.new("uint8_t[?]", 256)
bin_symbols = {}
for i = 0, 255 do
    bin_symbols[i + 1] = string.char(i)
    c_array[i] = i
end
bin_str = table.concat(bin_symbols)

assert(base64url.decode(base64url.encode(bin_str)) == bin_str)
assert(base64url.encode(c_array, 256) == base64url.encode(bin_str))