Skip to content

Commit 11c50ab

Browse files
committed
tests: added random test for base16 and base64
1 parent 5d0d65e commit 11c50ab

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

t/base16.t

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,39 @@ decode: aaa: invalid input
145145
decode: agA0: invalid input
146146
decode: aaaR: invalid input
147147
decode: aaaaaR: invalid input
148+
149+
150+
151+
=== TEST 6: random tests
152+
--- lua
153+
local start = ngx.now()
154+
while ture do
155+
for _ = 1, 1000 do
156+
local size = math.random(1, 20)
157+
local buf = table.new(size, 0)
158+
for i = 1, size do
159+
buf[i] = math.random(33, 126)
160+
end
161+
162+
local raw = string.char(unpack(buf))
163+
local encoded = base_encoding.encode_base16(raw)
164+
if base_encoding.decode_base16(encoded) ~= raw then
165+
ngx.say("failed case: ", raw)
166+
return
167+
end
168+
169+
local encoded = base_encoding.encode_base16(raw, true)
170+
if base_encoding.decode_base16(encoded) ~= raw then
171+
ngx.say("failed case with lowercase: ", raw)
172+
return
173+
end
174+
end
175+
176+
ngx.update_time()
177+
if ngx.now() - start > 3 then
178+
break
179+
end
180+
end
181+
ngx.say("ok")
182+
--- response_body
183+
ok

t/base64.t

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,45 @@ local res, err = decode_base64url(" ")
225225
ngx.say("decode_base64url returned: ", res, ", ", err)
226226
--- response_body
227227
decode_base64url returned: nil, invalid input
228+
229+
230+
231+
=== TEST 15: random tests
232+
--- lua
233+
local start = ngx.now()
234+
while ture do
235+
for _ = 1, 1000 do
236+
local size = math.random(1, 20)
237+
local buf = table.new(size, 0)
238+
for i = 1, size do
239+
buf[i] = math.random(33, 126)
240+
end
241+
242+
local raw = string.char(unpack(buf))
243+
local encoded = base_encoding.encode_base64(raw)
244+
if base_encoding.decode_base64(encoded) ~= raw then
245+
ngx.say("failed case: ", raw)
246+
return
247+
end
248+
249+
local encoded = base_encoding.encode_base64(raw, true)
250+
if base_encoding.decode_base64(encoded) ~= raw then
251+
ngx.say("failed case without padding: ", raw)
252+
return
253+
end
254+
255+
local encoded = base_encoding.encode_base64url(raw)
256+
if base_encoding.decode_base64url(encoded) ~= raw then
257+
ngx.say("failed case in url variant: ", raw)
258+
return
259+
end
260+
end
261+
262+
ngx.update_time()
263+
if ngx.now() - start > 3 then
264+
break
265+
end
266+
end
267+
ngx.say("ok")
268+
--- response_body
269+
ok

0 commit comments

Comments
 (0)