LCOV - code coverage report
Current view: top level - src/test - key_io_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 93.5 % 92 86
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 6 6
Branches: 51.1 % 440 225

             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/key_io_invalid.json.h>
       6                 :             : #include <test/data/key_io_valid.json.h>
       7                 :             : 
       8                 :             : #include <key.h>
       9                 :             : #include <key_io.h>
      10                 :             : #include <script/script.h>
      11                 :             : #include <test/util/json.h>
      12                 :             : #include <test/util/setup_common.h>
      13                 :             : #include <util/chaintype.h>
      14                 :             : #include <util/strencodings.h>
      15                 :             : 
      16                 :             : #include <boost/test/unit_test.hpp>
      17                 :             : 
      18                 :             : #include <univalue.h>
      19                 :             : 
      20                 :             : BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup)
      21                 :             : 
      22                 :             : // Goal: check that parsed keys match test payload
      23   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(key_io_valid_parse)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      24                 :             : {
      25                 :           1 :     UniValue tests = read_json(json_tests::key_io_valid);
      26                 :           1 :     CKey privkey;
      27                 :           1 :     CTxDestination destination;
      28         [ +  - ]:           1 :     SelectParams(ChainType::MAIN);
      29                 :             : 
      30         [ +  + ]:          71 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
      31         [ +  - ]:          70 :         const UniValue& test = tests[idx];
      32         [ +  - ]:          70 :         std::string strTest = test.write();
      33         [ -  + ]:          70 :         if (test.size() < 3) { // Allow for extra stuff (useful for comments)
      34   [ #  #  #  # ]:           0 :             BOOST_ERROR("Bad test: " << strTest);
      35                 :           0 :             continue;
      36                 :             :         }
      37   [ +  -  +  -  :          70 :         std::string exp_base58string = test[0].get_str();
                   +  - ]
      38   [ +  -  +  -  :          70 :         const std::vector<std::byte> exp_payload{ParseHex<std::byte>(test[1].get_str())};
                   +  - ]
      39   [ +  -  +  - ]:          70 :         const UniValue &metadata = test[2].get_obj();
      40   [ +  -  +  - ]:          70 :         bool isPrivkey = metadata.find_value("isPrivkey").get_bool();
      41   [ +  -  +  -  :         140 :         SelectParams(ChainTypeFromString(metadata.find_value("chain").get_str()).value());
             +  -  +  - ]
      42   [ +  -  +  +  :          70 :         bool try_case_flip = metadata.find_value("tryCaseFlip").isNull() ? false : metadata.find_value("tryCaseFlip").get_bool();
             +  -  +  - ]
      43         [ +  + ]:          70 :         if (isPrivkey) {
      44   [ +  -  +  - ]:          16 :             bool isCompressed = metadata.find_value("isCompressed").get_bool();
      45                 :             :             // Must be valid private key
      46         [ +  - ]:          32 :             privkey = DecodeSecret(exp_base58string);
      47   [ +  -  +  -  :          32 :             BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest);
                   +  - ]
      48   [ +  -  +  -  :          32 :             BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest);
                   +  - ]
      49   [ +  -  +  -  :          48 :             BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest);
             +  -  +  - ]
      50                 :             : 
      51                 :             :             // Private key must be invalid public key
      52         [ +  - ]:          32 :             destination = DecodeDestination(exp_base58string);
      53   [ +  -  +  -  :          32 :             BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid privkey as pubkey:" + strTest);
             +  -  +  - ]
      54                 :             :         } else {
      55                 :             :             // Must be valid public key
      56         [ +  - ]:         108 :             destination = DecodeDestination(exp_base58string);
      57         [ +  - ]:          54 :             CScript script = GetScriptForDestination(destination);
      58   [ +  -  +  -  :         108 :             BOOST_CHECK_MESSAGE(IsValidDestination(destination), "!IsValid:" + strTest);
             +  -  +  - ]
      59   [ +  -  +  -  :         108 :             BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload));
             +  -  +  - ]
      60                 :             : 
      61                 :             :             // Try flipped case version
      62         [ +  + ]:        2526 :             for (char& c : exp_base58string) {
      63         [ +  + ]:        2472 :                 if (c >= 'a' && c <= 'z') {
      64                 :        1554 :                     c = (c - 'a') + 'A';
      65         [ +  + ]:         918 :                 } else if (c >= 'A' && c <= 'Z') {
      66                 :         314 :                     c = (c - 'A') + 'a';
      67                 :             :                 }
      68                 :             :             }
      69         [ +  - ]:         108 :             destination = DecodeDestination(exp_base58string);
      70   [ +  -  +  -  :         108 :             BOOST_CHECK_MESSAGE(IsValidDestination(destination) == try_case_flip, "!IsValid case flipped:" + strTest);
             +  -  +  - ]
      71   [ +  -  +  + ]:          54 :             if (IsValidDestination(destination)) {
      72         [ +  - ]:          64 :                 script = GetScriptForDestination(destination);
      73   [ +  -  +  -  :          64 :                 BOOST_CHECK_EQUAL(HexStr(script), HexStr(exp_payload));
             +  -  +  - ]
      74                 :             :             }
      75                 :             : 
      76                 :             :             // Public key must be invalid private key
      77         [ +  - ]:         108 :             privkey = DecodeSecret(exp_base58string);
      78   [ +  -  +  -  :         108 :             BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid pubkey as privkey:" + strTest);
                   +  - ]
      79                 :          54 :         }
      80                 :          70 :     }
      81                 :           1 : }
      82                 :             : 
      83                 :             : // Goal: check that generated keys match test vectors
      84   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(key_io_valid_gen)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      85                 :             : {
      86                 :           1 :     UniValue tests = read_json(json_tests::key_io_valid);
      87                 :             : 
      88         [ +  + ]:          71 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
      89         [ +  - ]:          70 :         const UniValue& test = tests[idx];
      90         [ +  - ]:          70 :         std::string strTest = test.write();
      91         [ -  + ]:          70 :         if (test.size() < 3) // Allow for extra stuff (useful for comments)
      92                 :             :         {
      93   [ #  #  #  # ]:           0 :             BOOST_ERROR("Bad test: " << strTest);
      94                 :           0 :             continue;
      95                 :             :         }
      96   [ +  -  +  -  :          70 :         std::string exp_base58string = test[0].get_str();
                   +  - ]
      97   [ +  -  +  -  :          70 :         std::vector<unsigned char> exp_payload = ParseHex(test[1].get_str());
                   +  - ]
      98   [ +  -  +  - ]:          70 :         const UniValue &metadata = test[2].get_obj();
      99   [ +  -  +  - ]:          70 :         bool isPrivkey = metadata.find_value("isPrivkey").get_bool();
     100   [ +  -  +  -  :         140 :         SelectParams(ChainTypeFromString(metadata.find_value("chain").get_str()).value());
             +  -  +  - ]
     101         [ +  + ]:          70 :         if (isPrivkey) {
     102   [ +  -  +  - ]:          16 :             bool isCompressed = metadata.find_value("isCompressed").get_bool();
     103                 :          16 :             CKey key;
     104         [ +  - ]:          16 :             key.Set(exp_payload.begin(), exp_payload.end(), isCompressed);
     105         [ -  + ]:          16 :             assert(key.IsValid());
     106   [ +  -  +  -  :          32 :             BOOST_CHECK_MESSAGE(EncodeSecret(key) == exp_base58string, "result mismatch: " + strTest);
             +  -  +  - ]
     107                 :          16 :         } else {
     108                 :          54 :             CTxDestination dest;
     109                 :          54 :             CScript exp_script(exp_payload.begin(), exp_payload.end());
     110   [ +  -  +  -  :         108 :             BOOST_CHECK(ExtractDestination(exp_script, dest));
             +  -  +  - ]
     111         [ +  - ]:          54 :             std::string address = EncodeDestination(dest);
     112                 :             : 
     113   [ +  -  +  - ]:          54 :             BOOST_CHECK_EQUAL(address, exp_base58string);
     114                 :          54 :         }
     115                 :          70 :     }
     116                 :             : 
     117         [ +  - ]:           1 :     SelectParams(ChainType::MAIN);
     118                 :           1 : }
     119                 :             : 
     120                 :             : 
     121                 :             : // Goal: check that base58 parsing code is robust against a variety of corrupted data
     122   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(key_io_invalid)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     123                 :             : {
     124                 :           1 :     UniValue tests = read_json(json_tests::key_io_invalid); // Negative testcases
     125                 :           1 :     CKey privkey;
     126                 :           1 :     CTxDestination destination;
     127                 :             : 
     128         [ +  + ]:          71 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
     129         [ +  - ]:          70 :         const UniValue& test = tests[idx];
     130         [ +  - ]:          70 :         std::string strTest = test.write();
     131         [ -  + ]:          70 :         if (test.size() < 1) // Allow for extra stuff (useful for comments)
     132                 :             :         {
     133   [ #  #  #  # ]:           0 :             BOOST_ERROR("Bad test: " << strTest);
     134                 :           0 :             continue;
     135                 :             :         }
     136   [ +  -  +  -  :          70 :         std::string exp_base58string = test[0].get_str();
                   +  - ]
     137                 :             : 
     138                 :             :         // must be invalid as public and as private key
     139         [ +  + ]:         350 :         for (const auto& chain : {ChainType::MAIN, ChainType::TESTNET, ChainType::SIGNET, ChainType::REGTEST}) {
     140         [ +  - ]:         280 :             SelectParams(chain);
     141         [ +  - ]:         560 :             destination = DecodeDestination(exp_base58string);
     142   [ +  -  +  -  :         560 :             BOOST_CHECK_MESSAGE(!IsValidDestination(destination), "IsValid pubkey in mainnet:" + strTest);
             +  -  +  - ]
     143         [ +  - ]:         560 :             privkey = DecodeSecret(exp_base58string);
     144   [ +  -  +  -  :         560 :             BOOST_CHECK_MESSAGE(!privkey.IsValid(), "IsValid privkey in mainnet:" + strTest);
                   +  - ]
     145                 :             :         }
     146                 :          70 :     }
     147                 :           1 : }
     148                 :             : 
     149                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1