Esempio n. 1
0
def test_block_hash_value(block_builder):
    """Check that after commiting the chain head is hash of the block."""
    chain = block_builder.chain
    assert chain.head is None

    block = block_builder.commit()
    block_hash = chain.object_store.hash_object(encode(block))
    assert chain.head == block_hash
Esempio n. 2
0
def test_chain_get_block_by_hash_fails_if_hash_wrong(chain_and_hashes):
    """Check that exception is raised if the hash is incorrect."""
    chain, hashes = chain_and_hashes
    chain._cache.clear()
    target_hash = hashes[0]

    substitute_block = ChainBlock(payload='Hacked!')
    chain.object_store._backend[target_hash] = encode(substitute_block)

    with pytest.raises(IntegrityValidationError):
        chain._get_block_by_hash(target_hash)
Esempio n. 3
0
def test_chain_proof_verif_malformed(chain_and_hashes):
    """Check proof of inclusion fails when it's malformed."""
    chain, hashes = chain_and_hashes
    if chain.head_block.index == 0:
        return

    store = chain.object_store.__class__()
    result, proof = chain.get_block_by_index(0, return_proof=True)
    proof[0].fingers = None
    bad_head = store.hash_object(encode(proof[0]))

    with pytest.warns(UserWarning, match='Exception occured'):
        assert not verify_chain_inclusion_proof(store, bad_head, result, proof)
Esempio n. 4
0
def test_custom_defaults():
    def mock_encoder(obj):
        return b'encoded!'

    def mock_decoder(obj):
        return b'decoded!'

    mock_params = EncodingParams()
    mock_params.encoder = mock_encoder
    mock_params.decoder = mock_decoder

    with mock_params.as_default():
        obj = b'dummy'
        assert encode(obj) == b'encoded!'
        assert decode(obj) == b'decoded!'
Esempio n. 5
0
def test_chain_inclusion_proof(object_store):
    """Check returned inclusion proof."""
    chain1 = Chain(object_store)
    for i in range(10):
        block_builder = BlockBuilder(chain1)
        block_builder.payload = 'Block %i' % i
        block_builder.commit()

    result, proof = chain1.get_block_by_index(2, return_proof=True)
    proof_store = chain1.object_store.__class__()

    for block in proof:
        serialized_block = encode(block)
        proof_store.add(serialized_block)

    chain2 = Chain(proof_store, head=chain1.head)
    assert chain2.get_block_by_index(2).payload == 'Block 2'
Esempio n. 6
0
def test_builder_commit_blocks(block_builder, chain_size):
    """Commit couple of blocks and check that the chain head moves."""
    expected_head = None
    for i in range(chain_size):
        assert block_builder.chain.head == expected_head

        block_builder.payload = 'Block {}'.format(i)
        expected_fingers = block_builder.fingers
        expected_index = block_builder.index
        block = block_builder.commit()

        # Committed block fields are as set by the builder.
        assert block.payload == 'Block {}'.format(i)
        assert block.index == expected_index
        assert block.fingers == expected_fingers

        # After committing, attribues of the block builder change.
        assert block_builder.payload is None
        assert block_builder.index == block.index + 1
        assert block_builder.fingers != block.fingers

        expected_head = block_builder.chain.object_store.hash_object(
            encode(block))
Esempio n. 7
0
def test_chain_iterator(chain_and_hashes):
    """Check the iterator."""
    chain, hashes = chain_and_hashes
    for block, expected_block_hash in zip(chain, reversed(hashes)):
        block_hash = chain.object_store.hash_object(encode(block))
        assert block_hash == expected_block_hash
Esempio n. 8
0
def test_chain_get_block_by_hash_fails_if_not_block(chain_and_hashes):
    """Check that exception is raised if the hash is incorrect."""
    chain, hashes = chain_and_hashes
    extra_obj_hash = chain.object_store.add(encode(b'extra'))
    with pytest.raises(ValueError):
        chain._get_block_by_hash(extra_obj_hash)
Esempio n. 9
0
def test_tree_get_node_by_hash_fails_if_not_node(populated_tree):
    """Check that exception is raised if the object is not a node."""
    extra_obj_hash = populated_tree.object_store.add(encode(b'extra'))
    with pytest.raises(TypeError):
        populated_tree._get_node_by_hash(extra_obj_hash)
Esempio n. 10
0
def test_msgpack_serialization(obj):
    """Check serialization correctness."""
    a = obj
    serialized = encode(a)
    b = decode(serialized)
    assert a == b