From c20a7958c243879c34e5aad372a7af0dd1c16402 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Wed, 9 Sep 2015 15:18:29 +0200 Subject: [PATCH] simplify test code --- io/socks/socks_test.go | 8 ++---- io/vmess/decryptionreader_test.go | 35 +++++++++++--------------- testing/unit/assertions.go | 10 ++++++++ testing/unit/errorsubject.go | 42 +++++++++++++++++++++++++++++++ testing/unit/stringsubject.go | 32 +++++++++++++++++++++++ vid_test.go | 8 +++--- 6 files changed, 105 insertions(+), 30 deletions(-) create mode 100644 testing/unit/errorsubject.go create mode 100644 testing/unit/stringsubject.go diff --git a/io/socks/socks_test.go b/io/socks/socks_test.go index 4e05fe0da..8fa63eed2 100644 --- a/io/socks/socks_test.go +++ b/io/socks/socks_test.go @@ -16,9 +16,7 @@ func TestAuthenticationRequestRead(t *testing.T) { 0x02, // methods } request, err := ReadAuthentication(bytes.NewReader(rawRequest)) - if err != nil { - t.Errorf("Unexpected error %v", err) - } + assert.Error(err).IsNil() 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) @@ -48,9 +46,7 @@ func TestRequestRead(t *testing.T) { 0x00, 0x35, // port 53 } request, err := ReadRequest(bytes.NewReader(rawRequest)) - if err != nil { - t.Errorf("Unexpected error %v", err) - } + assert.Error(err).IsNil() 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) diff --git a/io/vmess/decryptionreader_test.go b/io/vmess/decryptionreader_test.go index feec3035c..2f7836285 100644 --- a/io/vmess/decryptionreader_test.go +++ b/io/vmess/decryptionreader_test.go @@ -7,19 +7,21 @@ import ( "crypto/rand" mrand "math/rand" "testing" + + "github.com/v2ray/v2ray-core/testing/unit" ) func randomBytes(p []byte, t *testing.T) { + assert := unit.Assert(t) + nBytes, err := rand.Read(p) - if err != nil { - t.Fatal(err) - } - if nBytes != len(p) { - t.Error("Unable to generate %d bytes of random buffer", len(p)) - } + assert.Error(err).IsNil() + assert.Int(nBytes).Named("# bytes of random buffer").Equals(len(p)) } func TestNormalReading(t *testing.T) { + assert := unit.Assert(t) + testSize := 256 plaintext := make([]byte, testSize) randomBytes(plaintext, t) @@ -31,9 +33,8 @@ func TestNormalReading(t *testing.T) { randomBytes(iv, t) aesBlock, err := aes.NewCipher(key) - if err != nil { - t.Fatal(err) - } + assert.Error(err).IsNil() + aesMode := cipher.NewCBCEncrypter(aesBlock, iv) ciphertext := make([]byte, testSize) @@ -43,9 +44,7 @@ func TestNormalReading(t *testing.T) { copy(ciphertextcopy, ciphertext) reader, err := NewDecryptionReader(bytes.NewReader(ciphertextcopy), key, iv) - if err != nil { - t.Fatal(err) - } + assert.Error(err).IsNil() readtext := make([]byte, testSize) readSize := 0 @@ -55,15 +54,9 @@ func TestNormalReading(t *testing.T) { nBytes = testSize - readSize } bytesRead, err := reader.Read(readtext[readSize : readSize+nBytes]) - if err != nil { - t.Fatal(err) - } - if bytesRead != nBytes { - t.Errorf("Expected to read %d bytes, but only read %d bytes", nBytes, bytesRead) - } + assert.Error(err).IsNil() + assert.Int(bytesRead).Equals(nBytes) readSize += nBytes } - if !bytes.Equal(readtext, plaintext) { - t.Errorf("Expected plaintext %v, but got %v", plaintext, readtext) - } + assert.Bytes(readtext).Named("Plaintext").Equals(plaintext) } diff --git a/testing/unit/assertions.go b/testing/unit/assertions.go index 889863a1c..17c7426b9 100644 --- a/testing/unit/assertions.go +++ b/testing/unit/assertions.go @@ -4,6 +4,8 @@ import ( "testing" ) +// Assertion is an assertion library inspired by Truth. +// See http://google.github.io/truth/ type Assertion struct { t *testing.T } @@ -29,3 +31,11 @@ func (a *Assertion) Byte(value byte) *ByteSubject { func (a *Assertion) Bytes(value []byte) *BytesSubject { return NewBytesSubject(NewSubject(a), value) } + +func (a *Assertion) String(value string) *StringSubject { + return NewStringSubject(NewSubject(a), value) +} + +func (a *Assertion) Error(value error) *ErrorSubject { + return NewErrorSubject(NewSubject(a), value) +} diff --git a/testing/unit/errorsubject.go b/testing/unit/errorsubject.go new file mode 100644 index 000000000..7c94f099d --- /dev/null +++ b/testing/unit/errorsubject.go @@ -0,0 +1,42 @@ +package unit + +import ( + "fmt" +) + +type ErrorSubject struct { + *Subject + value error +} + +func NewErrorSubject(base *Subject, value error) *ErrorSubject { + subject := new(StringSubject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *ErrorSubject) Named(name string) *ErrorSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *ErrorSubject) Fail(verb string, other error) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.Error() + ">.") +} + +func (subject *ErrorSubject) DisplayString() string { + return subject.Subject.DisplayString(subject.value.Error()) +} + +func (subject *ErrorSubject) Equals(expectation error) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *ErrorSubject) IsNil() { + if subject.value != nil { + subject.FailWithMethod("Not true that " + subject.DisplayString() + " is nil.") + } +} \ No newline at end of file diff --git a/testing/unit/stringsubject.go b/testing/unit/stringsubject.go new file mode 100644 index 000000000..50bef5072 --- /dev/null +++ b/testing/unit/stringsubject.go @@ -0,0 +1,32 @@ +package unit + +type StringSubject struct { + *Subject + value string +} + +func NewStringSubject(base *Subject, value string) *StringSubject { + subject := new(StringSubject) + subject.Subject = base + subject.value = value + return subject +} + +func (subject *StringSubject) Named(name string) *StringSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *StringSubject) Fail(verb string, other string) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other + ">.") +} + +func (subject *StringSubject) DisplayString() string { + return subject.Subject.DisplayString(subject.value) +} + +func (subject *StringSubject) Equals(expectation string) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} diff --git a/vid_test.go b/vid_test.go index 0e2263883..bd558a90a 100644 --- a/vid_test.go +++ b/vid_test.go @@ -3,14 +3,16 @@ package core import ( "bytes" "testing" + + "github.com/v2ray/v2ray-core/testing/unit" ) func TestUUIDToVID(t *testing.T) { + assert := unit.Assert(t) + uuid := "2418d087-648d-4990-86e8-19dca1d006d3" expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} actualBytes, _ := UUIDToVID(uuid) - if !bytes.Equal(expectedBytes, actualBytes[:]) { - t.Errorf("Expected bytes %v, but got %v", expectedBytes, actualBytes) - } + assert.Bytes(actualBytes[:]).Named("UUID").Equals(expectedBytes) }