Branch data Line data Source code
1 : : // Copyright (c) 2012-2022 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 : :
9 : : #include <algorithm>
10 : : #include <string>
11 : :
12 : : using namespace std::literals;
13 : :
14 : : BOOST_AUTO_TEST_SUITE(base32_tests)
15 : :
16 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(base32_testvectors)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
17 : : {
18 [ + - + - : 3 : static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
+ - + - +
- + - + -
+ - + - -
- ]
19 [ + - + - : 2 : static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};
+ - + - +
- + - + -
+ - + - -
- ]
20 [ + - + - : 2 : static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"};
+ - + - +
- + - + -
+ - + - -
- ]
21 [ + + ]: 8 : for (unsigned int i=0; i<std::size(vstrIn); i++)
22 : : {
23 : 7 : std::string strEnc = EncodeBase32(vstrIn[i]);
24 [ + - + - ]: 7 : BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
25 [ + - ]: 7 : strEnc = EncodeBase32(vstrIn[i], false);
26 [ + - + - ]: 7 : BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
27 [ + - ]: 7 : auto dec = DecodeBase32(vstrOut[i]);
28 [ + - + - : 14 : BOOST_REQUIRE(dec);
+ - ]
29 [ + - + - ]: 14 : BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]);
30 : 7 : }
31 : :
32 : : // Decoding strings with embedded NUL characters should fail
33 [ + - + - : 2 : BOOST_CHECK(!DecodeBase32("invalid\0"s)); // correct size, invalid due to \0
+ - ]
34 [ + - + - : 2 : BOOST_CHECK(DecodeBase32("AWSX3VPP"s)); // valid
+ - ]
35 [ + - + - : 2 : BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"s)); // correct size, invalid due to \0
+ - ]
36 [ + - + - : 2 : BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid"s)); // invalid size
+ - ]
37 : 1 : }
38 : :
39 : : BOOST_AUTO_TEST_SUITE_END()
|