Another pass: rough shape of tree and tests

It isn't working yet, the tests don't pass.

But it is on the way!
This commit is contained in:
Luke Murphy
2019-08-01 13:06:32 +02:00
parent c89cafe022
commit 45e24073d2
4 changed files with 154 additions and 25 deletions

22
test/conftest.py Normal file

@ -0,0 +1,22 @@
import hashlib
import pytest
@pytest.fixture
def leaf():
def _leaf(node):
return hashlib.sha256(leaf.data).hexdigest()
return _leaf
@pytest.fixture
def parent():
def _parent(left, right):
sha256 = hashlib.sha256()
sha256.update(left)
sha256.update(right)
return sha256.hexdigest()
return _parent

66
test/test_generator.py Normal file

@ -0,0 +1,66 @@
"""Generator test module."""
import hashlib
from merkle_tree_stream import MerkleTreeGenerator, MerkleTreeNode
def test_hashes(leaf, parent):
stream = MerkleTreeGenerator(leaf=leaf, parent=parent)
stream.next(b'a')
first_node = (
MerkleTreeNode(
index=0,
parent=1,
hash=hashlib.sha256(b'a').hexdigest(),
size=1,
data=b'a',
),
)
stream.next(b'b')
second_node = (
MerkleTreeNode(
index=2,
parent=1,
hash=hashlib.sha256(b'b').hexdigest(),
size=1,
data=b'a',
),
)
stream.next(b'c')
third = hashlib.sha256(b'a')
third.update(b'b')
third_hash = third.hexdigest()
third_node = (
MerkleTreeNode(index=1, parent=3, hash=third_hash, size=2, data=b'a'),
)
assert stream.nodes == [first_node, second_node, third_node]
def test_single_root(leaf, parent):
stream = MerkleTreeGenerator(leaf=leaf, parent=parent)
stream.next('a')
stream.next('b')
stream.next('c')
stream.next('d')
assert stream.roots.length == 1
def multiple_roots(leaf, parent):
stream = MerkleTreeGenerator(leaf=leaf, parent=parent)
stream.next('a')
stream.next('b')
stream.next('c')
assert stream.roots.length > 1