From e76392e330cdc9cdc9f3edc7f2bf66e072d46cfd Mon Sep 17 00:00:00 2001 From: Ricardo Domingos Date: Wed, 20 Dec 2017 23:21:30 +0100 Subject: [PATCH 1/3] types: Update String() test to assert Prevote type --- types/vote_test.go | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/types/vote_test.go b/types/vote_test.go index 032cc947..cb7f02e4 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -32,6 +32,29 @@ func exampleVote() *Vote { } } +func examplePrevote() *Vote { + var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z") + if err != nil { + panic(err) + } + + return &Vote{ + ValidatorAddress: []byte("addr"), + ValidatorIndex: 56789, + Height: 12345, + Round: 2, + Timestamp: stamp, + Type: byte(1), + BlockID: BlockID{ + Hash: []byte("hash"), + PartsHeader: PartSetHeader{ + Total: 1000000, + Hash: []byte("parts_hash"), + }, + }, + } +} + func TestVoteSignable(t *testing.T) { vote := exampleVote() signBytes := SignBytes("test_chain_id", vote) @@ -45,10 +68,22 @@ func TestVoteSignable(t *testing.T) { } 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) + tc := []struct { + name string + in string + out string + }{ + {"Precommit", exampleVote().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) + } + }) } } From 19eeef0aad53ded38993a9c5e9694453e0d03a2e Mon Sep 17 00:00:00 2001 From: Ricardo Domingos Date: Thu, 21 Dec 2017 17:25:27 +0100 Subject: [PATCH 2/3] types: Rename exampleVote to examplePrecommit on vote_test exampleVote doesn't express the type of the vote. --- types/vote_test.go | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/types/vote_test.go b/types/vote_test.go index cb7f02e4..d22b398e 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -9,30 +9,15 @@ import ( wire "github.com/tendermint/go-wire" ) -func exampleVote() *Vote { - var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z") - if err != nil { - panic(err) - } - - return &Vote{ - ValidatorAddress: []byte("addr"), - ValidatorIndex: 56789, - Height: 12345, - Round: 2, - Timestamp: stamp, - Type: byte(2), - BlockID: BlockID{ - Hash: []byte("hash"), - PartsHeader: PartSetHeader{ - Total: 1000000, - Hash: []byte("parts_hash"), - }, - }, - } +func examplePrevote() *Vote { + return exampleVote(VoteTypePrevote) } -func examplePrevote() *Vote { +func examplePrecommit() *Vote { + return exampleVote(VoteTypePrecommit) +} + +func exampleVote(t byte) *Vote { var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z") if err != nil { panic(err) @@ -44,7 +29,7 @@ func examplePrevote() *Vote { Height: 12345, Round: 2, Timestamp: stamp, - Type: byte(1), + Type: t, BlockID: BlockID{ Hash: []byte("hash"), PartsHeader: PartSetHeader{ @@ -56,7 +41,7 @@ func examplePrevote() *Vote { } func TestVoteSignable(t *testing.T) { - vote := exampleVote() + vote := examplePrecommit() signBytes := SignBytes("test_chain_id", vote) signStr := string(signBytes) @@ -73,7 +58,7 @@ func TestVoteString(t *testing.T) { in string out string }{ - {"Precommit", exampleVote().String(), `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {} @ 2017-12-25T03:00:01.234Z}`}, + {"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}`}, } @@ -91,7 +76,7 @@ func TestVoteVerifySignature(t *testing.T) { privVal := GenPrivValidatorFS("") pubKey := privVal.GetPubKey() - vote := exampleVote() + vote := examplePrecommit() signBytes := SignBytes("test_chain_id", vote) // sign it From d5baa6601cc19f5a5a48304e9f9c736fce07e3f2 Mon Sep 17 00:00:00 2001 From: Ricardo Domingos Date: Thu, 21 Dec 2017 17:33:47 +0100 Subject: [PATCH 3/3] types: Add test for IsVoteTypeValid --- types/vote_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/types/vote_test.go b/types/vote_test.go index d22b398e..51eca12d 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -99,3 +99,24 @@ func TestVoteVerifySignature(t *testing.T) { valid = pubKey.VerifyBytes(newSignBytes, signature) require.True(t, valid) } + +func TestIsVoteTypeValid(t *testing.T) { + tc := []struct { + name string + in byte + out bool + }{ + {"Prevote", VoteTypePrevote, true}, + {"Precommit", VoteTypePrecommit, true}, + {"InvalidType", byte(3), false}, + } + + for _, tt := range tc { + tt := tt + t.Run(tt.name, func(st *testing.T) { + if rs := IsVoteTypeValid(tt.in); rs != tt.out { + t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out) + } + }) + } +}