diff --git a/types/proposal_test.go b/types/proposal_test.go index a45ae873c..0d2af71e2 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -3,6 +3,10 @@ package types import ( "testing" "time" + + "github.com/stretchr/testify/require" + + wire "github.com/tendermint/go-wire" ) var testProposal *Proposal @@ -31,6 +35,42 @@ func TestProposalSignable(t *testing.T) { } } +func TestProposalString(t *testing.T) { + str := testProposal.String() + expected := `Proposal{12345/23456 111:626C6F636B70 (-1,:0:000000000000) {} @ 2018-02-11T07:09:22.765Z}` + if str != expected { + t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str) + } +} + +func TestProposalVerifySignature(t *testing.T) { + privVal := GenPrivValidatorFS("") + pubKey := privVal.GetPubKey() + + prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{}) + signBytes := SignBytes("test_chain_id", prop) + + // sign it + signature, err := privVal.Signer.Sign(signBytes) + require.NoError(t, err) + + // verify the same proposal + valid := pubKey.VerifyBytes(SignBytes("test_chain_id", prop), signature) + require.True(t, valid) + + // serialize, deserialize and verify again.... + newProp := new(Proposal) + bs := wire.BinaryBytes(prop) + err = wire.ReadBinaryBytes(bs, &newProp) + require.NoError(t, err) + + // verify the transmitted proposal + newSignBytes := SignBytes("test_chain_id", newProp) + require.Equal(t, string(signBytes), string(newSignBytes)) + valid = pubKey.VerifyBytes(newSignBytes, signature) + require.True(t, valid) +} + func BenchmarkProposalWriteSignBytes(b *testing.B) { for i := 0; i < b.N; i++ { SignBytes("test_chain_id", testProposal) diff --git a/types/vote_test.go b/types/vote_test.go index bd8eda347..032cc9472 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/require" + wire "github.com/tendermint/go-wire" ) @@ -18,7 +19,7 @@ func exampleVote() *Vote { ValidatorAddress: []byte("addr"), ValidatorIndex: 56789, Height: 12345, - Round: 23456, + Round: 2, Timestamp: stamp, Type: byte(2), BlockID: BlockID{ @@ -36,13 +37,21 @@ func TestVoteSignable(t *testing.T) { signBytes := SignBytes("test_chain_id", vote) signStr := string(signBytes) - expected := `{"chain_id":"test_chain_id","vote":{"block_id":{"hash":"68617368","parts":{"hash":"70617274735F68617368","total":1000000}},"height":12345,"round":23456,"timestamp":"2017-12-25T03:00:01.234Z","type":2}}` + expected := `{"chain_id":"test_chain_id","vote":{"block_id":{"hash":"68617368","parts":{"hash":"70617274735F68617368","total":1000000}},"height":12345,"round":2,"timestamp":"2017-12-25T03:00:01.234Z","type":2}}` if signStr != expected { // NOTE: when this fails, you probably want to fix up consensus/replay_test too t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr) } } +func TestVoteString(t *testing.T) { + str := exampleVote().String() + expected := `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {} @ 2017-12-25T03:00:01.234Z}` + if str != expected { + t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str) + } +} + func TestVoteVerifySignature(t *testing.T) { privVal := GenPrivValidatorFS("") pubKey := privVal.GetPubKey()