mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-13 12:56:38 -05:00
update tests
This commit is contained in:
parent
ffe18d94e4
commit
5e25741742
10
common/compare/string.go
Normal file
10
common/compare/string.go
Normal 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
|
||||||
|
}
|
@ -13,18 +13,18 @@ func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream {
|
|||||||
return NewAesStreamMethod(key, iv, cipher.NewCFBDecrypter)
|
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.
|
// 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.
|
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
|
||||||
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
|
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
|
||||||
return NewAesStreamMethod(key, iv, cipher.NewCFBEncrypter)
|
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 {
|
func NewAesCTRStream(key []byte, iv []byte) cipher.Stream {
|
||||||
return NewAesStreamMethod(key, iv, cipher.NewCTR)
|
return NewAesStreamMethod(key, iv, cipher.NewCTR)
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
. "v2ray.com/core/common/crypto"
|
. "v2ray.com/core/common/crypto"
|
||||||
"v2ray.com/core/common/protocol"
|
"v2ray.com/core/common/protocol"
|
||||||
@ -75,7 +76,7 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) {
|
|||||||
assert := With(t)
|
assert := With(t)
|
||||||
|
|
||||||
key := make([]byte, 16)
|
key := make([]byte, 16)
|
||||||
rand.Read(key)
|
common.Must2(rand.Read(key))
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/compare"
|
||||||
. "v2ray.com/core/common/crypto"
|
. "v2ray.com/core/common/crypto"
|
||||||
. "v2ray.com/ext/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func mustDecodeHex(s string) []byte {
|
func mustDecodeHex(s string) []byte {
|
||||||
@ -17,8 +17,6 @@ func mustDecodeHex(s string) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChaCha20Stream(t *testing.T) {
|
func TestChaCha20Stream(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
var cases = []struct {
|
var cases = []struct {
|
||||||
key []byte
|
key []byte
|
||||||
iv []byte
|
iv []byte
|
||||||
@ -51,26 +49,28 @@ func TestChaCha20Stream(t *testing.T) {
|
|||||||
input := make([]byte, len(c.output))
|
input := make([]byte, len(c.output))
|
||||||
actualOutout := make([]byte, len(c.output))
|
actualOutout := make([]byte, len(c.output))
|
||||||
s.XORKeyStream(actualOutout, input)
|
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) {
|
func TestChaCha20Decoding(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
rand.Read(key)
|
common.Must2(rand.Read(key))
|
||||||
iv := make([]byte, 8)
|
iv := make([]byte, 8)
|
||||||
rand.Read(iv)
|
common.Must2(rand.Read(iv))
|
||||||
stream := NewChaCha20Stream(key, iv)
|
stream := NewChaCha20Stream(key, iv)
|
||||||
|
|
||||||
payload := make([]byte, 1024)
|
payload := make([]byte, 1024)
|
||||||
rand.Read(payload)
|
common.Must2(rand.Read(payload))
|
||||||
|
|
||||||
x := make([]byte, len(payload))
|
x := make([]byte, len(payload))
|
||||||
stream.XORKeyStream(x, payload)
|
stream.XORKeyStream(x, payload)
|
||||||
|
|
||||||
stream2 := NewChaCha20Stream(key, iv)
|
stream2 := NewChaCha20Stream(key, iv)
|
||||||
stream2.XORKeyStream(x, x)
|
stream2.XORKeyStream(x, x)
|
||||||
assert(x, Equals, payload)
|
if err := compare.BytesEqualWithDetail(x, payload); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
. "v2ray.com/core/common/crypto"
|
. "v2ray.com/core/common/crypto"
|
||||||
. "v2ray.com/ext/assert"
|
. "v2ray.com/ext/assert"
|
||||||
@ -19,18 +20,21 @@ func TestChunkStreamIO(t *testing.T) {
|
|||||||
|
|
||||||
b := buf.New()
|
b := buf.New()
|
||||||
b.AppendBytes('a', 'b', 'c', 'd')
|
b.AppendBytes('a', 'b', 'c', 'd')
|
||||||
assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil)
|
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
|
||||||
|
|
||||||
b = buf.New()
|
b = buf.New()
|
||||||
b.AppendBytes('e', 'f', 'g')
|
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()
|
mb, err := reader.ReadMultiBuffer()
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
|
|
||||||
assert(mb.Len(), Equals, int32(4))
|
assert(mb.Len(), Equals, int32(4))
|
||||||
assert(mb[0].Bytes(), Equals, []byte("abcd"))
|
assert(mb[0].Bytes(), Equals, []byte("abcd"))
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/compare"
|
||||||
. "v2ray.com/core/common/errors"
|
. "v2ray.com/core/common/errors"
|
||||||
"v2ray.com/core/common/log"
|
"v2ray.com/core/common/log"
|
||||||
. "v2ray.com/ext/assert"
|
. "v2ray.com/ext/assert"
|
||||||
@ -28,8 +29,6 @@ func TestError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorMessage(t *testing.T) {
|
func TestErrorMessage(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
data := []struct {
|
data := []struct {
|
||||||
err error
|
err error
|
||||||
msg string
|
msg string
|
||||||
@ -45,6 +44,8 @@ func TestErrorMessage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range data {
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,9 @@ package log_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/compare"
|
||||||
"v2ray.com/core/common/log"
|
"v2ray.com/core/common/log"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
. "v2ray.com/ext/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type testLogger struct {
|
type testLogger struct {
|
||||||
@ -17,8 +17,6 @@ func (l *testLogger) Handle(msg log.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLogRecord(t *testing.T) {
|
func TestLogRecord(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
var logger testLogger
|
var logger testLogger
|
||||||
log.RegisterHandler(&logger)
|
log.RegisterHandler(&logger)
|
||||||
|
|
||||||
@ -28,5 +26,7 @@ func TestLogRecord(t *testing.T) {
|
|||||||
Content: net.ParseAddress(ip),
|
Content: net.ParseAddress(ip),
|
||||||
})
|
})
|
||||||
|
|
||||||
assert(logger.value, Equals, "[Error] "+ip)
|
if err := compare.StringEqualWithDetail("[Error] "+ip, logger.value); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,25 +3,23 @@ package log_test
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
. "v2ray.com/core/common/log"
|
. "v2ray.com/core/common/log"
|
||||||
. "v2ray.com/ext/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFileLogger(t *testing.T) {
|
func TestFileLogger(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
f, err := ioutil.TempFile("", "vtest")
|
f, err := ioutil.TempFile("", "vtest")
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
path := f.Name()
|
path := f.Name()
|
||||||
common.Must(f.Close())
|
common.Must(f.Close())
|
||||||
|
|
||||||
creator, err := CreateFileLogWriter(path)
|
creator, err := CreateFileLogWriter(path)
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
|
|
||||||
handler := NewLogger(creator)
|
handler := NewLogger(creator)
|
||||||
handler.Handle(&GeneralMessage{Content: "Test Log"})
|
handler.Handle(&GeneralMessage{Content: "Test Log"})
|
||||||
@ -30,11 +28,12 @@ func TestFileLogger(t *testing.T) {
|
|||||||
common.Must(common.Close(handler))
|
common.Must(common.Close(handler))
|
||||||
|
|
||||||
f, err = os.Open(path)
|
f, err = os.Open(path)
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
|
defer f.Close() // nolint: errcheck
|
||||||
|
|
||||||
b, err := buf.ReadAllToBytes(f)
|
b, err := buf.ReadAllToBytes(f)
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
assert(string(b), HasSubstring, "Test Log")
|
if !strings.Contains(string(b), "Test Log") {
|
||||||
|
t.Fatal("Expect log text contains 'Test Log', but actually: ", string(b))
|
||||||
common.Must(f.Close())
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,14 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
|
"v2ray.com/core/common/compare"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
. "v2ray.com/core/common/protocol"
|
. "v2ray.com/core/common/protocol"
|
||||||
. "v2ray.com/ext/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAddressReading(t *testing.T) {
|
func TestAddressReading(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
data := []struct {
|
data := []struct {
|
||||||
Options []AddressOption
|
Options []AddressOption
|
||||||
Input []byte
|
Input []byte
|
||||||
@ -83,18 +82,26 @@ func TestAddressReading(t *testing.T) {
|
|||||||
addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
|
addr, port, err := parser.ReadAddressPort(b, bytes.NewReader(tc.Input))
|
||||||
b.Release()
|
b.Release()
|
||||||
if tc.Error {
|
if tc.Error {
|
||||||
assert(err, IsNotNil)
|
if err == nil {
|
||||||
|
t.Errorf("Expect error but not: %v", tc)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assert(err, IsNil)
|
if err != nil {
|
||||||
assert(addr, Equals, tc.Address)
|
t.Errorf("Expect no error but: %s %v", err.Error(), tc)
|
||||||
assert(port, Equals, tc.Port)
|
}
|
||||||
|
|
||||||
|
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) {
|
func TestAddressWriting(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
data := []struct {
|
data := []struct {
|
||||||
Options []AddressOption
|
Options []AddressOption
|
||||||
Address net.Address
|
Address net.Address
|
||||||
@ -116,9 +123,14 @@ func TestAddressWriting(t *testing.T) {
|
|||||||
b := buf.New()
|
b := buf.New()
|
||||||
err := parser.WriteAddressPort(b, tc.Address, tc.Port)
|
err := parser.WriteAddressPort(b, tc.Address, tc.Port)
|
||||||
if tc.Error {
|
if tc.Error {
|
||||||
assert(err, IsNotNil)
|
if err == nil {
|
||||||
|
t.Error("Expect error but nil")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assert(b.Bytes(), Equals, tc.Bytes)
|
common.Must(err)
|
||||||
|
if err := compare.BytesEqualWithDetail(tc.Bytes, b.Bytes()); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,39 +3,47 @@ package uuid_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/compare"
|
||||||
. "v2ray.com/core/common/uuid"
|
. "v2ray.com/core/common/uuid"
|
||||||
. "v2ray.com/ext/assert"
|
. "v2ray.com/ext/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseBytes(t *testing.T) {
|
func TestParseBytes(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
str := "2418d087-648d-4990-86e8-19dca1d006d3"
|
str := "2418d087-648d-4990-86e8-19dca1d006d3"
|
||||||
bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
|
bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
|
||||||
|
|
||||||
uuid, err := ParseBytes(bytes)
|
uuid, err := ParseBytes(bytes)
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
assert(uuid.String(), Equals, str)
|
if err := compare.StringEqualWithDetail(uuid.String(), str); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = ParseBytes([]byte{1, 3, 2, 4})
|
_, err = ParseBytes([]byte{1, 3, 2, 4})
|
||||||
assert(err, IsNotNil)
|
if err == nil {
|
||||||
|
t.Fatal("Expect error but nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseString(t *testing.T) {
|
func TestParseString(t *testing.T) {
|
||||||
assert := With(t)
|
|
||||||
|
|
||||||
str := "2418d087-648d-4990-86e8-19dca1d006d3"
|
str := "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}
|
||||||
|
|
||||||
uuid, err := ParseString(str)
|
uuid, err := ParseString(str)
|
||||||
assert(err, IsNil)
|
common.Must(err)
|
||||||
assert(uuid.Bytes(), Equals, expectedBytes)
|
if err := compare.BytesEqualWithDetail(expectedBytes, uuid.Bytes()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
uuid, err = ParseString("2418d087")
|
_, err = ParseString("2418d087")
|
||||||
assert(err, IsNotNil)
|
if err == nil {
|
||||||
|
t.Fatal("Expect error but nil")
|
||||||
|
}
|
||||||
|
|
||||||
uuid, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")
|
_, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3")
|
||||||
assert(err, IsNotNil)
|
if err == nil {
|
||||||
|
t.Fatal("Expect error but nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewUUID(t *testing.T) {
|
func TestNewUUID(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user