v2ray-core/common/net/address_test.go

75 lines
1.7 KiB
Go
Raw Normal View History

2015-12-02 16:18:12 +00:00
package net_test
2015-09-16 20:19:42 +00:00
import (
2015-10-14 06:43:04 +00:00
"net"
2015-09-16 20:19:42 +00:00
"testing"
2017-02-16 20:08:10 +00:00
. "v2ray.com/core/common/net"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/testing/assert"
2015-09-16 20:19:42 +00:00
)
func TestIPv4Address(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2015-09-16 20:19:42 +00:00
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
2017-02-16 20:08:10 +00:00
addr := IPAddress(ip)
2015-09-16 20:19:42 +00:00
2016-05-24 13:29:08 +00:00
assert.Address(addr).IsIPv4()
assert.Address(addr).IsNotIPv6()
assert.Address(addr).IsNotDomain()
assert.Bytes(addr.IP()).Equals(ip)
2016-05-24 19:55:46 +00:00
assert.Address(addr).EqualsString("1.2.3.4")
2015-09-16 20:19:42 +00:00
}
func TestIPv6Address(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2015-09-16 20:19:42 +00: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 20:08:10 +00:00
addr := IPAddress(ip)
2015-09-16 20:19:42 +00:00
2016-05-24 13:29:08 +00:00
assert.Address(addr).IsIPv6()
assert.Address(addr).IsNotIPv4()
assert.Address(addr).IsNotDomain()
2016-05-24 19:55:46 +00:00
assert.IP(addr.IP()).Equals(net.IP(ip))
assert.Address(addr).EqualsString("[102:304:102:304:102:304:102:304]")
2015-09-16 20:19:42 +00:00
}
func TestIPv4Asv6(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-21 12:05:28 +00: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 20:08:10 +00:00
addr := IPAddress(ip)
2016-05-24 19:55:46 +00:00
assert.Address(addr).EqualsString("1.2.3.4")
}
2015-09-16 20:19:42 +00:00
func TestDomainAddress(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2015-09-16 20:19:42 +00:00
domain := "v2ray.com"
2017-02-16 20:08:10 +00:00
addr := DomainAddress(domain)
2015-09-16 20:19:42 +00:00
2016-05-24 13:29:08 +00:00
assert.Address(addr).IsDomain()
assert.Address(addr).IsNotIPv6()
assert.Address(addr).IsNotIPv4()
2016-05-24 19:55:46 +00:00
assert.String(addr.Domain()).Equals(domain)
assert.Address(addr).EqualsString("v2ray.com")
2015-09-16 20:19:42 +00:00
}
2015-10-14 06:43:04 +00:00
func TestNetIPv4Address(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2015-10-14 06:43:04 +00:00
ip := net.IPv4(1, 2, 3, 4)
2017-02-16 20:08:10 +00:00
addr := IPAddress(ip)
2016-05-24 13:29:08 +00:00
assert.Address(addr).IsIPv4()
2016-05-24 19:55:46 +00:00
assert.Address(addr).EqualsString("1.2.3.4")
2015-10-14 06:43:04 +00:00
}