| 164 | BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup) |
| 165 | |
| 166 | void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, uint32_t flags, const std::string& message, int scriptError, CAmount nValue = 0) |
| 167 | { |
| 168 | bool expect = (scriptError == SCRIPT_ERR_OK); |
| 169 | if (flags & SCRIPT_VERIFY_CLEANSTACK) { |
| 170 | flags |= SCRIPT_VERIFY_P2SH; |
| 171 | flags |= SCRIPT_VERIFY_WITNESS; |
| 172 | } |
| 173 | ScriptError err; |
| 174 | const CTransaction txCredit{BuildCreditingTransaction(scriptPubKey, nValue)}; |
| 175 | CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit); |
| 176 | CMutableTransaction tx2 = tx; |
| 177 | BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message); |
| 178 | BOOST_CHECK_MESSAGE(err == scriptError, FormatScriptError(err) + " where " + FormatScriptError((ScriptError_t)scriptError) + " expected: " + message); |
| 179 | |
| 180 | // Verify that removing flags from a passing test or adding flags to a failing test does not change the result. |
| 181 | for (int i = 0; i < 16; ++i) { |
| 182 | uint32_t extra_flags(InsecureRandBits(16)); |
| 183 | uint32_t combined_flags{expect ? (flags & ~extra_flags) : (flags | extra_flags)}; |
| 184 | // Weed out some invalid flag combinations. |
| 185 | if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) continue; |
| 186 | if (combined_flags & SCRIPT_VERIFY_WITNESS && ~combined_flags & SCRIPT_VERIFY_P2SH) continue; |
| 187 | BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message + strprintf(" (with flags %x)", combined_flags)); |
| 188 | } |
| 189 | |
| 190 | #if defined(HAVE_CONSENSUS_LIB) |
| 191 | CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); |
| 192 | stream << tx2; |
| 193 | CDataStream streamVal(SER_NETWORK, PROTOCOL_VERSION); |
| 194 | streamVal << txCredit.vout[0].nValue; |
| 195 | CDataStream streamVal0(SER_NETWORK, PROTOCOL_VERSION); |
| 196 | streamVal0 << CConfidentialValue(0); |
| 197 | uint32_t libconsensus_flags{flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL}; |
| 198 | if (libconsensus_flags == flags) { |
| 199 | int expectedSuccessCode = expect ? 1 : 0; |
| 200 | if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) { |
| 201 | BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(NULL, scriptPubKey.data(), scriptPubKey.size(), UCharCast(streamVal.data()), streamVal.size(), UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message); |
| 202 | } else { |
| 203 | BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(NULL, scriptPubKey.data(), scriptPubKey.size(), UCharCast(streamVal0.data()), streamVal0.size(), UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message); |
| 204 | BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(NULL, scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message); |
| 205 | } |
| 206 | } |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | void static NegateSignatureS(std::vector<unsigned char>& vchSig) { |
| 211 | // Parse the signature. |
no test coverage detected