Beispiel #1
0
def test_LeafConstructionError_upon_update():
    """Tests that a `LeafConstructionError` is raised if both `record` and `digest`
    are provided as arguments to the `MerkleTree.update()` method
    """

    t = MerkleTree()

    with pytest.raises(LeafConstructionError):
        t.update(
            record='some record',
            digest=
            '540ef8fc9eefa3ec0fbe55bc5d10dbea03d5bac5591b3d7db3af79ec24b3f74c')
Beispiel #2
0
def test___repr__non_default_empty_tree():
    tree = MerkleTree(hash_type='sha512', encoding='UTF-32', security=False)
    assert tree.__repr__() == '\n    uuid      : %s\
                \n\
                \n    hash-type : SHA512\
                \n    encoding  : UTF-32\
                \n    security  : DEACTIVATED\
                \n\
                \n    root-hash : [None]\
                \n\
                \n    length    : 0\
                \n    size      : 0\
                \n    height    : 0\n' % tree.uuid
Beispiel #3
0
def test___repr__default_non_empty_tree():
    tree = MerkleTree(b'first', b'second', b'third')
    assert tree.__repr__() == '\n    uuid      : %s\
                \n\
                \n    hash-type : SHA256\
                \n    encoding  : UTF-8\
                \n    security  : ACTIVATED\
                \n\
                \n    root-hash : %s\
                \n\
                \n    length    : 3\
                \n    size      : 5\
                \n    height    : 2\n' % (tree.uuid,
                                          tree.rootHash.decode(tree.encoding))
Beispiel #4
0
def test_NotSupportedEncodingError():
    """Tests that a `NotSupportedEncodingError` is raised when a Merkle-tree
    for an unsupported encoding type is requested
    """

    with pytest.raises(NotSupportedEncodingError):
        MerkleTree(encoding='anything unsupported...')
Beispiel #5
0
def test_NotSupportedHashTypeError():
    """Tests that a `NotSupportedHashTypeError` is raised when a Merkle-tree
    for an unsupported hash-type is requested
    """

    with pytest.raises(NotSupportedHashTypeError):
        MerkleTree(hash_type='anything unsupported...')
Beispiel #6
0
def test_clear():

    tree = MerkleTree('a', 'b', 'c')
    tree.clear()

    assert tree.__dict__ == {
        'uuid': tree.uuid,
        'hash_type': 'sha256',
        'encoding': 'utf_8',
        'security': True,
        'hash': tree.hash,
        'multi_hash': tree.multi_hash,
        'leaves': [],
        'nodes': set(),
        '_root': None
    }
Beispiel #7
0
def test_rootHash_of_non_empty_MerkleTree():
    """Tests the root-hash of a Merkle-tree with one and two leaves
    """

    t = MerkleTree('first record')
    s = MerkleTree('first record', 'second record')

    assert t.rootHash == t.hash('first record') and s.rootHash == s.hash(
        s.hash('first record'), s.hash('second record'))
Beispiel #8
0
def test_rootHash_empty_tree_exception():
    """Tests that requesting the root-hash of an empty Merkle-tree raises an `EmptyTreeException`
    """

    empty = MerkleTree()

    with pytest.raises(EmptyTreeException):
        empty.rootHash
Beispiel #9
0
def test_defautl_MerkleTree_constructor_without_initial_records():

    tree = MerkleTree()

    assert tree.__dict__ == {
        'uuid': tree.uuid,
        'hash_type': 'sha256',
        'encoding': 'utf_8',
        'security': True,
        'hash': tree.hash,
        'multi_hash': tree.multi_hash,
        'leaves': [],
        'nodes': set()
    }
Beispiel #10
0
def test_non_default_MerkleTree_constructor_without_initial_records():

    tree = MerkleTree(hash_type='sha512', encoding='utf-32', security=False)

    assert tree.__dict__ == {
        'uuid': tree.uuid,
        'hash_type': 'sha512',
        'encoding': 'utf_32',
        'security': False,
        'hash': tree.hash,
        'multi_hash': tree.multi_hash,
        'leaves': [],
        'nodes': set()
    }
Beispiel #11
0
def test_defautl_MerkleTree_constructor_with_initial_records():

    tree = MerkleTree('first record...', 'second record...')

    assert tree.__dict__ == {
        'uuid': tree.uuid,
        'hash_type': 'sha256',
        'encoding': 'utf_8',
        'security': True,
        'hash': tree.hash,
        'multi_hash': tree.multi_hash,
        'leaves': tree.leaves,
        'nodes': tree.nodes,
        '_root': tree.root
    }
Beispiel #12
0
def test_UndecodableRecordError_upon_tree_construction(_byte, _encoding,
                                                       _security):

    with pytest.raises(UndecodableRecordError):
        MerkleTree('a', _byte, encoding=_encoding, security=_security)
Beispiel #13
0
def test_properties_of_empty_tree():
    tree = MerkleTree()
    assert (tree.length, tree.size, tree.height) == (0, 0, 0)
Beispiel #14
0
def test_UndecodableRecordError_upon_update(_byte, _encoding, _security):

    t = MerkleTree('a', 'b', 'c', encoding=_encoding, security=_security)

    with pytest.raises(UndecodableRecordError):
        t.update(record=_byte)
Beispiel #15
0
def test_MerkleTree_bool_implementation():
    """Tests that a Merkle-tree is equivalent to `False` iff it is empty
    """
    assert not MerkleTree() and MerkleTree('some record')
Beispiel #16
0
def test_loadFromFile():

    assert tree.serialize() == MerkleTree.loadFromFile(export_path).serialize()
Beispiel #17
0
def test_WrongJSONFormat_with_loadFromFile():

    with pytest.raises(WrongJSONFormat):
        MerkleTree.loadFromFile(
            os.path.join(os.path.dirname(__file__), 'objects', 'sample.json'))
Beispiel #18
0
def test_properties_of_tree_with_three_leaves():
    tree = MerkleTree('first', 'second', 'third')
    assert (tree.length, tree.size, tree.height) == (3, 5, 2)
Beispiel #19
0
 def generate_MerkleTree(*records):
     global TREE
     TREE = MerkleTree(*records, hash_type=HASH_TYPE, encoding=ENCODING)
Beispiel #20
0
    tree = MerkleTree(b'first', b'second', b'third')
    assert tree.__repr__() == '\n    uuid      : %s\
                \n\
                \n    hash-type : SHA256\
                \n    encoding  : UTF-8\
                \n    security  : ACTIVATED\
                \n\
                \n    root-hash : %s\
                \n\
                \n    length    : 3\
                \n    size      : 5\
                \n    height    : 2\n' % (tree.uuid,
                                          tree.rootHash.decode(tree.encoding))


empty_tree = MerkleTree()
one_leaf_tree = MerkleTree('first')
three_leaves_tree = MerkleTree('first', 'second', 'third')

serializations = [
    (empty_tree, {
        "hashes": [],
        "header": {
            "encoding": "utf_8",
            "hash_type": "sha256",
            "security": True
        }
    }),
    (one_leaf_tree, {
        "hashes":
        ["a1af030231ca2fd20ecf30c5294baf8f69321d09bb16ac53885ccd17a385280d"],