Merge pull request #996 from ricardohsd/types-add-tests-to-vote
Add more tests to types/vote.go
This commit is contained in:
commit
6c39c77fc5
|
@ -9,7 +9,15 @@ import (
|
||||||
wire "github.com/tendermint/go-wire"
|
wire "github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
func exampleVote() *Vote {
|
func examplePrevote() *Vote {
|
||||||
|
return exampleVote(VoteTypePrevote)
|
||||||
|
}
|
||||||
|
|
||||||
|
func examplePrecommit() *Vote {
|
||||||
|
return exampleVote(VoteTypePrecommit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func exampleVote(t byte) *Vote {
|
||||||
var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
|
var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -21,7 +29,7 @@ func exampleVote() *Vote {
|
||||||
Height: 12345,
|
Height: 12345,
|
||||||
Round: 2,
|
Round: 2,
|
||||||
Timestamp: stamp,
|
Timestamp: stamp,
|
||||||
Type: byte(2),
|
Type: t,
|
||||||
BlockID: BlockID{
|
BlockID: BlockID{
|
||||||
Hash: []byte("hash"),
|
Hash: []byte("hash"),
|
||||||
PartsHeader: PartSetHeader{
|
PartsHeader: PartSetHeader{
|
||||||
|
@ -33,7 +41,7 @@ func exampleVote() *Vote {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVoteSignable(t *testing.T) {
|
func TestVoteSignable(t *testing.T) {
|
||||||
vote := exampleVote()
|
vote := examplePrecommit()
|
||||||
signBytes := SignBytes("test_chain_id", vote)
|
signBytes := SignBytes("test_chain_id", vote)
|
||||||
signStr := string(signBytes)
|
signStr := string(signBytes)
|
||||||
|
|
||||||
|
@ -45,10 +53,22 @@ func TestVoteSignable(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVoteString(t *testing.T) {
|
func TestVoteString(t *testing.T) {
|
||||||
str := exampleVote().String()
|
tc := []struct {
|
||||||
expected := `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {<nil>} @ 2017-12-25T03:00:01.234Z}`
|
name string
|
||||||
if str != expected {
|
in string
|
||||||
t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
|
out string
|
||||||
|
}{
|
||||||
|
{"Precommit", examplePrecommit().String(), `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {<nil>} @ 2017-12-25T03:00:01.234Z}`},
|
||||||
|
{"Prevote", examplePrevote().String(), `Vote{56789:616464720000 12345/02/1(Prevote) 686173680000 {<nil>} @ 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +76,7 @@ func TestVoteVerifySignature(t *testing.T) {
|
||||||
privVal := GenPrivValidatorFS("")
|
privVal := GenPrivValidatorFS("")
|
||||||
pubKey := privVal.GetPubKey()
|
pubKey := privVal.GetPubKey()
|
||||||
|
|
||||||
vote := exampleVote()
|
vote := examplePrecommit()
|
||||||
signBytes := SignBytes("test_chain_id", vote)
|
signBytes := SignBytes("test_chain_id", vote)
|
||||||
|
|
||||||
// sign it
|
// sign it
|
||||||
|
@ -79,3 +99,24 @@ func TestVoteVerifySignature(t *testing.T) {
|
||||||
valid = pubKey.VerifyBytes(newSignBytes, signature)
|
valid = pubKey.VerifyBytes(newSignBytes, signature)
|
||||||
require.True(t, valid)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue