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.

121 lines
3.0 KiB

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