address_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package net_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. . "v2ray.com/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("v2ray.com"),
  60. Output: addrProprty{
  61. Domain: "v2ray.com",
  62. Family: AddressFamilyDomain,
  63. String: "v2ray.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("[::ffff:123.151.71.143]"),
  84. Output: addrProprty{
  85. IP: []byte{123, 151, 71, 143},
  86. Family: AddressFamilyIPv4,
  87. String: "123.151.71.143",
  88. },
  89. },
  90. {
  91. Input: NewIPOrDomain(ParseAddress("v2ray.com")).AsAddress(),
  92. Output: addrProprty{
  93. Domain: "v2ray.com",
  94. Family: AddressFamilyDomain,
  95. String: "v2ray.com",
  96. },
  97. },
  98. {
  99. Input: NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(),
  100. Output: addrProprty{
  101. IP: []byte{8, 8, 8, 8},
  102. Family: AddressFamilyIPv4,
  103. String: "8.8.8.8",
  104. },
  105. },
  106. {
  107. Input: NewIPOrDomain(ParseAddress("[2001:4860:0:2001::68]")).AsAddress(),
  108. Output: addrProprty{
  109. IP: []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
  110. Family: AddressFamilyIPv6,
  111. String: "[2001:4860:0:2001::68]",
  112. },
  113. },
  114. }
  115. for _, testCase := range testCases {
  116. actual := addrProprty{
  117. Family: testCase.Input.Family(),
  118. String: testCase.Input.String(),
  119. }
  120. if testCase.Input.Family().IsIP() {
  121. actual.IP = testCase.Input.IP()
  122. } else {
  123. actual.Domain = testCase.Input.Domain()
  124. }
  125. if r := cmp.Diff(actual, testCase.Output); r != "" {
  126. t.Error("for input: ", testCase.Input, ":", r)
  127. }
  128. }
  129. }
  130. func TestInvalidAddressConvertion(t *testing.T) {
  131. panics := func(f func()) (ret bool) {
  132. defer func() {
  133. if r := recover(); r != nil {
  134. ret = true
  135. }
  136. }()
  137. f()
  138. return false
  139. }
  140. testCases := []func(){
  141. func() { ParseAddress("8.8.8.8").Domain() },
  142. func() { ParseAddress("2001:4860:0:2001::68").Domain() },
  143. func() { ParseAddress("v2ray.com").IP() },
  144. }
  145. for idx, testCase := range testCases {
  146. if !panics(testCase) {
  147. t.Error("case ", idx, " failed")
  148. }
  149. }
  150. }
  151. func BenchmarkParseAddressIPv4(b *testing.B) {
  152. for i := 0; i < b.N; i++ {
  153. addr := ParseAddress("8.8.8.8")
  154. if addr.Family() != AddressFamilyIPv4 {
  155. panic("not ipv4")
  156. }
  157. }
  158. }
  159. func BenchmarkParseAddressIPv6(b *testing.B) {
  160. for i := 0; i < b.N; i++ {
  161. addr := ParseAddress("2001:4860:0:2001::68")
  162. if addr.Family() != AddressFamilyIPv6 {
  163. panic("not ipv6")
  164. }
  165. }
  166. }
  167. func BenchmarkParseAddressDomain(b *testing.B) {
  168. for i := 0; i < b.N; i++ {
  169. addr := ParseAddress("v2ray.com")
  170. if addr.Family() != AddressFamilyDomain {
  171. panic("not domain")
  172. }
  173. }
  174. }