local testimony = require("testimony") local litls = require("litls.core") local testify = testimony.new("== LITLS Base64 ==") -- RFC 4648 Section 10 test vectors testify:that("base64 encode empty string", function() testimony.assert_equal("", litls.base64_encode("")) end) testify:that("base64 encode 'f'", function() testimony.assert_equal("Zg==", litls.base64_encode("f")) end) testify:that("base64 encode 'fo'", function() testimony.assert_equal("Zm8=", litls.base64_encode("fo")) end) testify:that("base64 encode 'foo'", function() testimony.assert_equal("Zm9v", litls.base64_encode("foo")) end) testify:that("base64 encode 'foob'", function() testimony.assert_equal("Zm9vYg==", litls.base64_encode("foob")) end) testify:that("base64 encode 'fooba'", function() testimony.assert_equal("Zm9vYmE=", litls.base64_encode("fooba")) end) testify:that("base64 encode 'foobar'", function() testimony.assert_equal("Zm9vYmFy", litls.base64_encode("foobar")) end) testify:that("base64 decode 'Zm9vYmFy'", function() testimony.assert_equal("foobar", litls.base64_decode("Zm9vYmFy")) end) testify:that("base64 decode with padding", function() testimony.assert_equal("foob", litls.base64_decode("Zm9vYg==")) testimony.assert_equal("fooba", litls.base64_decode("Zm9vYmE=")) end) testify:that("base64 decode empty", function() testimony.assert_equal("", litls.base64_decode("")) end) testify:that("base64 round-trip binary data", function() local bin = litls.hex_decode("deadbeef00ff") local encoded = litls.base64_encode(bin) local decoded = litls.base64_decode(encoded) testimony.assert_equal(bin, decoded) end) -- Base64url testify:that("base64url encode (no padding)", function() -- base64url: + -> -, / -> _, no padding local bin = litls.hex_decode("fbff") -- would produce +/8 in base64 local b64 = litls.base64_encode(bin) local b64url = litls.base64url_encode(bin) -- base64url should not have = padding or + or / testimony.assert_true(not b64url:find("[=+/]")) end) testify:that("base64url round-trip", function() local data = "Hello, World!" local encoded = litls.base64url_encode(data) local decoded = litls.base64url_decode(encoded) testimony.assert_equal(data, decoded) end) -- Random bytes testify:that("random_bytes returns requested length", function() local r = litls.random_bytes(32) testimony.assert_equal(32, #r) end) testify:that("random_bytes returns different values", function() local a = litls.random_bytes(16) local b = litls.random_bytes(16) testimony.assert_true(a ~= b) end) -- Hex helpers testify:that("hex encode/decode round-trip", function() local hex = litls.hex_encode(litls.hex_decode("48656c6c6f")) testimony.assert_equal("48656c6c6f", hex) end) testify:conclude()