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.

56 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/go-crypto"
  7. )
  8. func TestHeartbeatCopy(t *testing.T) {
  9. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  10. hbCopy := hb.Copy()
  11. require.Equal(t, hbCopy, hb, "heartbeat copy should be the same")
  12. hbCopy.Round = hb.Round + 10
  13. require.NotEqual(t, hbCopy, hb, "heartbeat copy mutation should not change original")
  14. var nilHb *Heartbeat
  15. nilHbCopy := nilHb.Copy()
  16. require.Nil(t, nilHbCopy, "copy of nil should also return nil")
  17. }
  18. func TestHeartbeatString(t *testing.T) {
  19. var nilHb *Heartbeat
  20. require.Contains(t, nilHb.String(), "nil", "expecting a string and no panic")
  21. hb := &Heartbeat{ValidatorIndex: 1, Height: 11, Round: 2}
  22. require.Equal(t, hb.String(), "Heartbeat{1:000000000000 11/02 (0) {<nil>}}")
  23. var key crypto.PrivKeyEd25519
  24. hb.Signature = key.Sign([]byte("Tendermint"))
  25. require.Equal(t, hb.String(), "Heartbeat{1:000000000000 11/02 (0) {/FF41E371B9BF.../}}")
  26. }
  27. func TestHeartbeatWriteSignBytes(t *testing.T) {
  28. var n int
  29. var err error
  30. buf := new(bytes.Buffer)
  31. hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1}
  32. hb.WriteSignBytes("0xdeadbeef", buf, &n, &err)
  33. require.Equal(t, buf.String(), `{"chain_id":"0xdeadbeef","heartbeat":{"height":10,"round":1,"sequence":0,"validator_address":"","validator_index":1}}`)
  34. buf.Reset()
  35. plainHb := &Heartbeat{}
  36. plainHb.WriteSignBytes("0xdeadbeef", buf, &n, &err)
  37. require.Equal(t, buf.String(), `{"chain_id":"0xdeadbeef","heartbeat":{"height":0,"round":0,"sequence":0,"validator_address":"","validator_index":0}}`)
  38. require.Panics(t, func() {
  39. buf.Reset()
  40. var nilHb *Heartbeat
  41. nilHb.WriteSignBytes("0xdeadbeef", buf, &n, &err)
  42. require.Equal(t, buf.String(), "null")
  43. })
  44. }