#include #include #include #include #include #include #include #include #include #include #include namespace { using Void::Encoding::Base32::decode; using Void::Encoding::Base32::decode_bytes; using Void::Encoding::Base32::decode_crockford; using Void::Encoding::Base32::encode; using Void::Encoding::Base32::encode_crockford; std::string strip_padding(std::string text) { while (!text.empty() && text.back() == '=') { text.pop_back(); } return text; } std::string lowercase(std::string text) { for (char& c : text) { if (c >= 'A' && c <= 'Z') { c = static_cast(c + ('a' - 'A')); } } return text; } std::string pattern(std::size_t size) { std::string out; out.reserve(size); for (std::size_t i = 0; i < size; ++i) { out.push_back(static_cast((i * 37 + 11) & 0xFF)); } return out; } std::array five_bytes(std::uint64_t value) { std::array bytes{}; for (std::size_t i = 0; i < bytes.size(); ++i) { bytes[i] = static_cast( (value >> (8 * (bytes.size() - 1 - i))) & 0xFF); } return bytes; } std::string as_text(const std::array& bytes) { return std::string{reinterpret_cast(bytes.data()), bytes.size()}; } } // namespace // ── RFC 4648 ──────────────────────────────────────────────────────────────── Void::Testing::Test base32_rfc4648_section10_vectors() { struct Vector { std::string_view plain; std::string_view encoded; }; constexpr std::array vectors{ Vector{"", ""}, Vector{"f", "MY======"}, Vector{"fo", "MZXQ===="}, Vector{"foo", "MZXW6==="}, Vector{"foob", "MZXW6YQ="}, Vector{"fooba", "MZXW6YTB"}, Vector{"foobar", "MZXW6YTBOI======"}, }; for (const auto& [plain, encoded] : vectors) { const std::string padded{encoded}; const std::string unpadded = strip_padding(padded); Void::Testing::expect_eq(encode(plain), padded); const auto padded_decoded = decode(padded); Void::Testing::require(padded_decoded.has_value()); Void::Testing::expect_eq(*padded_decoded, std::string{plain}); const auto unpadded_decoded = decode(unpadded); Void::Testing::require(unpadded_decoded.has_value()); Void::Testing::expect_eq(*unpadded_decoded, std::string{plain}); const auto lower_decoded = decode(lowercase(padded)); Void::Testing::require(lower_decoded.has_value()); Void::Testing::expect_eq(*lower_decoded, std::string{plain}); const auto lower_unpadded_decoded = decode(lowercase(unpadded)); Void::Testing::require(lower_unpadded_decoded.has_value()); Void::Testing::expect_eq(*lower_unpadded_decoded, std::string{plain}); } } Void::Testing::Test base32_rfc4648_every_alphabet_symbol() { constexpr std::string_view alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; for (std::uint64_t value = 0; value < 32; ++value) { std::uint64_t packed = 0; for (int group = 0; group < 8; ++group) { packed = (packed << 5) | value; } const auto bytes = five_bytes(packed); const std::string text = as_text(bytes); const std::string encoded = encode(std::span{bytes}); Void::Testing::expect_eq(encoded, std::string(8, alphabet[value])); const auto decoded = decode(encoded); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, text); } } Void::Testing::Test base32_rfc4648_rejects_invalid_text() { // Invalid characters (including whitespace, which is not skipped). for (const std::string_view text : {"MZ$Q====", "MZXW6YT!", "MY======\n", " MY====="}) { Void::Testing::expect(!decode(text).has_value()); } // Impossible symbol counts: remainder 1, 3 and 6 modulo 8. for (const std::string_view text : {"M", "MZX", "MZXW6Y"}) { Void::Testing::expect(!decode(text).has_value()); } // Malformed padding. for (const std::string_view text : {"MY=====", "MY=======", "M=======", "MY=====A", "MY=A====", "MZXW6Y=", "MZXW6YTB="}) { Void::Testing::expect(!decode(text).has_value()); } // Non-zero trailing pad bits for every remainder class. for (const std::string_view text : {"MZ======", "MZXR", "MZXW7", "MZXW6YR"}) { Void::Testing::expect(!decode(text).has_value()); } // The empty text is valid and decodes to nothing. const auto empty = decode(""); Void::Testing::require(empty.has_value()); Void::Testing::expect(empty->empty()); } Void::Testing::Test base32_rfc4648_round_trips() { constexpr std::array boundaries{ 1, 2, 3, 4, 5, 6, 9, 10, 11, 15, 16, 17, 31, 32, 33, 100, 255, 1024}; for (std::size_t size = 0; size <= 64; ++size) { const std::string original = pattern(size); const std::string encoded = encode(original); const auto padded = decode(encoded); Void::Testing::require(padded.has_value()); Void::Testing::expect_eq(*padded, original); const auto unpadded = decode(strip_padding(encoded)); Void::Testing::require(unpadded.has_value()); Void::Testing::expect_eq(*unpadded, original); const auto lower = decode(lowercase(encoded)); Void::Testing::require(lower.has_value()); Void::Testing::expect_eq(*lower, original); const auto bytes = decode_bytes(encoded); Void::Testing::require(bytes.has_value()); Void::Testing::expect_eq(bytes->size(), original.size()); } for (const std::size_t size : boundaries) { const std::string original = pattern(size); const auto decoded = decode(encode(original)); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, original); const auto crockford = decode_crockford(encode_crockford(original)); Void::Testing::require(crockford.has_value()); Void::Testing::expect_eq(*crockford, original); } } // ── Crockford ─────────────────────────────────────────────────────────────── Void::Testing::Test base32_crockford_aliases_and_separators() { const auto one = decode_crockford("10"); const auto zero = decode_crockford("00"); Void::Testing::require(one.has_value()); Void::Testing::require(zero.has_value()); for (const std::string_view alias : {"I0", "L0", "i0", "l0"}) { const auto decoded = decode_crockford(alias); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, *one); } for (const std::string_view alias : {"O0", "o0"}) { const auto decoded = decode_crockford(alias); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, *zero); } const auto separated = decode_crockford("A-B-C0"); const auto plain = decode_crockford("ABC0"); Void::Testing::require(separated.has_value()); Void::Testing::require(plain.has_value()); Void::Testing::expect_eq(*separated, *plain); // U is rejected, as are other invalid characters and padding. for (const std::string_view text : {"U0", "u0", "!0", "AB==", "01"}) { Void::Testing::expect(!decode_crockford(text).has_value()); } } Void::Testing::Test base32_crockford_every_alphabet_symbol() { constexpr std::string_view alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; for (std::uint64_t value = 0; value < 32; ++value) { std::uint64_t packed = 0; for (int group = 0; group < 8; ++group) { packed = (packed << 5) | value; } const auto bytes = five_bytes(packed); const std::string text = as_text(bytes); const std::string encoded = encode_crockford(std::span{bytes}); Void::Testing::expect_eq(encoded, std::string(8, alphabet[value])); const auto decoded = decode_crockford(encoded); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, text); } } Void::Testing::Test base32_crockford_round_trips() { for (std::size_t size = 0; size <= 64; ++size) { const std::string original = pattern(size); const std::string encoded = encode_crockford(original); Void::Testing::expect(encoded.find('=') == std::string::npos); const auto decoded = decode_crockford(encoded); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, original); const auto lower = decode_crockford(lowercase(encoded)); Void::Testing::require(lower.has_value()); Void::Testing::expect_eq(*lower, original); } } // ── Bounds ────────────────────────────────────────────────────────────────── Void::Testing::Test base32_respects_text_limits() { using Void::Text::TextLimits; const std::string valid = "MZXW6YTB"; // 5 decoded bytes Void::Testing::expect(decode(valid).has_value()); Void::Testing::expect(!decode(valid, TextLimits{.max_input = 7}).has_value()); Void::Testing::expect( !decode(valid, TextLimits{.max_output = 4}).has_value()); Void::Testing::expect( decode(valid, TextLimits{.max_output = 5}).has_value()); Void::Testing::expect( !decode_crockford("MZXW6YTB", TextLimits{.max_input = 7}).has_value()); Void::Testing::expect( !decode_crockford("MZXW6YTB", TextLimits{.max_output = 4}).has_value()); } // ── Void::Text facade ────────────────────────────────────────────────────── Void::Testing::Test base32_text_facade_matches_encoding_namespace() { using Void::Text::base32_decode; using Void::Text::base32_encode; const std::array data{std::byte{'f'}, std::byte{'o'}, std::byte{'o'}}; const std::string padded = base32_encode(std::span{data}); Void::Testing::expect_eq(padded, std::string{"MZXW6==="}); Void::Testing::expect_eq( base32_encode(std::span{data}, false), std::string{"MZXW6"}); const auto decoded = base32_decode(padded); Void::Testing::require(decoded.has_value()); Void::Testing::expect_eq(*decoded, std::string{"foo"}); const auto decoded_unpadded = base32_decode(std::string_view{"MZXW6"}); Void::Testing::require(decoded_unpadded.has_value()); Void::Testing::expect_eq(*decoded_unpadded, std::string{"foo"}); const auto bytes = std::as_bytes(std::span{padded.data(), padded.size()}); const auto decoded_bytes = base32_decode(bytes); Void::Testing::require(decoded_bytes.has_value()); Void::Testing::expect_eq(*decoded_bytes, std::string{"foo"}); Void::Testing::expect(!base32_decode(std::string_view{"MZXW6!"}).has_value()); Void::Testing::expect(!base32_decode(std::string_view{"MZXW6=="}).has_value()); }