Bitcoin Core  22.99.0
P2P Digital Currency
base64_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-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(base64_tests)
13 
14 BOOST_AUTO_TEST_CASE(base64_testvectors)
15 {
16  static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
17  static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
18  for (unsigned int i=0; i<std::size(vstrIn); i++)
19  {
20  std::string strEnc = EncodeBase64(vstrIn[i]);
21  BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
22  std::string strDec = DecodeBase64(strEnc);
23  BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
24  }
25 
26  // Decoding strings with embedded NUL characters should fail
27  bool failure;
28  (void)DecodeBase64("invalid\0"s, &failure);
29  BOOST_CHECK(failure);
30  (void)DecodeBase64("nQB/pZw="s, &failure);
31  BOOST_CHECK(!failure);
32  (void)DecodeBase64("nQB/pZw=\0invalid"s, &failure);
33  BOOST_CHECK(failure);
34  (void)DecodeBase64("nQB/pZw=invalid\0"s, &failure);
35  BOOST_CHECK(failure);
36 }
37 
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
EncodeBase64
std::string EncodeBase64(Span< const unsigned char > input)
Definition: strencodings.cpp:131
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
strencodings.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(base64_testvectors)
Definition: base64_tests.cpp:14
DecodeBase64
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid)
Definition: strencodings.cpp:147
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18