Forráskód Böngészése

simplify test code

V2Ray 10 éve
szülő
commit
c20a7958c2

+ 2 - 6
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)

+ 14 - 21
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)
 }

+ 10 - 0
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)
+}

+ 42 - 0
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.")
+  }
+}

+ 32 - 0
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)
+	}
+}

+ 5 - 3
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)
 }