2015-09-16 16:19:42 -04:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2015-10-14 02:43:04 -04:00
|
|
|
"net"
|
2015-09-16 16:19:42 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/testing/unit"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIPv4Address(t *testing.T) {
|
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
|
|
|
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
|
|
|
port := uint16(80)
|
|
|
|
addr := IPAddress(ip, port)
|
|
|
|
|
|
|
|
assert.Bool(addr.IsIPv4()).IsTrue()
|
2015-10-10 17:20:47 -04:00
|
|
|
assert.Bool(addr.IsIPv6()).IsFalse()
|
|
|
|
assert.Bool(addr.IsDomain()).IsFalse()
|
2015-09-20 10:55:45 -04:00
|
|
|
assert.Bytes(addr.IP()).Equals(ip)
|
|
|
|
assert.Uint16(addr.Port()).Equals(port)
|
2015-09-16 16:24:04 -04:00
|
|
|
assert.String(addr.String()).Equals("1.2.3.4:80")
|
2015-09-16 16:19:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPv6Address(t *testing.T) {
|
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
port := uint16(443)
|
|
|
|
addr := IPAddress(ip, port)
|
|
|
|
|
|
|
|
assert.Bool(addr.IsIPv6()).IsTrue()
|
2015-10-10 17:20:47 -04:00
|
|
|
assert.Bool(addr.IsIPv4()).IsFalse()
|
|
|
|
assert.Bool(addr.IsDomain()).IsFalse()
|
2015-09-20 10:55:45 -04:00
|
|
|
assert.Bytes(addr.IP()).Equals(ip)
|
|
|
|
assert.Uint16(addr.Port()).Equals(port)
|
2015-09-16 16:24:04 -04:00
|
|
|
assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443")
|
2015-09-16 16:19:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDomainAddress(t *testing.T) {
|
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
|
|
|
domain := "v2ray.com"
|
|
|
|
port := uint16(443)
|
|
|
|
addr := DomainAddress(domain, port)
|
|
|
|
|
|
|
|
assert.Bool(addr.IsDomain()).IsTrue()
|
2015-10-10 17:20:47 -04:00
|
|
|
assert.Bool(addr.IsIPv4()).IsFalse()
|
|
|
|
assert.Bool(addr.IsIPv6()).IsFalse()
|
2015-09-20 10:55:45 -04:00
|
|
|
assert.String(addr.Domain()).Equals(domain)
|
|
|
|
assert.Uint16(addr.Port()).Equals(port)
|
2015-09-16 16:24:04 -04:00
|
|
|
assert.String(addr.String()).Equals("v2ray.com:443")
|
2015-09-16 16:19:42 -04:00
|
|
|
}
|
2015-10-14 02:43:04 -04:00
|
|
|
|
|
|
|
func TestNetIPv4Address(t *testing.T) {
|
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
|
|
|
ip := net.IPv4(1, 2, 3, 4)
|
|
|
|
port := uint16(80)
|
|
|
|
addr := IPAddress(ip, port)
|
|
|
|
assert.Bool(addr.IsIPv4()).IsTrue()
|
|
|
|
assert.String(addr.String()).Equals("1.2.3.4:80")
|
|
|
|
}
|