2019-08-07 05:58:26 +00:00
|
|
|
"""Merkle tree test module."""
|
2019-08-01 15:21:28 +00:00
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from merkle_tree_stream import MerkleTreeIterator, MerkleTreeNode
|
|
|
|
|
|
|
|
|
|
|
|
def test_hashes(leaf, parent):
|
|
|
|
merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent)
|
|
|
|
|
|
|
|
merkle_iter.write(b'a')
|
|
|
|
merkle_iter.write(b'b')
|
|
|
|
|
|
|
|
expected_count = 2 + 1 # nodes plus parent
|
|
|
|
assert len(merkle_iter) == expected_count
|
|
|
|
|
|
|
|
assert next(merkle_iter) == MerkleTreeNode(
|
2019-10-06 14:11:49 +00:00
|
|
|
index=0, parent=1, hash=hashlib.sha256(b'a').digest(), size=1, data=b'a'
|
2019-08-01 15:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert next(merkle_iter) == MerkleTreeNode(
|
2019-10-06 14:11:49 +00:00
|
|
|
index=2, parent=1, hash=hashlib.sha256(b'b').digest(), size=1, data=b'b'
|
2019-08-01 15:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
hashed = hashlib.sha256(b'a')
|
|
|
|
hashed.update(b'b')
|
|
|
|
|
|
|
|
assert next(merkle_iter) == MerkleTreeNode(
|
2019-10-06 14:11:49 +00:00
|
|
|
index=1, parent=3, hash=hashed.digest(), size=2, data=b''
|
2019-08-01 15:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(StopIteration):
|
|
|
|
next(merkle_iter)
|
|
|
|
|
|
|
|
|
|
|
|
def test_single_root(leaf, parent):
|
|
|
|
merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent)
|
|
|
|
|
|
|
|
merkle_iter.write(b'a')
|
|
|
|
merkle_iter.write(b'b')
|
|
|
|
merkle_iter.write(b'c')
|
|
|
|
merkle_iter.write(b'd')
|
|
|
|
|
|
|
|
assert len(merkle_iter.roots) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def multiple_roots(leaf, parent):
|
|
|
|
merkle_iter = MerkleTreeIterator(leaf=leaf, parent=parent)
|
|
|
|
|
|
|
|
merkle_iter.write(b'a')
|
|
|
|
merkle_iter.write(b'b')
|
|
|
|
merkle_iter.write(b'c')
|
|
|
|
|
|
|
|
assert len(merkle_iter.roots) > 1
|