LCOV - code coverage report
Current view: top level - src/test - sighash_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 93.5 % 107 100
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 7 7
Branches: 54.0 % 274 148

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2013-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 <common/system.h>
       6                 :             : #include <consensus/tx_check.h>
       7                 :             : #include <consensus/validation.h>
       8                 :             : #include <hash.h>
       9                 :             : #include <script/interpreter.h>
      10                 :             : #include <script/script.h>
      11                 :             : #include <serialize.h>
      12                 :             : #include <streams.h>
      13                 :             : #include <test/data/sighash.json.h>
      14                 :             : #include <test/util/json.h>
      15                 :             : #include <test/util/random.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <util/strencodings.h>
      18                 :             : 
      19                 :             : #include <iostream>
      20                 :             : 
      21                 :             : #include <boost/test/unit_test.hpp>
      22                 :             : 
      23                 :             : #include <univalue.h>
      24                 :             : 
      25                 :             : // Old script.cpp SignatureHash function
      26                 :       50000 : uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
      27                 :             : {
      28         [ -  + ]:       50000 :     if (nIn >= txTo.vin.size())
      29                 :             :     {
      30                 :           0 :         return uint256::ONE;
      31                 :             :     }
      32                 :       50000 :     CMutableTransaction txTmp(txTo);
      33                 :             : 
      34                 :             :     // In case concatenating two scripts ends up with two codeseparators,
      35                 :             :     // or an extra one at the end, this prevents all those possible incompatibilities.
      36   [ +  -  +  - ]:       50000 :     FindAndDelete(scriptCode, CScript(OP_CODESEPARATOR));
      37                 :             : 
      38                 :             :     // Blank out other inputs' signatures
      39         [ +  + ]:      175027 :     for (unsigned int i = 0; i < txTmp.vin.size(); i++)
      40                 :      125027 :         txTmp.vin[i].scriptSig = CScript();
      41                 :       50000 :     txTmp.vin[nIn].scriptSig = scriptCode;
      42                 :             : 
      43                 :             :     // Blank out some of the outputs
      44         [ +  + ]:       50000 :     if ((nHashType & 0x1f) == SIGHASH_NONE)
      45                 :             :     {
      46                 :             :         // Wildcard payee
      47                 :        1597 :         txTmp.vout.clear();
      48                 :             : 
      49                 :             :         // Let the others update at will
      50         [ +  + ]:        5573 :         for (unsigned int i = 0; i < txTmp.vin.size(); i++)
      51         [ +  + ]:        3976 :             if (i != nIn)
      52                 :        2379 :                 txTmp.vin[i].nSequence = 0;
      53                 :             :     }
      54         [ +  + ]:       48403 :     else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
      55                 :             :     {
      56                 :             :         // Only lock-in the txout payee at same index as txin
      57                 :        1517 :         unsigned int nOut = nIn;
      58         [ -  + ]:        1517 :         if (nOut >= txTmp.vout.size())
      59                 :             :         {
      60                 :           0 :             return uint256::ONE;
      61                 :             :         }
      62         [ +  - ]:        1517 :         txTmp.vout.resize(nOut+1);
      63         [ +  + ]:        2656 :         for (unsigned int i = 0; i < nOut; i++)
      64                 :        1139 :             txTmp.vout[i].SetNull();
      65                 :             : 
      66                 :             :         // Let the others update at will
      67         [ +  + ]:        5285 :         for (unsigned int i = 0; i < txTmp.vin.size(); i++)
      68         [ +  + ]:        3768 :             if (i != nIn)
      69                 :        2251 :                 txTmp.vin[i].nSequence = 0;
      70                 :             :     }
      71                 :             : 
      72                 :             :     // Blank out other inputs completely, not recommended for open transactions
      73         [ +  + ]:       50000 :     if (nHashType & SIGHASH_ANYONECANPAY)
      74                 :             :     {
      75         [ +  - ]:       25054 :         txTmp.vin[0] = txTmp.vin[nIn];
      76         [ +  - ]:       25054 :         txTmp.vin.resize(1);
      77                 :             :     }
      78                 :             : 
      79                 :             :     // Serialize and hash
      80         [ +  - ]:       50000 :     HashWriter ss{};
      81   [ +  -  +  - ]:       50000 :     ss << TX_NO_WITNESS(txTmp) << nHashType;
      82         [ +  - ]:       50000 :     return ss.GetHash();
      83                 :       50000 : }
      84                 :             : 
      85                 :      300348 : void static RandomScript(CScript &script) {
      86                 :      300348 :     static const opcodetype oplist[] = {OP_FALSE, OP_1, OP_2, OP_3, OP_CHECKSIG, OP_IF, OP_VERIF, OP_RETURN, OP_CODESEPARATOR};
      87                 :      300348 :     script = CScript();
      88                 :      300348 :     int ops = (InsecureRandRange(10));
      89         [ +  + ]:     1651135 :     for (int i=0; i<ops; i++)
      90                 :     1350787 :         script << oplist[InsecureRandRange(std::size(oplist))];
      91                 :      300348 : }
      92                 :             : 
      93                 :       50000 : void static RandomTransaction(CMutableTransaction& tx, bool fSingle)
      94                 :             : {
      95                 :       50000 :     tx.version = InsecureRand32();
      96                 :       50000 :     tx.vin.clear();
      97                 :       50000 :     tx.vout.clear();
      98         [ +  + ]:       50000 :     tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0;
      99                 :       50000 :     int ins = (InsecureRandBits(2)) + 1;
     100         [ +  + ]:       98483 :     int outs = fSingle ? ins : (InsecureRandBits(2)) + 1;
     101         [ +  + ]:      175027 :     for (int in = 0; in < ins; in++) {
     102                 :      125027 :         tx.vin.emplace_back();
     103                 :      125027 :         CTxIn &txin = tx.vin.back();
     104                 :      125027 :         txin.prevout.hash = Txid::FromUint256(InsecureRand256());
     105                 :      125027 :         txin.prevout.n = InsecureRandBits(2);
     106                 :      125027 :         RandomScript(txin.scriptSig);
     107         [ +  + ]:      187493 :         txin.nSequence = (InsecureRandBool()) ? InsecureRand32() : std::numeric_limits<uint32_t>::max();
     108                 :             :     }
     109         [ +  + ]:      175321 :     for (int out = 0; out < outs; out++) {
     110                 :      125321 :         tx.vout.emplace_back();
     111                 :      125321 :         CTxOut &txout = tx.vout.back();
     112                 :      125321 :         txout.nValue = InsecureRandMoneyAmount();
     113                 :      125321 :         RandomScript(txout.scriptPubKey);
     114                 :             :     }
     115                 :       50000 : }
     116                 :             : 
     117                 :             : BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
     118                 :             : 
     119   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sighash_test)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     120                 :             : {
     121                 :             :     #if defined(PRINT_SIGHASH_JSON)
     122                 :             :     std::cout << "[\n";
     123                 :             :     std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
     124                 :             :     int nRandomTests = 500;
     125                 :             :     #else
     126                 :           1 :     int nRandomTests = 50000;
     127                 :             :     #endif
     128         [ +  + ]:       50001 :     for (int i=0; i<nRandomTests; i++) {
     129                 :       50000 :         int nHashType{int(InsecureRand32())};
     130                 :       50000 :         CMutableTransaction txTo;
     131         [ +  - ]:       50000 :         RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
     132                 :       50000 :         CScript scriptCode;
     133         [ +  - ]:       50000 :         RandomScript(scriptCode);
     134                 :       50000 :         int nIn = InsecureRandRange(txTo.vin.size());
     135                 :             : 
     136                 :       50000 :         uint256 sh, sho;
     137   [ +  -  +  - ]:      150000 :         sho = SignatureHashOld(scriptCode, CTransaction(txTo), nIn, nHashType);
     138         [ +  - ]:       50000 :         sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SigVersion::BASE);
     139                 :             :         #if defined(PRINT_SIGHASH_JSON)
     140                 :             :         DataStream ss;
     141                 :             :         ss << TX_WITH_WITNESS(txTo);
     142                 :             : 
     143                 :             :         std::cout << "\t[\"" ;
     144                 :             :         std::cout << HexStr(ss) << "\", \"";
     145                 :             :         std::cout << HexStr(scriptCode) << "\", ";
     146                 :             :         std::cout << nIn << ", ";
     147                 :             :         std::cout << nHashType << ", \"";
     148                 :             :         std::cout << sho.GetHex() << "\"]";
     149                 :             :         if (i+1 != nRandomTests) {
     150                 :             :           std::cout << ",";
     151                 :             :         }
     152                 :             :         std::cout << "\n";
     153                 :             :         #endif
     154   [ +  -  +  - ]:      100000 :         BOOST_CHECK(sh == sho);
     155                 :      100000 :     }
     156                 :             :     #if defined(PRINT_SIGHASH_JSON)
     157                 :             :     std::cout << "]\n";
     158                 :             :     #endif
     159                 :           1 : }
     160                 :             : 
     161                 :             : // Goal: check that SignatureHash generates correct hash
     162   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sighash_from_data)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     163                 :             : {
     164                 :           1 :     UniValue tests = read_json(json_tests::sighash);
     165                 :             : 
     166         [ +  + ]:         502 :     for (unsigned int idx = 0; idx < tests.size(); idx++) {
     167         [ +  - ]:         501 :         const UniValue& test = tests[idx];
     168         [ +  - ]:         501 :         std::string strTest = test.write();
     169         [ -  + ]:         501 :         if (test.size() < 1) // Allow for extra stuff (useful for comments)
     170                 :             :         {
     171   [ #  #  #  # ]:           0 :             BOOST_ERROR("Bad test: " << strTest);
     172                 :           0 :             continue;
     173                 :             :         }
     174         [ +  + ]:         501 :         if (test.size() == 1) continue; // comment
     175                 :             : 
     176         [ +  - ]:         500 :         std::string raw_tx, raw_script, sigHashHex;
     177                 :         500 :         int nIn, nHashType;
     178                 :         500 :         uint256 sh;
     179                 :         500 :         CTransactionRef tx;
     180                 :         500 :         CScript scriptCode = CScript();
     181                 :             : 
     182                 :         500 :         try {
     183                 :             :           // deserialize test data
     184   [ +  -  +  -  :         500 :           raw_tx = test[0].get_str();
                   +  - ]
     185   [ +  -  +  -  :         500 :           raw_script = test[1].get_str();
                   +  - ]
     186   [ +  -  +  - ]:         500 :           nIn = test[2].getInt<int>();
     187   [ +  -  +  - ]:         500 :           nHashType = test[3].getInt<int>();
     188   [ +  -  +  -  :         500 :           sigHashHex = test[4].get_str();
                   +  - ]
     189                 :             : 
     190   [ +  -  +  - ]:         500 :           DataStream stream(ParseHex(raw_tx));
     191         [ +  - ]:         500 :           stream >> TX_WITH_WITNESS(tx);
     192                 :             : 
     193         [ +  - ]:         500 :           TxValidationState state;
     194   [ +  -  +  -  :        1000 :           BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);
             +  -  +  - ]
     195   [ +  -  +  -  :        1000 :           BOOST_CHECK(state.IsValid());
                   +  - ]
     196                 :             : 
     197         [ +  - ]:         500 :           std::vector<unsigned char> raw = ParseHex(raw_script);
     198                 :         500 :           scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
     199                 :        1000 :         } catch (...) {
     200   [ -  -  -  -  :           0 :           BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
                   -  - ]
     201                 :           0 :           continue;
     202         [ -  - ]:           0 :         }
     203                 :             : 
     204         [ +  - ]:         500 :         sh = SignatureHash(scriptCode, *tx, nIn, nHashType, 0, SigVersion::BASE);
     205   [ +  -  +  -  :        1000 :         BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
                   +  - ]
     206   [ +  -  -  - ]:        1001 :     }
     207                 :           1 : }
     208                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1