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