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.

128 lines
4.5 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package p2p
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tendermint/tendermint/crypto/ed25519"
  6. )
  7. func TestNodeInfoValidate(t *testing.T) {
  8. // empty fails
  9. ni := DefaultNodeInfo{}
  10. assert.Error(t, ni.Validate())
  11. channels := make([]byte, maxNumChannels)
  12. for i := 0; i < maxNumChannels; i++ {
  13. channels[i] = byte(i)
  14. }
  15. dupChannels := make([]byte, 5)
  16. copy(dupChannels, channels[:5])
  17. dupChannels = append(dupChannels, testCh)
  18. nonASCII := "¢§µ"
  19. emptyTab := "\t"
  20. emptySpace := " "
  21. testCases := []struct {
  22. testName string
  23. malleateNodeInfo func(*DefaultNodeInfo)
  24. expectErr bool
  25. }{
  26. {
  27. "Too Many Channels",
  28. func(ni *DefaultNodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, // nolint: gocritic
  29. true,
  30. },
  31. {"Duplicate Channel", func(ni *DefaultNodeInfo) { ni.Channels = dupChannels }, true},
  32. {"Good Channels", func(ni *DefaultNodeInfo) { ni.Channels = ni.Channels[:5] }, false},
  33. {"Invalid NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "not-an-address" }, true},
  34. {"Good NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false},
  35. {"Non-ASCII Version", func(ni *DefaultNodeInfo) { ni.Version = nonASCII }, true},
  36. {"Empty tab Version", func(ni *DefaultNodeInfo) { ni.Version = emptyTab }, true},
  37. {"Empty space Version", func(ni *DefaultNodeInfo) { ni.Version = emptySpace }, true},
  38. {"Empty Version", func(ni *DefaultNodeInfo) { ni.Version = "" }, false},
  39. {"Non-ASCII Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = nonASCII }, true},
  40. {"Empty tab Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptyTab }, true},
  41. {"Empty space Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptySpace }, true},
  42. {"Empty Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "" }, true},
  43. {"Good Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "hey its me" }, false},
  44. {"Non-ASCII TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = nonASCII }, true},
  45. {"Empty tab TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptyTab }, true},
  46. {"Empty space TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptySpace }, true},
  47. {"Empty TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "" }, false},
  48. {"Off TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "off" }, false},
  49. {"Non-ASCII RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = nonASCII }, true},
  50. {"Empty tab RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptyTab }, true},
  51. {"Empty space RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptySpace }, true},
  52. {"Empty RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "" }, false},
  53. {"Good RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false},
  54. }
  55. nodeKey := NodeKey{PrivKey: ed25519.GenPrivKey()}
  56. name := "testing"
  57. // test case passes
  58. ni = testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  59. ni.Channels = channels
  60. assert.NoError(t, ni.Validate())
  61. for _, tc := range testCases {
  62. ni := testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo)
  63. ni.Channels = channels
  64. tc.malleateNodeInfo(&ni)
  65. err := ni.Validate()
  66. if tc.expectErr {
  67. assert.Error(t, err, tc.testName)
  68. } else {
  69. assert.NoError(t, err, tc.testName)
  70. }
  71. }
  72. }
  73. func TestNodeInfoCompatible(t *testing.T) {
  74. nodeKey1 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  75. nodeKey2 := NodeKey{PrivKey: ed25519.GenPrivKey()}
  76. name := "testing"
  77. var newTestChannel byte = 0x2
  78. // test NodeInfo is compatible
  79. ni1 := testNodeInfo(nodeKey1.ID(), name).(DefaultNodeInfo)
  80. ni2 := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo)
  81. assert.NoError(t, ni1.CompatibleWith(ni2))
  82. // add another channel; still compatible
  83. ni2.Channels = append(ni2.Channels, newTestChannel)
  84. assert.True(t, ni2.HasChannel(newTestChannel))
  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++ }},
  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. }