1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/common/net/address_test.go

123 lines
3.1 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"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
2015-09-16 16:19:42 -04:00
)
func TestIPv4Address(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-16 16:19:42 -04:00
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
2015-12-16 17:53:38 -05:00
addr := v2net.IPAddress(ip)
2015-09-16 16:19:42 -04:00
2016-05-24 09:29:08 -04:00
assert.Address(addr).IsIPv4()
assert.Address(addr).IsNotIPv6()
assert.Address(addr).IsNotDomain()
assert.Bytes(addr.IP()).Equals(ip)
2016-05-24 15:55:46 -04:00
assert.Address(addr).EqualsString("1.2.3.4")
2015-09-16 16:19:42 -04:00
}
func TestIPv6Address(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(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),
}
2015-12-16 17:53:38 -05:00
addr := v2net.IPAddress(ip)
2015-09-16 16:19:42 -04:00
2016-05-24 09:29:08 -04:00
assert.Address(addr).IsIPv6()
assert.Address(addr).IsNotIPv4()
assert.Address(addr).IsNotDomain()
2016-05-24 15:55:46 -04:00
assert.IP(addr.IP()).Equals(net.IP(ip))
assert.Address(addr).EqualsString("[102:304:102:304:102:304:102:304]")
2015-09-16 16:19:42 -04:00
}
func TestIPv4Asv6(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(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),
}
addr := v2net.IPAddress(ip)
2016-05-24 15:55:46 -04:00
assert.Address(addr).EqualsString("1.2.3.4")
}
2015-09-16 16:19:42 -04:00
func TestDomainAddress(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-09-16 16:19:42 -04:00
domain := "v2ray.com"
2015-12-16 17:53:38 -05:00
addr := v2net.DomainAddress(domain)
2015-09-16 16:19:42 -04:00
2016-05-24 09:29:08 -04:00
assert.Address(addr).IsDomain()
assert.Address(addr).IsNotIPv6()
assert.Address(addr).IsNotIPv4()
2016-05-24 15:55:46 -04:00
assert.String(addr.Domain()).Equals(domain)
assert.Address(addr).EqualsString("v2ray.com")
2015-09-16 16:19:42 -04:00
}
2015-10-14 02:43:04 -04:00
func TestNetIPv4Address(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-10-14 02:43:04 -04:00
ip := net.IPv4(1, 2, 3, 4)
2015-12-16 17:53:38 -05:00
addr := v2net.IPAddress(ip)
2016-05-24 09:29:08 -04:00
assert.Address(addr).IsIPv4()
2016-05-24 15:55:46 -04:00
assert.Address(addr).EqualsString("1.2.3.4")
2015-10-14 02:43:04 -04:00
}
2016-01-21 07:05:28 -05:00
func TestIPv4AddressEquals(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-21 07:05:28 -05:00
addr := v2net.IPAddress([]byte{1, 2, 3, 4})
2016-05-24 15:55:46 -04:00
assert.Address(addr).NotEquals(nil)
2016-01-21 07:05:28 -05:00
addr2 := v2net.IPAddress([]byte{1, 2, 3, 4})
2016-05-24 15:55:46 -04:00
assert.Address(addr).Equals(addr2)
2016-01-21 07:05:28 -05:00
addr3 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
2016-05-24 15:55:46 -04:00
assert.Address(addr).NotEquals(addr3)
2016-01-21 07:05:28 -05:00
addr4 := v2net.IPAddress([]byte{1, 2, 3, 5})
2016-05-24 15:55:46 -04:00
assert.Address(addr).NotEquals(addr4)
2016-01-21 07:05:28 -05:00
}
func TestIPv6AddressEquals(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-21 07:05:28 -05:00
addr := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
assert.Bool(addr.Equals(nil)).IsFalse()
addr2 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
assert.Bool(addr.Equals(addr2)).IsTrue()
addr3 := v2net.IPAddress([]byte{1, 2, 3, 4})
assert.Bool(addr.Equals(addr3)).IsFalse()
addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
assert.Bool(addr.Equals(addr4)).IsFalse()
}
func TestDomainAddressEquals(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2016-01-21 07:05:28 -05:00
addr := v2net.DomainAddress("v2ray.com")
assert.Bool(addr.Equals(nil)).IsFalse()
addr2 := v2net.DomainAddress("v2ray.com")
assert.Bool(addr.Equals(addr2)).IsTrue()
addr3 := v2net.DomainAddress("www.v2ray.com")
assert.Bool(addr.Equals(addr3)).IsFalse()
addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6})
assert.Bool(addr.Equals(addr4)).IsFalse()
}