diff --git a/src/util/DecoderTests.cpp b/src/util/DecoderTests.cpp index c4fa3acaf5..b34d6c8a81 100644 --- a/src/util/DecoderTests.cpp +++ b/src/util/DecoderTests.cpp @@ -5,8 +5,136 @@ #include #include #include +#include -TEST_CASE("base64 tests", "[crypto]") +namespace +{ + +auto b32_data = std::map{ + {"", ""}, + {"1", "GE======"}, + {"12", "GEZA===="}, + {"123", "GEZDG==="}, + {"1234", "GEZDGNA="}, + {"12345", "GEZDGNBV"}, + {"123456", "GEZDGNBVGY======"}, + {"1234567", "GEZDGNBVGY3Q===="}, + {"12345678", "GEZDGNBVGY3TQ==="}, + {"123456789", "GEZDGNBVGY3TQOI="}, + {"123456789a", "GEZDGNBVGY3TQOLB"}, + {"123456789ab", "GEZDGNBVGY3TQOLBMI======"}, + {"123456789abc", "GEZDGNBVGY3TQOLBMJRQ===="}, + {"123456789abcd", "GEZDGNBVGY3TQOLBMJRWI==="}, + {"123456789abcde", "GEZDGNBVGY3TQOLBMJRWIZI="}, + {"123456789abcdef", "GEZDGNBVGY3TQOLBMJRWIZLG"}}; + +auto b64_data = std::map{ + {"", ""}, + {"1", "MQ=="}, + {"12", "MTI="}, + {"123", "MTIz"}, + {"1234", "MTIzNA=="}, + {"12345", "MTIzNDU="}, + {"123456", "MTIzNDU2"}, + {"1234567", "MTIzNDU2Nw=="}, + {"12345678", "MTIzNDU2Nzg="}, + {"123456789", "MTIzNDU2Nzg5"}, + {"123456789a", "MTIzNDU2Nzg5YQ=="}, + {"123456789ab", "MTIzNDU2Nzg5YWI="}, + {"123456789abc", "MTIzNDU2Nzg5YWJj"}, + {"123456789abcd", "MTIzNDU2Nzg5YWJjZA=="}, + {"123456789abcde", "MTIzNDU2Nzg5YWJjZGU="}, + {"123456789abcdef", "MTIzNDU2Nzg5YWJjZGVm"}}; +} + +TEST_CASE("encode_b32", "[decoder]") +{ + for (auto const& item : b32_data) + { + SECTION(item.first) + { + REQUIRE(item.second == bn::encode_b32(item.first)); + } + } +} + +TEST_CASE("encode_b64", "[decoder]") +{ + for (auto const& item : b64_data) + { + SECTION(item.first) + { + REQUIRE(item.second == bn::encode_b64(item.first)); + } + } +} + +TEST_CASE("encoded_size32", "[decoder]") +{ + for (auto const& item : b32_data) + { + SECTION(item.first) + { + REQUIRE(item.second.size() == + bn::encoded_size32(item.first.size())); + } + } +} + +TEST_CASE("encoded_size64", "[decoder]") +{ + for (auto const& item : b64_data) + { + SECTION(item.first) + { + REQUIRE(item.second.size() == + bn::encoded_size64(item.first.size())); + } + } +} + +TEST_CASE("decode_b32", "[decoder]") +{ + for (auto const& item : b32_data) + { + SECTION(item.second) + { + auto out = std::string{}; + bn::decode_b32(item.second, out); + REQUIRE(item.first == out); + } + } +} + +TEST_CASE("decode_b64", "[decoder]") +{ + for (auto const& item : b64_data) + { + SECTION(item.second) + { + auto out = std::string{}; + bn::decode_b64(item.second, out); + REQUIRE(item.first == out); + } + } +} + +TEST_CASE("decode_b64 with iterators", "[decoder]") +{ + for (auto const& item : b64_data) + { + SECTION(item.second) + { + auto out = std::string{}; + out.reserve(item.second.size()); + bn::decode_b64(std::begin(item.second), std::end(item.second), + std::back_inserter(out)); + REQUIRE(item.first == out); + } + } +} + +TEST_CASE("base64 roundtrip", "[decoder]") { autocheck::generator> input; // check round trip