Add more well-formedness checks/tests to tree.

This commit is contained in:
Sean Bowe 2016-04-21 11:44:16 -06:00
parent 01e4ff0f74
commit d0c4b0e850
2 changed files with 50 additions and 0 deletions

View File

@ -7,6 +7,8 @@
#include <iostream>
#include <stdexcept>
#include "utilstrencodings.h"
#include "version.h"
@ -319,6 +321,8 @@ BOOST_AUTO_TEST_CASE(tree_test_vectors)
}
BOOST_AUTO_TEST_CASE( deserializeInvalid ) {
// attempt to deserialize a small tree from a serialized large tree
// (exceeds depth well-formedness check)
ZCIncrementalMerkleTree newTree;
for (size_t i = 0; i < 16; i++) {
@ -334,6 +338,42 @@ BOOST_AUTO_TEST_CASE( deserializeInvalid ) {
BOOST_CHECK_THROW({ss >> newTreeSmall;}, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE( deserializeInvalid2 ) {
// the most ancestral parent is empty
CDataStream ss(
ParseHex("0155b852781b9995a44c939b64e441ae2724b96f99c8f4fb9a141cfc9842c4b0e3000100"),
SER_NETWORK,
PROTOCOL_VERSION
);
ZCIncrementalMerkleTree tree;
BOOST_CHECK_THROW(ss >> tree, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE( deserializeInvalid3 ) {
// left doesn't exist but right does
CDataStream ss(
ParseHex("000155b852781b9995a44c939b64e441ae2724b96f99c8f4fb9a141cfc9842c4b0e300"),
SER_NETWORK,
PROTOCOL_VERSION
);
ZCIncrementalMerkleTree tree;
BOOST_CHECK_THROW(ss >> tree, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE( deserializeInvalid4 ) {
// left doesn't exist but a parent does
CDataStream ss(
ParseHex("000001018695873d63ec0bceeadb5bf4ccc6723ac803c1826fc7cfb34fc76180305ae27d"),
SER_NETWORK,
PROTOCOL_VERSION
);
ZCIncrementalMerkleTree tree;
BOOST_CHECK_THROW(ss >> tree, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE( testZeroElements ) {
for (int start = 0; start < 20; start++) {
ZCIncrementalMerkleTree newTree;

View File

@ -61,6 +61,16 @@ void IncrementalMerkleTree<Depth, Hash>::wfcheck() const {
if (wasnull) {
throw std::ios_base::failure("tree has non-canonical representation of parent");
}
// Left cannot be empty when right exists.
if (!left && right) {
throw std::ios_base::failure("tree has non-canonical representation of tree");
}
// Left cannot be empty when parents is nonempty.
if (!left && parents.size() > 0) {
throw std::ios_base::failure("tree has non-canonical representation of tree");
}
}
template<size_t Depth, typename Hash>