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.

123 lines
4.4 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. {"Too Many Channels", func(ni *DefaultNodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, true}, // nolint: gocritic
  28. {"Duplicate Channel", func(ni *DefaultNodeInfo) { ni.Channels = dupChannels }, true},
  29. {"Good Channels", func(ni *DefaultNodeInfo) { ni.Channels = ni.Channels[:5] }, false},
  30. {"Invalid NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "not-an-address" }, true},
  31. {"Good NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false},
  32. {"Non-ASCII Version", func(ni *DefaultNodeInfo) { ni.Version = nonAscii }, true},
  33. {"Empty tab Version", func(ni *DefaultNodeInfo) { ni.Version = emptyTab }, true},
  34. {"Empty space Version", func(ni *DefaultNodeInfo) { ni.Version = emptySpace }, true},
  35. {"Empty Version", func(ni *DefaultNodeInfo) { ni.Version = "" }, false},
  36. {"Non-ASCII Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = nonAscii }, true},
  37. {"Empty tab Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptyTab }, true},
  38. {"Empty space Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptySpace }, true},
  39. {"Empty Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "" }, true},
  40. {"Good Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "hey its me" }, false},
  41. {"Non-ASCII TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = nonAscii }, true},
  42. {"Empty tab TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptyTab }, true},
  43. {"Empty space TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptySpace }, true},
  44. {"Empty TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "" }, false},
  45. {"Off TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "off" }, false},
  46. {"Non-ASCII RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = nonAscii }, true},
  47. {"Empty tab RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptyTab }, true},
  48. {"Empty space RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptySpace }, true},
  49. {"Empty RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "" }, false},
  50. {"Good RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
  51. }
  52. nodeKey := NodeKey{PrivKey: ed25519.GenPrivKey()}
  53. name := "testing"
  54. // test case passes
  55. ni = testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  56. ni.Channels = channels
  57. assert.NoError(t, ni.Validate())
  58. for _, tc := range testCases {
  59. ni := testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  60. ni.Channels = channels
  61. tc.malleateNodeInfo(&ni)
  62. err := ni.Validate()
  63. if tc.expectErr {
  64. assert.Error(t, err, tc.testName)
  65. } else {
  66. assert.NoError(t, err, tc.testName)
  67. }
  68. }
  69. }
  70. func TestNodeInfoCompatible(t *testing.T) {
  71. nodeKey1 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  72. nodeKey2 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  73. name := "testing"
  74. var newTestChannel byte = 0x2
  75. // test NodeInfo is compatible
  76. ni1 := testNodeInfo(nodeKey1.ID(), name).(DefaultNodeInfo)
  77. ni2 := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo)
  78. assert.NoError(t, ni1.CompatibleWith(ni2))
  79. // add another channel; still compatible
  80. ni2.Channels = []byte{newTestChannel, testCh}
  81. assert.NoError(t, ni1.CompatibleWith(ni2))
  82. // wrong NodeInfo type is not compatible
  83. _, netAddr := CreateRoutableAddr()
  84. ni3 := mockNodeInfo{netAddr}
  85. assert.Error(t, ni1.CompatibleWith(ni3))
  86. testCases := []struct {
  87. testName string
  88. malleateNodeInfo func(*DefaultNodeInfo)
  89. }{
  90. {"Wrong block version", func(ni *DefaultNodeInfo) { ni.ProtocolVersion.Block += 1 }},
  91. {"Wrong network", func(ni *DefaultNodeInfo) { ni.Network += "-wrong" }},
  92. {"No common channels", func(ni *DefaultNodeInfo) { ni.Channels = []byte{newTestChannel} }},
  93. }
  94. for _, tc := range testCases {
  95. ni := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo)
  96. tc.malleateNodeInfo(&ni)
  97. assert.Error(t, ni1.CompatibleWith(ni))
  98. }
  99. }