add tests to get coverage of consensus.py

This commit is contained in:
Scott Sadler 2016-11-25 14:10:37 +01:00
parent f38028d727
commit f2e6e4d13d
5 changed files with 59 additions and 21 deletions

View File

@ -35,8 +35,4 @@ VOTE_SCHEMA_PATH, VOTE_SCHEMA = _load_schema('vote')
def validate_vote_schema(vote):
""" Validate a vote dict """
# A vote does not have an ID, but the database may add one.
if 'id' in vote:
vote = dict(vote)
del vote['id']
_validate_schema(VOTE_SCHEMA, vote)

View File

@ -38,6 +38,7 @@ class BaseConsensusRules():
return True
except SchemaValidationError as exc:
logger.warning(exc)
logger.warning("Vote failed signature verification: "
"%s with voters: %s", signed_vote, voters)
else:
logger.warning("Vote failed signature verification: "
"%s with voters: %s", signed_vote, voters)
return False

View File

@ -4,18 +4,8 @@ from bigchaindb.common.exceptions import SchemaValidationError
from bigchaindb.common.schema import validate_vote_schema
def test_validate_vote():
validate_vote_schema({
'node_pubkey': 'c' * 44,
'signature': 'd' * 86,
'vote': {
'voting_for_block': 'a' * 64,
'previous_block': 'b' * 64,
'is_block_valid': False,
'invalid_reason': None,
'timestamp': '1111111111'
}
})
def test_validate_vote(structurally_valid_vote):
validate_vote_schema(structurally_valid_vote)
def test_validate_vote_fails():

View File

@ -38,7 +38,8 @@ def ignore_local_config_file(monkeypatch):
def mock_file_config(filename=None):
raise FileNotFoundError()
monkeypatch.setattr('bigchaindb.config_utils.file_config', mock_file_config)
monkeypatch.setattr('bigchaindb.config_utils.file_config',
mock_file_config)
@pytest.fixture(scope='function', autouse=True)
@ -86,3 +87,18 @@ def signed_transfer_tx(signed_create_tx, user_pk, user_sk):
inputs = signed_create_tx.to_inputs()
tx = Transaction.transfer(inputs, [([user_pk], 1)], signed_create_tx.asset)
return tx.sign([user_sk])
@pytest.fixture
def structurally_valid_vote():
return {
'node_pubkey': 'c' * 44,
'signature': 'd' * 86,
'vote': {
'voting_for_block': 'a' * 64,
'previous_block': 'b' * 64,
'is_block_valid': False,
'invalid_reason': None,
'timestamp': '1111111111'
}
}

View File

@ -1,2 +1,37 @@
class TestBaseConsensusRules(object):
pass
from bigchaindb.consensus import BaseConsensusRules
from bigchaindb.common import crypto
from bigchaindb.common.util import serialize
def test_verify_vote_passes(b, structurally_valid_vote):
vote_body = structurally_valid_vote['vote']
vote_data = serialize(vote_body)
signature = crypto.PrivateKey(b.me_private).sign(vote_data.encode())
vote_signed = {
'node_pubkey': b.me,
'signature': signature.decode(),
'vote': vote_body
}
assert BaseConsensusRules.verify_vote([b.me], vote_signed)
def test_verify_vote_fails_signature(b, structurally_valid_vote):
vote_body = structurally_valid_vote['vote']
vote_signed = {
'node_pubkey': b.me,
'signature': 'a' * 86,
'vote': vote_body
}
assert not BaseConsensusRules.verify_vote([b.me], vote_signed)
def test_verify_vote_fails_schema(b):
vote_body = {}
vote_data = serialize(vote_body)
signature = crypto.PrivateKey(b.me_private).sign(vote_data.encode())
vote_signed = {
'node_pubkey': b.me,
'signature': signature.decode(),
'vote': vote_body
}
assert not BaseConsensusRules.verify_vote([b.me], vote_signed)