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.

147 lines
3.2 KiB

  1. package privval
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/libs/log"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func TestIPCPVVote(t *testing.T) {
  14. var (
  15. chainID = cmn.RandStr(12)
  16. sc, rs = testSetupIPCSocketPair(t, chainID, types.NewMockPV())
  17. ts = time.Now()
  18. vType = types.PrecommitType
  19. want = &types.Vote{Timestamp: ts, Type: vType}
  20. have = &types.Vote{Timestamp: ts, Type: vType}
  21. )
  22. defer sc.Stop()
  23. defer rs.Stop()
  24. require.NoError(t, rs.privVal.SignVote(chainID, want))
  25. require.NoError(t, sc.SignVote(chainID, have))
  26. assert.Equal(t, want.Signature, have.Signature)
  27. }
  28. func TestIPCPVVoteResetDeadline(t *testing.T) {
  29. var (
  30. chainID = cmn.RandStr(12)
  31. sc, rs = testSetupIPCSocketPair(t, chainID, types.NewMockPV())
  32. ts = time.Now()
  33. vType = types.PrecommitType
  34. want = &types.Vote{Timestamp: ts, Type: vType}
  35. have = &types.Vote{Timestamp: ts, Type: vType}
  36. )
  37. defer sc.Stop()
  38. defer rs.Stop()
  39. time.Sleep(3 * time.Millisecond)
  40. require.NoError(t, rs.privVal.SignVote(chainID, want))
  41. require.NoError(t, sc.SignVote(chainID, have))
  42. assert.Equal(t, want.Signature, have.Signature)
  43. // This would exceed the deadline if it was not extended by the previous message
  44. time.Sleep(3 * time.Millisecond)
  45. require.NoError(t, rs.privVal.SignVote(chainID, want))
  46. require.NoError(t, sc.SignVote(chainID, have))
  47. assert.Equal(t, want.Signature, have.Signature)
  48. }
  49. func TestIPCPVVoteKeepalive(t *testing.T) {
  50. var (
  51. chainID = cmn.RandStr(12)
  52. sc, rs = testSetupIPCSocketPair(t, chainID, types.NewMockPV())
  53. ts = time.Now()
  54. vType = types.PrecommitType
  55. want = &types.Vote{Timestamp: ts, Type: vType}
  56. have = &types.Vote{Timestamp: ts, Type: vType}
  57. )
  58. defer sc.Stop()
  59. defer rs.Stop()
  60. time.Sleep(10 * time.Millisecond)
  61. require.NoError(t, rs.privVal.SignVote(chainID, want))
  62. require.NoError(t, sc.SignVote(chainID, have))
  63. assert.Equal(t, want.Signature, have.Signature)
  64. }
  65. func testSetupIPCSocketPair(
  66. t *testing.T,
  67. chainID string,
  68. privValidator types.PrivValidator,
  69. ) (*IPCVal, *IPCRemoteSigner) {
  70. addr, err := testUnixAddr()
  71. require.NoError(t, err)
  72. var (
  73. logger = log.TestingLogger()
  74. privVal = privValidator
  75. readyc = make(chan struct{})
  76. rs = NewIPCRemoteSigner(
  77. logger,
  78. chainID,
  79. addr,
  80. privVal,
  81. )
  82. sc = NewIPCVal(
  83. logger,
  84. addr,
  85. )
  86. )
  87. IPCValConnTimeout(5 * time.Millisecond)(sc)
  88. IPCValHeartbeat(time.Millisecond)(sc)
  89. IPCRemoteSignerConnDeadline(time.Millisecond * 5)(rs)
  90. testStartIPCRemoteSigner(t, readyc, rs)
  91. <-readyc
  92. require.NoError(t, sc.Start())
  93. assert.True(t, sc.IsRunning())
  94. return sc, rs
  95. }
  96. func testStartIPCRemoteSigner(t *testing.T, readyc chan struct{}, rs *IPCRemoteSigner) {
  97. go func(rs *IPCRemoteSigner) {
  98. require.NoError(t, rs.Start())
  99. assert.True(t, rs.IsRunning())
  100. readyc <- struct{}{}
  101. }(rs)
  102. }
  103. func testUnixAddr() (string, error) {
  104. f, err := ioutil.TempFile("/tmp", "nettest")
  105. if err != nil {
  106. return "", err
  107. }
  108. addr := f.Name()
  109. err = f.Close()
  110. if err != nil {
  111. return "", err
  112. }
  113. err = os.Remove(addr)
  114. if err != nil {
  115. return "", err
  116. }
  117. return addr, nil
  118. }