47 lines
2.1 KiB
Luau
47 lines
2.1 KiB
Luau
-- codecs/Base64 tests: encode / decode against Python's base64 (standard, URL-safe, unpadded) for every length 0..70
|
|
-- and some large inputs, whitespace and missing padding on decode, data: URIs, bad input, and the fuzz no-throw pass.
|
|
-- Fixtures: tools/fixtures/base64.py.
|
|
local T = require("@tests/T")
|
|
local Base64 = require("@formats/codecs/Base64")
|
|
local Cases = require("@tests/gen/base64_cases")
|
|
|
|
local t = T.suite("base64")
|
|
|
|
for _, c in Cases do
|
|
local b = buffer.fromstring(c.data)
|
|
t:eq(Base64.encode(b), c.std, "encode " .. #c.data)
|
|
t:eq(Base64.encode(b, { url = true }), c.url, "encode url " .. #c.data)
|
|
t:eq(Base64.encode(b, { pad = false }), c.nopad, "encode unpadded " .. #c.data)
|
|
for _, s in { c.std, c.url, c.nopad } do
|
|
local d, err = Base64.decode(s)
|
|
t:check(d and buffer.tostring(d) == c.data, "decode " .. #c.data .. ": " .. tostring(err))
|
|
end
|
|
-- with line breaks every 76 characters (MIME)
|
|
local wrapped = string.gsub(c.std, "(" .. string.rep(".", 76) .. ")", "%1\r\n")
|
|
local d = Base64.decode(wrapped)
|
|
t:check(d and buffer.tostring(d) == c.data, "decode wrapped " .. #c.data)
|
|
end
|
|
-- a sub-range
|
|
t:eq(Base64.encode(buffer.fromstring("xxhelloyy"), nil, 2, 5), "aGVsbG8=", "encode a range")
|
|
-- data URIs
|
|
local b, mime = Base64.decodeDataUri("data:application/gltf-buffer;base64,AAEC")
|
|
t:check(b and buffer.len(b) == 3 and buffer.readu8(b, 2) == 2 and mime == "application/gltf-buffer", "data URI")
|
|
local b2, m2 = Base64.decodeDataUri("data:,a%20b")
|
|
t:check(b2 and buffer.tostring(b2) == "a b" and m2 == "text/plain", "percent-encoded data URI")
|
|
t:eq(Base64.dataUri(buffer.fromstring("hi"), "text/plain"), "data:text/plain;base64,aGk=", "dataUri")
|
|
-- bad input
|
|
t:check(Base64.decode("ab$d") == nil, "bad character")
|
|
t:check(Base64.decode("abcde") == nil, "a lone last character")
|
|
t:check(Base64.decode("ab==cd") == nil, "data after padding")
|
|
t:check(Base64.decodeDataUri("http://x") == nil, "not a data URI")
|
|
local samples = {}
|
|
for i, c in Cases do
|
|
if i % 10 == 0 then
|
|
table.insert(samples, c.std)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(x: buffer)
|
|
return Base64.decode(x)
|
|
end, samples)
|
|
t:done()
|