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.

52 lines
1.7 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. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  30. bz := hb.SignBytes("0xdeadbeef")
  31. // XXX HMMMMMMM
  32. require.Equal(t, string(bz), `{"@chain_id":"0xdeadbeef","@type":"heartbeat","height":"10","round":"1","sequence":"0","validator_address":"","validator_index":"1"}`)
  33. plainHb := &Heartbeat{}
  34. bz = plainHb.SignBytes("0xdeadbeef")
  35. require.Equal(t, string(bz), `{"@chain_id":"0xdeadbeef","@type":"heartbeat","height":"0","round":"0","sequence":"0","validator_address":"","validator_index":"0"}`)
  36. require.Panics(t, func() {
  37. var nilHb *Heartbeat
  38. bz := nilHb.SignBytes("0xdeadbeef")
  39. require.Equal(t, string(bz), "null")
  40. })
  41. }