1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/protocol/address_test.go

243 lines
6.0 KiB
Go
Raw Normal View History

2018-02-23 17:42:01 -05:00
package protocol_test
import (
"bytes"
"testing"
2018-11-15 05:17:20 -05:00
"github.com/google/go-cmp/cmp"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/buf"
"github.com/v2fly/v2ray-core/v4/common/net"
. "github.com/v2fly/v2ray-core/v4/common/protocol"
2018-02-23 17:42:01 -05:00
)
2018-02-23 20:13:40 -05:00
func TestAddressReading(t *testing.T) {
2018-02-23 17:42:01 -05:00
data := []struct {
Options []AddressOption
Input []byte
Address net.Address
Port net.Port
Error bool
}{
2018-02-23 20:13:40 -05:00
{
Options: []AddressOption{},
Input: []byte{},
Error: true,
},
2018-02-23 17:42:01 -05:00
{
Options: []AddressOption{},
Input: []byte{0, 0, 0, 0, 0},
Error: true,
},
{
Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
Input: []byte{1, 0, 0, 0, 0, 0, 53},
Address: net.IPAddress([]byte{0, 0, 0, 0}),
Port: net.Port(53),
},
{
Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4), PortThenAddress()},
Input: []byte{0, 53, 1, 0, 0, 0, 0},
Address: net.IPAddress([]byte{0, 0, 0, 0}),
Port: net.Port(53),
},
2018-02-23 17:42:01 -05:00
{
Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
Input: []byte{1, 0, 0, 0, 0},
Error: true,
},
{
Options: []AddressOption{AddressFamilyByte(0x04, net.AddressFamilyIPv6)},
Input: []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80},
Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
Port: net.Port(80),
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
2021-02-16 15:31:50 -05:00
Input: []byte{3, 9, 118, 50, 102, 108, 121, 46, 111, 114, 103, 0, 80},
Address: net.DomainAddress("v2fly.org"),
2018-02-23 17:42:01 -05:00
Port: net.Port(80),
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
2021-02-16 15:31:50 -05:00
Input: []byte{3, 9, 118, 50, 102, 108, 121, 46, 111, 114, 103, 0},
2018-02-23 17:42:01 -05:00
Error: true,
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
Input: []byte{3, 7, 56, 46, 56, 46, 56, 46, 56, 0, 80},
Address: net.ParseAddress("8.8.8.8"),
Port: net.Port(80),
},
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
Input: []byte{3, 7, 10, 46, 56, 46, 56, 46, 56, 0, 80},
Error: true,
},
2018-07-03 10:56:57 -04:00
{
Options: []AddressOption{AddressFamilyByte(0x03, net.AddressFamilyDomain)},
Input: append(append([]byte{3, 24}, []byte("2a00:1450:4007:816::200e")...), 0, 80),
Address: net.ParseAddress("2a00:1450:4007:816::200e"),
Port: net.Port(80),
},
2018-02-23 17:42:01 -05:00
}
for _, tc := range data {
b := buf.New()
parser := NewAddressParser(tc.Options...)
addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
b.Release()
if tc.Error {
2018-07-13 08:36:09 -04:00
if err == nil {
t.Errorf("Expect error but not: %v", tc)
}
2018-02-23 17:42:01 -05:00
} else {
2018-07-13 08:36:09 -04:00
if err != nil {
t.Errorf("Expect no error but: %s %v", err.Error(), tc)
}
if addr != tc.Address {
t.Error("Got address ", addr.String(), " want ", tc.Address.String())
}
if tc.Port != port {
t.Error("Got port ", port, " want ", tc.Port)
}
2018-02-23 20:13:40 -05:00
}
}
}
func TestAddressWriting(t *testing.T) {
data := []struct {
Options []AddressOption
Address net.Address
Port net.Port
Bytes []byte
Error bool
}{
{
Options: []AddressOption{AddressFamilyByte(0x01, net.AddressFamilyIPv4)},
Address: net.LocalHostIP,
Port: net.Port(80),
Bytes: []byte{1, 127, 0, 0, 1, 0, 80},
},
}
for _, tc := range data {
parser := NewAddressParser(tc.Options...)
b := buf.New()
err := parser.WriteAddressPort(b, tc.Address, tc.Port)
if tc.Error {
2018-07-13 08:36:09 -04:00
if err == nil {
t.Error("Expect error but nil")
}
2018-02-23 20:13:40 -05:00
} else {
2018-07-13 08:36:09 -04:00
common.Must(err)
2018-11-15 05:17:20 -05:00
if diff := cmp.Diff(tc.Bytes, b.Bytes()); diff != "" {
2018-07-13 08:36:09 -04:00
t.Error(err)
}
2018-02-23 17:42:01 -05:00
}
}
}
func BenchmarkAddressReadingIPv4(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x01, net.AddressFamilyIPv4))
cache := buf.New()
defer cache.Release()
payload := buf.New()
defer payload.Release()
raw := []byte{1, 0, 0, 0, 0, 0, 53}
payload.Write(raw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := parser.ReadAddressPort(cache, payload)
common.Must(err)
cache.Clear()
payload.Clear()
payload.Extend(int32(len(raw)))
}
}
func BenchmarkAddressReadingIPv6(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x04, net.AddressFamilyIPv6))
cache := buf.New()
defer cache.Release()
payload := buf.New()
defer payload.Release()
raw := []byte{4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80}
payload.Write(raw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := parser.ReadAddressPort(cache, payload)
common.Must(err)
cache.Clear()
payload.Clear()
payload.Extend(int32(len(raw)))
}
}
func BenchmarkAddressReadingDomain(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x03, net.AddressFamilyDomain))
cache := buf.New()
defer cache.Release()
payload := buf.New()
defer payload.Release()
raw := []byte{3, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80}
payload.Write(raw)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := parser.ReadAddressPort(cache, payload)
common.Must(err)
cache.Clear()
payload.Clear()
payload.Extend(int32(len(raw)))
}
}
func BenchmarkAddressWritingIPv4(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x01, net.AddressFamilyIPv4))
writer := buf.New()
defer writer.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
common.Must(parser.WriteAddressPort(writer, net.LocalHostIP, net.Port(80)))
writer.Clear()
}
}
func BenchmarkAddressWritingIPv6(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x04, net.AddressFamilyIPv6))
writer := buf.New()
defer writer.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
common.Must(parser.WriteAddressPort(writer, net.LocalHostIPv6, net.Port(80)))
writer.Clear()
}
}
func BenchmarkAddressWritingDomain(b *testing.B) {
parser := NewAddressParser(AddressFamilyByte(0x02, net.AddressFamilyDomain))
writer := buf.New()
defer writer.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
2021-02-16 15:31:50 -05:00
common.Must(parser.WriteAddressPort(writer, net.DomainAddress("www.v2fly.org"), net.Port(80)))
writer.Clear()
}
}