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.

50 lines
1.6 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/tendermint/go-crypto"
  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, hb.String(), "Heartbeat{1:000000000000 11/02 (0) {<nil>}}")
  22. var key crypto.PrivKeyEd25519
  23. hb.Signature = key.Sign([]byte("Tendermint"))
  24. require.Equal(t, hb.String(), "Heartbeat{1:000000000000 11/02 (0) {/FF41E371B9BF.../}}")
  25. }
  26. func TestHeartbeatWriteSignBytes(t *testing.T) {
  27. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  28. bz := hb.SignBytes("0xdeadbeef")
  29. require.Equal(t, string(bz), `{"chain_id":"0xdeadbeef","heartbeat":{"height":10,"round":1,"sequence":0,"validator_address":"","validator_index":1}}`)
  30. plainHb := &Heartbeat{}
  31. bz = plainHb.SignBytes("0xdeadbeef")
  32. require.Equal(t, string(bz), `{"chain_id":"0xdeadbeef","heartbeat":{"height":0,"round":0,"sequence":0,"validator_address":"","validator_index":0}}`)
  33. require.Panics(t, func() {
  34. var nilHb *Heartbeat
  35. bz := nilHb.SignBytes("0xdeadbeef")
  36. require.Equal(t, string(bz), "null")
  37. })
  38. }