1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

net assert

This commit is contained in:
Darien Raymond 2017-10-23 23:47:20 +02:00
parent 34ea53e5b7
commit 6ddafdbd73
2 changed files with 54 additions and 6 deletions

View File

@ -5,20 +5,22 @@ import (
"testing"
. "v2ray.com/core/common/net"
. "v2ray.com/core/common/net/testing"
"v2ray.com/core/testing/assert"
. "v2ray.com/ext/assert"
)
func TestIPv4Address(t *testing.T) {
assert := assert.On(t)
assert := With(t)
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
addr := IPAddress(ip)
assert.Address(addr).IsIPv4()
assert.Address(addr).IsNotIPv6()
assert.Address(addr).IsNotDomain()
assert.Bytes(addr.IP()).Equals(ip)
assert.Address(addr).EqualsString("1.2.3.4")
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")
}
func TestIPv6Address(t *testing.T) {

View File

@ -0,0 +1,46 @@
package testing
import (
"reflect"
"v2ray.com/core/common/net"
"v2ray.com/ext/assert"
)
var IsIPv4 = assert.CreateOperator(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool {
return a.Family().IsIPv4()
}), 1, "is IPv4")
var IsIPv6 = assert.CreateOperator(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool {
return a.Family().IsIPv6()
}), 1, "is IPv6")
var IsIP = assert.CreateOperator(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool {
return a.Family().IsIPv4() || a.Family().IsIPv6()
}), 1, "is IP")
var IsTCP = assert.CreateOperator(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a net.Destination) bool {
return a.Network == net.Network_TCP
}), 1, "is TCP")
var IsUDP = assert.CreateOperator(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a net.Destination) bool {
return a.Network == net.Network_UDP
}), 1, "is UDP")
var IsDomain = assert.CreateOperator(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool {
return a.Family().IsDomain()
}), 1, "is Domain")
func init() {
assert.RegisterEqualsOperator(reflect.TypeOf((*net.Address)(nil)).Elem(), reflect.ValueOf(func(a, b net.Address) bool {
return a == b
}))
assert.RegisterEqualsOperator(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a, b net.Destination) bool {
return a == b
}))
assert.RegisterEqualsOperator(reflect.TypeOf(net.Port(0)), reflect.ValueOf(func(a, b net.Port) bool {
return a == b
}))
}