Browse Source

Use actually valid transactions for script tests

0.10
Pieter Wuille 10 years ago
parent
commit
76ec867796
  1. 9
      src/test/data/script_invalid.json
  2. 9
      src/test/data/script_valid.json
  3. 49
      src/test/script_tests.cpp

9
src/test/data/script_invalid.json

@ -1,4 +1,13 @@ @@ -1,4 +1,13 @@
[
["
Format is: [scriptPubKey, scriptSig, flags, ... comments]
It is evaluated as if there was a crediting coinbase transaction with two 0
pushes as scriptSig, and one output of 0 satoshi and given scriptPubKey,
followed by a spending transaction which spends this output as only input (and
correct prevout hash), using the given scriptSig. All nLockTimes are 0, all
nSequences are max.
"],
["", "DEPTH", "P2SH,STRICTENC", "Test the test: we should have an empty stack after scriptSig evaluation"],
[" ", "DEPTH", "P2SH,STRICTENC", "and multiple spaces should not change that."],
[" ", "DEPTH", "P2SH,STRICTENC"],

9
src/test/data/script_valid.json

@ -1,4 +1,13 @@ @@ -1,4 +1,13 @@
[
["
Format is: [scriptPubKey, scriptSig, flags, ... comments]
It is evaluated as if there was a crediting coinbase transaction with two 0
pushes as scriptSig, and one output of 0 satoshi and given scriptPubKey,
followed by a spending transaction which spends this output as only input (and
correct prevout hash), using the given scriptSig. All nLockTimes are 0, all
nSequences are max.
"],
["", "DEPTH 0 EQUAL", "P2SH,STRICTENC", "Test the test: we should have an empty stack after scriptSig evaluation"],
[" ", "DEPTH 0 EQUAL", "P2SH,STRICTENC", "and multiple spaces should not change that."],
[" ", "DEPTH 0 EQUAL", "P2SH,STRICTENC"],

49
src/test/script_tests.cpp

@ -52,6 +52,41 @@ read_json(const std::string& jsondata) @@ -52,6 +52,41 @@ read_json(const std::string& jsondata)
BOOST_AUTO_TEST_SUITE(script_tests)
CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
{
CMutableTransaction txCredit;
txCredit.nVersion = 1;
txCredit.nLockTime = 0;
txCredit.vin.resize(1);
txCredit.vout.resize(1);
txCredit.vin[0].prevout.SetNull();
txCredit.vin[0].scriptSig = CScript() << CScriptNum(0) << CScriptNum(0);
txCredit.vin[0].nSequence = std::numeric_limits<unsigned int>::max();
txCredit.vout[0].scriptPubKey = scriptPubKey;
txCredit.vout[0].nValue = 0;
return txCredit;
}
CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CScript& scriptPubKey)
{
CMutableTransaction txCredit = BuildCreditingTransaction(scriptPubKey);
CMutableTransaction txSpend;
txSpend.nVersion = 1;
txSpend.nLockTime = 0;
txSpend.vin.resize(1);
txSpend.vout.resize(1);
txSpend.vin[0].prevout.hash = txCredit.GetHash();
txSpend.vin[0].prevout.n = 0;
txSpend.vin[0].scriptSig = scriptSig;
txSpend.vin[0].nSequence = std::numeric_limits<unsigned int>::max();
txSpend.vout[0].scriptPubKey = CScript();
txSpend.vout[0].nValue = 0;
return txSpend;
}
BOOST_AUTO_TEST_CASE(script_valid)
{
// Read tests from test/data/script_valid.json
@ -67,7 +102,9 @@ BOOST_AUTO_TEST_CASE(script_valid) @@ -67,7 +102,9 @@ BOOST_AUTO_TEST_CASE(script_valid)
string strTest = write_string(tv, false);
if (test.size() < 3) // Allow size > 3; extra stuff ignored (useful for comments)
{
BOOST_ERROR("Bad test: " << strTest);
if (test.size() != 1) {
BOOST_ERROR("Bad test: " << strTest);
}
continue;
}
string scriptSigString = test[0].get_str();
@ -77,7 +114,7 @@ BOOST_AUTO_TEST_CASE(script_valid) @@ -77,7 +114,7 @@ BOOST_AUTO_TEST_CASE(script_valid)
unsigned int scriptflags = ParseScriptFlags(test[2].get_str());
CTransaction tx;
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, tx, 0, scriptflags), strTest);
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, BuildSpendingTransaction(scriptSig, scriptPubKey), 0, scriptflags), strTest);
}
}
@ -90,9 +127,11 @@ BOOST_AUTO_TEST_CASE(script_invalid) @@ -90,9 +127,11 @@ BOOST_AUTO_TEST_CASE(script_invalid)
{
Array test = tv.get_array();
string strTest = write_string(tv, false);
if (test.size() < 2) // Allow size > 2; extra stuff ignored (useful for comments)
if (test.size() < 3) // Allow size > 3; extra stuff ignored (useful for comments)
{
BOOST_ERROR("Bad test: " << strTest);
if (test.size() != 1) {
BOOST_ERROR("Bad test: " << strTest);
}
continue;
}
string scriptSigString = test[0].get_str();
@ -102,7 +141,7 @@ BOOST_AUTO_TEST_CASE(script_invalid) @@ -102,7 +141,7 @@ BOOST_AUTO_TEST_CASE(script_invalid)
unsigned int scriptflags = ParseScriptFlags(test[2].get_str());
CTransaction tx;
BOOST_CHECK_MESSAGE(!VerifyScript(scriptSig, scriptPubKey, tx, 0, scriptflags), strTest);
BOOST_CHECK_MESSAGE(!VerifyScript(scriptSig, scriptPubKey, BuildSpendingTransaction(scriptSig, scriptPubKey), 0, scriptflags), strTest);
}
}

Loading…
Cancel
Save