mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-15 06:12:37 -03:00
0c9de67f34
0aef39d067
Silence "Test case [...] did not check any assertions" warnings when running "test_bitcoin --log_level=test_suite" (practicalswift)5fd73c8694
Avoid repeated log messages in tests by connecting to signal handlers (ThreadSafeMessageBox, etc.) only once (practicalswift)e502c3c515
tests: Reduce noise level in test_bitcoin output (practicalswift) Pull request description: Reduce noise level in `test_bitcoin` output. Context: When working on the non-determinism issues in the unit tests (see #15296) I got a bit tired of the amount of noise in the `test_bitcoin` output :-) Before: ``` $ src/test/test_bitcoin --log_level=test_suite 2>&1 | grep -vE '(Entering|Leaving)' | uniq -c 1 Running 341 test cases... 1 Test case blockencodings_tests/TransactionsRequestDeserializationOverflowTest did not check any assertions 1 CheckSplitTorReplyLine(PROTOCOLINFO PIVERSION) 1 CheckSplitTorReplyLine(AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/home/x/.tor/control_auth_cookie") 1 CheckSplitTorReplyLine(AUTH METHODS=NULL) 1 CheckSplitTorReplyLine(AUTH METHODS=HASHEDPASSWORD) 1 CheckSplitTorReplyLine(VERSION Tor="0.2.9.8 (git-a0df013ea241b026)") 1 CheckSplitTorReplyLine(AUTHCHALLENGE SERVERHASH=aaaa SERVERNONCE=bbbb) 1 CheckSplitTorReplyLine(COMMAND) 1 CheckSplitTorReplyLine(COMMAND SOME ARGS) 1 CheckSplitTorReplyLine(COMMAND ARGS) 1 CheckSplitTorReplyLine(COMMAND EVEN+more ARGS) 1 CheckParseTorReplyMapping(METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/home/x/.tor/control_auth_cookie") 1 CheckParseTorReplyMapping(METHODS=NULL) 1 CheckParseTorReplyMapping(METHODS=HASHEDPASSWORD) 1 CheckParseTorReplyMapping(Tor="0.2.9.8 (git-a0df013ea241b026)") 1 CheckParseTorReplyMapping(SERVERHASH=aaaa SERVERNONCE=bbbb) 1 CheckParseTorReplyMapping(ServiceID=exampleonion1234) 1 CheckParseTorReplyMapping(PrivateKey=RSA1024:BLOB) 1 CheckParseTorReplyMapping(ClientAuth=bob:BLOB) 1 CheckParseTorReplyMapping(Foo=Bar=Baz Spam=Eggs) 1 CheckParseTorReplyMapping(Foo="Bar=Baz") 1 CheckParseTorReplyMapping(Foo="Bar Baz") 1 CheckParseTorReplyMapping(Foo="Bar\ Baz") 1 CheckParseTorReplyMapping(Foo="Bar\Baz") 1 CheckParseTorReplyMapping(Foo="Bar\@Baz") 1 CheckParseTorReplyMapping(Foo="Bar\"Baz" Spam="\"Eggs\"") 1 CheckParseTorReplyMapping(Foo="Bar\\Baz") 1 CheckParseTorReplyMapping(Foo="Bar\nBaz\t" Spam="\rEggs" Octals="\1a\11\17\18\81\377\378\400\2222" Final=Check) 1 CheckParseTorReplyMapping(Valid=Mapping Escaped="Escape\\") 1 CheckParseTorReplyMapping(Valid=Mapping Bare="Escape\") 1 CheckParseTorReplyMapping(OneOctal="OneEnd\1" TwoOctal="TwoEnd\11") 1 CheckParseTorReplyMapping(Null="\0") 1 CheckParseTorReplyMapping(SOME=args,here MORE optional=arguments here) 1 CheckParseTorReplyMapping(ARGS) 1 CheckParseTorReplyMapping(MORE ARGS) 1 CheckParseTorReplyMapping(MORE ARGS) 1 CheckParseTorReplyMapping(EVEN more=ARGS) 1 CheckParseTorReplyMapping(EVEN+more ARGS) 1 Test case util_tests/util_criticalsection did not check any assertions 1 Testing known outcomes 326 Error: Specified -walletdir "/tmp/test_bitcoin/1553850209_943311758/tempdir/path_does_not_exist" does not exist 327 Error: Specified -walletdir "/tmp/test_bitcoin/1553850209_643733972/tempdir/not_a_directory.dat" is not a directory 328 Error: Specified -walletdir "wallets" is a relative path 1 1 *** No errors detected ``` After: ``` $ src/test/test_bitcoin --log_level=test_suite 2>&1 | grep -vE '(Entering|Leaving)' | uniq -c 1 Running 341 test cases... 1 Error: Specified -walletdir "/tmp/test_bitcoin/1553850026_943311758/tempdir/path_does_not_exist" does not exist 1 Error: Specified -walletdir "/tmp/test_bitcoin/1553850026_643733972/tempdir/not_a_directory.dat" is not a directory 1 Error: Specified -walletdir "wallets" is a relative path 1 1 *** No errors detected ``` ACKs for commit 0aef39: MarcoFalke: utACK0aef39d067
Tree-SHA512: 9cc22f64aa5c875861bae6533d18675ad978c623f053754deef6a8e271ea70bda3f72fb4ec5c8fd19b841757f21380639051d5f5b44301b9d2464b57655e9c05
393 lines
14 KiB
C++
393 lines
14 KiB
C++
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <blockencodings.h>
|
|
#include <consensus/merkle.h>
|
|
#include <chainparams.h>
|
|
#include <pow.h>
|
|
#include <random.h>
|
|
|
|
#include <test/setup_common.h>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
std::vector<std::pair<uint256, CTransactionRef>> extra_txn;
|
|
|
|
struct RegtestingSetup : public TestingSetup {
|
|
RegtestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {}
|
|
};
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(blockencodings_tests, RegtestingSetup)
|
|
|
|
static CBlock BuildBlockTestCase() {
|
|
CBlock block;
|
|
CMutableTransaction tx;
|
|
tx.vin.resize(1);
|
|
tx.vin[0].scriptSig.resize(10);
|
|
tx.vout.resize(1);
|
|
tx.vout[0].nValue = 42;
|
|
|
|
block.vtx.resize(3);
|
|
block.vtx[0] = MakeTransactionRef(tx);
|
|
block.nVersion = 42;
|
|
block.hashPrevBlock = InsecureRand256();
|
|
block.nBits = 0x207fffff;
|
|
|
|
tx.vin[0].prevout.hash = InsecureRand256();
|
|
tx.vin[0].prevout.n = 0;
|
|
block.vtx[1] = MakeTransactionRef(tx);
|
|
|
|
tx.vin.resize(10);
|
|
for (size_t i = 0; i < tx.vin.size(); i++) {
|
|
tx.vin[i].prevout.hash = InsecureRand256();
|
|
tx.vin[i].prevout.n = 0;
|
|
}
|
|
block.vtx[2] = MakeTransactionRef(tx);
|
|
|
|
bool mutated;
|
|
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
|
|
assert(!mutated);
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus())) ++block.nNonce;
|
|
return block;
|
|
}
|
|
|
|
// Number of shared use_counts we expect for a tx we haven't touched
|
|
// (block + mempool + our copy from the GetSharedTx call)
|
|
constexpr long SHARED_TX_OFFSET{3};
|
|
|
|
BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
|
|
{
|
|
CTxMemPool pool;
|
|
TestMemPoolEntryHelper entry;
|
|
CBlock block(BuildBlockTestCase());
|
|
|
|
LOCK2(cs_main, pool.cs);
|
|
pool.addUnchecked(entry.FromTx(block.vtx[2]));
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
|
|
|
|
// Do a simple ShortTxIDs RT
|
|
{
|
|
CBlockHeaderAndShortTxIDs shortIDs(block, true);
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << shortIDs;
|
|
|
|
CBlockHeaderAndShortTxIDs shortIDs2;
|
|
stream >> shortIDs2;
|
|
|
|
PartiallyDownloadedBlock partialBlock(&pool);
|
|
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(0));
|
|
BOOST_CHECK(!partialBlock.IsTxAvailable(1));
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(2));
|
|
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
|
|
|
|
size_t poolSize = pool.size();
|
|
pool.removeRecursive(*block.vtx[2]);
|
|
BOOST_CHECK_EQUAL(pool.size(), poolSize - 1);
|
|
|
|
CBlock block2;
|
|
{
|
|
PartiallyDownloadedBlock tmp = partialBlock;
|
|
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_INVALID); // No transactions
|
|
partialBlock = tmp;
|
|
}
|
|
|
|
// Wrong transaction
|
|
{
|
|
PartiallyDownloadedBlock tmp = partialBlock;
|
|
partialBlock.FillBlock(block2, {block.vtx[2]}); // Current implementation doesn't check txn here, but don't require that
|
|
partialBlock = tmp;
|
|
}
|
|
bool mutated;
|
|
BOOST_CHECK(block.hashMerkleRoot != BlockMerkleRoot(block2, &mutated));
|
|
|
|
CBlock block3;
|
|
BOOST_CHECK(partialBlock.FillBlock(block3, {block.vtx[1]}) == READ_STATUS_OK);
|
|
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block3.GetHash().ToString());
|
|
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block3, &mutated).ToString());
|
|
BOOST_CHECK(!mutated);
|
|
}
|
|
}
|
|
|
|
class TestHeaderAndShortIDs {
|
|
// Utility to encode custom CBlockHeaderAndShortTxIDs
|
|
public:
|
|
CBlockHeader header;
|
|
uint64_t nonce;
|
|
std::vector<uint64_t> shorttxids;
|
|
std::vector<PrefilledTransaction> prefilledtxn;
|
|
|
|
explicit TestHeaderAndShortIDs(const CBlockHeaderAndShortTxIDs& orig) {
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << orig;
|
|
stream >> *this;
|
|
}
|
|
explicit TestHeaderAndShortIDs(const CBlock& block) :
|
|
TestHeaderAndShortIDs(CBlockHeaderAndShortTxIDs(block, true)) {}
|
|
|
|
uint64_t GetShortID(const uint256& txhash) const {
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << *this;
|
|
CBlockHeaderAndShortTxIDs base;
|
|
stream >> base;
|
|
return base.GetShortID(txhash);
|
|
}
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
template <typename Stream, typename Operation>
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
|
READWRITE(header);
|
|
READWRITE(nonce);
|
|
size_t shorttxids_size = shorttxids.size();
|
|
READWRITE(VARINT(shorttxids_size));
|
|
shorttxids.resize(shorttxids_size);
|
|
for (size_t i = 0; i < shorttxids.size(); i++) {
|
|
uint32_t lsb = shorttxids[i] & 0xffffffff;
|
|
uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
|
|
READWRITE(lsb);
|
|
READWRITE(msb);
|
|
shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
|
|
}
|
|
READWRITE(prefilledtxn);
|
|
}
|
|
};
|
|
|
|
BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
|
|
{
|
|
CTxMemPool pool;
|
|
TestMemPoolEntryHelper entry;
|
|
CBlock block(BuildBlockTestCase());
|
|
|
|
LOCK2(cs_main, pool.cs);
|
|
pool.addUnchecked(entry.FromTx(block.vtx[2]));
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
|
|
|
|
uint256 txhash;
|
|
|
|
// Test with pre-forwarding tx 1, but not coinbase
|
|
{
|
|
TestHeaderAndShortIDs shortIDs(block);
|
|
shortIDs.prefilledtxn.resize(1);
|
|
shortIDs.prefilledtxn[0] = {1, block.vtx[1]};
|
|
shortIDs.shorttxids.resize(2);
|
|
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[0]->GetHash());
|
|
shortIDs.shorttxids[1] = shortIDs.GetShortID(block.vtx[2]->GetHash());
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << shortIDs;
|
|
|
|
CBlockHeaderAndShortTxIDs shortIDs2;
|
|
stream >> shortIDs2;
|
|
|
|
PartiallyDownloadedBlock partialBlock(&pool);
|
|
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
|
|
BOOST_CHECK(!partialBlock.IsTxAvailable(0));
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(1));
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(2));
|
|
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1); // +1 because of partialBlock
|
|
|
|
CBlock block2;
|
|
{
|
|
PartiallyDownloadedBlock tmp = partialBlock;
|
|
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_INVALID); // No transactions
|
|
partialBlock = tmp;
|
|
}
|
|
|
|
// Wrong transaction
|
|
{
|
|
PartiallyDownloadedBlock tmp = partialBlock;
|
|
partialBlock.FillBlock(block2, {block.vtx[1]}); // Current implementation doesn't check txn here, but don't require that
|
|
partialBlock = tmp;
|
|
}
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 2); // +2 because of partialBlock and block2
|
|
bool mutated;
|
|
BOOST_CHECK(block.hashMerkleRoot != BlockMerkleRoot(block2, &mutated));
|
|
|
|
CBlock block3;
|
|
PartiallyDownloadedBlock partialBlockCopy = partialBlock;
|
|
BOOST_CHECK(partialBlock.FillBlock(block3, {block.vtx[0]}) == READ_STATUS_OK);
|
|
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block3.GetHash().ToString());
|
|
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block3, &mutated).ToString());
|
|
BOOST_CHECK(!mutated);
|
|
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 3); // +2 because of partialBlock and block2 and block3
|
|
|
|
txhash = block.vtx[2]->GetHash();
|
|
block.vtx.clear();
|
|
block2.vtx.clear();
|
|
block3.vtx.clear();
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1 - 1); // + 1 because of partialBlock; -1 because of block.
|
|
}
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET - 1); // -1 because of block
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
|
|
{
|
|
CTxMemPool pool;
|
|
TestMemPoolEntryHelper entry;
|
|
CBlock block(BuildBlockTestCase());
|
|
|
|
LOCK2(cs_main, pool.cs);
|
|
pool.addUnchecked(entry.FromTx(block.vtx[1]));
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);
|
|
|
|
uint256 txhash;
|
|
|
|
// Test with pre-forwarding coinbase + tx 2 with tx 1 in mempool
|
|
{
|
|
TestHeaderAndShortIDs shortIDs(block);
|
|
shortIDs.prefilledtxn.resize(2);
|
|
shortIDs.prefilledtxn[0] = {0, block.vtx[0]};
|
|
shortIDs.prefilledtxn[1] = {1, block.vtx[2]}; // id == 1 as it is 1 after index 1
|
|
shortIDs.shorttxids.resize(1);
|
|
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[1]->GetHash());
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << shortIDs;
|
|
|
|
CBlockHeaderAndShortTxIDs shortIDs2;
|
|
stream >> shortIDs2;
|
|
|
|
PartiallyDownloadedBlock partialBlock(&pool);
|
|
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(0));
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(1));
|
|
BOOST_CHECK( partialBlock.IsTxAvailable(2));
|
|
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
|
|
|
|
CBlock block2;
|
|
PartiallyDownloadedBlock partialBlockCopy = partialBlock;
|
|
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_OK);
|
|
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
|
|
bool mutated;
|
|
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block2, &mutated).ToString());
|
|
BOOST_CHECK(!mutated);
|
|
|
|
txhash = block.vtx[1]->GetHash();
|
|
block.vtx.clear();
|
|
block2.vtx.clear();
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1 - 1); // + 1 because of partialBlock; -1 because of block.
|
|
}
|
|
BOOST_CHECK_EQUAL(pool.mapTx.find(txhash)->GetSharedTx().use_count(), SHARED_TX_OFFSET - 1); // -1 because of block
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
|
|
{
|
|
CTxMemPool pool;
|
|
CMutableTransaction coinbase;
|
|
coinbase.vin.resize(1);
|
|
coinbase.vin[0].scriptSig.resize(10);
|
|
coinbase.vout.resize(1);
|
|
coinbase.vout[0].nValue = 42;
|
|
|
|
CBlock block;
|
|
block.vtx.resize(1);
|
|
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
|
|
block.nVersion = 42;
|
|
block.hashPrevBlock = InsecureRand256();
|
|
block.nBits = 0x207fffff;
|
|
|
|
bool mutated;
|
|
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
|
|
assert(!mutated);
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus())) ++block.nNonce;
|
|
|
|
// Test simple header round-trip with only coinbase
|
|
{
|
|
CBlockHeaderAndShortTxIDs shortIDs(block, false);
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << shortIDs;
|
|
|
|
CBlockHeaderAndShortTxIDs shortIDs2;
|
|
stream >> shortIDs2;
|
|
|
|
PartiallyDownloadedBlock partialBlock(&pool);
|
|
BOOST_CHECK(partialBlock.InitData(shortIDs2, extra_txn) == READ_STATUS_OK);
|
|
BOOST_CHECK(partialBlock.IsTxAvailable(0));
|
|
|
|
CBlock block2;
|
|
std::vector<CTransactionRef> vtx_missing;
|
|
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_OK);
|
|
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
|
|
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block2, &mutated).ToString());
|
|
BOOST_CHECK(!mutated);
|
|
}
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(TransactionsRequestSerializationTest) {
|
|
BlockTransactionsRequest req1;
|
|
req1.blockhash = InsecureRand256();
|
|
req1.indexes.resize(4);
|
|
req1.indexes[0] = 0;
|
|
req1.indexes[1] = 1;
|
|
req1.indexes[2] = 3;
|
|
req1.indexes[3] = 4;
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << req1;
|
|
|
|
BlockTransactionsRequest req2;
|
|
stream >> req2;
|
|
|
|
BOOST_CHECK_EQUAL(req1.blockhash.ToString(), req2.blockhash.ToString());
|
|
BOOST_CHECK_EQUAL(req1.indexes.size(), req2.indexes.size());
|
|
BOOST_CHECK_EQUAL(req1.indexes[0], req2.indexes[0]);
|
|
BOOST_CHECK_EQUAL(req1.indexes[1], req2.indexes[1]);
|
|
BOOST_CHECK_EQUAL(req1.indexes[2], req2.indexes[2]);
|
|
BOOST_CHECK_EQUAL(req1.indexes[3], req2.indexes[3]);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationMaxTest) {
|
|
// Check that the highest legal index is decoded correctly
|
|
BlockTransactionsRequest req0;
|
|
req0.blockhash = InsecureRand256();
|
|
req0.indexes.resize(1);
|
|
req0.indexes[0] = 0xffff;
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << req0;
|
|
|
|
BlockTransactionsRequest req1;
|
|
stream >> req1;
|
|
BOOST_CHECK_EQUAL(req0.indexes.size(), req1.indexes.size());
|
|
BOOST_CHECK_EQUAL(req0.indexes[0], req1.indexes[0]);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(TransactionsRequestDeserializationOverflowTest) {
|
|
// Any set of index deltas that starts with N values that sum to (0x10000 - N)
|
|
// causes the edge-case overflow that was originally not checked for. Such
|
|
// a request cannot be created by serializing a real BlockTransactionsRequest
|
|
// due to the overflow, so here we'll serialize from raw deltas.
|
|
BlockTransactionsRequest req0;
|
|
req0.blockhash = InsecureRand256();
|
|
req0.indexes.resize(3);
|
|
req0.indexes[0] = 0x7000;
|
|
req0.indexes[1] = 0x10000 - 0x7000 - 2;
|
|
req0.indexes[2] = 0;
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
stream << req0.blockhash;
|
|
WriteCompactSize(stream, req0.indexes.size());
|
|
WriteCompactSize(stream, req0.indexes[0]);
|
|
WriteCompactSize(stream, req0.indexes[1]);
|
|
WriteCompactSize(stream, req0.indexes[2]);
|
|
|
|
BlockTransactionsRequest req1;
|
|
try {
|
|
stream >> req1;
|
|
// before patch: deserialize above succeeds and this check fails, demonstrating the overflow
|
|
BOOST_CHECK(req1.indexes[1] < req1.indexes[2]);
|
|
// this shouldn't be reachable before or after patch
|
|
BOOST_CHECK(0);
|
|
} catch(std::ios_base::failure &) {
|
|
// deserialize should fail
|
|
BOOST_CHECK(true); // Needed to suppress "Test case [...] did not check any assertions"
|
|
}
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|