From 5e25741742bdf183d90efd50c6c098e8e4b8c35c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 13 Jul 2018 14:36:09 +0200 Subject: [PATCH] update tests --- common/compare/string.go | 10 +++++++++ common/crypto/aes.go | 12 +++++------ common/crypto/auth_test.go | 3 ++- common/crypto/chacha20_test.go | 20 ++++++++--------- common/crypto/chunk_test.go | 14 +++++++----- common/errors/errors_test.go | 7 +++--- common/log/log_test.go | 8 +++---- common/log/logger_test.go | 19 ++++++++--------- common/protocol/address_test.go | 34 +++++++++++++++++++---------- common/uuid/uuid_test.go | 38 ++++++++++++++++++++------------- 10 files changed, 100 insertions(+), 65 deletions(-) create mode 100644 common/compare/string.go diff --git a/common/compare/string.go b/common/compare/string.go new file mode 100644 index 00000000..4d48861d --- /dev/null +++ b/common/compare/string.go @@ -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 +} diff --git a/common/crypto/aes.go b/common/crypto/aes.go index 1eb111b5..5a002561 100644 --- a/common/crypto/aes.go +++ b/common/crypto/aes.go @@ -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) } diff --git a/common/crypto/auth_test.go b/common/crypto/auth_test.go index 3fde8f2f..ea16cfcc 100644 --- a/common/crypto/auth_test.go +++ b/common/crypto/auth_test.go @@ -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) diff --git a/common/crypto/chacha20_test.go b/common/crypto/chacha20_test.go index 40ea44c3..6d43a0c2 100644 --- a/common/crypto/chacha20_test.go +++ b/common/crypto/chacha20_test.go @@ -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) + } } diff --git a/common/crypto/chunk_test.go b/common/crypto/chunk_test.go index f1ef5d28..38a3ec43 100644 --- a/common/crypto/chunk_test.go +++ b/common/crypto/chunk_test.go @@ -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")) diff --git a/common/errors/errors_test.go b/common/errors/errors_test.go index a5bbf01c..480f325e 100644 --- a/common/errors/errors_test.go +++ b/common/errors/errors_test.go @@ -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) + } } } diff --git a/common/log/log_test.go b/common/log/log_test.go index 3555e5cc..a568b7b7 100644 --- a/common/log/log_test.go +++ b/common/log/log_test.go @@ -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) + } } diff --git a/common/log/logger_test.go b/common/log/logger_test.go index 03018e2c..b41f41df 100644 --- a/common/log/logger_test.go +++ b/common/log/logger_test.go @@ -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)) + } } diff --git a/common/protocol/address_test.go b/common/protocol/address_test.go index e9bd7522..c9ea0e17 100644 --- a/common/protocol/address_test.go +++ b/common/protocol/address_test.go @@ -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) + } } } } diff --git a/common/uuid/uuid_test.go b/common/uuid/uuid_test.go index fba4ec63..f6697abc 100644 --- a/common/uuid/uuid_test.go +++ b/common/uuid/uuid_test.go @@ -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) - - uuid, err = ParseString("2418d087") - assert(err, IsNotNil) - - uuid, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3") - assert(err, IsNotNil) + common.Must(err) + if err := compare.BytesEqualWithDetail(expectedBytes, uuid.Bytes()); err != nil { + t.Fatal(err) + } + + _, err = ParseString("2418d087") + if err == nil { + t.Fatal("Expect error but nil") + } + + _, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3") + if err == nil { + t.Fatal("Expect error but nil") + } } func TestNewUUID(t *testing.T) {