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.

104 lines
3.1 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. "github.com/tendermint/tendermint/crypto/secp256k1"
  8. )
  9. func TestHeartbeatCopy(t *testing.T) {
  10. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  11. hbCopy := hb.Copy()
  12. require.Equal(t, hbCopy, hb, "heartbeat copy should be the same")
  13. hbCopy.Round = hb.Round + 10
  14. require.NotEqual(t, hbCopy, hb, "heartbeat copy mutation should not change original")
  15. var nilHb *Heartbeat
  16. nilHbCopy := nilHb.Copy()
  17. require.Nil(t, nilHbCopy, "copy of nil should also return nil")
  18. }
  19. func TestHeartbeatString(t *testing.T) {
  20. var nilHb *Heartbeat
  21. require.Contains(t, nilHb.String(), "nil", "expecting a string and no panic")
  22. hb := &Heartbeat{ValidatorIndex: 1, Height: 11, Round: 2}
  23. require.Equal(t, "Heartbeat{1:000000000000 11/02 (0) /000000000000.../}", hb.String())
  24. var key ed25519.PrivKeyEd25519
  25. sig, err := key.Sign([]byte("Tendermint"))
  26. require.NoError(t, err)
  27. hb.Signature = sig
  28. require.Equal(t, "Heartbeat{1:000000000000 11/02 (0) /FF41E371B9BF.../}", hb.String())
  29. }
  30. func TestHeartbeatWriteSignBytes(t *testing.T) {
  31. chainID := "test_chain_id"
  32. {
  33. testHeartbeat := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  34. signBytes := testHeartbeat.SignBytes(chainID)
  35. expected, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeHeartbeat(chainID, testHeartbeat))
  36. require.NoError(t, err)
  37. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Heartbeat")
  38. }
  39. {
  40. testHeartbeat := &Heartbeat{}
  41. signBytes := testHeartbeat.SignBytes(chainID)
  42. expected, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeHeartbeat(chainID, testHeartbeat))
  43. require.NoError(t, err)
  44. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Heartbeat")
  45. }
  46. require.Panics(t, func() {
  47. var nilHb *Heartbeat
  48. signBytes := nilHb.SignBytes(chainID)
  49. require.Equal(t, string(signBytes), "null")
  50. })
  51. }
  52. func TestHeartbeatValidateBasic(t *testing.T) {
  53. testCases := []struct {
  54. testName string
  55. malleateHeartBeat func(*Heartbeat)
  56. expectErr bool
  57. }{
  58. {"Good HeartBeat", func(hb *Heartbeat) {}, false},
  59. {"Invalid address size", func(hb *Heartbeat) {
  60. hb.ValidatorAddress = nil
  61. }, true},
  62. {"Negative validator index", func(hb *Heartbeat) {
  63. hb.ValidatorIndex = -1
  64. }, true},
  65. {"Negative height", func(hb *Heartbeat) {
  66. hb.Height = -1
  67. }, true},
  68. {"Negative round", func(hb *Heartbeat) {
  69. hb.Round = -1
  70. }, true},
  71. {"Negative sequence", func(hb *Heartbeat) {
  72. hb.Sequence = -1
  73. }, true},
  74. {"Missing signature", func(hb *Heartbeat) {
  75. hb.Signature = nil
  76. }, true},
  77. {"Signature too big", func(hb *Heartbeat) {
  78. hb.Signature = make([]byte, MaxSignatureSize+1)
  79. }, true},
  80. }
  81. for _, tc := range testCases {
  82. t.Run(tc.testName, func(t *testing.T) {
  83. hb := &Heartbeat{
  84. ValidatorAddress: secp256k1.GenPrivKey().PubKey().Address(),
  85. Signature: make([]byte, 4),
  86. ValidatorIndex: 1, Height: 10, Round: 1}
  87. tc.malleateHeartBeat(hb)
  88. assert.Equal(t, tc.expectErr, hb.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  89. })
  90. }
  91. }