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.

148 lines
4.0 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 TestNewNetAddressStringWithOptionalID(t *testing.T) {
  18. testCases := []struct {
  19. addr string
  20. expected string
  21. correct bool
  22. }{
  23. {"127.0.0.1:8080", "127.0.0.1:8080", true},
  24. {"tcp://127.0.0.1:8080", "127.0.0.1:8080", true},
  25. {"udp://127.0.0.1:8080", "127.0.0.1:8080", true},
  26. {"udp//127.0.0.1:8080", "", false},
  27. // {"127.0.0:8080", false},
  28. {"notahost", "", false},
  29. {"127.0.0.1:notapath", "", false},
  30. {"notahost:8080", "", false},
  31. {"8082", "", false},
  32. {"127.0.0:8080000", "", false},
  33. {"deadbeef@127.0.0.1:8080", "", false},
  34. {"this-isnot-hex@127.0.0.1:8080", "", false},
  35. {"xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  36. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  37. {"tcp://deadbeef@127.0.0.1:8080", "", false},
  38. {"tcp://this-isnot-hex@127.0.0.1:8080", "", false},
  39. {"tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  40. {"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  41. {"tcp://@127.0.0.1:8080", "", false},
  42. {"tcp://@", "", false},
  43. {"", "", false},
  44. {"@", "", false},
  45. {" @", "", false},
  46. {" @ ", "", false},
  47. }
  48. for _, tc := range testCases {
  49. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  50. if tc.correct {
  51. if assert.Nil(t, err, tc.addr) {
  52. assert.Equal(t, tc.expected, addr.String())
  53. }
  54. } else {
  55. assert.NotNil(t, err, tc.addr)
  56. }
  57. }
  58. }
  59. func TestNewNetAddressString(t *testing.T) {
  60. testCases := []struct {
  61. addr string
  62. expected string
  63. correct bool
  64. }{
  65. {"127.0.0.1:8080", "127.0.0.1:8080", false},
  66. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  67. }
  68. for _, tc := range testCases {
  69. addr, err := NewNetAddressString(tc.addr)
  70. if tc.correct {
  71. if assert.Nil(t, err, tc.addr) {
  72. assert.Equal(t, tc.expected, addr.String())
  73. }
  74. } else {
  75. assert.NotNil(t, err, tc.addr)
  76. }
  77. }
  78. }
  79. func TestNewNetAddressStrings(t *testing.T) {
  80. addrs, errs := NewNetAddressStrings([]string{
  81. "127.0.0.1:8080",
  82. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
  83. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeed@127.0.0.2:8080"})
  84. assert.Len(t, errs, 1)
  85. assert.Equal(t, 2, len(addrs))
  86. }
  87. func TestNewNetAddressIPPort(t *testing.T) {
  88. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  89. assert.Equal(t, "127.0.0.1:8080", addr.String())
  90. }
  91. func TestNetAddressProperties(t *testing.T) {
  92. // TODO add more test cases
  93. testCases := []struct {
  94. addr string
  95. valid bool
  96. local bool
  97. routable bool
  98. }{
  99. {"127.0.0.1:8080", true, true, false},
  100. {"ya.ru:80", true, false, true},
  101. }
  102. for _, tc := range testCases {
  103. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  104. require.Nil(t, err)
  105. assert.Equal(t, tc.valid, addr.Valid())
  106. assert.Equal(t, tc.local, addr.Local())
  107. assert.Equal(t, tc.routable, addr.Routable())
  108. }
  109. }
  110. func TestNetAddressReachabilityTo(t *testing.T) {
  111. // TODO add more test cases
  112. testCases := []struct {
  113. addr string
  114. other string
  115. reachability int
  116. }{
  117. {"127.0.0.1:8080", "127.0.0.1:8081", 0},
  118. {"ya.ru:80", "127.0.0.1:8080", 1},
  119. }
  120. for _, tc := range testCases {
  121. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  122. require.Nil(t, err)
  123. other, err := NewNetAddressStringWithOptionalID(tc.other)
  124. require.Nil(t, err)
  125. assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
  126. }
  127. }