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.

135 lines
4.6 KiB

  1. package p2p
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestNewNetAddress(t *testing.T) {
  9. tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
  10. require.Nil(t, err)
  11. addr := NewNetAddress("", tcpAddr)
  12. assert.Equal(t, "127.0.0.1:8080", addr.String())
  13. assert.NotPanics(t, func() {
  14. NewNetAddress("", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000})
  15. }, "Calling NewNetAddress with UDPAddr should not panic in testing")
  16. }
  17. func TestNewNetAddressString(t *testing.T) {
  18. testCases := []struct {
  19. name string
  20. addr string
  21. expected string
  22. correct bool
  23. }{
  24. {"no node id and no protocol", "127.0.0.1:8080", "", false},
  25. {"no node id w/ tcp input", "tcp://127.0.0.1:8080", "", false},
  26. {"no node id w/ udp input", "udp://127.0.0.1:8080", "", false},
  27. {"no protocol", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  28. {"tcp input", "tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  29. {"udp input", "udp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  30. {"malformed tcp input", "tcp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  31. {"malformed udp input", "udp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  32. // {"127.0.0:8080", false},
  33. {"invalid host", "notahost", "", false},
  34. {"invalid port", "127.0.0.1:notapath", "", false},
  35. {"invalid host w/ port", "notahost:8080", "", false},
  36. {"just a port", "8082", "", false},
  37. {"non-existent port", "127.0.0:8080000", "", false},
  38. {"too short nodeId", "deadbeef@127.0.0.1:8080", "", false},
  39. {"too short, not hex nodeId", "this-isnot-hex@127.0.0.1:8080", "", false},
  40. {"not hex nodeId", "xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  41. {"too short nodeId w/tcp", "tcp://deadbeef@127.0.0.1:8080", "", false},
  42. {"too short notHex nodeId w/tcp", "tcp://this-isnot-hex@127.0.0.1:8080", "", false},
  43. {"notHex nodeId w/tcp", "tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  44. {"correct nodeId w/tcp", "tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  45. {"no node id", "tcp://@127.0.0.1:8080", "", false},
  46. {"no node id or IP", "tcp://@", "", false},
  47. {"tcp no host, w/ port", "tcp://:26656", "", false},
  48. {"empty", "", "", false},
  49. {"node id delimiter 1", "@", "", false},
  50. {"node id delimiter 2", " @", "", false},
  51. {"node id delimiter 3", " @ ", "", false},
  52. }
  53. for _, tc := range testCases {
  54. t.Run(tc.name, func(t *testing.T) {
  55. addr, err := NewNetAddressString(tc.addr)
  56. if tc.correct {
  57. if assert.Nil(t, err, tc.addr) {
  58. assert.Equal(t, tc.expected, addr.String())
  59. }
  60. } else {
  61. assert.NotNil(t, err, tc.addr)
  62. }
  63. })
  64. }
  65. }
  66. func TestNewNetAddressStrings(t *testing.T) {
  67. addrs, errs := NewNetAddressStrings([]string{
  68. "127.0.0.1:8080",
  69. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
  70. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeed@127.0.0.2:8080"})
  71. assert.Len(t, errs, 1)
  72. assert.Equal(t, 2, len(addrs))
  73. }
  74. func TestNewNetAddressIPPort(t *testing.T) {
  75. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  76. assert.Equal(t, "127.0.0.1:8080", addr.String())
  77. }
  78. func TestNetAddressProperties(t *testing.T) {
  79. // TODO add more test cases
  80. testCases := []struct {
  81. addr string
  82. valid bool
  83. local bool
  84. routable bool
  85. }{
  86. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true, true, false},
  87. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", true, false, true},
  88. }
  89. for _, tc := range testCases {
  90. addr, err := NewNetAddressString(tc.addr)
  91. require.Nil(t, err)
  92. assert.Equal(t, tc.valid, addr.Valid())
  93. assert.Equal(t, tc.local, addr.Local())
  94. assert.Equal(t, tc.routable, addr.Routable())
  95. }
  96. }
  97. func TestNetAddressReachabilityTo(t *testing.T) {
  98. // TODO add more test cases
  99. testCases := []struct {
  100. addr string
  101. other string
  102. reachability int
  103. }{
  104. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8081", 0},
  105. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", 1},
  106. }
  107. for _, tc := range testCases {
  108. addr, err := NewNetAddressString(tc.addr)
  109. require.Nil(t, err)
  110. other, err := NewNetAddressString(tc.other)
  111. require.Nil(t, err)
  112. assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
  113. }
  114. }