LCOV - code coverage report
Current view: top level - src/test - script_p2sh_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 253 253
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 16 16
Branches: 51.7 % 1174 607

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-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 <consensus/tx_verify.h>
       6                 :             : #include <key.h>
       7                 :             : #include <policy/policy.h>
       8                 :             : #include <policy/settings.h>
       9                 :             : #include <script/script.h>
      10                 :             : #include <script/script_error.h>
      11                 :             : #include <script/sign.h>
      12                 :             : #include <script/signingprovider.h>
      13                 :             : #include <test/util/setup_common.h>
      14                 :             : #include <validation.h>
      15                 :             : 
      16                 :             : #include <vector>
      17                 :             : 
      18                 :             : #include <boost/test/unit_test.hpp>
      19                 :             : 
      20                 :             : // Helpers:
      21                 :           8 : static bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, std::string& reason)
      22                 :             : {
      23                 :           8 :     return IsStandardTx(tx, std::nullopt, permit_bare_multisig, CFeeRate{DUST_RELAY_TX_FEE}, reason);
      24                 :             : }
      25                 :             : 
      26                 :           2 : static bool IsStandardTx(const CTransaction& tx, std::string& reason)
      27                 :             : {
      28   [ +  -  -  + ]:           4 :     return IsStandardTx(tx, std::nullopt, /*permit_bare_multisig=*/true, CFeeRate{DUST_RELAY_TX_FEE}, reason) &&
      29                 :           2 :            IsStandardTx(tx, std::nullopt, /*permit_bare_multisig=*/false, CFeeRate{DUST_RELAY_TX_FEE}, reason);
      30                 :             : }
      31                 :             : 
      32                 :           4 : static std::vector<unsigned char> Serialize(const CScript& s)
      33                 :             : {
      34         [ +  - ]:           8 :     std::vector<unsigned char> sSerialized(s.begin(), s.end());
      35                 :           4 :     return sSerialized;
      36                 :             : }
      37                 :             : 
      38                 :           4 : static bool Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict, ScriptError& err)
      39                 :             : {
      40                 :             :     // Create dummy to/from transactions:
      41                 :           4 :     CMutableTransaction txFrom;
      42         [ +  - ]:           4 :     txFrom.vout.resize(1);
      43                 :           4 :     txFrom.vout[0].scriptPubKey = scriptPubKey;
      44                 :             : 
      45         [ +  - ]:           4 :     CMutableTransaction txTo;
      46         [ +  - ]:           4 :     txTo.vin.resize(1);
      47         [ +  - ]:           4 :     txTo.vout.resize(1);
      48         [ +  - ]:           4 :     txTo.vin[0].prevout.n = 0;
      49         [ +  - ]:           4 :     txTo.vin[0].prevout.hash = txFrom.GetHash();
      50                 :           4 :     txTo.vin[0].scriptSig = scriptSig;
      51         [ +  + ]:           4 :     txTo.vout[0].nValue = 1;
      52                 :             : 
      53   [ +  +  +  - ]:           5 :     return VerifyScript(scriptSig, scriptPubKey, nullptr, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err);
      54                 :           8 : }
      55                 :             : 
      56                 :             : 
      57                 :             : BOOST_FIXTURE_TEST_SUITE(script_p2sh_tests, BasicTestingSetup)
      58                 :             : 
      59   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sign)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      60                 :             : {
      61                 :             :     // Pay-to-script-hash looks like this:
      62                 :             :     // scriptSig:    <sig> <sig...> <serialized_script>
      63                 :             :     // scriptPubKey: HASH160 <hash> EQUAL
      64                 :             : 
      65                 :             :     // Test SignSignature() (and therefore the version of Solver() that signs transactions)
      66                 :           1 :     FillableSigningProvider keystore;
      67                 :           4 :     CKey key[4];
      68         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
      69                 :             :     {
      70         [ +  - ]:           4 :         key[i].MakeNewKey(true);
      71   [ +  -  +  -  :           8 :         BOOST_CHECK(keystore.AddKey(key[i]));
                   +  - ]
      72                 :             :     }
      73                 :             : 
      74                 :             :     // 8 Scripts: checking all combinations of
      75                 :             :     // different keys, straight/P2SH, pubkey/pubkeyhash
      76                 :           5 :     CScript standardScripts[4];
      77   [ +  -  +  -  :           1 :     standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
                   +  - ]
      78   [ +  -  +  -  :           2 :     standardScripts[1] = GetScriptForDestination(PKHash(key[1].GetPubKey()));
                   +  - ]
      79   [ +  -  +  -  :           1 :     standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
                   +  - ]
      80   [ +  -  +  -  :           2 :     standardScripts[3] = GetScriptForDestination(PKHash(key[2].GetPubKey()));
                   +  - ]
      81                 :           5 :     CScript evalScripts[4];
      82         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
      83                 :             :     {
      84   [ +  -  +  -  :           8 :         BOOST_CHECK(keystore.AddCScript(standardScripts[i]));
             +  -  +  - ]
      85   [ +  -  +  - ]:           4 :         evalScripts[i] = GetScriptForDestination(ScriptHash(standardScripts[i]));
      86                 :             :     }
      87                 :             : 
      88         [ +  - ]:           1 :     CMutableTransaction txFrom;  // Funding transaction:
      89         [ +  - ]:           1 :     std::string reason;
      90         [ +  - ]:           1 :     txFrom.vout.resize(8);
      91         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
      92                 :             :     {
      93                 :           4 :         txFrom.vout[i].scriptPubKey = evalScripts[i];
      94                 :           4 :         txFrom.vout[i].nValue = COIN;
      95                 :           4 :         txFrom.vout[i+4].scriptPubKey = standardScripts[i];
      96                 :           4 :         txFrom.vout[i+4].nValue = COIN;
      97                 :             :     }
      98   [ +  -  +  -  :           3 :     BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason));
             +  -  +  - ]
      99                 :             : 
     100   [ +  -  +  +  :          25 :     CMutableTransaction txTo[8]; // Spending transactions
                   -  - ]
     101         [ +  + ]:           9 :     for (int i = 0; i < 8; i++)
     102                 :             :     {
     103         [ +  - ]:           8 :         txTo[i].vin.resize(1);
     104         [ +  - ]:           8 :         txTo[i].vout.resize(1);
     105         [ +  - ]:           8 :         txTo[i].vin[0].prevout.n = i;
     106         [ +  - ]:           8 :         txTo[i].vin[0].prevout.hash = txFrom.GetHash();
     107                 :           8 :         txTo[i].vout[0].nValue = 1;
     108                 :             :     }
     109         [ +  + ]:           9 :     for (int i = 0; i < 8; i++)
     110                 :             :     {
     111                 :           8 :         SignatureData empty;
     112   [ +  -  +  -  :          24 :         BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL, empty), strprintf("SignSignature %d", i));
          +  -  +  -  +  
                      - ]
     113                 :           8 :     }
     114                 :             :     // All of the above should be OK, and the txTos have valid signatures
     115                 :             :     // Check to make sure signature verification fails if we use the wrong ScriptSig:
     116         [ +  - ]:           1 :     SignatureCache signature_cache{DEFAULT_SIGNATURE_CACHE_BYTES};
     117         [ +  + ]:           9 :     for (int i = 0; i < 8; i++) {
     118         [ +  - ]:           8 :         PrecomputedTransactionData txdata(txTo[i]);
     119         [ +  + ]:          72 :         for (int j = 0; j < 8; j++)
     120                 :             :         {
     121                 :          64 :             CScript sigSave = txTo[i].vin[0].scriptSig;
     122                 :          64 :             txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
     123   [ +  -  +  - ]:          64 :             bool sigOK = CScriptCheck(txFrom.vout[txTo[i].vin[0].prevout.n], CTransaction(txTo[i]), signature_cache, 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)();
     124         [ +  + ]:          64 :             if (i == j)
     125   [ +  -  +  -  :          16 :                 BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
                   +  - ]
     126                 :             :             else
     127   [ +  -  +  -  :         112 :                 BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
                   +  - ]
     128                 :          64 :             txTo[i].vin[0].scriptSig = sigSave;
     129                 :          64 :         }
     130                 :           8 :     }
     131   [ +  +  +  +  :          26 : }
          +  +  +  +  -  
          -  -  -  -  -  
                   -  - ]
     132                 :             : 
     133   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(norecurse)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     134                 :             : {
     135                 :           1 :     ScriptError err;
     136                 :             :     // Make sure only the outer pay-to-script-hash does the
     137                 :             :     // extra-validation thing:
     138                 :           1 :     CScript invalidAsScript;
     139   [ +  -  +  - ]:           1 :     invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
     140                 :             : 
     141   [ +  -  +  - ]:           1 :     CScript p2sh = GetScriptForDestination(ScriptHash(invalidAsScript));
     142                 :             : 
     143                 :           1 :     CScript scriptSig;
     144         [ +  - ]:           1 :     scriptSig << Serialize(invalidAsScript);
     145                 :             : 
     146                 :             :     // Should not verify, because it will try to execute OP_INVALIDOPCODE
     147   [ +  -  +  -  :           2 :     BOOST_CHECK(!Verify(scriptSig, p2sh, true, err));
             +  -  +  - ]
     148   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_BAD_OPCODE, ScriptErrorString(err));
                   +  - ]
     149                 :             : 
     150                 :             :     // Try to recur, and verification should succeed because
     151                 :             :     // the inner HASH160 <> EQUAL should only check the hash:
     152   [ +  -  +  - ]:           1 :     CScript p2sh2 = GetScriptForDestination(ScriptHash(p2sh));
     153                 :           1 :     CScript scriptSig2;
     154   [ +  -  +  - ]:           1 :     scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
     155                 :             : 
     156   [ +  -  +  -  :           2 :     BOOST_CHECK(Verify(scriptSig2, p2sh2, true, err));
             +  -  +  - ]
     157   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     158                 :           1 : }
     159                 :             : 
     160   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(set)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     161                 :             : {
     162                 :             :     // Test the CScript::Set* methods
     163                 :           1 :     FillableSigningProvider keystore;
     164                 :           4 :     CKey key[4];
     165                 :           1 :     std::vector<CPubKey> keys;
     166         [ +  - ]:           1 :     keys.reserve(4);
     167         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     168                 :             :     {
     169         [ +  - ]:           4 :         key[i].MakeNewKey(true);
     170   [ +  -  +  -  :           8 :         BOOST_CHECK(keystore.AddKey(key[i]));
             +  -  +  - ]
     171         [ +  - ]:           8 :         keys.push_back(key[i].GetPubKey());
     172                 :             :     }
     173                 :             : 
     174                 :           4 :     CScript inner[4];
     175   [ +  -  +  -  :           2 :     inner[0] = GetScriptForDestination(PKHash(key[0].GetPubKey()));
                   +  - ]
     176   [ +  -  +  -  :           2 :     inner[1] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
                   +  - ]
     177   [ +  -  +  -  :           2 :     inner[2] = GetScriptForMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
                   +  - ]
     178   [ +  -  +  - ]:           2 :     inner[3] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
     179                 :             : 
     180                 :           5 :     CScript outer[4];
     181         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     182                 :             :     {
     183   [ +  -  +  - ]:           4 :         outer[i] = GetScriptForDestination(ScriptHash(inner[i]));
     184   [ +  -  +  -  :           8 :         BOOST_CHECK(keystore.AddCScript(inner[i]));
                   +  - ]
     185                 :             :     }
     186                 :             : 
     187         [ +  - ]:           1 :     CMutableTransaction txFrom;  // Funding transaction:
     188         [ +  - ]:           1 :     std::string reason;
     189         [ +  - ]:           1 :     txFrom.vout.resize(4);
     190         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     191                 :             :     {
     192                 :           4 :         txFrom.vout[i].scriptPubKey = outer[i];
     193                 :           4 :         txFrom.vout[i].nValue = CENT;
     194                 :             :     }
     195   [ +  -  +  -  :           3 :     BOOST_CHECK(IsStandardTx(CTransaction(txFrom), reason));
             +  -  +  - ]
     196                 :             : 
     197   [ +  -  +  +  :          13 :     CMutableTransaction txTo[4]; // Spending transactions
                   -  - ]
     198         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     199                 :             :     {
     200         [ +  - ]:           4 :         txTo[i].vin.resize(1);
     201         [ +  - ]:           4 :         txTo[i].vout.resize(1);
     202         [ +  - ]:           4 :         txTo[i].vin[0].prevout.n = i;
     203         [ +  - ]:           4 :         txTo[i].vin[0].prevout.hash = txFrom.GetHash();
     204                 :           4 :         txTo[i].vout[0].nValue = 1*CENT;
     205                 :           4 :         txTo[i].vout[0].scriptPubKey = inner[i];
     206                 :             :     }
     207         [ +  + ]:           5 :     for (int i = 0; i < 4; i++)
     208                 :             :     {
     209                 :           4 :         SignatureData empty;
     210   [ +  -  +  -  :          12 :         BOOST_CHECK_MESSAGE(SignSignature(keystore, CTransaction(txFrom), txTo[i], 0, SIGHASH_ALL, empty), strprintf("SignSignature %d", i));
          +  -  +  -  +  
                      - ]
     211   [ +  -  +  -  :          12 :         BOOST_CHECK_MESSAGE(IsStandardTx(CTransaction(txTo[i]), /*permit_bare_multisig=*/true, reason), strprintf("txTo[%d].IsStandard", i));
          +  -  +  -  +  
                      - ]
     212   [ +  -  +  - ]:           4 :         bool no_pbms_is_std = IsStandardTx(CTransaction(txTo[i]), /*permit_bare_multisig=*/false, reason);
     213   [ +  -  +  -  :           8 :         BOOST_CHECK_MESSAGE((i == 0 ? no_pbms_is_std : !no_pbms_is_std), strprintf("txTo[%d].IsStandard(permbaremulti=false)", i));
             +  +  +  - ]
     214                 :           4 :     }
     215   [ +  +  +  +  :          22 : }
          +  +  +  +  -  
          -  -  -  -  -  
                   -  - ]
     216                 :             : 
     217   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(is)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     218                 :             : {
     219                 :             :     // Test CScript::IsPayToScriptHash()
     220                 :           1 :     uint160 dummy;
     221                 :           1 :     CScript p2sh;
     222   [ +  -  +  -  :           1 :     p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL;
                   +  - ]
     223   [ +  -  +  -  :           2 :     BOOST_CHECK(p2sh.IsPayToScriptHash());
             +  -  +  - ]
     224                 :             : 
     225         [ +  - ]:           1 :     std::vector<unsigned char> direct = {OP_HASH160, 20};
     226         [ +  - ]:           1 :     direct.insert(direct.end(), 20, 0);
     227         [ +  - ]:           1 :     direct.push_back(OP_EQUAL);
     228   [ +  -  +  -  :           2 :     BOOST_CHECK(CScript(direct.begin(), direct.end()).IsPayToScriptHash());
             +  -  +  - ]
     229                 :             : 
     230                 :             :     // Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
     231         [ +  - ]:           1 :     std::vector<unsigned char> pushdata1 = {OP_HASH160, OP_PUSHDATA1, 20};
     232         [ +  - ]:           1 :     pushdata1.insert(pushdata1.end(), 20, 0);
     233         [ +  - ]:           1 :     pushdata1.push_back(OP_EQUAL);
     234   [ +  -  +  -  :           2 :     BOOST_CHECK(!CScript(pushdata1.begin(), pushdata1.end()).IsPayToScriptHash());
             +  -  +  - ]
     235         [ +  - ]:           1 :     std::vector<unsigned char> pushdata2 = {OP_HASH160, OP_PUSHDATA2, 20, 0};
     236         [ +  - ]:           1 :     pushdata2.insert(pushdata2.end(), 20, 0);
     237         [ +  - ]:           1 :     pushdata2.push_back(OP_EQUAL);
     238   [ +  -  +  -  :           2 :     BOOST_CHECK(!CScript(pushdata2.begin(), pushdata2.end()).IsPayToScriptHash());
             +  -  +  - ]
     239         [ +  - ]:           1 :     std::vector<unsigned char> pushdata4 = {OP_HASH160, OP_PUSHDATA4, 20, 0, 0, 0};
     240         [ +  - ]:           1 :     pushdata4.insert(pushdata4.end(), 20, 0);
     241         [ +  - ]:           1 :     pushdata4.push_back(OP_EQUAL);
     242   [ +  -  +  -  :           2 :     BOOST_CHECK(!CScript(pushdata4.begin(), pushdata4.end()).IsPayToScriptHash());
             +  -  +  - ]
     243                 :             : 
     244                 :           1 :     CScript not_p2sh;
     245   [ +  -  +  -  :           2 :     BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
                   +  - ]
     246                 :             : 
     247   [ +  -  +  -  :           2 :     not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL;
             +  -  +  - ]
     248   [ +  -  +  -  :           2 :     BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
                   +  - ]
     249                 :             : 
     250   [ +  -  +  -  :           1 :     not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL;
                   +  - ]
     251   [ +  -  +  -  :           2 :     BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
                   +  - ]
     252                 :             : 
     253   [ +  -  +  -  :           1 :     not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG;
                   +  - ]
     254   [ +  -  +  -  :           2 :     BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
                   +  - ]
     255                 :           1 : }
     256                 :             : 
     257   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(switchover)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     258                 :             : {
     259                 :             :     // Test switch over code
     260                 :           1 :     CScript notValid;
     261                 :           1 :     ScriptError err;
     262   [ +  -  +  -  :           1 :     notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
                   +  - ]
     263                 :           1 :     CScript scriptSig;
     264         [ +  - ]:           1 :     scriptSig << Serialize(notValid);
     265                 :             : 
     266   [ +  -  +  - ]:           1 :     CScript fund = GetScriptForDestination(ScriptHash(notValid));
     267                 :             : 
     268                 :             : 
     269                 :             :     // Validation should succeed under old rules (hash is correct):
     270   [ +  -  +  -  :           2 :     BOOST_CHECK(Verify(scriptSig, fund, false, err));
             +  -  +  - ]
     271   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
                   +  - ]
     272                 :             :     // Fail under new:
     273   [ +  -  +  -  :           2 :     BOOST_CHECK(!Verify(scriptSig, fund, true, err));
             +  -  +  - ]
     274   [ +  -  +  -  :           2 :     BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EQUALVERIFY, ScriptErrorString(err));
                   +  - ]
     275                 :           1 : }
     276                 :             : 
     277   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(AreInputsStandard)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     278                 :             : {
     279                 :           1 :     CCoinsView coinsDummy;
     280         [ +  - ]:           1 :     CCoinsViewCache coins(&coinsDummy);
     281                 :           1 :     FillableSigningProvider keystore;
     282                 :           6 :     CKey key[6];
     283         [ +  + ]:           7 :     for (int i = 0; i < 6; i++)
     284                 :             :     {
     285         [ +  - ]:           6 :         key[i].MakeNewKey(true);
     286   [ +  -  +  -  :          12 :         BOOST_CHECK(keystore.AddKey(key[i]));
                   +  - ]
     287                 :             :     }
     288                 :           1 :     std::vector<CPubKey> keys;
     289         [ +  - ]:           1 :     keys.reserve(3);
     290         [ +  + ]:           4 :     for (int i = 0; i < 3; i++)
     291         [ +  - ]:           6 :         keys.push_back(key[i].GetPubKey());
     292                 :             : 
     293         [ +  - ]:           1 :     CMutableTransaction txFrom;
     294         [ +  - ]:           1 :     txFrom.vout.resize(7);
     295                 :             : 
     296                 :             :     // First three are standard:
     297   [ +  -  +  -  :           1 :     CScript pay1 = GetScriptForDestination(PKHash(key[0].GetPubKey()));
                   +  - ]
     298   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(pay1));
             +  -  +  - ]
     299         [ +  - ]:           1 :     CScript pay1of3 = GetScriptForMultisig(1, keys);
     300                 :             : 
     301   [ +  -  +  - ]:           1 :     txFrom.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(pay1)); // P2SH (OP_CHECKSIG)
     302                 :           1 :     txFrom.vout[0].nValue = 1000;
     303                 :           1 :     txFrom.vout[1].scriptPubKey = pay1; // ordinary OP_CHECKSIG
     304                 :           1 :     txFrom.vout[1].nValue = 2000;
     305                 :           1 :     txFrom.vout[2].scriptPubKey = pay1of3; // ordinary OP_CHECKMULTISIG
     306         [ +  - ]:           1 :     txFrom.vout[2].nValue = 3000;
     307                 :             : 
     308                 :             :     // vout[3] is complicated 1-of-3 AND 2-of-3
     309                 :             :     // ... that is OK if wrapped in P2SH:
     310                 :           1 :     CScript oneAndTwo;
     311   [ +  -  +  -  :           1 :     oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     312   [ +  -  +  - ]:           1 :     oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY;
     313   [ +  -  +  -  :           1 :     oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     314   [ +  -  +  - ]:           1 :     oneAndTwo << OP_3 << OP_CHECKMULTISIG;
     315   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(oneAndTwo));
             +  -  +  - ]
     316   [ +  -  +  - ]:           1 :     txFrom.vout[3].scriptPubKey = GetScriptForDestination(ScriptHash(oneAndTwo));
     317         [ +  - ]:           1 :     txFrom.vout[3].nValue = 4000;
     318                 :             : 
     319                 :             :     // vout[4] is max sigops:
     320         [ +  - ]:           1 :     CScript fifteenSigops; fifteenSigops << OP_1;
     321         [ +  + ]:          16 :     for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++)
     322   [ +  -  +  - ]:          15 :         fifteenSigops << ToByteVector(key[i%3].GetPubKey());
     323   [ +  -  +  - ]:           1 :     fifteenSigops << OP_15 << OP_CHECKMULTISIG;
     324   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(fifteenSigops));
             +  -  +  - ]
     325   [ +  -  +  - ]:           1 :     txFrom.vout[4].scriptPubKey = GetScriptForDestination(ScriptHash(fifteenSigops));
     326         [ +  - ]:           1 :     txFrom.vout[4].nValue = 5000;
     327                 :             : 
     328                 :             :     // vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS
     329   [ +  -  +  - ]:           1 :     CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG;
     330   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(sixteenSigops));
             +  -  +  - ]
     331   [ +  -  +  - ]:           1 :     txFrom.vout[5].scriptPubKey = GetScriptForDestination(ScriptHash(sixteenSigops));
     332         [ +  - ]:           1 :     txFrom.vout[5].nValue = 5000;
     333         [ +  - ]:           1 :     CScript twentySigops; twentySigops << OP_CHECKMULTISIG;
     334   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(twentySigops));
             +  -  +  - ]
     335   [ +  -  +  - ]:           1 :     txFrom.vout[6].scriptPubKey = GetScriptForDestination(ScriptHash(twentySigops));
     336         [ +  - ]:           1 :     txFrom.vout[6].nValue = 6000;
     337                 :             : 
     338   [ +  -  +  - ]:           1 :     AddCoins(coins, CTransaction(txFrom), 0);
     339                 :             : 
     340         [ +  - ]:           1 :     CMutableTransaction txTo;
     341         [ +  - ]:           1 :     txTo.vout.resize(1);
     342   [ +  -  +  -  :           1 :     txTo.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[1].GetPubKey()));
                   +  - ]
     343                 :             : 
     344         [ +  - ]:           1 :     txTo.vin.resize(5);
     345         [ +  + ]:           6 :     for (int i = 0; i < 5; i++)
     346                 :             :     {
     347         [ +  - ]:           5 :         txTo.vin[i].prevout.n = i;
     348         [ +  - ]:           5 :         txTo.vin[i].prevout.hash = txFrom.GetHash();
     349                 :             :     }
     350                 :           1 :     SignatureData empty;
     351   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL, empty));
             +  -  +  - ]
     352                 :           1 :     SignatureData empty_b;
     353   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 1, SIGHASH_ALL, empty_b));
             +  -  +  - ]
     354                 :           1 :     SignatureData empty_c;
     355   [ +  -  +  -  :           3 :     BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 2, SIGHASH_ALL, empty_c));
          +  -  +  -  +  
                      - ]
     356                 :             :     // SignSignature doesn't know how to sign these. We're
     357                 :             :     // not testing validating signatures, so just create
     358                 :             :     // dummy signatures that DO include the correct P2SH scripts:
     359   [ +  -  +  -  :           2 :     txTo.vin[3].scriptSig << OP_11 << OP_11 << std::vector<unsigned char>(oneAndTwo.begin(), oneAndTwo.end());
          +  -  +  -  +  
                      - ]
     360   [ +  -  +  -  :           2 :     txTo.vin[4].scriptSig << std::vector<unsigned char>(fifteenSigops.begin(), fifteenSigops.end());
                   +  - ]
     361                 :             : 
     362   [ +  -  +  -  :           3 :     BOOST_CHECK(::AreInputsStandard(CTransaction(txTo), coins));
          +  -  +  -  +  
                      - ]
     363                 :             :     // 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4]
     364   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txTo), coins), 22U);
             +  -  +  - ]
     365                 :             : 
     366         [ +  - ]:           1 :     CMutableTransaction txToNonStd1;
     367         [ +  - ]:           1 :     txToNonStd1.vout.resize(1);
     368   [ +  -  +  -  :           1 :     txToNonStd1.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[1].GetPubKey()));
                   +  - ]
     369         [ +  - ]:           1 :     txToNonStd1.vout[0].nValue = 1000;
     370         [ +  - ]:           1 :     txToNonStd1.vin.resize(1);
     371         [ +  - ]:           1 :     txToNonStd1.vin[0].prevout.n = 5;
     372         [ +  - ]:           1 :     txToNonStd1.vin[0].prevout.hash = txFrom.GetHash();
     373   [ -  +  +  -  :           1 :     txToNonStd1.vin[0].scriptSig << std::vector<unsigned char>(sixteenSigops.begin(), sixteenSigops.end());
                   +  - ]
     374                 :             : 
     375   [ +  -  +  -  :           3 :     BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd1), coins));
          +  -  +  -  +  
                      - ]
     376   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd1), coins), 16U);
             +  -  +  - ]
     377                 :             : 
     378         [ +  - ]:           1 :     CMutableTransaction txToNonStd2;
     379         [ +  - ]:           1 :     txToNonStd2.vout.resize(1);
     380   [ +  -  +  -  :           1 :     txToNonStd2.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key[1].GetPubKey()));
                   +  - ]
     381         [ +  - ]:           1 :     txToNonStd2.vout[0].nValue = 1000;
     382         [ +  - ]:           1 :     txToNonStd2.vin.resize(1);
     383         [ +  - ]:           1 :     txToNonStd2.vin[0].prevout.n = 6;
     384         [ +  - ]:           1 :     txToNonStd2.vin[0].prevout.hash = txFrom.GetHash();
     385   [ -  +  +  -  :           1 :     txToNonStd2.vin[0].scriptSig << std::vector<unsigned char>(twentySigops.begin(), twentySigops.end());
                   +  - ]
     386                 :             : 
     387   [ +  -  +  -  :           3 :     BOOST_CHECK(!::AreInputsStandard(CTransaction(txToNonStd2), coins));
          +  -  +  -  +  
                      - ]
     388   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(txToNonStd2), coins), 20U);
             +  -  +  - ]
     389   [ +  +  -  - ]:          11 : }
     390                 :             : 
     391                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1