mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-05 00:47:51 -05:00
net assert
This commit is contained in:
parent
34ea53e5b7
commit
6ddafdbd73
@ -5,20 +5,22 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
. "v2ray.com/core/common/net"
|
. "v2ray.com/core/common/net"
|
||||||
|
. "v2ray.com/core/common/net/testing"
|
||||||
"v2ray.com/core/testing/assert"
|
"v2ray.com/core/testing/assert"
|
||||||
|
. "v2ray.com/ext/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIPv4Address(t *testing.T) {
|
func TestIPv4Address(t *testing.T) {
|
||||||
assert := assert.On(t)
|
assert := With(t)
|
||||||
|
|
||||||
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
||||||
addr := IPAddress(ip)
|
addr := IPAddress(ip)
|
||||||
|
|
||||||
assert.Address(addr).IsIPv4()
|
assert(addr, IsIPv4)
|
||||||
assert.Address(addr).IsNotIPv6()
|
assert(addr, Not(IsIPv6))
|
||||||
assert.Address(addr).IsNotDomain()
|
assert(addr, Not(IsDomain))
|
||||||
assert.Bytes(addr.IP()).Equals(ip)
|
assert([]byte(addr.IP()), Equals, ip)
|
||||||
assert.Address(addr).EqualsString("1.2.3.4")
|
assert(addr.String(), Equals, "1.2.3.4")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIPv6Address(t *testing.T) {
|
func TestIPv6Address(t *testing.T) {
|
||||||
|
46
common/net/testing/assert.go
Normal file
46
common/net/testing/assert.go
Normal 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
|
||||||
|
}))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user