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.

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