Esempio n. 1
0
def test_hash_recalculation_UndecodableRecordError(_byte, _machine):
    with pytest.raises(UndecodableRecordError):

        _left = Leaf(
            record='left record',
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )

        _right = Leaf(
            record='right record',
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )

        _node  = Node(
            left=_left,
            right=_right,
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )

        _left = Leaf(
            record=_byte,
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash,
        )

        _node.set_left(_left)

        _node.recalculate_hash(_machine.hash)
Esempio n. 2
0
def test_leaf_UndecodableRecordError(_byte, _machine):
    with pytest.raises(UndecodableRecordError):
        Leaf(
            record=_byte,
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )
Esempio n. 3
0
def test_leaf_construction_exception_with_both_record_and_digest():
    """Tests that the Leaf constructor raises `TypeError`
    if both `record` and `digest` are provided
    """
    with pytest.raises(LeafConstructionError):
        Leaf(
            hashfunc=HASH,
            encoding=ENCODING,
            record=b'anything...',
            digest=HASH('whatever...')
        )
Esempio n. 4
0
def test_node_UndecodableArgumentError(_byte, _machine):
    with pytest.raises(UndecodableRecordError):

        _left = Leaf(
            record=_byte,
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )

        _right = Leaf(
            record=_byte,
            encoding=_machine.ENCODING,
            hashfunc=_machine.hash
        )

        with pytest.raises(UndecodableRecordError):
            Node(
                left=_left,
                right=_right,
                encoding=_machine.ENCODING,
                hashfunc=_machine.hash
            )
Esempio n. 5
0
def test_hash_recalculation():
    """Tests hash recalculation at the node_34 and root after modifying the hash stored by leaf_4
    """

    new_leaf = Leaf(
        hashfunc=HASH,
        encoding=ENCODING,
        record=b'new record...'
    )

    node_34.set_right(new_leaf)

    node_34.recalculate_hash(hashfunc=HASH)

    root.recalculate_hash(hashfunc=HASH)

    assert node_34.digest == HASH(
        leaf_3.digest, new_leaf.digest
    ) and root.digest == HASH(
        node_12.digest, node_34.digest
    )
Esempio n. 6
0
def nodes_benchmark():
    def access_digest_func(_node):
        def access_attribute():
            _node.digest

        return access_attribute

    def access_left_parent_func(_node):
        def access_attribute():
            _node.left

        return access_attribute

    def access_right_parent_func(_node):
        def access_attribute():
            _node.right

        return access_attribute

    def access_child_func(_node):
        def access_attribute():
            _node.child

        return access_attribute

    left = Leaf(hashfunc=HASH, encoding=ENCODING, record='first record...')
    right = Leaf(hashfunc=HASH, encoding=ENCODING, record='second record...')
    node = Node(hashfunc=HASH, encoding=ENCODING, left=left, right=right)

    # Size measurement

    sys.stdout.write(
        '\nSize of leaf with encoding %s and hash type %s (bytes): %d' %
        (ENCODING, HASH_TYPE, _size(left)))
    sys.stdout.write(
        '\nSize of internal node with encoding %s and hash type %s (bytes): %d'
        % (ENCODING, HASH_TYPE, _size(left)))

    # Digest access

    access_digest_measurements = timeit.repeat(access_digest_func(left),
                                               repeat=ITERATIONS,
                                               number=ROUNDS)

    mean, stdev = _mean_value(access_digest_measurements,
                              precision=precision_1,
                              with_stdev=True)
    _show_stats('Time needed to access digest (secs)', mean, stdev)

    # Left parent access

    access_left_parent_measurements = timeit.repeat(
        access_left_parent_func(node), repeat=ITERATIONS, number=ROUNDS)

    mean, stdev = _mean_value(access_left_parent_measurements,
                              precision=precision_1,
                              with_stdev=True)
    _show_stats('Time needed to access left parent (secs)', mean, stdev)

    # Right parent access

    access_right_parent_measurements = timeit.repeat(
        access_right_parent_func(node), repeat=ITERATIONS, number=ROUNDS)

    mean, stdev = _mean_value(access_right_parent_measurements,
                              precision=precision_1,
                              with_stdev=True)
    _show_stats('Time needed to access right parent (secs)', mean, stdev)

    # Child access

    access_child_measurements = timeit.repeat(access_child_func(right),
                                              repeat=ITERATIONS,
                                              number=ROUNDS)

    mean, stdev = _mean_value(access_child_measurements,
                              precision=precision_1,
                              with_stdev=True)
    _show_stats('Time needed to access child (secs)', mean, stdev)
Esempio n. 7
0
def test_leaf_construction_exception_with_neither_record_nor_digest():
    """Tests that the Leaf constructor raises `TypeError`
    if neither `record` nor `digest` is provided
    """
    with pytest.raises(LeafConstructionError):
        Leaf(hashfunc=HASH, encoding=ENCODING)
Esempio n. 8
0
# ----------------------------------- Setup -----------------------------------

MACHINE  = hash_machine()       # prepends security prefices by default
ENCODING = MACHINE.ENCODING     # utf-8
HASH     = MACHINE.hash         # SHA256


# A pair of childless leaves

_leaves = (

    # Leaf constructed by providing a record to digest

    Leaf(
        hashfunc=HASH,
        encoding=ENCODING,
        record=b'some record...'
    ),

    # Leaf constructed by providing the digest directly

    Leaf(
        hashfunc=HASH,
        encoding=ENCODING,
        digest='5f4e54b52702884b03c21efc76b7433607fa3b35343b9fd322521c9c1ed633b4'
    )
)

# A full binary structure (child-parent relations): 4 leaves, 7 nodes in total

leaf_1  = Leaf(