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