1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/common/net/address_test.go

104 lines
2.5 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"
2017-02-16 15:08:10 -05:00
. "v2ray.com/core/common/net"
2017-10-23 17:47:20 -04:00
. "v2ray.com/core/common/net/testing"
. "v2ray.com/ext/assert"
2015-09-16 16:19:42 -04:00
)
func TestIPv4Address(t *testing.T) {
2017-10-23 17:47:20 -04:00
assert := With(t)
2015-09-16 16:19:42 -04:00
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
2017-02-16 15:08:10 -05:00
addr := IPAddress(ip)
2015-09-16 16:19:42 -04:00
2017-10-23 17:47:20 -04:00
assert(addr, IsIPv4)
assert(addr, Not(IsIPv6))
assert(addr, Not(IsDomain))
assert([]byte(addr.IP()), Equals, ip)
assert(addr.String(), Equals, "1.2.3.4")
2015-09-16 16:19:42 -04:00
}
func TestIPv6Address(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2015-09-16 16:19:42 -04:00
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),
}
2017-02-16 15:08:10 -05:00
addr := IPAddress(ip)
2015-09-16 16:19:42 -04:00
2017-10-24 10:15:35 -04:00
assert(addr, IsIPv6)
assert(addr, Not(IsIPv4))
assert(addr, Not(IsDomain))
assert(addr.IP(), Equals, net.IP(ip))
assert(addr.String(), Equals, "[102:304:102:304:102:304:102:304]")
2015-09-16 16:19:42 -04:00
}
func TestIPv4Asv6(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-01-21 07:05:28 -05:00
ip := []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),
}
2017-02-16 15:08:10 -05:00
addr := IPAddress(ip)
2017-10-24 10:15:35 -04:00
assert(addr.String(), Equals, "1.2.3.4")
}
2015-09-16 16:19:42 -04:00
func TestDomainAddress(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2015-09-16 16:19:42 -04:00
domain := "v2ray.com"
2017-02-16 15:08:10 -05:00
addr := DomainAddress(domain)
2015-09-16 16:19:42 -04:00
2017-10-24 10:15:35 -04:00
assert(addr, IsDomain)
assert(addr, Not(IsIPv6))
assert(addr, Not(IsIPv4))
assert(addr.Domain(), Equals, domain)
assert(addr.String(), Equals, "v2ray.com")
2015-09-16 16:19:42 -04:00
}
2015-10-14 02:43:04 -04:00
func TestNetIPv4Address(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2015-10-14 02:43:04 -04:00
ip := net.IPv4(1, 2, 3, 4)
2017-02-16 15:08:10 -05:00
addr := IPAddress(ip)
2017-10-24 10:15:35 -04:00
assert(addr, IsIPv4)
assert(addr.String(), Equals, "1.2.3.4")
2015-10-14 02:43:04 -04:00
}
2017-11-21 12:46:28 -05:00
func TestParseIPv6Address(t *testing.T) {
assert := With(t)
ip := ParseAddress("[2001:4860:0:2001::68]")
assert(ip, IsIPv6)
assert(ip.String(), Equals, "[2001:4860:0:2001::68]")
ip = ParseAddress("[::ffff:123.151.71.143]")
assert(ip, IsIPv4)
assert(ip.String(), Equals, "123.151.71.143")
}
2017-11-23 08:48:43 -05:00
func TestInvalidAddressConvertion(t *testing.T) {
assert := With(t)
assert(func() { ParseAddress("8.8.8.8").Domain() }, Panics)
assert(func() { ParseAddress("2001:4860:0:2001::68").Domain() }, Panics)
assert(func() { ParseAddress("v2ray.com").IP() }, Panics)
}
func TestIPOrDomain(t *testing.T) {
assert := With(t)
assert(NewIPOrDomain(ParseAddress("v2ray.com")).AsAddress(), Equals, ParseAddress("v2ray.com"))
assert(NewIPOrDomain(ParseAddress("8.8.8.8")).AsAddress(), Equals, ParseAddress("8.8.8.8"))
assert(NewIPOrDomain(ParseAddress("2001:4860:0:2001::68")).AsAddress(), Equals, ParseAddress("2001:4860:0:2001::68"))
}