diff options
Diffstat (limited to 'src/test/pmt_tests.cpp')
-rw-r--r-- | src/test/pmt_tests.cpp | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp index 9dce4daac6..4406b08e56 100644 --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -1,13 +1,18 @@ // Copyright (c) 2012-2013 The Bitcoin Core developers -// Distributed under the MIT/X11 software license, see the accompanying +// Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "main.h" +#include "merkleblock.h" #include "serialize.h" +#include "streams.h" #include "uint256.h" +#include "arith_uint256.h" +#include "version.h" +#include "random.h" #include <vector> +#include <boost/assign/list_of.hpp> #include <boost/test/unit_test.hpp> using namespace std; @@ -17,10 +22,9 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree public: // flip one bit in one of the hashes - this should break the authentication void Damage() { - unsigned int n = rand() % vHash.size(); - int bit = rand() % 256; - uint256 &hash = vHash[n]; - hash ^= ((uint256)1 << bit); + unsigned int n = insecure_rand() % vHash.size(); + int bit = insecure_rand() % 256; + *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); } }; @@ -28,6 +32,7 @@ BOOST_AUTO_TEST_SUITE(pmt_tests) BOOST_AUTO_TEST_CASE(pmt_test1) { + seed_insecure_rand(false); static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095}; for (int n = 0; n < 12; n++) { @@ -37,13 +42,13 @@ BOOST_AUTO_TEST_CASE(pmt_test1) CBlock block; for (unsigned int j=0; j<nTx; j++) { CMutableTransaction tx; - tx.nLockTime = rand(); // actual transaction data doesn't matter; just make the nLockTime's unique + tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique block.vtx.push_back(CTransaction(tx)); } // calculate actual merkle root and height uint256 merkleRoot1 = block.BuildMerkleTree(); - std::vector<uint256> vTxid(nTx, 0); + std::vector<uint256> vTxid(nTx, uint256()); for (unsigned int j=0; j<nTx; j++) vTxid[j] = block.vtx[j].GetHash(); int nHeight = 1, nTx_ = nTx; @@ -58,7 +63,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1) std::vector<bool> vMatch(nTx, false); std::vector<uint256> vMatchTxid1; for (unsigned int j=0; j<nTx; j++) { - bool fInclude = (rand() & ((1 << (att/2)) - 1)) == 0; + bool fInclude = (insecure_rand() & ((1 << (att/2)) - 1)) == 0; vMatch[j] = fInclude; if (fInclude) vMatchTxid1.push_back(vTxid[j]); @@ -85,7 +90,7 @@ BOOST_AUTO_TEST_CASE(pmt_test1) // check that it has the same merkle root as the original, and a valid one BOOST_CHECK(merkleRoot1 == merkleRoot2); - BOOST_CHECK(merkleRoot2 != 0); + BOOST_CHECK(!merkleRoot2.IsNull()); // check that it contains the matched transactions (in the same order!) BOOST_CHECK(vMatchTxid1 == vMatchTxid2); @@ -102,4 +107,20 @@ BOOST_AUTO_TEST_CASE(pmt_test1) } } +BOOST_AUTO_TEST_CASE(pmt_malleability) +{ + std::vector<uint256> vTxid = boost::assign::list_of + (ArithToUint256(1))(ArithToUint256(2)) + (ArithToUint256(3))(ArithToUint256(4)) + (ArithToUint256(5))(ArithToUint256(6)) + (ArithToUint256(7))(ArithToUint256(8)) + (ArithToUint256(9))(ArithToUint256(10)) + (ArithToUint256(9))(ArithToUint256(10)); + std::vector<bool> vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false); + + CPartialMerkleTree tree(vTxid, vMatch); + std::vector<uint256> vTxid2; + BOOST_CHECK(tree.ExtractMatches(vTxid).IsNull()); +} + BOOST_AUTO_TEST_SUITE_END() |