From c44c9d0282ae7c85b67f1dfb5517085e9e28593c Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Thu, 23 Feb 2017 21:52:59 +0100 Subject: [PATCH] tests for vote schema and signature validation --- bigchaindb/voting.py | 2 +- tests/test_voting.py | 62 ++++++++++++++------------------------------ 2 files changed, 21 insertions(+), 43 deletions(-) diff --git a/bigchaindb/voting.py b/bigchaindb/voting.py index c5482e92..21ce8195 100644 --- a/bigchaindb/voting.py +++ b/bigchaindb/voting.py @@ -170,5 +170,5 @@ class Voting: try: validate_vote_schema(vote) return True - except SchemaValidationError: + except SchemaValidationError as e: return False diff --git a/tests/test_voting.py b/tests/test_voting.py index e70addf1..cea34de0 100644 --- a/tests/test_voting.py +++ b/tests/test_voting.py @@ -106,52 +106,30 @@ def test_decide_votes_checks_arguments(): n_agree_prev_block=2) +################################################################################ +# Tests for vote signature + + +def test_verify_vote_signature_passes(b): + vote = b.vote('block', 'a', True) + assert Voting.verify_vote_signature(vote) + + +def test_verify_vote_signature_fails(b): + vote = b.vote('block', 'a', True) + vote['signature'] = '' + assert not Voting.verify_vote_signature(vote) + ################################################################################ - -# DEBT +# Tests for vote schema - -def _test_verify_vote_passes(b, structurally_valid_vote): - from bigchaindb.consensus import BaseConsensusRules - from bigchaindb.common import crypto - from bigchaindb.common.utils import serialize - 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): - from bigchaindb.consensus import BaseConsensusRules - 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): - from bigchaindb.consensus import BaseConsensusRules - from bigchaindb.common import crypto - from bigchaindb.common.utils import serialize - 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) +def test_verify_vote_schema(b): + vote = b.vote('b' * 64, 'a' * 64, True) + assert Voting.verify_vote_schema(vote) + vote = b.vote('b', 'a', True) + assert not Voting.verify_vote_schema(vote) """