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.

124 lines
3.2 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. }
  43. for _, tc := range testCases {
  44. addr, err := NewNetAddressString(tc.addr)
  45. if tc.correct {
  46. if assert.Nil(t, err, tc.addr) {
  47. assert.Equal(t, tc.expected, addr.String())
  48. }
  49. } else {
  50. assert.NotNil(t, err, tc.addr)
  51. }
  52. }
  53. }
  54. func TestNewNetAddressStrings(t *testing.T) {
  55. addrs, errs := NewNetAddressStrings([]string{"127.0.0.1:8080", "127.0.0.2:8080"})
  56. assert.Len(t, errs, 0)
  57. assert.Equal(t, 2, len(addrs))
  58. }
  59. func TestNewNetAddressIPPort(t *testing.T) {
  60. assert := assert.New(t)
  61. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  62. assert.Equal("127.0.0.1:8080", addr.String())
  63. }
  64. func TestNetAddressProperties(t *testing.T) {
  65. assert, require := assert.New(t), require.New(t)
  66. // TODO add more test cases
  67. tests := []struct {
  68. addr string
  69. valid bool
  70. local bool
  71. routable bool
  72. }{
  73. {"127.0.0.1:8080", true, true, false},
  74. {"ya.ru:80", true, false, true},
  75. }
  76. for _, t := range tests {
  77. addr, err := NewNetAddressString(t.addr)
  78. require.Nil(err)
  79. assert.Equal(t.valid, addr.Valid())
  80. assert.Equal(t.local, addr.Local())
  81. assert.Equal(t.routable, addr.Routable())
  82. }
  83. }
  84. func TestNetAddressReachabilityTo(t *testing.T) {
  85. assert, require := assert.New(t), require.New(t)
  86. // TODO add more test cases
  87. tests := []struct {
  88. addr string
  89. other string
  90. reachability int
  91. }{
  92. {"127.0.0.1:8080", "127.0.0.1:8081", 0},
  93. {"ya.ru:80", "127.0.0.1:8080", 1},
  94. }
  95. for _, t := range tests {
  96. addr, err := NewNetAddressString(t.addr)
  97. require.Nil(err)
  98. other, err := NewNetAddressString(t.other)
  99. require.Nil(err)
  100. assert.Equal(t.reachability, addr.ReachabilityTo(other))
  101. }
  102. }