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.

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