github.com/EagleQL/Xray-core@v1.4.3/common/protocol/address_test.go (about) 1 package protocol_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 9 "github.com/xtls/xray-core/common" 10 "github.com/xtls/xray-core/common/buf" 11 "github.com/xtls/xray-core/common/net" 12 . "github.com/xtls/xray-core/common/protocol" 13 ) 14 15 func TestAddressReading(t *testing.T) { 16 data := []struct { 17 Options []AddressOption 18 Input []byte 19 Address net.Address 20 Port net.Port 21 Error bool 22 }{ 23 { 24 Options: []AddressOption{}, 25 Input: []byte{}, 26 Error: true, 27 }, 28 { 29 Options: []AddressOption{}, 30 Input: []byte{0, 0, 0, 0, 0}, 31 Error: true, 32 }, 33 { 34 Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)}, 35 Input: []byte{1, 0, 0, 0, 0, 0, 53}, 36 Address: net.IPAddress([]byte{0, 0, 0, 0}), 37 Port: net.Port(53), 38 }, 39 { 40 Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4), PortThenAddress()}, 41 Input: []byte{0, 53, 1, 0, 0, 0, 0}, 42 Address: net.IPAddress([]byte{0, 0, 0, 0}), 43 Port: net.Port(53), 44 }, 45 { 46 Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)}, 47 Input: []byte{1, 0, 0, 0, 0}, 48 Error: true, 49 }, 50 { 51 Options: []AddressOption{AddressFamilyByte(0x04, net.AddressFamilyIPv6)}, 52 Input: []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80}, 53 Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}), 54 Port: net.Port(80), 55 }, 56 { 57 Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)}, 58 Input: []byte{3, 11, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 0, 80}, 59 Address: net.DomainAddress("example.com"), 60 Port: net.Port(80), 61 }, 62 { 63 Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)}, 64 Input: []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0}, 65 Error: true, 66 }, 67 { 68 Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)}, 69 Input: []byte{3, 7, 56, 46, 56, 46, 56, 46, 56, 0, 80}, 70 Address: net.ParseAddress("8.8.8.8"), 71 Port: net.Port(80), 72 }, 73 { 74 Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)}, 75 Input: []byte{3, 7, 10, 46, 56, 46, 56, 46, 56, 0, 80}, 76 Error: true, 77 }, 78 { 79 Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)}, 80 Input: append(append([]byte{3, 24}, []byte("2a00:1450:4007:816::200e")...), 0, 80), 81 Address: net.ParseAddress("2a00:1450:4007:816::200e"), 82 Port: net.Port(80), 83 }, 84 } 85 86 for _, tc := range data { 87 parser := NewAddressParser(tc.Options...) 88 89 b := buf.New() 90 addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input)) 91 b.Release() 92 if tc.Error { 93 if err == nil { 94 t.Errorf("Expect error but not: %v", tc) 95 } 96 } else { 97 if err != nil { 98 t.Errorf("Expect no error but: %s %v", err.Error(), tc) 99 } 100 101 if addr != tc.Address { 102 t.Error("Got address ", addr.String(), " want ", tc.Address.String()) 103 } 104 105 if tc.Port != port { 106 t.Error("Got port ", port, " want ", tc.Port) 107 } 108 } 109 } 110 } 111 112 func TestAddressWriting(t *testing.T) { 113 data := []struct { 114 Options []AddressOption 115 Address net.Address 116 Port net.Port 117 Bytes []byte 118 Error bool 119 }{ 120 { 121 Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)}, 122 Address: net.LocalHostIP, 123 Port: net.Port(80), 124 Bytes: []byte{1, 127, 0, 0, 1, 0, 80}, 125 }, 126 } 127 128 for _, tc := range data { 129 parser := NewAddressParser(tc.Options...) 130 131 b := buf.New() 132 err := parser.WriteAddressPort(b, tc.Address, tc.Port) 133 if tc.Error { 134 if err == nil { 135 t.Error("Expect error but nil") 136 } 137 } else { 138 common.Must(err) 139 if diff := cmp.Diff(tc.Bytes, b.Bytes()); diff != "" { 140 t.Error(err) 141 } 142 } 143 } 144 } 145 146 func BenchmarkAddressReadingIPv4(b *testing.B) { 147 parser := NewAddressParser(AddressFamilyByte(0x01, net.AddressFamilyIPv4)) 148 cache := buf.New() 149 defer cache.Release() 150 151 payload := buf.New() 152 defer payload.Release() 153 154 raw := []byte{1, 0, 0, 0, 0, 0, 53} 155 payload.Write(raw) 156 157 b.ResetTimer() 158 for i := 0; i < b.N; i++ { 159 _, _, err := parser.ReadAddressPort(cache, payload) 160 common.Must(err) 161 cache.Clear() 162 payload.Clear() 163 payload.Extend(int32(len(raw))) 164 } 165 } 166 167 func BenchmarkAddressReadingIPv6(b *testing.B) { 168 parser := NewAddressParser(AddressFamilyByte(0x04, net.AddressFamilyIPv6)) 169 cache := buf.New() 170 defer cache.Release() 171 172 payload := buf.New() 173 defer payload.Release() 174 175 raw := []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80} 176 payload.Write(raw) 177 178 b.ResetTimer() 179 for i := 0; i < b.N; i++ { 180 _, _, err := parser.ReadAddressPort(cache, payload) 181 common.Must(err) 182 cache.Clear() 183 payload.Clear() 184 payload.Extend(int32(len(raw))) 185 } 186 } 187 188 func BenchmarkAddressReadingDomain(b *testing.B) { 189 parser := NewAddressParser(AddressFamilyByte(0x03, net.AddressFamilyDomain)) 190 cache := buf.New() 191 defer cache.Release() 192 193 payload := buf.New() 194 defer payload.Release() 195 196 raw := []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80} 197 payload.Write(raw) 198 199 b.ResetTimer() 200 for i := 0; i < b.N; i++ { 201 _, _, err := parser.ReadAddressPort(cache, payload) 202 common.Must(err) 203 cache.Clear() 204 payload.Clear() 205 payload.Extend(int32(len(raw))) 206 } 207 } 208 209 func BenchmarkAddressWritingIPv4(b *testing.B) { 210 parser := NewAddressParser(AddressFamilyByte(0x01, net.AddressFamilyIPv4)) 211 writer := buf.New() 212 defer writer.Release() 213 214 b.ResetTimer() 215 for i := 0; i < b.N; i++ { 216 common.Must(parser.WriteAddressPort(writer, net.LocalHostIP, net.Port(80))) 217 writer.Clear() 218 } 219 } 220 221 func BenchmarkAddressWritingIPv6(b *testing.B) { 222 parser := NewAddressParser(AddressFamilyByte(0x04, net.AddressFamilyIPv6)) 223 writer := buf.New() 224 defer writer.Release() 225 226 b.ResetTimer() 227 for i := 0; i < b.N; i++ { 228 common.Must(parser.WriteAddressPort(writer, net.LocalHostIPv6, net.Port(80))) 229 writer.Clear() 230 } 231 } 232 233 func BenchmarkAddressWritingDomain(b *testing.B) { 234 parser := NewAddressParser(AddressFamilyByte(0x02, net.AddressFamilyDomain)) 235 writer := buf.New() 236 defer writer.Release() 237 238 b.ResetTimer() 239 for i := 0; i < b.N; i++ { 240 common.Must(parser.WriteAddressPort(writer, net.DomainAddress("www.example.com"), net.Port(80))) 241 writer.Clear() 242 } 243 }