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.

127 lines
4.5 KiB

  1. package p2p
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. )
  8. func TestNodeInfoValidate(t *testing.T) {
  9. // empty fails
  10. ni := DefaultNodeInfo{}
  11. assert.Error(t, ni.Validate())
  12. channels := make([]byte, maxNumChannels)
  13. for i := 0; i < maxNumChannels; i++ {
  14. channels[i] = byte(i)
  15. }
  16. dupChannels := make([]byte, 5)
  17. copy(dupChannels, channels[:5])
  18. dupChannels = append(dupChannels, testCh)
  19. nonAscii := "¢§µ"
  20. emptyTab := fmt.Sprintf("\t")
  21. emptySpace := fmt.Sprintf(" ")
  22. testCases := []struct {
  23. testName string
  24. malleateNodeInfo func(*DefaultNodeInfo)
  25. expectErr bool
  26. }{
  27. {
  28. "Too Many Channels",
  29. func(ni *DefaultNodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, // nolint: gocritic
  30. true,
  31. },
  32. {"Duplicate Channel", func(ni *DefaultNodeInfo) { ni.Channels = dupChannels }, true},
  33. {"Good Channels", func(ni *DefaultNodeInfo) { ni.Channels = ni.Channels[:5] }, false},
  34. {"Invalid NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "not-an-address" }, true},
  35. {"Good NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false},
  36. {"Non-ASCII Version", func(ni *DefaultNodeInfo) { ni.Version = nonAscii }, true},
  37. {"Empty tab Version", func(ni *DefaultNodeInfo) { ni.Version = emptyTab }, true},
  38. {"Empty space Version", func(ni *DefaultNodeInfo) { ni.Version = emptySpace }, true},
  39. {"Empty Version", func(ni *DefaultNodeInfo) { ni.Version = "" }, false},
  40. {"Non-ASCII Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = nonAscii }, true},
  41. {"Empty tab Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptyTab }, true},
  42. {"Empty space Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptySpace }, true},
  43. {"Empty Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "" }, true},
  44. {"Good Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "hey its me" }, false},
  45. {"Non-ASCII TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = nonAscii }, true},
  46. {"Empty tab TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptyTab }, true},
  47. {"Empty space TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptySpace }, true},
  48. {"Empty TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "" }, false},
  49. {"Off TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "off" }, false},
  50. {"Non-ASCII RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = nonAscii }, true},
  51. {"Empty tab RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptyTab }, true},
  52. {"Empty space RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptySpace }, true},
  53. {"Empty RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "" }, false},
  54. {"Good RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
  55. }
  56. nodeKey := NodeKey{PrivKey: ed25519.GenPrivKey()}
  57. name := "testing"
  58. // test case passes
  59. ni = testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  60. ni.Channels = channels
  61. assert.NoError(t, ni.Validate())
  62. for _, tc := range testCases {
  63. ni := testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  64. ni.Channels = channels
  65. tc.malleateNodeInfo(&ni)
  66. err := ni.Validate()
  67. if tc.expectErr {
  68. assert.Error(t, err, tc.testName)
  69. } else {
  70. assert.NoError(t, err, tc.testName)
  71. }
  72. }
  73. }
  74. func TestNodeInfoCompatible(t *testing.T) {
  75. nodeKey1 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  76. nodeKey2 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  77. name := "testing"
  78. var newTestChannel byte = 0x2
  79. // test NodeInfo is compatible
  80. ni1 := testNodeInfo(nodeKey1.ID(), name).(DefaultNodeInfo)
  81. ni2 := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo)
  82. assert.NoError(t, ni1.CompatibleWith(ni2))
  83. // add another channel; still compatible
  84. ni2.Channels = []byte{newTestChannel, testCh}
  85. assert.NoError(t, ni1.CompatibleWith(ni2))
  86. // wrong NodeInfo type is not compatible
  87. _, netAddr := CreateRoutableAddr()
  88. ni3 := mockNodeInfo{netAddr}
  89. assert.Error(t, ni1.CompatibleWith(ni3))
  90. testCases := []struct {
  91. testName string
  92. malleateNodeInfo func(*DefaultNodeInfo)
  93. }{
  94. {"Wrong block version", func(ni *DefaultNodeInfo) { ni.ProtocolVersion.Block += 1 }},
  95. {"Wrong network", func(ni *DefaultNodeInfo) { ni.Network += "-wrong" }},
  96. {"No common channels", func(ni *DefaultNodeInfo) { ni.Channels = []byte{newTestChannel} }},
  97. }
  98. for _, tc := range testCases {
  99. ni := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo)
  100. tc.malleateNodeInfo(&ni)
  101. assert.Error(t, ni1.CompatibleWith(ni))
  102. }
  103. }