Bitcoin Core  22.99.0
P2P Digital Currency
base32_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <util/strencodings.h>
6 
7 #include <boost/test/unit_test.hpp>
8 #include <string>
9 
10 using namespace std::literals;
11 
12 BOOST_AUTO_TEST_SUITE(base32_tests)
13 
14 BOOST_AUTO_TEST_CASE(base32_testvectors)
15 {
16  static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
17  static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};
18  static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"};
19  for (unsigned int i=0; i<std::size(vstrIn); i++)
20  {
21  std::string strEnc = EncodeBase32(vstrIn[i]);
22  BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
23  strEnc = EncodeBase32(vstrIn[i], false);
24  BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
25  std::string strDec = DecodeBase32(vstrOut[i]);
26  BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
27  }
28 
29  // Decoding strings with embedded NUL characters should fail
30  bool failure;
31  (void)DecodeBase32("invalid\0"s, &failure); // correct size, invalid due to \0
32  BOOST_CHECK(failure);
33  (void)DecodeBase32("AWSX3VPP"s, &failure); // valid
34  BOOST_CHECK(!failure);
35  (void)DecodeBase32("AWSX3VPP\0invalid"s, &failure); // correct size, invalid due to \0
36  BOOST_CHECK(failure);
37  (void)DecodeBase32("AWSX3VPPinvalid"s, &failure); // invalid size
38  BOOST_CHECK(failure);
39 }
40 
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(base32_testvectors)
Definition: base32_tests.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
strencodings.h
EncodeBase32
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
Definition: strencodings.cpp:206
DecodeBase32
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
Definition: strencodings.cpp:226
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18