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.

120 lines
3.8 KiB

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