mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 07:26:24 -05:00
simplify test code
This commit is contained in:
parent
08a96e5fe1
commit
c20a7958c2
@ -16,9 +16,7 @@ func TestAuthenticationRequestRead(t *testing.T) {
|
|||||||
0x02, // methods
|
0x02, // methods
|
||||||
}
|
}
|
||||||
request, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
request, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Errorf("Unexpected error %v", err)
|
|
||||||
}
|
|
||||||
assert.Byte(request.version).Named("Version").Equals(0x05)
|
assert.Byte(request.version).Named("Version").Equals(0x05)
|
||||||
assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
|
assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
|
||||||
assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
|
assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
|
||||||
@ -48,9 +46,7 @@ func TestRequestRead(t *testing.T) {
|
|||||||
0x00, 0x35, // port 53
|
0x00, 0x35, // port 53
|
||||||
}
|
}
|
||||||
request, err := ReadRequest(bytes.NewReader(rawRequest))
|
request, err := ReadRequest(bytes.NewReader(rawRequest))
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Errorf("Unexpected error %v", err)
|
|
||||||
}
|
|
||||||
assert.Byte(request.Version).Named("Version").Equals(0x05)
|
assert.Byte(request.Version).Named("Version").Equals(0x05)
|
||||||
assert.Byte(request.Command).Named("Command").Equals(0x01)
|
assert.Byte(request.Command).Named("Command").Equals(0x01)
|
||||||
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
|
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
|
||||||
|
@ -7,19 +7,21 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func randomBytes(p []byte, t *testing.T) {
|
func randomBytes(p []byte, t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
nBytes, err := rand.Read(p)
|
nBytes, err := rand.Read(p)
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Fatal(err)
|
assert.Int(nBytes).Named("# bytes of random buffer").Equals(len(p))
|
||||||
}
|
|
||||||
if nBytes != len(p) {
|
|
||||||
t.Error("Unable to generate %d bytes of random buffer", len(p))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNormalReading(t *testing.T) {
|
func TestNormalReading(t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
testSize := 256
|
testSize := 256
|
||||||
plaintext := make([]byte, testSize)
|
plaintext := make([]byte, testSize)
|
||||||
randomBytes(plaintext, t)
|
randomBytes(plaintext, t)
|
||||||
@ -31,9 +33,8 @@ func TestNormalReading(t *testing.T) {
|
|||||||
randomBytes(iv, t)
|
randomBytes(iv, t)
|
||||||
|
|
||||||
aesBlock, err := aes.NewCipher(key)
|
aesBlock, err := aes.NewCipher(key)
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
aesMode := cipher.NewCBCEncrypter(aesBlock, iv)
|
aesMode := cipher.NewCBCEncrypter(aesBlock, iv)
|
||||||
|
|
||||||
ciphertext := make([]byte, testSize)
|
ciphertext := make([]byte, testSize)
|
||||||
@ -43,9 +44,7 @@ func TestNormalReading(t *testing.T) {
|
|||||||
copy(ciphertextcopy, ciphertext)
|
copy(ciphertextcopy, ciphertext)
|
||||||
|
|
||||||
reader, err := NewDecryptionReader(bytes.NewReader(ciphertextcopy), key, iv)
|
reader, err := NewDecryptionReader(bytes.NewReader(ciphertextcopy), key, iv)
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
readtext := make([]byte, testSize)
|
readtext := make([]byte, testSize)
|
||||||
readSize := 0
|
readSize := 0
|
||||||
@ -55,15 +54,9 @@ func TestNormalReading(t *testing.T) {
|
|||||||
nBytes = testSize - readSize
|
nBytes = testSize - readSize
|
||||||
}
|
}
|
||||||
bytesRead, err := reader.Read(readtext[readSize : readSize+nBytes])
|
bytesRead, err := reader.Read(readtext[readSize : readSize+nBytes])
|
||||||
if err != nil {
|
assert.Error(err).IsNil()
|
||||||
t.Fatal(err)
|
assert.Int(bytesRead).Equals(nBytes)
|
||||||
}
|
|
||||||
if bytesRead != nBytes {
|
|
||||||
t.Errorf("Expected to read %d bytes, but only read %d bytes", nBytes, bytesRead)
|
|
||||||
}
|
|
||||||
readSize += nBytes
|
readSize += nBytes
|
||||||
}
|
}
|
||||||
if !bytes.Equal(readtext, plaintext) {
|
assert.Bytes(readtext).Named("Plaintext").Equals(plaintext)
|
||||||
t.Errorf("Expected plaintext %v, but got %v", plaintext, readtext)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Assertion is an assertion library inspired by Truth.
|
||||||
|
// See http://google.github.io/truth/
|
||||||
type Assertion struct {
|
type Assertion struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
}
|
}
|
||||||
@ -29,3 +31,11 @@ func (a *Assertion) Byte(value byte) *ByteSubject {
|
|||||||
func (a *Assertion) Bytes(value []byte) *BytesSubject {
|
func (a *Assertion) Bytes(value []byte) *BytesSubject {
|
||||||
return NewBytesSubject(NewSubject(a), value)
|
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
testing/unit/errorsubject.go
Normal file
42
testing/unit/errorsubject.go
Normal file
@ -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
testing/unit/stringsubject.go
Normal file
32
testing/unit/stringsubject.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -3,14 +3,16 @@ package core
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUUIDToVID(t *testing.T) {
|
func TestUUIDToVID(t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
|
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
|
||||||
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
|
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
|
||||||
|
|
||||||
actualBytes, _ := UUIDToVID(uuid)
|
actualBytes, _ := UUIDToVID(uuid)
|
||||||
if !bytes.Equal(expectedBytes, actualBytes[:]) {
|
assert.Bytes(actualBytes[:]).Named("UUID").Equals(expectedBytes)
|
||||||
t.Errorf("Expected bytes %v, but got %v", expectedBytes, actualBytes)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user