1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-26 12:07:47 -05:00

update tests

This commit is contained in:
Darien Raymond 2018-07-13 14:36:09 +02:00
parent ffe18d94e4
commit 5e25741742
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
10 changed files with 98 additions and 63 deletions

10
common/compare/string.go Normal file
View File

@ -0,0 +1,10 @@
package compare
import "v2ray.com/core/common/errors"
func StringEqualWithDetail(a string, b string) error {
if a != b {
return errors.New("Got ", b, " but want ", a)
}
return nil
}

View File

@ -13,18 +13,18 @@ func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream {
return NewAesStreamMethod(key, iv, cipher.NewCFBDecrypter)
}
func NewAesStreamMethod(key []byte, iv []byte, f func(cipher.Block, []byte) cipher.Stream) cipher.Stream {
aesBlock, err := aes.NewCipher(key)
common.Must(err)
return f(aesBlock, iv)
}
// NewAesEncryptionStream creates a new AES description stream based on given key and IV.
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
return NewAesStreamMethod(key, iv, cipher.NewCFBEncrypter)
}
func NewAesStreamMethod(key []byte, iv []byte, f func(cipher.Block, []byte) cipher.Stream) cipher.Stream {
aesBlock, err := aes.NewCipher(key)
common.Must(err)
return f(aesBlock, iv)
}
func NewAesCTRStream(key []byte, iv []byte) cipher.Stream {
return NewAesStreamMethod(key, iv, cipher.NewCTR)
}

View File

@ -7,6 +7,7 @@ import (
"io"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/crypto"
"v2ray.com/core/common/protocol"
@ -75,7 +76,7 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) {
assert := With(t)
key := make([]byte, 16)
rand.Read(key)
common.Must2(rand.Read(key))
block, err := aes.NewCipher(key)
assert(err, IsNil)

View File

@ -6,8 +6,8 @@ import (
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/crypto"
. "v2ray.com/ext/assert"
)
func mustDecodeHex(s string) []byte {
@ -17,8 +17,6 @@ func mustDecodeHex(s string) []byte {
}
func TestChaCha20Stream(t *testing.T) {
assert := With(t)
var cases = []struct {
key []byte
iv []byte
@ -51,26 +49,28 @@ func TestChaCha20Stream(t *testing.T) {
input := make([]byte, len(c.output))
actualOutout := make([]byte, len(c.output))
s.XORKeyStream(actualOutout, input)
assert(c.output, Equals, actualOutout)
if err := compare.BytesEqualWithDetail(c.output, actualOutout); err != nil {
t.Fatal(err)
}
}
}
func TestChaCha20Decoding(t *testing.T) {
assert := With(t)
key := make([]byte, 32)
rand.Read(key)
common.Must2(rand.Read(key))
iv := make([]byte, 8)
rand.Read(iv)
common.Must2(rand.Read(iv))
stream := NewChaCha20Stream(key, iv)
payload := make([]byte, 1024)
rand.Read(payload)
common.Must2(rand.Read(payload))
x := make([]byte, len(payload))
stream.XORKeyStream(x, payload)
stream2 := NewChaCha20Stream(key, iv)
stream2.XORKeyStream(x, x)
assert(x, Equals, payload)
if err := compare.BytesEqualWithDetail(x, payload); err != nil {
t.Fatal(err)
}
}

View File

@ -4,6 +4,7 @@ import (
"io"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/crypto"
. "v2ray.com/ext/assert"
@ -19,18 +20,21 @@ func TestChunkStreamIO(t *testing.T) {
b := buf.New()
b.AppendBytes('a', 'b', 'c', 'd')
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
b = buf.New()
b.AppendBytes('e', 'f', 'g')
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
assert(writer.WriteMultiBuffer(buf.MultiBuffer{}), IsNil)
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))
assert(cache.Len(), Equals, int32(13))
if cache.Len() != 13 {
t.Fatalf("Cache length is %d, want 13", cache.Len())
}
mb, err := reader.ReadMultiBuffer()
assert(err, IsNil)
common.Must(err)
assert(mb.Len(), Equals, int32(4))
assert(mb[0].Bytes(), Equals, []byte("abcd"))

View File

@ -4,6 +4,7 @@ import (
"io"
"testing"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
. "v2ray.com/ext/assert"
@ -28,8 +29,6 @@ func TestError(t *testing.T) {
}
func TestErrorMessage(t *testing.T) {
assert := With(t)
data := []struct {
err error
msg string
@ -45,6 +44,8 @@ func TestErrorMessage(t *testing.T) {
}
for _, d := range data {
assert(d.err.Error(), Equals, d.msg)
if err := compare.StringEqualWithDetail(d.msg, d.err.Error()); err != nil {
t.Fatal(err)
}
}
}

View File

@ -3,9 +3,9 @@ package log_test
import (
"testing"
"v2ray.com/core/common/compare"
"v2ray.com/core/common/log"
"v2ray.com/core/common/net"
. "v2ray.com/ext/assert"
)
type testLogger struct {
@ -17,8 +17,6 @@ func (l *testLogger) Handle(msg log.Message) {
}
func TestLogRecord(t *testing.T) {
assert := With(t)
var logger testLogger
log.RegisterHandler(&logger)
@ -28,5 +26,7 @@ func TestLogRecord(t *testing.T) {
Content: net.ParseAddress(ip),
})
assert(logger.value, Equals, "[Error] "+ip)
if err := compare.StringEqualWithDetail("[Error] "+ip, logger.value); err != nil {
t.Fatal(err)
}
}

View File

@ -3,25 +3,23 @@ package log_test
import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/common/log"
. "v2ray.com/ext/assert"
)
func TestFileLogger(t *testing.T) {
assert := With(t)
f, err := ioutil.TempFile("", "vtest")
assert(err, IsNil)
common.Must(err)
path := f.Name()
common.Must(f.Close())
creator, err := CreateFileLogWriter(path)
assert(err, IsNil)
common.Must(err)
handler := NewLogger(creator)
handler.Handle(&GeneralMessage{Content: "Test Log"})
@ -30,11 +28,12 @@ func TestFileLogger(t *testing.T) {
common.Must(common.Close(handler))
f, err = os.Open(path)
assert(err, IsNil)
common.Must(err)
defer f.Close() // nolint: errcheck
b, err := buf.ReadAllToBytes(f)
assert(err, IsNil)
assert(string(b), HasSubstring, "Test Log")
common.Must(f.Close())
common.Must(err)
if !strings.Contains(string(b), "Test Log") {
t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
}
}

View File

@ -4,15 +4,14 @@ import (
"bytes"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/compare"
"v2ray.com/core/common/net"
. "v2ray.com/core/common/protocol"
. "v2ray.com/ext/assert"
)
func TestAddressReading(t *testing.T) {
assert := With(t)
data := []struct {
Options []AddressOption
Input []byte
@ -83,18 +82,26 @@ func TestAddressReading(t *testing.T) {
addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
b.Release()
if tc.Error {
assert(err, IsNotNil)
if err == nil {
t.Errorf("Expect error but not: %v", tc)
}
} else {
assert(err, IsNil)
assert(addr, Equals, tc.Address)
assert(port, Equals, tc.Port)
if err != nil {
t.Errorf("Expect no error but: %s %v", err.Error(), tc)
}
if addr != tc.Address {
t.Error("Got address ", addr.String(), " want ", tc.Address.String())
}
if tc.Port != port {
t.Error("Got port ", port, " want ", tc.Port)
}
}
}
}
func TestAddressWriting(t *testing.T) {
assert := With(t)
data := []struct {
Options []AddressOption
Address net.Address
@ -116,9 +123,14 @@ func TestAddressWriting(t *testing.T) {
b := buf.New()
err := parser.WriteAddressPort(b, tc.Address, tc.Port)
if tc.Error {
assert(err, IsNotNil)
if err == nil {
t.Error("Expect error but nil")
}
} else {
assert(b.Bytes(), Equals, tc.Bytes)
common.Must(err)
if err := compare.BytesEqualWithDetail(tc.Bytes, b.Bytes()); err != nil {
t.Error(err)
}
}
}
}

View File

@ -3,39 +3,47 @@ package uuid_test
import (
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/uuid"
. "v2ray.com/ext/assert"
)
func TestParseBytes(t *testing.T) {
assert := With(t)
str := "2418d087-648d-4990-86e8-19dca1d006d3"
bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
uuid, err := ParseBytes(bytes)
assert(err, IsNil)
assert(uuid.String(), Equals, str)
common.Must(err)
if err := compare.StringEqualWithDetail(uuid.String(), str); err != nil {
t.Fatal(err)
}
_, err = ParseBytes([]byte{1, 3, 2, 4})
assert(err, IsNotNil)
if err == nil {
t.Fatal("Expect error but nil")
}
}
func TestParseString(t *testing.T) {
assert := With(t)
str := "2418d087-648d-4990-86e8-19dca1d006d3"
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
uuid, err := ParseString(str)
assert(err, IsNil)
assert(uuid.Bytes(), Equals, expectedBytes)
common.Must(err)
if err := compare.BytesEqualWithDetail(expectedBytes, uuid.Bytes()); err != nil {
t.Fatal(err)
}
uuid, err = ParseString("2418d087")
assert(err, IsNotNil)
_, err = ParseString("2418d087")
if err == nil {
t.Fatal("Expect error but nil")
}
uuid, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")
assert(err, IsNotNil)
_, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")
if err == nil {
t.Fatal("Expect error but nil")
}
}
func TestNewUUID(t *testing.T) {