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.

60 lines
1.8 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/tendermint/tendermint/crypto/ed25519"
  6. )
  7. func TestHeartbeatCopy(t *testing.T) {
  8. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  9. hbCopy := hb.Copy()
  10. require.Equal(t, hbCopy, hb, "heartbeat copy should be the same")
  11. hbCopy.Round = hb.Round + 10
  12. require.NotEqual(t, hbCopy, hb, "heartbeat copy mutation should not change original")
  13. var nilHb *Heartbeat
  14. nilHbCopy := nilHb.Copy()
  15. require.Nil(t, nilHbCopy, "copy of nil should also return nil")
  16. }
  17. func TestHeartbeatString(t *testing.T) {
  18. var nilHb *Heartbeat
  19. require.Contains(t, nilHb.String(), "nil", "expecting a string and no panic")
  20. hb := &Heartbeat{ValidatorIndex: 1, Height: 11, Round: 2}
  21. require.Equal(t, "Heartbeat{1:000000000000 11/02 (0) /000000000000.../}", hb.String())
  22. var key ed25519.PrivKeyEd25519
  23. sig, err := key.Sign([]byte("Tendermint"))
  24. require.NoError(t, err)
  25. hb.Signature = sig
  26. require.Equal(t, "Heartbeat{1:000000000000 11/02 (0) /FF41E371B9BF.../}", hb.String())
  27. }
  28. func TestHeartbeatWriteSignBytes(t *testing.T) {
  29. chainID := "test_chain_id"
  30. {
  31. testHeartbeat := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  32. signBytes := testHeartbeat.SignBytes(chainID)
  33. expected, err := cdc.MarshalBinary(CanonicalizeHeartbeat(chainID, testHeartbeat))
  34. require.NoError(t, err)
  35. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Heartbeat")
  36. }
  37. {
  38. testHeartbeat := &Heartbeat{}
  39. signBytes := testHeartbeat.SignBytes(chainID)
  40. expected, err := cdc.MarshalBinary(CanonicalizeHeartbeat(chainID, testHeartbeat))
  41. require.NoError(t, err)
  42. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Heartbeat")
  43. }
  44. require.Panics(t, func() {
  45. var nilHb *Heartbeat
  46. signBytes := nilHb.SignBytes(chainID)
  47. require.Equal(t, string(signBytes), "null")
  48. })
  49. }