1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

Unit test library to simplify test writing

This commit is contained in:
V2Ray 2015-09-09 14:51:26 +02:00
parent 526c3d3a9b
commit 08a96e5fe1
7 changed files with 266 additions and 33 deletions

View File

@ -3,9 +3,13 @@ package socks
import (
"bytes"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestAuthenticationRequestRead(t *testing.T) {
assert := unit.Assert(t)
rawRequest := []byte{
0x05, // version
0x01, // nMethods
@ -15,31 +19,26 @@ func TestAuthenticationRequestRead(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if request.version != 0x05 {
t.Errorf("Expected version 5, but got %d", request.version)
}
if request.nMethods != 0x01 {
t.Errorf("Expected nMethod 1, but got %d", request.nMethods)
}
if request.authMethods[0] != 0x02 {
t.Errorf("Expected method 2, but got %d", request.authMethods[0])
}
assert.Byte(request.version).Named("Version").Equals(0x05)
assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
}
func TestAuthenticationResponseToBytes(t *testing.T) {
assert := unit.Assert(t)
socksVersion := uint8(5)
authMethod := uint8(1)
response := Socks5AuthenticationResponse{socksVersion, authMethod}
bytes := response.ToBytes()
if bytes[0] != socksVersion {
t.Errorf("Unexpected Socks version %d", bytes[0])
}
if bytes[1] != authMethod {
t.Errorf("Unexpected Socks auth method %d", bytes[1])
}
assert.Byte(bytes[0]).Named("Version").Equals(socksVersion)
assert.Byte(bytes[1]).Named("Auth Method").Equals(authMethod)
}
func TestRequestRead(t *testing.T) {
assert := unit.Assert(t)
rawRequest := []byte{
0x05, // version
0x01, // cmd connect
@ -52,24 +51,16 @@ func TestRequestRead(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if request.Version != 0x05 {
t.Errorf("Expected version 5, but got %d", request.Version)
}
if request.Command != 0x01 {
t.Errorf("Expected command 1, but got %d", request.Command)
}
if request.AddrType != 0x01 {
t.Errorf("Expected addresstype 1, but got %d", request.AddrType)
}
if !bytes.Equal([]byte{0x72, 0x72, 0x72, 0x72}, request.IPv4[:]) {
t.Errorf("Expected IPv4 address 114.114.114.114, but got %v", request.IPv4[:])
}
if request.Port != 53 {
t.Errorf("Expected port 53, but got %d", request.Port)
}
assert.Byte(request.Version).Named("Version").Equals(0x05)
assert.Byte(request.Command).Named("Command").Equals(0x01)
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
assert.Uint16(request.Port).Named("Port").Equals(53)
}
func TestResponseToBytes(t *testing.T) {
assert := unit.Assert(t)
response := Socks5Response{
socksVersion,
ErrorSuccess,
@ -88,7 +79,5 @@ func TestResponseToBytes(t *testing.T) {
0x72, 0x72, 0x72, 0x72,
byte(0x00), byte(0x035),
}
if !bytes.Equal(rawResponse, expectedBytes) {
t.Errorf("Expected response %v, but got %v", expectedBytes, rawResponse)
}
assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes)
}

View File

@ -0,0 +1,31 @@
package unit
import (
"testing"
)
type Assertion struct {
t *testing.T
}
func Assert(t *testing.T) *Assertion {
assert := new(Assertion)
assert.t = t
return assert
}
func (a *Assertion) Int(value int) *IntSubject {
return NewIntSubject(NewSubject(a), value)
}
func (a *Assertion) Uint16(value uint16) *Uint16Subject {
return NewUint16Subject(NewSubject(a), value)
}
func (a *Assertion) Byte(value byte) *ByteSubject {
return NewByteSubject(NewSubject(a), value)
}
func (a *Assertion) Bytes(value []byte) *BytesSubject {
return NewBytesSubject(NewSubject(a), value)
}

View File

@ -0,0 +1,38 @@
package unit
import (
"bytes"
"fmt"
)
type BytesSubject struct {
*Subject
value []byte
}
func NewBytesSubject(base *Subject, value []byte) *BytesSubject {
subject := new(BytesSubject)
subject.Subject = base
subject.value = value
return subject
}
func (subject *BytesSubject) Named(name string) *BytesSubject {
subject.Subject.Named(name)
return subject
}
func (subject *BytesSubject) Fail(verb string, other []byte) {
otherString := fmt.Sprintf("%v", other)
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + otherString + ">.")
}
func (subject *BytesSubject) DisplayString() string {
return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value))
}
func (subject *BytesSubject) Equals(expectation []byte) {
if !bytes.Equal(subject.value, expectation) {
subject.Fail("is equal to", expectation)
}
}

View File

@ -0,0 +1,48 @@
package unit
import (
"strconv"
)
type ByteSubject struct {
*Subject
value byte
}
func NewByteSubject(base *Subject, value byte) *ByteSubject {
subject := new(ByteSubject)
subject.Subject = base
subject.value = value
return subject
}
func (subject *ByteSubject) Named(name string) *ByteSubject {
subject.Subject.Named(name)
return subject
}
func (subject *ByteSubject) Fail(verb string, other byte) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.")
}
func (subject *ByteSubject) DisplayString() string {
return subject.Subject.DisplayString(strconv.Itoa(int(subject.value)))
}
func (subject *ByteSubject) Equals(expectation byte) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *ByteSubject) GreaterThan(expectation byte) {
if subject.value <= expectation {
subject.Fail("is greater than", expectation)
}
}
func (subject *ByteSubject) LessThan(expectation byte) {
if subject.value >= expectation {
subject.Fail("is less than", expectation)
}
}

View File

@ -0,0 +1,48 @@
package unit
import (
"strconv"
)
type IntSubject struct {
*Subject
value int
}
func NewIntSubject(base *Subject, value int) *IntSubject {
subject := new(IntSubject)
subject.Subject = base
subject.value = value
return subject
}
func (subject *IntSubject) Named(name string) *IntSubject {
subject.Subject.Named(name)
return subject
}
func (subject *IntSubject) Fail(verb string, other int) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(other) + ">.")
}
func (subject *IntSubject) DisplayString() string {
return subject.Subject.DisplayString(strconv.Itoa(subject.value))
}
func (subject *IntSubject) Equals(expectation int) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *IntSubject) GreaterThan(expectation int) {
if subject.value <= expectation {
subject.Fail("is greater than", expectation)
}
}
func (subject *IntSubject) LessThan(expectation int) {
if subject.value >= expectation {
subject.Fail("is less than", expectation)
}
}

31
testing/unit/subject.go Normal file
View File

@ -0,0 +1,31 @@
package unit
type Subject struct {
assert *Assertion
name string
}
func NewSubject(assert *Assertion) *Subject {
subject := new(Subject)
subject.assert = assert
subject.name = ""
return subject
}
func (subject *Subject) FailWithMessage(message string) {
subject.assert.t.Error(message)
}
func (subject *Subject) Named(name string) {
subject.name = name
}
func (subject *Subject) DisplayString(value string) string {
if len(value) == 0 {
value = "unknown"
}
if len(subject.name) == 0 {
return "<" + value + ">"
}
return subject.name + "(<" + value + ">)"
}

View File

@ -0,0 +1,48 @@
package unit
import (
"strconv"
)
type Uint16Subject struct {
*Subject
value uint16
}
func NewUint16Subject(base *Subject, value uint16) *Uint16Subject {
subject := new(Uint16Subject)
subject.Subject = base
subject.value = value
return subject
}
func (subject *Uint16Subject) Named(name string) *Uint16Subject {
subject.Subject.Named(name)
return subject
}
func (subject *Uint16Subject) Fail(verb string, other uint16) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.")
}
func (subject *Uint16Subject) DisplayString() string {
return subject.Subject.DisplayString(strconv.Itoa(int(subject.value)))
}
func (subject *Uint16Subject) Equals(expectation uint16) {
if subject.value != expectation {
subject.Fail("is equal to", expectation)
}
}
func (subject *Uint16Subject) GreaterThan(expectation uint16) {
if subject.value <= expectation {
subject.Fail("is greater than", expectation)
}
}
func (subject *Uint16Subject) LessThan(expectation uint16) {
if subject.value >= expectation {
subject.Fail("is less than", expectation)
}
}