Skip to content

Commit

Permalink
test: add trivial validation invalid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Mar 24, 2024
1 parent 9c1e4ca commit 4b0ed88
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ JSON_TEST_FILES = \
test/data/tx_valid.json \
test/data/sighash.json \
test/data/specialtx_valid.json \
test/data/specialtx_invalid.json \
test/data/merkle_roots_sapling.json \
test/data/merkle_serialization_sapling.json \
test/data/merkle_witness_serialization_sapling.json \
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(JSON_TEST_FILES
${CMAKE_CURRENT_SOURCE_DIR}/data/tx_valid.json
${CMAKE_CURRENT_SOURCE_DIR}/data/sighash.json
${CMAKE_CURRENT_SOURCE_DIR}/data/specialtx_valid.json
${CMAKE_CURRENT_SOURCE_DIR}/data/specialtx_invalid.json
${CMAKE_CURRENT_SOURCE_DIR}/data/merkle_roots_sapling.json
${CMAKE_CURRENT_SOURCE_DIR}/data/merkle_serialization_sapling.json
${CMAKE_CURRENT_SOURCE_DIR}/data/merkle_witness_serialization_sapling.json
Expand Down
106 changes: 63 additions & 43 deletions src/test/evo_specialtx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php.

#include "test/data/specialtx_invalid.json.h"
#include "test/data/specialtx_valid.json.h"
#include "test/test_pivx.h"

Expand Down Expand Up @@ -133,12 +134,65 @@ static bool EqualCommitments(const llmq::CFinalCommitment& a, const llmq::CFinal
}

template <typename T>
static void TrivialCheckSpecialTx(const CMutableTransaction& mtx)
static void TrivialCheckSpecialTx(const CMutableTransaction& mtx, const bool shouldFail, const std::string& rejectReason)
{
T pl;
CValidationState state;
GetTxPayload(mtx, pl);
BOOST_CHECK(pl.IsTriviallyValid(state));
BOOST_CHECK(pl.IsTriviallyValid(state) == !shouldFail);
if (shouldFail) {
BOOST_CHECK(state.GetRejectReason() == rejectReason);
}
}

static void SpecialTxTrivialValidator(const UniValue& tests)
{
for (size_t i = 1; i < tests.size(); i++) {
const auto& test = tests[i];

uint256 txHash;
std::string txType;
CMutableTransaction mtx;
std::string rejectReason = "";
try {
txHash = uint256S(test[0].get_str());

txType = test[1].get_str();
CDataStream stream(ParseHex(test[2].get_str()), SER_NETWORK, PROTOCOL_VERSION);
stream >> mtx;

bool shouldFail = test.size() > 3;
if (shouldFail) {
rejectReason = test[3].get_str();
}
BOOST_CHECK(mtx.GetHash() == txHash);

switch (mtx.nType) {
case CTransaction::TxType::PROREG:
BOOST_CHECK(txType == "proreg");
TrivialCheckSpecialTx<ProRegPL>(mtx, shouldFail, rejectReason);
break;
case CTransaction::TxType::PROUPSERV:
BOOST_CHECK(txType == "proupserv");
TrivialCheckSpecialTx<ProUpServPL>(mtx, shouldFail, rejectReason);
break;
case CTransaction::TxType::PROUPREG:
BOOST_CHECK(txType == "proupreg");
TrivialCheckSpecialTx<ProUpRegPL>(mtx, shouldFail, rejectReason);
break;
case CTransaction::TxType::PROUPREV:
BOOST_CHECK(txType == "prouprev");
TrivialCheckSpecialTx<ProUpRevPL>(mtx, shouldFail, rejectReason);
break;
default:
BOOST_CHECK(false);
}
} catch (...) {
std::string strTest = test.write();
BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
continue;
}
}
}

BOOST_AUTO_TEST_CASE(protx_validation_test)
Expand Down Expand Up @@ -290,50 +344,16 @@ BOOST_AUTO_TEST_CASE(llmqcomm_setpayload_test)
BOOST_CHECK(EqualCommitments(pl.commitment, pl2.commitment));
}

BOOST_AUTO_TEST_CASE(specialtx_trivial_validation)
BOOST_AUTO_TEST_CASE(specialtx_trivial_valid)
{
UniValue tests = read_json(std::string(json_tests::specialtx_valid, json_tests::specialtx_valid + sizeof(json_tests::specialtx_valid)));
for (size_t i = 1; i < tests.size(); i++) {
const auto& test = tests[i];

uint256 txHash;
std::string txType;
CMutableTransaction mtx;
try {
txHash = uint256S(test[0].get_str());

txType = test[1].get_str();
CDataStream stream(ParseHex(test[2].get_str()), SER_NETWORK, PROTOCOL_VERSION);
stream >> mtx;

BOOST_CHECK(mtx.GetHash() == txHash);
SpecialTxTrivialValidator(tests);
}

switch (mtx.nType) {
case CTransaction::TxType::PROREG:
BOOST_CHECK(txType == "proreg");
TrivialCheckSpecialTx<ProRegPL>(mtx);
break;
case CTransaction::TxType::PROUPSERV:
BOOST_CHECK(txType == "proupserv");
TrivialCheckSpecialTx<ProUpServPL>(mtx);
break;
case CTransaction::TxType::PROUPREG:
BOOST_CHECK(txType == "proupreg");
TrivialCheckSpecialTx<ProUpRegPL>(mtx);
break;
case CTransaction::TxType::PROUPREV:
BOOST_CHECK(txType == "prouprev");
TrivialCheckSpecialTx<ProUpRevPL>(mtx);
break;
default:
BOOST_CHECK(false);
}
} catch (...) {
std::string strTest = test.write();
BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
continue;
}
}
BOOST_AUTO_TEST_CASE(specialtx_trivial_invalid)
{
UniValue tests = read_json(std::string(json_tests::specialtx_invalid, json_tests::specialtx_invalid + sizeof(json_tests::specialtx_invalid)));
SpecialTxTrivialValidator(tests);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 4b0ed88

Please sign in to comment.