You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. wire "github.com/tendermint/go-wire"
  7. )
  8. func examplePrevote() *Vote {
  9. return exampleVote(VoteTypePrevote)
  10. }
  11. func examplePrecommit() *Vote {
  12. return exampleVote(VoteTypePrecommit)
  13. }
  14. func exampleVote(t byte) *Vote {
  15. var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
  16. if err != nil {
  17. panic(err)
  18. }
  19. return &Vote{
  20. ValidatorAddress: []byte("addr"),
  21. ValidatorIndex: 56789,
  22. Height: 12345,
  23. Round: 2,
  24. Timestamp: stamp,
  25. Type: t,
  26. BlockID: BlockID{
  27. Hash: []byte("hash"),
  28. PartsHeader: PartSetHeader{
  29. Total: 1000000,
  30. Hash: []byte("parts_hash"),
  31. },
  32. },
  33. }
  34. }
  35. func TestVoteSignable(t *testing.T) {
  36. vote := examplePrecommit()
  37. signBytes := SignBytes("test_chain_id", vote)
  38. signStr := string(signBytes)
  39. 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}}`
  40. if signStr != expected {
  41. // NOTE: when this fails, you probably want to fix up consensus/replay_test too
  42. t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr)
  43. }
  44. }
  45. func TestVoteString(t *testing.T) {
  46. tc := []struct {
  47. name string
  48. in string
  49. out string
  50. }{
  51. {"Precommit", examplePrecommit().String(), `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {<nil>} @ 2017-12-25T03:00:01.234Z}`},
  52. {"Prevote", examplePrevote().String(), `Vote{56789:616464720000 12345/02/1(Prevote) 686173680000 {<nil>} @ 2017-12-25T03:00:01.234Z}`},
  53. }
  54. for _, tt := range tc {
  55. tt := tt
  56. t.Run(tt.name, func(st *testing.T) {
  57. if tt.in != tt.out {
  58. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", tt.in, tt.out)
  59. }
  60. })
  61. }
  62. }
  63. func TestVoteVerifySignature(t *testing.T) {
  64. privVal := GenPrivValidatorFS("")
  65. pubKey := privVal.GetPubKey()
  66. vote := examplePrecommit()
  67. signBytes := SignBytes("test_chain_id", vote)
  68. // sign it
  69. signature, err := privVal.Signer.Sign(signBytes)
  70. require.NoError(t, err)
  71. // verify the same vote
  72. valid := pubKey.VerifyBytes(SignBytes("test_chain_id", vote), signature)
  73. require.True(t, valid)
  74. // serialize, deserialize and verify again....
  75. precommit := new(Vote)
  76. bs := wire.BinaryBytes(vote)
  77. err = wire.ReadBinaryBytes(bs, &precommit)
  78. require.NoError(t, err)
  79. // verify the transmitted vote
  80. newSignBytes := SignBytes("test_chain_id", precommit)
  81. require.Equal(t, string(signBytes), string(newSignBytes))
  82. valid = pubKey.VerifyBytes(newSignBytes, signature)
  83. require.True(t, valid)
  84. }
  85. func TestIsVoteTypeValid(t *testing.T) {
  86. tc := []struct {
  87. name string
  88. in byte
  89. out bool
  90. }{
  91. {"Prevote", VoteTypePrevote, true},
  92. {"Precommit", VoteTypePrecommit, true},
  93. {"InvalidType", byte(3), false},
  94. }
  95. for _, tt := range tc {
  96. tt := tt
  97. t.Run(tt.name, func(st *testing.T) {
  98. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  99. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  100. }
  101. })
  102. }
  103. }