simplify test code

This commit is contained in:
V2Ray committed 2015-09-09 15:18:29 +02:00
1 parent 08a96e5fe1
commit c20a7958c2
6 files changed
+105 -30

No files matched your search

+2 -6
View File
@@ -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
View File
@@ -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)
}