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.

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