LCOV - code coverage report
Current view: top level - src/test - base58_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 93.0 % 57 53
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 6 6
Branches: 50.0 % 416 208

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

Generated by: LCOV version 2.0-1