1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-03 16:57:38 -05:00
v2fly/common/net/address_test.go

195 lines
4.4 KiB
Go
Raw Normal View History

2015-12-02 11:18:12 -05:00
package net_test
2015-09-16 16:19:42 -04:00
import (
2015-10-14 02:43:04 -04:00
"net"
2015-09-16 16:19:42 -04:00
"testing"
2019-01-07 17:27:59 -05:00
"github.com/google/go-cmp/cmp"
2017-02-16 15:08:10 -05:00
. "v2ray.com/core/common/net"
2015-09-16 16:19:42 -04:00
)
2019-01-07 17:27:59 -05:00
func TestAddressProperty(t *testing.T) {
type addrProprty struct {
IP []byte
Domain string
Family AddressFamily
String string
2015-09-16 16:19:42 -04:00
}
2019-01-07 17:27:59 -05:00
testCases := []struct {
Input Address
Output addrProprty
}{
{
Input: IPAddress([]byte{byte(1), byte(2), byte(3), byte(4)}),
Output: addrProprty{
IP: []byte{byte(1), byte(2), byte(3), byte(4)},
Family: AddressFamilyIPv4,
String: "1.2.3.4",
},
},
{
Input: IPAddress([]byte{
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
}),
Output: addrProprty{
IP: []byte{
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
byte(1), byte(2), byte(3), byte(4),
},
Family: AddressFamilyIPv6,
String: "[102:304:102:304:102:304:102:304]",
},
},
{
Input: IPAddress([]byte{
byte(0), byte(0), byte(0), byte(0),
byte(0), byte(0), byte(0), byte(0),
byte(0), byte(0), byte(255), byte(255),
byte(1), byte(2), byte(3), byte(4),
}),
Output: addrProprty{
IP: []byte{byte(1), byte(2), byte(3), byte(4)},
Family: AddressFamilyIPv4,
String: "1.2.3.4",
},
},
{
Input: DomainAddress("v2ray.com"),
Output: addrProprty{
Domain: "v2ray.com",
Family: AddressFamilyDomain,
String: "v2ray.com",
},
},
{
Input: IPAddress(net.IPv4(1, 2, 3, 4)),
Output: addrProprty{
IP: []byte{byte(1), byte(2), byte(3), byte(4)},
Family: AddressFamilyIPv4,
String: "1.2.3.4",
},
},
{
Input: ParseAddress("[2001:4860:0:2001::68]"),
Output: addrProprty{
IP: []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
Family: AddressFamilyIPv6,
String: "[2001:4860:0:2001::68]",
},
},
2019-01-19 04:59:37 -05:00
{
Input: ParseAddress("::0"),
Output: addrProprty{
IP: AnyIPv6.IP(),
Family: AddressFamilyIPv6,
String: "[::]",
},
},
2019-01-07 17:27:59 -05:00
{
Input: ParseAddress("[::ffff:123.151.71.143]"),
Output: addrProprty{
IP: []byte{123, 151, 71, 143},
Family: AddressFamilyIPv4,
String: "123.151.71.143",
},
},
{
Input: NewIPOrDomain(ParseAddress("v2ray.com")).AsAddress(),
Output: addrProprty{
Domain: "v2ray.com",
Family: AddressFamilyDomain,
String: "v2ray.com",
},
},
{
Input: NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(),
Output: addrProprty{
IP: []byte{8, 8, 8, 8},
Family: AddressFamilyIPv4,
String: "8.8.8.8",
},
},
{
Input: NewIPOrDomain(ParseAddress("[2001:4860:0:2001::68]")).AsAddress(),
Output: addrProprty{
IP: []byte{0x20, 0x01, 0x48, 0x60, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68},
Family: AddressFamilyIPv6,
String: "[2001:4860:0:2001::68]",
},
},
}
2015-10-14 02:43:04 -04:00
2019-01-07 17:27:59 -05:00
for _, testCase := range testCases {
actual := addrProprty{
Family: testCase.Input.Family(),
String: testCase.Input.String(),
}
if testCase.Input.Family().IsIP() {
actual.IP = testCase.Input.IP()
} else {
actual.Domain = testCase.Input.Domain()
}
2017-11-21 12:46:28 -05:00
2019-01-07 17:27:59 -05:00
if r := cmp.Diff(actual, testCase.Output); r != "" {
t.Error("for input: ", testCase.Input, ":", r)
}
}
2017-11-21 12:46:28 -05:00
}
2017-11-23 08:48:43 -05:00
func TestInvalidAddressConvertion(t *testing.T) {
2019-01-07 17:27:59 -05:00
panics := func(f func()) (ret bool) {
defer func() {
if r := recover(); r != nil {
ret = true
}
}()
f()
return false
}
2017-11-23 08:48:43 -05:00
2019-01-07 17:27:59 -05:00
testCases := []func(){
func() { ParseAddress("8.8.8.8").Domain() },
func() { ParseAddress("2001:4860:0:2001::68").Domain() },
func() { ParseAddress("v2ray.com").IP() },
}
for idx, testCase := range testCases {
if !panics(testCase) {
t.Error("case ", idx, " failed")
}
}
2017-11-23 08:48:43 -05:00
}
2018-11-04 19:16:06 -05:00
func BenchmarkParseAddressIPv4(b *testing.B) {
for i := 0; i < b.N; i++ {
2018-11-19 05:18:20 -05:00
addr := ParseAddress("8.8.8.8")
if addr.Family() != AddressFamilyIPv4 {
panic("not ipv4")
}
2018-11-04 19:16:06 -05:00
}
}
func BenchmarkParseAddressIPv6(b *testing.B) {
for i := 0; i < b.N; i++ {
2018-11-19 05:18:20 -05:00
addr := ParseAddress("2001:4860:0:2001::68")
if addr.Family() != AddressFamilyIPv6 {
panic("not ipv6")
}
2018-11-04 19:16:06 -05:00
}
}
func BenchmarkParseAddressDomain(b *testing.B) {
for i := 0; i < b.N; i++ {
2018-11-19 05:18:20 -05:00
addr := ParseAddress("v2ray.com")
if addr.Family() != AddressFamilyDomain {
panic("not domain")
}
2018-11-04 19:16:06 -05:00
}
}