core/Base64 takes over codecs/Base64's API (encode(buf | string, { url, pad, wrap }, offset, length), decode of both
alphabets, decodeDataUri, dataUri) plus line wrapping (opts.wrap) for plist; codecs/Base64 is removed and glTF /
DAE require core/Base64. The private copies in data/plist (Base64.luau), data/yaml (Parse / Emit) and codecs/Brotli
(the static tables) are gone. tests/codecs/base64 merged into tests/core/base64.
69 lines
3.3 KiB
Luau
69 lines
3.3 KiB
Luau
-- core/Base64 tests: the RFC 4648 vectors, 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/core/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
|
|
for _, v in { { "", "" }, { "f", "Zg==" }, { "fo", "Zm8=" }, { "foo", "Zm9v" }, { "foob", "Zm9vYg==" }, { "fooba", "Zm9vYmE=" }, { "foobar", "Zm9vYmFy" } } do
|
|
t:eq(Base64.encode(v[1]), v[2], "encode " .. v[1])
|
|
t:eq(buffer.tostring(Base64.decode(v[2]) :: buffer), v[1], "decode " .. v[2])
|
|
end
|
|
t:eq(buffer.tostring(Base64.decode(" Zm9v\nYmE ") :: buffer), "fooba", "whitespace and no padding")
|
|
t:eq(buffer.tostring(Base64.decode("-_8=") :: buffer), "\251\255", "URL-safe alphabet")
|
|
local b, err = Base64.decode("Zm9v!")
|
|
t:check(b == nil and err, "bad character")
|
|
local b2 = Base64.decode("Zg==Zg")
|
|
t:check(b2 == nil, "data after padding")
|
|
local rand = T.rng(5)
|
|
for n = 0, 40 do
|
|
local s = buffer.create(n)
|
|
for i = 0, n - 1 do
|
|
buffer.writeu8(s, i, rand(256))
|
|
end
|
|
t:eq(buffer.tostring(Base64.decode(Base64.encode(s)) :: buffer), buffer.tostring(s), "round trip " .. n)
|
|
end
|
|
-- line wrapping (plist / MIME)
|
|
t:eq(Base64.encode("foobarfoobarfo", { wrap = 8 }), "Zm9vYmFy\nZm9vYmFy\nZm8=", "wrap 8")
|
|
t:eq(Base64.encode("foobarfoobar", { wrap = 8 }), "Zm9vYmFy\nZm9vYmFy", "wrap at the end")
|
|
t:eq(Base64.encode("foobarfoobar", { wrap = 9 }), "Zm9vYmFy\nZm9vYmFy", "wrap rounds to groups")
|
|
-- 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()
|