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.

183 lines
5.1 KiB

  1. package types
  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 TestNewNetAddressIPPort(t *testing.T) {
  108. addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
  109. assert.Equal(t, "127.0.0.1:8080", addr.String())
  110. }
  111. func TestNetAddressProperties(t *testing.T) {
  112. // TODO add more test cases
  113. testCases := []struct {
  114. addr string
  115. valid bool
  116. local bool
  117. routable bool
  118. }{
  119. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true, true, false},
  120. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", true, false, true},
  121. }
  122. for _, tc := range testCases {
  123. addr, err := NewNetAddressString(tc.addr)
  124. require.Nil(t, err)
  125. err = addr.Valid()
  126. if tc.valid {
  127. assert.NoError(t, err)
  128. } else {
  129. assert.Error(t, err)
  130. }
  131. assert.Equal(t, tc.local, addr.Local())
  132. assert.Equal(t, tc.routable, addr.Routable())
  133. }
  134. }
  135. func TestNetAddressReachabilityTo(t *testing.T) {
  136. // TODO add more test cases
  137. testCases := []struct {
  138. addr string
  139. other string
  140. reachability int
  141. }{
  142. {
  143. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
  144. "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8081",
  145. 0,
  146. },
  147. {"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", 1},
  148. }
  149. for _, tc := range testCases {
  150. addr, err := NewNetAddressString(tc.addr)
  151. require.Nil(t, err)
  152. other, err := NewNetAddressString(tc.other)
  153. require.Nil(t, err)
  154. assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
  155. }
  156. }