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.

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