From d103aaf53fbe3b56677be6decb5f04918c30070f Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 12 Jul 2018 18:50:32 +0400 Subject: [PATCH] add test for Vote#Verify remove test for String (very brittle) --- types/vote_test.go | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/types/vote_test.go b/types/vote_test.go index 6364fa9d6..0b8c2a1e7 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -4,7 +4,9 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto" ) func examplePrevote() *Vote { @@ -50,29 +52,9 @@ func TestVoteSignable(t *testing.T) { } } -func TestVoteString(t *testing.T) { - tc := []struct { - name string - in string - out string - }{ - {"Precommit", examplePrecommit().String(), `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 @ 2017-12-25T03:00:01.234Z}`}, - {"Prevote", examplePrevote().String(), `Vote{56789:616464720000 12345/02/1(Prevote) 686173680000 @ 2017-12-25T03:00:01.234Z}`}, - } - - for _, tt := range tc { - tt := tt - t.Run(tt.name, func(st *testing.T) { - if tt.in != tt.out { - t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", tt.in, tt.out) - } - }) - } -} - func TestVoteVerifySignature(t *testing.T) { privVal := NewMockPV() - pubKey := privVal.PubKey() + pubkey := privVal.PubKey() vote := examplePrecommit() signBytes := vote.SignBytes("test_chain_id") @@ -82,7 +64,7 @@ func TestVoteVerifySignature(t *testing.T) { require.NoError(t, err) // verify the same vote - valid := pubKey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature) + valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature) require.True(t, valid) // serialize, deserialize and verify again.... @@ -95,7 +77,7 @@ func TestVoteVerifySignature(t *testing.T) { // verify the transmitted vote newSignBytes := precommit.SignBytes("test_chain_id") require.Equal(t, string(signBytes), string(newSignBytes)) - valid = pubKey.VerifyBytes(newSignBytes, precommit.Signature) + valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature) require.True(t, valid) } @@ -119,3 +101,21 @@ func TestIsVoteTypeValid(t *testing.T) { }) } } + +func TestVoteVerify(t *testing.T) { + privVal := NewMockPV() + pubkey := privVal.PubKey() + + vote := examplePrevote() + vote.ValidatorAddress = pubkey.Address() + + err := vote.Verify("test_chain_id", crypto.GenPrivKeyEd25519().PubKey()) + if assert.Error(t, err) { + assert.Equal(t, ErrVoteInvalidValidatorAddress, err) + } + + err = vote.Verify("test_chain_id", pubkey) + if assert.Error(t, err) { + assert.Equal(t, ErrVoteInvalidSignature, err) + } +}