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.

135 lines
3.4 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. "github.com/tendermint/tendermint/crypto/tmhash"
  9. )
  10. func examplePrevote() *Vote {
  11. return exampleVote(VoteTypePrevote)
  12. }
  13. func examplePrecommit() *Vote {
  14. return exampleVote(VoteTypePrecommit)
  15. }
  16. func exampleVote(t byte) *Vote {
  17. var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
  18. if err != nil {
  19. panic(err)
  20. }
  21. return &Vote{
  22. ValidatorAddress: tmhash.Sum([]byte("validator_address")),
  23. ValidatorIndex: 56789,
  24. Height: 12345,
  25. Round: 2,
  26. Timestamp: stamp,
  27. Type: t,
  28. BlockID: BlockID{
  29. Hash: tmhash.Sum([]byte("blockID_hash")),
  30. PartsHeader: PartSetHeader{
  31. Total: 1000000,
  32. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  33. },
  34. },
  35. }
  36. }
  37. func TestVoteSignable(t *testing.T) {
  38. vote := examplePrecommit()
  39. signBytes := vote.SignBytes("test_chain_id")
  40. signStr := string(signBytes)
  41. expected := `{"@chain_id":"test_chain_id","@type":"vote","block_id":{"hash":"8B01023386C371778ECB6368573E539AFC3CC860","parts":{"hash":"72DB3D959635DFF1BB567BEDAA70573392C51596","total":"1000000"}},"height":"12345","round":"2","timestamp":"2017-12-25T03:00:01.234Z","type":2}`
  42. if signStr != expected {
  43. // NOTE: when this fails, you probably want to fix up consensus/replay_test too
  44. t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr)
  45. }
  46. }
  47. func TestVoteVerifySignature(t *testing.T) {
  48. privVal := NewMockPV()
  49. pubkey := privVal.GetPubKey()
  50. vote := examplePrecommit()
  51. signBytes := vote.SignBytes("test_chain_id")
  52. // sign it
  53. err := privVal.SignVote("test_chain_id", vote)
  54. require.NoError(t, err)
  55. // verify the same vote
  56. valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
  57. require.True(t, valid)
  58. // serialize, deserialize and verify again....
  59. precommit := new(Vote)
  60. bs, err := cdc.MarshalBinary(vote)
  61. require.NoError(t, err)
  62. err = cdc.UnmarshalBinary(bs, &precommit)
  63. require.NoError(t, err)
  64. // verify the transmitted vote
  65. newSignBytes := precommit.SignBytes("test_chain_id")
  66. require.Equal(t, string(signBytes), string(newSignBytes))
  67. valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
  68. require.True(t, valid)
  69. }
  70. func TestIsVoteTypeValid(t *testing.T) {
  71. tc := []struct {
  72. name string
  73. in byte
  74. out bool
  75. }{
  76. {"Prevote", VoteTypePrevote, true},
  77. {"Precommit", VoteTypePrecommit, true},
  78. {"InvalidType", byte(3), false},
  79. }
  80. for _, tt := range tc {
  81. tt := tt
  82. t.Run(tt.name, func(st *testing.T) {
  83. if rs := IsVoteTypeValid(tt.in); rs != tt.out {
  84. t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
  85. }
  86. })
  87. }
  88. }
  89. func TestVoteVerify(t *testing.T) {
  90. privVal := NewMockPV()
  91. pubkey := privVal.GetPubKey()
  92. vote := examplePrevote()
  93. vote.ValidatorAddress = pubkey.Address()
  94. err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
  95. if assert.Error(t, err) {
  96. assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
  97. }
  98. err = vote.Verify("test_chain_id", pubkey)
  99. if assert.Error(t, err) {
  100. assert.Equal(t, ErrVoteInvalidSignature, err)
  101. }
  102. }
  103. func TestMaxVoteBytes(t *testing.T) {
  104. vote := examplePrevote()
  105. privVal := NewMockPV()
  106. err := privVal.SignVote("test_chain_id", vote)
  107. require.NoError(t, err)
  108. bz, err := cdc.MarshalBinary(vote)
  109. require.NoError(t, err)
  110. assert.Equal(t, MaxVoteBytes, len(bz))
  111. }