// SPDX-License-Identifier: MPL-2.0 #include "base64.hpp" #include #include #include #include #include using namespace sequa::core::server; namespace { std::vector bytes_of(std::string_view text) { std::vector bytes; for (const char c : text) { bytes.push_back(static_cast(c)); } return bytes; } } // namespace TEST_CASE("Base64 : vecteurs de la RFC 4648 section 10") { const std::pair vectors[] = { {"", ""}, {"f", "Zg=="}, {"fo", "Zm8="}, {"foo", "Zm9v"}, {"foob", "Zm9vYg=="}, {"fooba", "Zm9vYmE="}, {"foobar", "Zm9vYmFy"}, }; for (const auto& [plain, encoded] : vectors) { CHECK(base64::encode(bytes_of(plain)) == encoded); const auto decoded = base64::decode(encoded); REQUIRE(decoded.has_value()); CHECK(*decoded == bytes_of(plain)); } } TEST_CASE("Base64 : decodage strict") { for (const std::string_view bad : {"Zg", "Zg=", "Z===", "====", "Zm9v!", "Zm=v", "Zh==", "Zm9=", "Zm 9v", "Zm9v\n"}) { CAPTURE(bad); CHECK_FALSE(base64::decode(bad).has_value()); } } TEST_CASE("Base64 : aller-retour de tous les octets") { std::vector all; for (int i = 0; i < 256; ++i) { all.push_back(static_cast(i)); } for (std::size_t length = 0; length <= all.size(); length += 37) { const std::vector part(all.begin(), all.begin() + static_cast(length)); const auto decoded = base64::decode(base64::encode(part)); REQUIRE(decoded.has_value()); CHECK(*decoded == part); } }