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.

152 lines
4.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. tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
  10. require.Nil(t, err)
  11. addr := NewNetAddress("", tcpAddr)
  12. assert.Equal(t, "127.0.0.1:8080", addr.String())
  13. assert.NotPanics(t, func() {
  14. NewNetAddress("", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000})
  15. }, "Calling NewNetAddress with UDPAddr should not panic in testing")
  16. }
  17. func TestNewNetAddressStringWithOptionalID(t *testing.T) {
  18. testCases := []struct {
  19. name string
  20. addr string
  21. expected string
  22. correct bool
  23. }{
  24. {"no node id, no protocol", "127.0.0.1:8080", "127.0.0.1:8080", true},
  25. {"no node id, tcp input", "tcp://127.0.0.1:8080", "127.0.0.1:8080", true},
  26. {"no node id, udp input", "udp://127.0.0.1:8080", "127.0.0.1:8080", true},
  27. {"malformed udp input", "udp//127.0.0.1:8080", "", false},
  28. // {"127.0.0:8080", false},
  29. {"invalid host", "notahost", "", false},
  30. {"invalid port", "127.0.0.1:notapath", "", false},
  31. {"invalid host w/ port", "notahost:8080", "", false},
  32. {"just a port", "8082", "", false},
  33. {"non-existent port", "127.0.0:8080000", "", false},
  34. {"too short nodeId", "deadbeef@127.0.0.1:8080", "", false},
  35. {"too short, not hex nodeId", "this-isnot-hex@127.0.0.1:8080", "", false},
  36. {"not hex nodeId", "xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  37. {"correct nodeId", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  38. {"too short nodeId w/tcp", "tcp://deadbeef@127.0.0.1:8080", "", false},
  39. {"too short notHex nodeId w/tcp", "tcp://this-isnot-hex@127.0.0.1:8080", "", false},
  40. {"notHex nodeId w/tcp", "tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
  41. {"correct nodeId w/tcp", "tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  42. {"no node id when expected", "tcp://@127.0.0.1:8080", "", false},
  43. {"no node id or IP", "tcp://@", "", false},
  44. {"tcp no host, w/ port", "tcp://:26656", "", false},
  45. {"empty", "", "", false},
  46. {"node id delimiter 1", "@", "", false},
  47. {"node id delimiter 2", " @", "", false},
  48. {"node id delimiter 3", " @ ", "", false},
  49. }
  50. for _, tc := range testCases {
  51. t.Run(tc.name, func(t *testing.T) {
  52. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  53. if tc.correct {
  54. if assert.Nil(t, err, tc.addr) {
  55. assert.Equal(t, tc.expected, addr.String())
  56. }
  57. } else {
  58. assert.NotNil(t, err, tc.addr)
  59. }
  60. })
  61. }
  62. }
  63. func TestNewNetAddressString(t *testing.T) {
  64. testCases := []struct {
  65. addr string
  66. expected string
  67. correct bool
  68. }{
  69. {"127.0.0.1:8080", "127.0.0.1:8080", false},
  70. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true},
  71. }
  72. for _, tc := range testCases {
  73. addr, err := NewNetAddressString(tc.addr)
  74. if tc.correct {
  75. if assert.Nil(t, err, tc.addr) {
  76. assert.Equal(t, tc.expected, addr.String())
  77. }
  78. } else {
  79. assert.NotNil(t, err, tc.addr)
  80. }
  81. }
  82. }
  83. func TestNewNetAddressStrings(t *testing.T) {
  84. addrs, errs := NewNetAddressStrings([]string{
  85. "127.0.0.1:8080",
  86. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
  87. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeed@127.0.0.2:8080"})
  88. assert.Len(t, errs, 1)
  89. assert.Equal(t, 2, len(addrs))
  90. }
  91. func TestNewNetAddressIPPort(t *testing.T) {
  92. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  93. assert.Equal(t, "127.0.0.1:8080", addr.String())
  94. }
  95. func TestNetAddressProperties(t *testing.T) {
  96. // TODO add more test cases
  97. testCases := []struct {
  98. addr string
  99. valid bool
  100. local bool
  101. routable bool
  102. }{
  103. {"127.0.0.1:8080", true, true, false},
  104. {"ya.ru:80", true, false, true},
  105. }
  106. for _, tc := range testCases {
  107. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  108. require.Nil(t, err)
  109. assert.Equal(t, tc.valid, addr.Valid())
  110. assert.Equal(t, tc.local, addr.Local())
  111. assert.Equal(t, tc.routable, addr.Routable())
  112. }
  113. }
  114. func TestNetAddressReachabilityTo(t *testing.T) {
  115. // TODO add more test cases
  116. testCases := []struct {
  117. addr string
  118. other string
  119. reachability int
  120. }{
  121. {"127.0.0.1:8080", "127.0.0.1:8081", 0},
  122. {"ya.ru:80", "127.0.0.1:8080", 1},
  123. }
  124. for _, tc := range testCases {
  125. addr, err := NewNetAddressStringWithOptionalID(tc.addr)
  126. require.Nil(t, err)
  127. other, err := NewNetAddressStringWithOptionalID(tc.other)
  128. require.Nil(t, err)
  129. assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
  130. }
  131. }