address_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package net_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. . "github.com/xtls/xray-core/common/net"
  7. )
  8. func TestAddressProperty(t *testing.T) {
  9. type addrProprty struct {
  10. IP []byte
  11. Domain string
  12. Family AddressFamily
  13. String string
  14. }
  15. testCases := []struct {
  16. Input Address
  17. Output addrProprty
  18. }{
  19. {
  20. Input: IPAddress([]byte{byte(1), byte(2), byte(3), byte(4)}),
  21. Output: addrProprty{
  22. IP: []byte{byte(1), byte(2), byte(3), byte(4)},
  23. Family: AddressFamilyIPv4,
  24. String: "1.2.3.4",
  25. },
  26. },
  27. {
  28. Input: IPAddress([]byte{
  29. byte(1), byte(2), byte(3), byte(4),
  30. byte(1), byte(2), byte(3), byte(4),
  31. byte(1), byte(2), byte(3), byte(4),
  32. byte(1), byte(2), byte(3), byte(4),
  33. }),
  34. Output: addrProprty{
  35. IP: []byte{
  36. byte(1), byte(2), byte(3), byte(4),
  37. byte(1), byte(2), byte(3), byte(4),
  38. byte(1), byte(2), byte(3), byte(4),
  39. byte(1), byte(2), byte(3), byte(4),
  40. },
  41. Family: AddressFamilyIPv6,
  42. String: "[102:304:102:304:102:304:102:304]",
  43. },
  44. },
  45. {
  46. Input: IPAddress([]byte{
  47. byte(0), byte(0), byte(0), byte(0),
  48. byte(0), byte(0), byte(0), byte(0),
  49. byte(0), byte(0), byte(255), byte(255),
  50. byte(1), byte(2), byte(3), byte(4),
  51. }),
  52. Output: addrProprty{
  53. IP: []byte{byte(1), byte(2), byte(3), byte(4)},
  54. Family: AddressFamilyIPv4,
  55. String: "1.2.3.4",
  56. },
  57. },
  58. {
  59. Input: DomainAddress("example.com"),
  60. Output: addrProprty{
  61. Domain: "example.com",
  62. Family: AddressFamilyDomain,
  63. String: "example.com",
  64. },
  65. },
  66. {
  67. Input: IPAddress(net.IPv4(1, 2, 3, 4)),
  68. Output: addrProprty{
  69. IP: []byte{byte(1), byte(2), byte(3), byte(4)},
  70. Family: AddressFamilyIPv4,
  71. String: "1.2.3.4",
  72. },
  73. },
  74. {
  75. Input: ParseAddress("[2001:4860:0:2001::68]"),
  76. Output: addrProprty{
  77. IP: []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
  78. Family: AddressFamilyIPv6,
  79. String: "[2001:4860:0:2001::68]",
  80. },
  81. },
  82. {
  83. Input: ParseAddress("::0"),
  84. Output: addrProprty{
  85. IP: AnyIPv6.IP(),
  86. Family: AddressFamilyIPv6,
  87. String: "[::]",
  88. },
  89. },
  90. {
  91. Input: ParseAddress("[::ffff:123.151.71.143]"),
  92. Output: addrProprty{
  93. IP: []byte{123, 151, 71, 143},
  94. Family: AddressFamilyIPv4,
  95. String: "123.151.71.143",
  96. },
  97. },
  98. {
  99. Input: NewIPOrDomain(ParseAddress("example.com")).AsAddress(),
  100. Output: addrProprty{
  101. Domain: "example.com",
  102. Family: AddressFamilyDomain,
  103. String: "example.com",
  104. },
  105. },
  106. {
  107. Input: NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(),
  108. Output: addrProprty{
  109. IP: []byte{8, 8, 8, 8},
  110. Family: AddressFamilyIPv4,
  111. String: "8.8.8.8",
  112. },
  113. },
  114. {
  115. Input: NewIPOrDomain(ParseAddress("[2001:4860:0:2001::68]")).AsAddress(),
  116. Output: addrProprty{
  117. IP: []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
  118. Family: AddressFamilyIPv6,
  119. String: "[2001:4860:0:2001::68]",
  120. },
  121. },
  122. }
  123. for _, testCase := range testCases {
  124. actual := addrProprty{
  125. Family: testCase.Input.Family(),
  126. String: testCase.Input.String(),
  127. }
  128. if testCase.Input.Family().IsIP() {
  129. actual.IP = testCase.Input.IP()
  130. } else {
  131. actual.Domain = testCase.Input.Domain()
  132. }
  133. if r := cmp.Diff(actual, testCase.Output); r != "" {
  134. t.Error("for input: ", testCase.Input, ":", r)
  135. }
  136. }
  137. }
  138. func TestInvalidAddressConvertion(t *testing.T) {
  139. panics := func(f func()) (ret bool) {
  140. defer func() {
  141. if r := recover(); r != nil {
  142. ret = true
  143. }
  144. }()
  145. f()
  146. return false
  147. }
  148. testCases := []func(){
  149. func() { ParseAddress("8.8.8.8").Domain() },
  150. func() { ParseAddress("2001:4860:0:2001::68").Domain() },
  151. func() { ParseAddress("example.com").IP() },
  152. }
  153. for idx, testCase := range testCases {
  154. if !panics(testCase) {
  155. t.Error("case ", idx, " failed")
  156. }
  157. }
  158. }
  159. func BenchmarkParseAddressIPv4(b *testing.B) {
  160. for i := 0; i < b.N; i++ {
  161. addr := ParseAddress("8.8.8.8")
  162. if addr.Family() != AddressFamilyIPv4 {
  163. panic("not ipv4")
  164. }
  165. }
  166. }
  167. func BenchmarkParseAddressIPv6(b *testing.B) {
  168. for i := 0; i < b.N; i++ {
  169. addr := ParseAddress("2001:4860:0:2001::68")
  170. if addr.Family() != AddressFamilyIPv6 {
  171. panic("not ipv6")
  172. }
  173. }
  174. }
  175. func BenchmarkParseAddressDomain(b *testing.B) {
  176. for i := 0; i < b.N; i++ {
  177. addr := ParseAddress("example.com")
  178. if addr.Family() != AddressFamilyDomain {
  179. panic("not domain")
  180. }
  181. }
  182. }