Skip to content

Commit

Permalink
test all used basen API
Browse files Browse the repository at this point in the history
Signed-off-by: Rafał Malinowski <[email protected]>
  • Loading branch information
vogel committed Apr 25, 2018
1 parent 503bca5 commit df2f318
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion src/util/DecoderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,136 @@
#include <autocheck/autocheck.hpp>
#include <lib/catch.hpp>
#include <lib/util/basen.h>
#include <map>

TEST_CASE("base64 tests", "[crypto]")
namespace
{

auto b32_data = std::map<std::string, std::string>{
{"", ""},
{"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<std::string, std::string>{
{"", ""},
{"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<std::vector<uint8_t>> input;
// check round trip
Expand Down

0 comments on commit df2f318

Please sign in to comment.