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.

192 lines
5.3 KiB

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