From f2e6e4d13d30b0cefd0b5ea7c09996a6d8261bf6 Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Fri, 25 Nov 2016 14:10:37 +0100 Subject: [PATCH] add tests to get coverage of consensus.py --- bigchaindb/common/schema/__init__.py | 4 --- bigchaindb/consensus.py | 5 ++-- tests/common/schema/test_vote_schema.py | 14 ++------- tests/conftest.py | 18 +++++++++++- tests/test_consensus.py | 39 +++++++++++++++++++++++-- 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/bigchaindb/common/schema/__init__.py b/bigchaindb/common/schema/__init__.py index 89a6a820..9f233dc9 100644 --- a/bigchaindb/common/schema/__init__.py +++ b/bigchaindb/common/schema/__init__.py @@ -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) diff --git a/bigchaindb/consensus.py b/bigchaindb/consensus.py index ae10b822..a3e57863 100644 --- a/bigchaindb/consensus.py +++ b/bigchaindb/consensus.py @@ -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 diff --git a/tests/common/schema/test_vote_schema.py b/tests/common/schema/test_vote_schema.py index a6340c5e..a9de9492 100644 --- a/tests/common/schema/test_vote_schema.py +++ b/tests/common/schema/test_vote_schema.py @@ -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(): diff --git a/tests/conftest.py b/tests/conftest.py index 957a5698..66305b6a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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' + } + } diff --git a/tests/test_consensus.py b/tests/test_consensus.py index ef988726..43c4ef3e 100644 --- a/tests/test_consensus.py +++ b/tests/test_consensus.py @@ -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)