mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
add assertions for net utilities
This commit is contained in:
parent
0a2e4343bc
commit
091f047ebb
61
common/net/testing/assert/address.go
Normal file
61
common/net/testing/assert/address.go
Normal file
@ -0,0 +1,61 @@
|
||||
package unit
|
||||
|
||||
import (
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
|
||||
func Address(value v2net.Address) *AddressSubject {
|
||||
return &AddressSubject{value: value}
|
||||
}
|
||||
|
||||
type AddressSubject struct {
|
||||
*assert.Subject
|
||||
value v2net.Address
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) Named(name string) *AddressSubject {
|
||||
subject.Subject.Named(name)
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) DisplayString() string {
|
||||
return subject.Subject.DisplayString(subject.value.String())
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsIPv4() {
|
||||
if !subject.value.IsIPv4() {
|
||||
subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("an IPv4 address"))
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsNotIPv4() {
|
||||
if subject.value.IsIPv4() {
|
||||
subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("an IPv4 address"))
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsIPv6() {
|
||||
if !subject.value.IsIPv6() {
|
||||
subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("an IPv6 address"))
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsNotIPv6() {
|
||||
if subject.value.IsIPv6() {
|
||||
subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("an IPv6 address"))
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsDomain() {
|
||||
if !subject.value.IsDomain() {
|
||||
subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("a domain address"))
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *AddressSubject) IsNotDomain() {
|
||||
if subject.value.IsDomain() {
|
||||
subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("a domain address"))
|
||||
}
|
||||
}
|
@ -19,28 +19,24 @@ func (subject *PortSubject) Named(name string) *PortSubject {
|
||||
return subject
|
||||
}
|
||||
|
||||
func (subject *PortSubject) Fail(verb string, other v2net.Port) {
|
||||
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.String() + ">.")
|
||||
}
|
||||
|
||||
func (subject *PortSubject) DisplayString() string {
|
||||
return subject.Subject.DisplayString(subject.value.String())
|
||||
}
|
||||
|
||||
func (subject *PortSubject) Equals(expectation v2net.Port) {
|
||||
if subject.value.Value() != expectation.Value() {
|
||||
subject.Fail("is equal to", expectation)
|
||||
subject.Fail(subject.DisplayString(), "is equal to", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *PortSubject) GreaterThan(expectation v2net.Port) {
|
||||
if subject.value.Value() <= expectation.Value() {
|
||||
subject.Fail("is greater than", expectation)
|
||||
subject.Fail(subject.DisplayString(), "is greater than", expectation)
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *PortSubject) LessThan(expectation v2net.Port) {
|
||||
if subject.value.Value() >= expectation.Value() {
|
||||
subject.Fail("is less than", expectation)
|
||||
subject.Fail(subject.DisplayString(), "is less than", expectation)
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
package net
|
||||
package unit
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@ -12,14 +14,14 @@ func TestIPv4Address(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
||||
port := NewPort(80)
|
||||
addr := IPAddress(ip, port.Value())
|
||||
port := v2net.NewPort(80)
|
||||
addr := v2net.IPAddress(ip, port.Value())
|
||||
|
||||
assert.Bool(addr.IsIPv4()).IsTrue()
|
||||
assert.Bool(addr.IsIPv6()).IsFalse()
|
||||
assert.Bool(addr.IsDomain()).IsFalse()
|
||||
v2netassert.Address(addr).IsIPv4()
|
||||
v2netassert.Address(addr).IsNotIPv6()
|
||||
v2netassert.Address(addr).IsNotDomain()
|
||||
assert.Bytes(addr.IP()).Equals(ip)
|
||||
assert.Uint16(addr.Port().Value()).Equals(port.Value())
|
||||
v2netassert.Port(addr.Port()).Equals(port)
|
||||
assert.String(addr.String()).Equals("1.2.3.4:80")
|
||||
}
|
||||
|
||||
@ -32,12 +34,12 @@ func TestIPv6Address(t *testing.T) {
|
||||
byte(1), byte(2), byte(3), byte(4),
|
||||
byte(1), byte(2), byte(3), byte(4),
|
||||
}
|
||||
port := NewPort(443)
|
||||
addr := IPAddress(ip, port.Value())
|
||||
port := v2net.NewPort(443)
|
||||
addr := v2net.IPAddress(ip, port.Value())
|
||||
|
||||
assert.Bool(addr.IsIPv6()).IsTrue()
|
||||
assert.Bool(addr.IsIPv4()).IsFalse()
|
||||
assert.Bool(addr.IsDomain()).IsFalse()
|
||||
v2netassert.Address(addr).IsIPv6()
|
||||
v2netassert.Address(addr).IsNotIPv4()
|
||||
v2netassert.Address(addr).IsNotDomain()
|
||||
assert.Bytes(addr.IP()).Equals(ip)
|
||||
assert.Uint16(addr.Port().Value()).Equals(port.Value())
|
||||
assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443")
|
||||
@ -47,12 +49,12 @@ func TestDomainAddress(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
domain := "v2ray.com"
|
||||
port := NewPort(443)
|
||||
addr := DomainAddress(domain, port.Value())
|
||||
port := v2net.NewPort(443)
|
||||
addr := v2net.DomainAddress(domain, port.Value())
|
||||
|
||||
assert.Bool(addr.IsDomain()).IsTrue()
|
||||
assert.Bool(addr.IsIPv4()).IsFalse()
|
||||
assert.Bool(addr.IsIPv6()).IsFalse()
|
||||
v2netassert.Address(addr).IsDomain()
|
||||
v2netassert.Address(addr).IsNotIPv6()
|
||||
v2netassert.Address(addr).IsNotIPv4()
|
||||
assert.String(addr.Domain()).Equals(domain)
|
||||
assert.Uint16(addr.Port().Value()).Equals(port.Value())
|
||||
assert.String(addr.String()).Equals("v2ray.com:443")
|
||||
@ -62,8 +64,8 @@ func TestNetIPv4Address(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
ip := net.IPv4(1, 2, 3, 4)
|
||||
port := NewPort(80)
|
||||
addr := IPAddress(ip, port.Value())
|
||||
assert.Bool(addr.IsIPv4()).IsTrue()
|
||||
port := v2net.NewPort(80)
|
||||
addr := v2net.IPAddress(ip, port.Value())
|
||||
v2netassert.Address(addr).IsIPv4()
|
||||
assert.String(addr.String()).Equals("1.2.3.4:80")
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package net
|
||||
package unit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@ -10,7 +11,7 @@ import (
|
||||
func TestTCPDestination(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
dest := NewTCPDestination(IPAddress([]byte{1, 2, 3, 4}, 80))
|
||||
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
|
||||
assert.Bool(dest.IsTCP()).IsTrue()
|
||||
assert.Bool(dest.IsUDP()).IsFalse()
|
||||
assert.String(dest.String()).Equals("tcp:1.2.3.4:80")
|
||||
@ -19,7 +20,7 @@ func TestTCPDestination(t *testing.T) {
|
||||
func TestUDPDestination(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
dest := NewUDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 53))
|
||||
dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 53))
|
||||
assert.Bool(dest.IsTCP()).IsFalse()
|
||||
assert.Bool(dest.IsUDP()).IsTrue()
|
||||
assert.String(dest.String()).Equals("udp:[2001:4860:4860::8888]:53")
|
@ -1,4 +1,4 @@
|
||||
package net
|
||||
package unit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@ -26,11 +27,11 @@ func TestReaderAndWrite(t *testing.T) {
|
||||
|
||||
transportChan := make(chan *alloc.Buffer, 1024)
|
||||
|
||||
err = ReaderToChan(transportChan, readerBuffer)
|
||||
err = v2net.ReaderToChan(transportChan, readerBuffer)
|
||||
assert.Error(err).Equals(io.EOF)
|
||||
close(transportChan)
|
||||
|
||||
err = ChanToWriter(writerBuffer, transportChan)
|
||||
err = v2net.ChanToWriter(writerBuffer, transportChan)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Bytes(buffer).Equals(writerBuffer.Bytes())
|
||||
@ -128,22 +129,22 @@ func runBenchmarkTransport(size int) {
|
||||
finishB := make(chan bool)
|
||||
|
||||
go func() {
|
||||
ChanToWriter(writerA, transportChanA)
|
||||
v2net.ChanToWriter(writerA, transportChanA)
|
||||
close(finishA)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
ReaderToChan(transportChanA, readerA)
|
||||
v2net.ReaderToChan(transportChanA, readerA)
|
||||
close(transportChanA)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
ChanToWriter(writerB, transportChanB)
|
||||
v2net.ChanToWriter(writerB, transportChanB)
|
||||
close(finishB)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
ReaderToChan(transportChanB, readerB)
|
||||
v2net.ReaderToChan(transportChanB, readerB)
|
||||
close(transportChanB)
|
||||
}()
|
||||
|
11
common/serial/string.go
Normal file
11
common/serial/string.go
Normal file
@ -0,0 +1,11 @@
|
||||
package serial
|
||||
|
||||
type String interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type StringLiteral string
|
||||
|
||||
func (this StringLiteral) String() string {
|
||||
return string(this)
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/common/serial"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
)
|
||||
|
||||
@ -14,6 +15,10 @@ func NewSubject() *Subject {
|
||||
}
|
||||
}
|
||||
|
||||
func (subject *Subject) Fail(displayString string, verb string, other serial.String) {
|
||||
subject.FailWithMessage("Not true that " + displayString + " " + verb + " <" + other.String() + ">.")
|
||||
}
|
||||
|
||||
func (subject *Subject) FailWithMessage(message string) {
|
||||
v2testing.Fail(message)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user