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.

131 lines
3.4 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. assert, require := assert.New(t), require.New(t)
  10. tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
  11. require.Nil(err)
  12. addr := NewNetAddress("", tcpAddr)
  13. assert.Equal("127.0.0.1:8080", addr.String())
  14. assert.NotPanics(func() {
  15. NewNetAddress("", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000})
  16. }, "Calling NewNetAddress with UDPAddr should not panic in testing")
  17. }
  18. func TestNewNetAddressString(t *testing.T) {
  19. testCases := []struct {
  20. addr string
  21. expected string
  22. correct bool
  23. }{
  24. {"127.0.0.1:8080", "127.0.0.1:8080", true},
  25. {"tcp://127.0.0.1:8080", "127.0.0.1:8080", true},
  26. {"udp://127.0.0.1:8080", "127.0.0.1:8080", true},
  27. {"udp//127.0.0.1:8080", "", false},
  28. // {"127.0.0:8080", false},
  29. {"notahost", "", false},
  30. {"127.0.0.1:notapath", "", false},
  31. {"notahost:8080", "", false},
  32. {"8082", "", false},
  33. {"127.0.0:8080000", "", false},
  34. {"deadbeef@127.0.0.1:8080", "", false},
  35. {"this-isnot-hex@127.0.0.1:8080", "", false},
  36. {"xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  37. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  38. {"tcp://deadbeef@127.0.0.1:8080", "", false},
  39. {"tcp://this-isnot-hex@127.0.0.1:8080", "", false},
  40. {"tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  41. {"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  42. {"tcp://@127.0.0.1:8080", "", false},
  43. {"tcp://@", "", false},
  44. {"", "", false},
  45. {"@", "", false},
  46. {" @", "", false},
  47. {" @ ", "", false},
  48. }
  49. for _, tc := range testCases {
  50. addr, err := NewNetAddressString(tc.addr)
  51. if tc.correct {
  52. if assert.Nil(t, err, tc.addr) {
  53. assert.Equal(t, tc.expected, addr.String())
  54. }
  55. } else {
  56. assert.NotNil(t, err, tc.addr)
  57. }
  58. }
  59. }
  60. func TestNewNetAddressStrings(t *testing.T) {
  61. addrs, errs := NewNetAddressStrings([]string{"127.0.0.1:8080", "127.0.0.2:8080"})
  62. assert.Len(t, errs, 0)
  63. assert.Equal(t, 2, len(addrs))
  64. }
  65. func TestNewNetAddressIPPort(t *testing.T) {
  66. assert := assert.New(t)
  67. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  68. assert.Equal("127.0.0.1:8080", addr.String())
  69. }
  70. func TestNetAddressProperties(t *testing.T) {
  71. assert, require := assert.New(t), require.New(t)
  72. // TODO add more test cases
  73. tests := []struct {
  74. addr string
  75. valid bool
  76. local bool
  77. routable bool
  78. }{
  79. {"127.0.0.1:8080", true, true, false},
  80. {"ya.ru:80", true, false, true},
  81. }
  82. for _, t := range tests {
  83. addr, err := NewNetAddressString(t.addr)
  84. require.Nil(err)
  85. assert.Equal(t.valid, addr.Valid())
  86. assert.Equal(t.local, addr.Local())
  87. assert.Equal(t.routable, addr.Routable())
  88. }
  89. }
  90. func TestNetAddressReachabilityTo(t *testing.T) {
  91. assert, require := assert.New(t), require.New(t)
  92. // TODO add more test cases
  93. tests := []struct {
  94. addr string
  95. other string
  96. reachability int
  97. }{
  98. {"127.0.0.1:8080", "127.0.0.1:8081", 0},
  99. {"ya.ru:80", "127.0.0.1:8080", 1},
  100. }
  101. for _, t := range tests {
  102. addr, err := NewNetAddressString(t.addr)
  103. require.Nil(err)
  104. other, err := NewNetAddressString(t.other)
  105. require.Nil(err)
  106. assert.Equal(t.reachability, addr.ReachabilityTo(other))
  107. }
  108. }