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.

144 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. t.Run(tc.name, func(t *testing.T) {
  58. addr, err := NewNetAddressString(tc.addr)
  59. if tc.correct {
  60. if assert.Nil(t, err, tc.addr) {
  61. assert.Equal(t, tc.expected, addr.String())
  62. }
  63. } else {
  64. assert.NotNil(t, err, tc.addr)
  65. }
  66. })
  67. }
  68. }
  69. func TestNewNetAddressStrings(t *testing.T) {
  70. addrs, errs := NewNetAddressStrings([]string{
  71. "127.0.0.1:8080",
  72. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
  73. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeed@127.0.0.2:8080"})
  74. assert.Len(t, errs, 1)
  75. assert.Equal(t, 2, len(addrs))
  76. }
  77. func TestNewNetAddressIPPort(t *testing.T) {
  78. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  79. assert.Equal(t, "127.0.0.1:8080", addr.String())
  80. }
  81. func TestNetAddressProperties(t *testing.T) {
  82. // TODO add more test cases
  83. testCases := []struct {
  84. addr string
  85. valid bool
  86. local bool
  87. routable bool
  88. }{
  89. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true, true, false},
  90. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", true, false, true},
  91. }
  92. for _, tc := range testCases {
  93. addr, err := NewNetAddressString(tc.addr)
  94. require.Nil(t, err)
  95. err = addr.Valid()
  96. if tc.valid {
  97. assert.NoError(t, err)
  98. } else {
  99. assert.Error(t, err)
  100. }
  101. assert.Equal(t, tc.local, addr.Local())
  102. assert.Equal(t, tc.routable, addr.Routable())
  103. }
  104. }
  105. func TestNetAddressReachabilityTo(t *testing.T) {
  106. // TODO add more test cases
  107. testCases := []struct {
  108. addr string
  109. other string
  110. reachability int
  111. }{
  112. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8081", 0},
  113. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", 1},
  114. }
  115. for _, tc := range testCases {
  116. addr, err := NewNetAddressString(tc.addr)
  117. require.Nil(t, err)
  118. other, err := NewNetAddressString(tc.other)
  119. require.Nil(t, err)
  120. assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
  121. }
  122. }