mirror of https://github.com/v2ray/v2ray-core
				
				
				
			add assertions for net utilities
							parent
							
								
									0a2e4343bc
								
							
						
					
					
						commit
						091f047ebb
					
				|  | @ -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 | 	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 { | func (subject *PortSubject) DisplayString() string { | ||||||
| 	return subject.Subject.DisplayString(subject.value.String()) | 	return subject.Subject.DisplayString(subject.value.String()) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (subject *PortSubject) Equals(expectation v2net.Port) { | func (subject *PortSubject) Equals(expectation v2net.Port) { | ||||||
| 	if subject.value.Value() != expectation.Value() { | 	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) { | func (subject *PortSubject) GreaterThan(expectation v2net.Port) { | ||||||
| 	if subject.value.Value() <= expectation.Value() { | 	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) { | func (subject *PortSubject) LessThan(expectation v2net.Port) { | ||||||
| 	if subject.value.Value() >= expectation.Value() { | 	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 ( | import ( | ||||||
| 	"net" | 	"net" | ||||||
| 	"testing" | 	"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" | 	v2testing "github.com/v2ray/v2ray-core/testing" | ||||||
| 	"github.com/v2ray/v2ray-core/testing/assert" | 	"github.com/v2ray/v2ray-core/testing/assert" | ||||||
| ) | ) | ||||||
|  | @ -12,14 +14,14 @@ func TestIPv4Address(t *testing.T) { | ||||||
| 	v2testing.Current(t) | 	v2testing.Current(t) | ||||||
| 
 | 
 | ||||||
| 	ip := []byte{byte(1), byte(2), byte(3), byte(4)} | 	ip := []byte{byte(1), byte(2), byte(3), byte(4)} | ||||||
| 	port := NewPort(80) | 	port := v2net.NewPort(80) | ||||||
| 	addr := IPAddress(ip, port.Value()) | 	addr := v2net.IPAddress(ip, port.Value()) | ||||||
| 
 | 
 | ||||||
| 	assert.Bool(addr.IsIPv4()).IsTrue() | 	v2netassert.Address(addr).IsIPv4() | ||||||
| 	assert.Bool(addr.IsIPv6()).IsFalse() | 	v2netassert.Address(addr).IsNotIPv6() | ||||||
| 	assert.Bool(addr.IsDomain()).IsFalse() | 	v2netassert.Address(addr).IsNotDomain() | ||||||
| 	assert.Bytes(addr.IP()).Equals(ip) | 	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") | 	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), | ||||||
| 		byte(1), byte(2), byte(3), byte(4), | 		byte(1), byte(2), byte(3), byte(4), | ||||||
| 	} | 	} | ||||||
| 	port := NewPort(443) | 	port := v2net.NewPort(443) | ||||||
| 	addr := IPAddress(ip, port.Value()) | 	addr := v2net.IPAddress(ip, port.Value()) | ||||||
| 
 | 
 | ||||||
| 	assert.Bool(addr.IsIPv6()).IsTrue() | 	v2netassert.Address(addr).IsIPv6() | ||||||
| 	assert.Bool(addr.IsIPv4()).IsFalse() | 	v2netassert.Address(addr).IsNotIPv4() | ||||||
| 	assert.Bool(addr.IsDomain()).IsFalse() | 	v2netassert.Address(addr).IsNotDomain() | ||||||
| 	assert.Bytes(addr.IP()).Equals(ip) | 	assert.Bytes(addr.IP()).Equals(ip) | ||||||
| 	assert.Uint16(addr.Port().Value()).Equals(port.Value()) | 	assert.Uint16(addr.Port().Value()).Equals(port.Value()) | ||||||
| 	assert.String(addr.String()).Equals("[102:304:102:304:102:304:102:304]:443") | 	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) | 	v2testing.Current(t) | ||||||
| 
 | 
 | ||||||
| 	domain := "v2ray.com" | 	domain := "v2ray.com" | ||||||
| 	port := NewPort(443) | 	port := v2net.NewPort(443) | ||||||
| 	addr := DomainAddress(domain, port.Value()) | 	addr := v2net.DomainAddress(domain, port.Value()) | ||||||
| 
 | 
 | ||||||
| 	assert.Bool(addr.IsDomain()).IsTrue() | 	v2netassert.Address(addr).IsDomain() | ||||||
| 	assert.Bool(addr.IsIPv4()).IsFalse() | 	v2netassert.Address(addr).IsNotIPv6() | ||||||
| 	assert.Bool(addr.IsIPv6()).IsFalse() | 	v2netassert.Address(addr).IsNotIPv4() | ||||||
| 	assert.String(addr.Domain()).Equals(domain) | 	assert.String(addr.Domain()).Equals(domain) | ||||||
| 	assert.Uint16(addr.Port().Value()).Equals(port.Value()) | 	assert.Uint16(addr.Port().Value()).Equals(port.Value()) | ||||||
| 	assert.String(addr.String()).Equals("v2ray.com:443") | 	assert.String(addr.String()).Equals("v2ray.com:443") | ||||||
|  | @ -62,8 +64,8 @@ func TestNetIPv4Address(t *testing.T) { | ||||||
| 	v2testing.Current(t) | 	v2testing.Current(t) | ||||||
| 
 | 
 | ||||||
| 	ip := net.IPv4(1, 2, 3, 4) | 	ip := net.IPv4(1, 2, 3, 4) | ||||||
| 	port := NewPort(80) | 	port := v2net.NewPort(80) | ||||||
| 	addr := IPAddress(ip, port.Value()) | 	addr := v2net.IPAddress(ip, port.Value()) | ||||||
| 	assert.Bool(addr.IsIPv4()).IsTrue() | 	v2netassert.Address(addr).IsIPv4() | ||||||
| 	assert.String(addr.String()).Equals("1.2.3.4:80") | 	assert.String(addr.String()).Equals("1.2.3.4:80") | ||||||
| } | } | ||||||
|  | @ -1,8 +1,9 @@ | ||||||
| package net | package unit | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
|  | 	v2net "github.com/v2ray/v2ray-core/common/net" | ||||||
| 	v2testing "github.com/v2ray/v2ray-core/testing" | 	v2testing "github.com/v2ray/v2ray-core/testing" | ||||||
| 	"github.com/v2ray/v2ray-core/testing/assert" | 	"github.com/v2ray/v2ray-core/testing/assert" | ||||||
| ) | ) | ||||||
|  | @ -10,7 +11,7 @@ import ( | ||||||
| func TestTCPDestination(t *testing.T) { | func TestTCPDestination(t *testing.T) { | ||||||
| 	v2testing.Current(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.IsTCP()).IsTrue() | ||||||
| 	assert.Bool(dest.IsUDP()).IsFalse() | 	assert.Bool(dest.IsUDP()).IsFalse() | ||||||
| 	assert.String(dest.String()).Equals("tcp:1.2.3.4:80") | 	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) { | func TestUDPDestination(t *testing.T) { | ||||||
| 	v2testing.Current(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.IsTCP()).IsFalse() | ||||||
| 	assert.Bool(dest.IsUDP()).IsTrue() | 	assert.Bool(dest.IsUDP()).IsTrue() | ||||||
| 	assert.String(dest.String()).Equals("udp:[2001:4860:4860::8888]:53") | 	assert.String(dest.String()).Equals("udp:[2001:4860:4860::8888]:53") | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| package net | package unit | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
|  | @ -8,6 +8,7 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/v2ray/v2ray-core/common/alloc" | 	"github.com/v2ray/v2ray-core/common/alloc" | ||||||
|  | 	v2net "github.com/v2ray/v2ray-core/common/net" | ||||||
| 	v2testing "github.com/v2ray/v2ray-core/testing" | 	v2testing "github.com/v2ray/v2ray-core/testing" | ||||||
| 	"github.com/v2ray/v2ray-core/testing/assert" | 	"github.com/v2ray/v2ray-core/testing/assert" | ||||||
| ) | ) | ||||||
|  | @ -26,11 +27,11 @@ func TestReaderAndWrite(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	transportChan := make(chan *alloc.Buffer, 1024) | 	transportChan := make(chan *alloc.Buffer, 1024) | ||||||
| 
 | 
 | ||||||
| 	err = ReaderToChan(transportChan, readerBuffer) | 	err = v2net.ReaderToChan(transportChan, readerBuffer) | ||||||
| 	assert.Error(err).Equals(io.EOF) | 	assert.Error(err).Equals(io.EOF) | ||||||
| 	close(transportChan) | 	close(transportChan) | ||||||
| 
 | 
 | ||||||
| 	err = ChanToWriter(writerBuffer, transportChan) | 	err = v2net.ChanToWriter(writerBuffer, transportChan) | ||||||
| 	assert.Error(err).IsNil() | 	assert.Error(err).IsNil() | ||||||
| 
 | 
 | ||||||
| 	assert.Bytes(buffer).Equals(writerBuffer.Bytes()) | 	assert.Bytes(buffer).Equals(writerBuffer.Bytes()) | ||||||
|  | @ -128,22 +129,22 @@ func runBenchmarkTransport(size int) { | ||||||
| 	finishB := make(chan bool) | 	finishB := make(chan bool) | ||||||
| 
 | 
 | ||||||
| 	go func() { | 	go func() { | ||||||
| 		ChanToWriter(writerA, transportChanA) | 		v2net.ChanToWriter(writerA, transportChanA) | ||||||
| 		close(finishA) | 		close(finishA) | ||||||
| 	}() | 	}() | ||||||
| 
 | 
 | ||||||
| 	go func() { | 	go func() { | ||||||
| 		ReaderToChan(transportChanA, readerA) | 		v2net.ReaderToChan(transportChanA, readerA) | ||||||
| 		close(transportChanA) | 		close(transportChanA) | ||||||
| 	}() | 	}() | ||||||
| 
 | 
 | ||||||
| 	go func() { | 	go func() { | ||||||
| 		ChanToWriter(writerB, transportChanB) | 		v2net.ChanToWriter(writerB, transportChanB) | ||||||
| 		close(finishB) | 		close(finishB) | ||||||
| 	}() | 	}() | ||||||
| 
 | 
 | ||||||
| 	go func() { | 	go func() { | ||||||
| 		ReaderToChan(transportChanB, readerB) | 		v2net.ReaderToChan(transportChanB, readerB) | ||||||
| 		close(transportChanB) | 		close(transportChanB) | ||||||
| 	}() | 	}() | ||||||
| 
 | 
 | ||||||
|  | @ -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 | package assert | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"github.com/v2ray/v2ray-core/common/serial" | ||||||
| 	v2testing "github.com/v2ray/v2ray-core/testing" | 	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) { | func (subject *Subject) FailWithMessage(message string) { | ||||||
| 	v2testing.Fail(message) | 	v2testing.Fail(message) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Darien Raymond
						Darien Raymond