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