Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add a few more base32/64 calculation corner cases #29847

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/test/base32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <util/strencodings.h>

#include <boost/test/unit_test.hpp>
#include <test/util/random.h>

#include <string>

using namespace std::literals;
Expand All @@ -29,9 +31,45 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)

// Decoding strings with embedded NUL characters should fail
BOOST_CHECK(!DecodeBase32("invalid\0"s)); // correct size, invalid due to \0
BOOST_CHECK(DecodeBase32("AWSX3VPP"s)); // valid
BOOST_CHECK( DecodeBase32("AWSX3VPP"s)); // valid
BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"s)); // correct size, invalid due to \0
BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid"s)); // invalid size
}

BOOST_AUTO_TEST_CASE(base32_padding)
{
// Is valid without padding
{
const std::vector<uint8_t> in{'f', 'o', 'o', 'b', 'a'};
const std::string out{"mzxw6ytb"};
BOOST_CHECK_EQUAL(EncodeBase32(in), out);
}

// Valid size
BOOST_CHECK(!DecodeBase32("========"s));
BOOST_CHECK(!DecodeBase32("a======="s));
BOOST_CHECK( DecodeBase32("aa======"s));
BOOST_CHECK(!DecodeBase32("aaa====="s));
BOOST_CHECK( DecodeBase32("aaaa===="s));
BOOST_CHECK( DecodeBase32("aaaaa==="s));
BOOST_CHECK(!DecodeBase32("aaaaaa=="s));
BOOST_CHECK( DecodeBase32("aaaaaaa="s));
}

BOOST_AUTO_TEST_CASE(base32_roundtrip) {
FastRandomContext rng;
for (int i = 0; i < 100; ++i) {
auto chars = rng.randbytes(rng.randrange(32));
std::string randomStr(chars.begin(), chars.end());
auto encoded = EncodeBase32(randomStr);

auto decodedOpt = DecodeBase32(encoded);
BOOST_REQUIRE_MESSAGE(decodedOpt, "Decoding failed for " + encoded);
auto decoded = *decodedOpt;

std::string decodedStr(decoded.begin(), decoded.end());
BOOST_CHECK_MESSAGE(randomStr == decodedStr, "Roundtrip mismatch: original=" + randomStr + ", roundtrip=" + decodedStr);
}
}

BOOST_AUTO_TEST_SUITE_END()
42 changes: 38 additions & 4 deletions src/test/base64_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <util/strencodings.h>

#include <boost/test/unit_test.hpp>
#include <test/util/random.h>

#include <string>

using namespace std::literals;
Expand Down Expand Up @@ -35,10 +37,42 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
}

// Decoding strings with embedded NUL characters should fail
BOOST_CHECK(!DecodeBase64("invalid\0"s));
BOOST_CHECK(DecodeBase64("nQB/pZw="s));
BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s));
BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s));
BOOST_CHECK(!DecodeBase64("invalid\0"s)); // correct size, invalid due to \0
BOOST_CHECK( DecodeBase64("nQB/pZw="s)); // valid
BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s)); // correct size, invalid due to \0
BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s)); // invalid size
}

BOOST_AUTO_TEST_CASE(base64_padding)
{
// Is valid without padding
{
const std::vector<uint8_t> in{'f', 'o', 'o', 'b', 'a', 'r'};
const std::string out{"Zm9vYmFy"};
BOOST_CHECK_EQUAL(EncodeBase64(in), out);
}

// Valid size
BOOST_CHECK(!DecodeBase64("===="s));
BOOST_CHECK(!DecodeBase64("a==="s));
BOOST_CHECK( DecodeBase64("YQ=="s));
BOOST_CHECK( DecodeBase64("YWE="s));
}

BOOST_AUTO_TEST_CASE(base64_roundtrip) {
FastRandomContext rng;
for (int i = 0; i < 100; ++i) {
auto chars = rng.randbytes(rng.randrange(64));
std::string randomStr(chars.begin(), chars.end());
auto encoded = EncodeBase64(randomStr);

auto decodedOpt = DecodeBase64(encoded);
BOOST_REQUIRE_MESSAGE(decodedOpt, "Decoding failed for " + encoded);
auto decoded = *decodedOpt;

std::string decodedStr(decoded.begin(), decoded.end());
BOOST_CHECK_MESSAGE(randomStr == decodedStr, "Roundtrip mismatch: original=" + randomStr + ", roundtrip=" + decodedStr);
}
}

BOOST_AUTO_TEST_SUITE_END()