Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_tests.cpp
Show First 20 Lines • Show All 308 Lines • ▼ Show 20 Lines | TestBuilder(const CScript &script_, const std::string &comment_, | ||||
spendTx = BuildSpendingTransaction(CScript(), *creditTx); | spendTx = BuildSpendingTransaction(CScript(), *creditTx); | ||||
} | } | ||||
TestBuilder &SetScriptError(ScriptError err) { | TestBuilder &SetScriptError(ScriptError err) { | ||||
scriptError = err; | scriptError = err; | ||||
return *this; | return *this; | ||||
} | } | ||||
TestBuilder &Add(const CScript &_script) { | TestBuilder &Opcode(const opcodetype &_op) { | ||||
DoPush(); | DoPush(); | ||||
spendTx.vin[0].scriptSig += _script; | spendTx.vin[0].scriptSig << _op; | ||||
return *this; | return *this; | ||||
} | } | ||||
TestBuilder &Num(int num) { | TestBuilder &Num(int num) { | ||||
DoPush(); | DoPush(); | ||||
spendTx.vin[0].scriptSig << num; | spendTx.vin[0].scriptSig << num; | ||||
return *this; | return *this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 752 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(script_build) { | ||||
tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) | tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) | ||||
<< ToByteVector(keys.pubkey1C) << OP_2 | << ToByteVector(keys.pubkey1C) << OP_2 | ||||
<< OP_CHECKMULTISIG, | << OP_CHECKMULTISIG, | ||||
"2-of-2 with two identical keys and sigs " | "2-of-2 with two identical keys and sigs " | ||||
"pushed using OP_DUP but no SIGPUSHONLY", | "pushed using OP_DUP but no SIGPUSHONLY", | ||||
0) | 0) | ||||
.Num(0) | .Num(0) | ||||
.PushSigECDSA(keys.key1) | .PushSigECDSA(keys.key1) | ||||
.Add(CScript() << OP_DUP)); | .Opcode(OP_DUP)); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder( | TestBuilder( | ||||
CScript() << OP_2 << ToByteVector(keys.pubkey1C) | CScript() << OP_2 << ToByteVector(keys.pubkey1C) | ||||
<< ToByteVector(keys.pubkey1C) << OP_2 | << ToByteVector(keys.pubkey1C) << OP_2 | ||||
<< OP_CHECKMULTISIG, | << OP_CHECKMULTISIG, | ||||
"2-of-2 with two identical keys and sigs pushed using OP_DUP", | "2-of-2 with two identical keys and sigs pushed using OP_DUP", | ||||
SCRIPT_VERIFY_SIGPUSHONLY) | SCRIPT_VERIFY_SIGPUSHONLY) | ||||
.Num(0) | .Num(0) | ||||
.PushSigECDSA(keys.key1) | .PushSigECDSA(keys.key1) | ||||
.Add(CScript() << OP_DUP) | .Opcode(OP_DUP) | ||||
.SetScriptError(ScriptError::SIG_PUSHONLY)); | .SetScriptError(ScriptError::SIG_PUSHONLY)); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder( | TestBuilder( | ||||
CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | ||||
"P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0, | "P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0, | ||||
true) | true) | ||||
.PushSigECDSA(keys.key2) | .PushSigECDSA(keys.key2) | ||||
.Add(CScript() << OP_NOP8) | .Opcode(OP_NOP8) | ||||
.PushRedeem()); | .PushRedeem()); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | ||||
"P2PK with non-push scriptSig but with P2SH validation", 0) | "P2PK with non-push scriptSig but with P2SH validation", 0) | ||||
.PushSigECDSA(keys.key2) | .PushSigECDSA(keys.key2) | ||||
.Add(CScript() << OP_NOP8)); | .Opcode(OP_NOP8)); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | ||||
"P2SH(P2PK) with non-push scriptSig but no SIGPUSHONLY", | "P2SH(P2PK) with non-push scriptSig but no SIGPUSHONLY", | ||||
SCRIPT_VERIFY_P2SH, true) | SCRIPT_VERIFY_P2SH, true) | ||||
.PushSigECDSA(keys.key2) | .PushSigECDSA(keys.key2) | ||||
.Add(CScript() << OP_NOP8) | .Opcode(OP_NOP8) | ||||
.PushRedeem() | .PushRedeem() | ||||
.SetScriptError(ScriptError::SIG_PUSHONLY)); | .SetScriptError(ScriptError::SIG_PUSHONLY)); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG, | ||||
"P2SH(P2PK) with non-push scriptSig but not P2SH", | "P2SH(P2PK) with non-push scriptSig but not P2SH", | ||||
SCRIPT_VERIFY_SIGPUSHONLY, true) | SCRIPT_VERIFY_SIGPUSHONLY, true) | ||||
.PushSigECDSA(keys.key2) | .PushSigECDSA(keys.key2) | ||||
.Add(CScript() << OP_NOP8) | .Opcode(OP_NOP8) | ||||
.PushRedeem() | .PushRedeem() | ||||
.SetScriptError(ScriptError::SIG_PUSHONLY)); | .SetScriptError(ScriptError::SIG_PUSHONLY)); | ||||
tests.push_back( | tests.push_back( | ||||
TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) | TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) | ||||
<< ToByteVector(keys.pubkey1C) << OP_2 | << ToByteVector(keys.pubkey1C) << OP_2 | ||||
<< OP_CHECKMULTISIG, | << OP_CHECKMULTISIG, | ||||
"2-of-2 with two identical keys and sigs pushed", | "2-of-2 with two identical keys and sigs pushed", | ||||
SCRIPT_VERIFY_SIGPUSHONLY) | SCRIPT_VERIFY_SIGPUSHONLY) | ||||
▲ Show 20 Lines • Show All 2,068 Lines • ▼ Show 20 Lines | for (uint8_t opcode = OP_1NEGATE; opcode < FIRST_UNDEFINED_OP_VALUE; | ||||
script = CScript() << opcode; | script = CScript() << opcode; | ||||
BOOST_CHECK(script.HasValidOps()); | BOOST_CHECK(script.HasValidOps()); | ||||
} | } | ||||
script = CScript() << FIRST_UNDEFINED_OP_VALUE; | script = CScript() << FIRST_UNDEFINED_OP_VALUE; | ||||
BOOST_CHECK(!script.HasValidOps()); | BOOST_CHECK(!script.HasValidOps()); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(script_can_append_self) { | |||||
CScript s, d; | |||||
s = ScriptFromHex("00"); | |||||
s += s; | |||||
d = ScriptFromHex("0000"); | |||||
BOOST_CHECK(s == d); | |||||
// check doubling a script that's large enough to require reallocation | |||||
static const char hex[] = | |||||
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6" | |||||
"bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"; | |||||
s = CScript() << ParseHex(hex) << OP_CHECKSIG; | |||||
d = CScript() << ParseHex(hex) << OP_CHECKSIG << ParseHex(hex) | |||||
<< OP_CHECKSIG; | |||||
s += s; | |||||
BOOST_CHECK(s == d); | |||||
} | |||||
#if defined(HAVE_CONSENSUS_LIB) | #if defined(HAVE_CONSENSUS_LIB) | ||||
/* Test simple (successful) usage of bitcoinconsensus_verify_script */ | /* Test simple (successful) usage of bitcoinconsensus_verify_script */ | ||||
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true) { | BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true) { | ||||
unsigned int libconsensus_flags = 0; | unsigned int libconsensus_flags = 0; | ||||
int nIn = 0; | int nIn = 0; | ||||
CScript scriptPubKey; | CScript scriptPubKey; | ||||
▲ Show 20 Lines • Show All 140 Lines • Show Last 20 Lines |