LCOV - code coverage report
Current view: top level - src/test - base58_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 55 59 93.2 %
Date: 2020-09-26 01:30:44 Functions: 23 23 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2011-2019 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 <test/data/base58_encode_decode.json.h>
       6             : 
       7             : #include <base58.h>
       8             : #include <test/util/setup_common.h>
       9             : #include <util/strencodings.h>
      10             : #include <util/vector.h>
      11             : 
      12             : #include <univalue.h>
      13             : 
      14             : #include <boost/test/unit_test.hpp>
      15             : 
      16             : 
      17             : extern UniValue read_json(const std::string& jsondata);
      18             : 
      19          89 : BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
      20             : 
      21             : // Goal: test low-level base58 encoding functionality
      22          95 : BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
      23             : {
      24           1 :     UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
      25          15 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
      26          14 :         UniValue test = tests[idx];
      27          14 :         std::string strTest = test.write();
      28          14 :         if (test.size() < 2) // Allow for extra stuff (useful for comments)
      29             :         {
      30           0 :             BOOST_ERROR("Bad test: " << strTest);
      31           0 :             continue;
      32             :         }
      33          14 :         std::vector<unsigned char> sourcedata = ParseHex(test[0].get_str());
      34          14 :         std::string base58string = test[1].get_str();
      35          14 :         BOOST_CHECK_MESSAGE(
      36             :                     EncodeBase58(sourcedata) == base58string,
      37             :                     strTest);
      38          14 :     }
      39           1 : }
      40             : 
      41             : // Goal: test low-level base58 decoding functionality
      42          95 : BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
      43             : {
      44           1 :     UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode)));
      45           1 :     std::vector<unsigned char> result;
      46             : 
      47          15 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
      48          14 :         UniValue test = tests[idx];
      49          14 :         std::string strTest = test.write();
      50          14 :         if (test.size() < 2) // Allow for extra stuff (useful for comments)
      51             :         {
      52           0 :             BOOST_ERROR("Bad test: " << strTest);
      53           0 :             continue;
      54             :         }
      55          14 :         std::vector<unsigned char> expected = ParseHex(test[0].get_str());
      56          14 :         std::string base58string = test[1].get_str();
      57          14 :         BOOST_CHECK_MESSAGE(DecodeBase58(base58string, result, 256), strTest);
      58          14 :         BOOST_CHECK_MESSAGE(result.size() == expected.size() && std::equal(result.begin(), result.end(), expected.begin()), strTest);
      59          14 :     }
      60             : 
      61           1 :     BOOST_CHECK(!DecodeBase58("invalid", result, 100));
      62           1 :     BOOST_CHECK(!DecodeBase58(std::string("invalid"), result, 100));
      63           1 :     BOOST_CHECK(!DecodeBase58(std::string("\0invalid", 8), result, 100));
      64             : 
      65           1 :     BOOST_CHECK(DecodeBase58(std::string("good", 4), result, 100));
      66           1 :     BOOST_CHECK(!DecodeBase58(std::string("bad0IOl", 7), result, 100));
      67           1 :     BOOST_CHECK(!DecodeBase58(std::string("goodbad0IOl", 11), result, 100));
      68           1 :     BOOST_CHECK(!DecodeBase58(std::string("good\0bad0IOl", 12), result, 100));
      69             : 
      70             :     // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
      71           1 :     BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result, 3));
      72           1 :     BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result, 3));
      73           1 :     std::vector<unsigned char> expected = ParseHex("971a55");
      74           1 :     BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
      75             : 
      76           1 :     BOOST_CHECK(DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh", 21), result, 100));
      77           1 :     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oi", 21), result, 100));
      78           1 :     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh0IOl", 25), result, 100));
      79           1 :     BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh\00IOl", 26), result, 100));
      80           1 : }
      81             : 
      82          95 : BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
      83             : {
      84        1001 :     for (int n = 0; n < 1000; ++n) {
      85        1000 :         unsigned int len = 1 + InsecureRandBits(8);
      86        1000 :         unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
      87        1000 :         auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
      88        1000 :         auto encoded = EncodeBase58Check(data);
      89        1000 :         std::vector<unsigned char> decoded;
      90        1000 :         auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
      91        1000 :         BOOST_CHECK(!ok_too_small);
      92        1000 :         auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
      93        1000 :         BOOST_CHECK(ok);
      94        1000 :         BOOST_CHECK(data == decoded);
      95        1000 :     }
      96           1 : }
      97             : 
      98          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15