tests for vote schema and signature validation

This commit is contained in:
Scott Sadler 2017-02-23 21:52:59 +01:00
parent e88c98a695
commit c44c9d0282
2 changed files with 21 additions and 43 deletions

View File

@ -170,5 +170,5 @@ class Voting:
try: try:
validate_vote_schema(vote) validate_vote_schema(vote)
return True return True
except SchemaValidationError: except SchemaValidationError as e:
return False return False

View File

@ -106,52 +106,30 @@ def test_decide_votes_checks_arguments():
n_agree_prev_block=2) 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)
################################################################################ ################################################################################
# Tests for vote schema
# DEBT
def test_verify_vote_schema(b):
def _test_verify_vote_passes(b, structurally_valid_vote): vote = b.vote('b' * 64, 'a' * 64, True)
from bigchaindb.consensus import BaseConsensusRules assert Voting.verify_vote_schema(vote)
from bigchaindb.common import crypto vote = b.vote('b', 'a', True)
from bigchaindb.common.utils import serialize assert not Voting.verify_vote_schema(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):
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)
""" """