From fc63f0432cb4966ea73190a46890ad306313ddad Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 24 May 2016 21:55:46 +0200 Subject: [PATCH] simplify testing --- app/dns/config_json_test.go | 3 +- app/dns/server_test.go | 3 +- app/router/rules/chinaip_json_test.go | 5 +- app/router/rules/chinaip_test.go | 3 +- app/router/rules/chinasites_json_test.go | 5 +- app/router/rules/chinasites_test.go | 3 +- app/router/rules/condition.go | 14 +++--- app/router/rules/config_json_test.go | 5 +- app/router/rules/router.go | 5 +- app/router/rules/router_test.go | 5 +- common/alloc/buffer_test.go | 11 ++--- common/collect/validity_map.go | 10 ++-- common/crypto/chacha20_test.go | 5 +- common/io/buffered_reader_test.go | 3 +- common/io/buffered_writer_test.go | 3 +- common/io/reader_test.go | 3 +- common/io/writer_test.go | 3 +- common/log/access.go | 5 +- common/log/internal/log_entry.go | 11 ++--- common/log/internal/log_entry_test.go | 11 ++--- common/net/address_json_test.go | 9 ++-- common/net/address_test.go | 39 ++++++++-------- common/net/destination_test.go | 13 +++--- common/net/ipnet_test.go | 7 +-- common/net/network_json_test.go | 7 ++- common/net/port.go | 6 +-- common/net/port_json_test.go | 11 ++--- common/net/port_test.go | 3 +- common/net/timed_io_test.go | 3 +- common/protocol/headers.go | 3 +- common/protocol/id_test.go | 3 +- common/protocol/raw/commands.go | 4 +- common/protocol/raw/commands_test.go | 7 ++- common/protocol/raw/encoding_test.go | 3 +- common/protocol/time_test.go | 3 +- common/protocol/user_json_test.go | 9 ++-- common/retry/retry_test.go | 9 ++-- common/serial/bytes.go | 18 +++++++- common/serial/interface.go | 9 ++++ common/serial/numbers.go | 16 +++++++ common/serial/string_json_test.go | 7 ++- common/serial/string_list_json_test.go | 3 +- common/serial/string_test.go | 24 ---------- common/uuid/uuid_test.go | 19 ++++---- proxy/dokodemo/dokodemo_test.go | 7 ++- proxy/freedom/freedom_test.go | 7 ++- proxy/http/config_json_test.go | 3 +- proxy/http/server_test.go | 25 +++++----- proxy/internal/config/config_cache_test.go | 5 +- proxy/shadowsocks/config_json_test.go | 3 +- proxy/shadowsocks/ota_test.go | 3 +- proxy/shadowsocks/protocol_test.go | 21 ++++----- proxy/socks/config_json_test.go | 5 +- proxy/socks/protocol/socks4_test.go | 13 +++--- proxy/socks/protocol/socks_test.go | 45 +++++++++--------- proxy/socks/protocol/udp_test.go | 7 ++- proxy/socks/server_test.go | 13 +++--- proxy/vmess/inbound/command.go | 3 +- proxy/vmess/io/io_test.go | 7 ++- proxy/vmess/io/writer.go | 2 +- proxy/vmess/outbound/command.go | 2 +- proxy/vmess/outbound/receiver_json_test.go | 7 ++- proxy/vmess/outbound/receiver_test.go | 3 +- proxy/vmess/vmess_test.go | 3 +- shell/point/config_json_test.go | 17 ++++--- testing/assert/address.go | 48 +++++++++++++------- testing/{global.go => assert/assert.go} | 22 +++++---- testing/assert/bool.go | 42 +++++++++++++++++ testing/assert/boolsubject.go | 49 -------------------- testing/assert/byte.go | 38 ++++++++++++++++ testing/assert/bytes.go | 34 ++++++++++++++ testing/assert/bytessubject.go | 41 ----------------- testing/assert/bytesubject.go | 45 ------------------ testing/assert/destination.go | 42 ++++++++++------- testing/assert/error.go | 38 ++++++++++++++++ testing/assert/errorsubject.go | 41 ----------------- testing/assert/int64.go | 44 ++++++++++++++++++ testing/assert/int64subject.go | 51 --------------------- testing/assert/intsubject.go | 33 ++++++-------- testing/assert/ip.go | 25 ++++------ testing/assert/pointer.go | 38 ++++++++++++++++ testing/assert/pointersubject.go | 45 ------------------ testing/assert/port.go | 28 +++++------- testing/assert/string.go | 44 ++++++++++++++++++ testing/assert/stringsubject.go | 53 ---------------------- testing/assert/subject.go | 32 ++++--------- testing/assert/uint16.go | 50 ++++++++++++++++++++ testing/assert/uint16subject.go | 47 ------------------- testing/scenarios/dokodemo_test.go | 5 +- testing/scenarios/dynamic_vmess_test.go | 5 +- testing/scenarios/http_test.go | 5 +- testing/scenarios/router_test.go | 5 +- testing/scenarios/shadowsocks_test.go | 9 ++-- testing/scenarios/socks_end_test.go | 9 ++-- tools/build/build_test.go | 3 +- tools/build/env_test.go | 5 +- tools/build/go_test.go | 5 +- tools/git/git_test.go | 5 +- transport/dialer/dialer_test.go | 5 +- 99 files changed, 726 insertions(+), 817 deletions(-) create mode 100644 common/serial/interface.go delete mode 100644 common/serial/string_test.go rename testing/{global.go => assert/assert.go} (88%) create mode 100644 testing/assert/bool.go delete mode 100644 testing/assert/boolsubject.go create mode 100644 testing/assert/byte.go create mode 100644 testing/assert/bytes.go delete mode 100644 testing/assert/bytessubject.go delete mode 100644 testing/assert/bytesubject.go create mode 100644 testing/assert/error.go delete mode 100644 testing/assert/errorsubject.go create mode 100644 testing/assert/int64.go delete mode 100644 testing/assert/int64subject.go create mode 100644 testing/assert/pointer.go delete mode 100644 testing/assert/pointersubject.go create mode 100644 testing/assert/string.go delete mode 100644 testing/assert/stringsubject.go create mode 100644 testing/assert/uint16.go delete mode 100644 testing/assert/uint16subject.go diff --git a/app/dns/config_json_test.go b/app/dns/config_json_test.go index 0689fbfeb..49e4b670d 100644 --- a/app/dns/config_json_test.go +++ b/app/dns/config_json_test.go @@ -8,12 +8,11 @@ import ( . "github.com/v2ray/v2ray-core/app/dns" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestConfigParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `{ "servers": ["8.8.8.8"] diff --git a/app/dns/server_test.go b/app/dns/server_test.go index 5ffd3176a..a99a12f83 100644 --- a/app/dns/server_test.go +++ b/app/dns/server_test.go @@ -11,12 +11,11 @@ import ( "github.com/v2ray/v2ray-core/app/proxyman" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy/freedom" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestDnsAdd(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) space := app.NewSpace() diff --git a/app/router/rules/chinaip_json_test.go b/app/router/rules/chinaip_json_test.go index b491792ca..d4f029278 100644 --- a/app/router/rules/chinaip_json_test.go +++ b/app/router/rules/chinaip_json_test.go @@ -6,18 +6,17 @@ import ( "testing" . "github.com/v2ray/v2ray-core/app/router/rules" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestChinaIPJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinaip", "outboundTag": "x" }`)) - assert.StringLiteral(rule.Tag).Equals("x") + assert.String(rule.Tag).Equals("x") assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com diff --git a/app/router/rules/chinaip_test.go b/app/router/rules/chinaip_test.go index 86d76277b..6cd959eed 100644 --- a/app/router/rules/chinaip_test.go +++ b/app/router/rules/chinaip_test.go @@ -6,7 +6,6 @@ import ( . "github.com/v2ray/v2ray-core/app/router/rules" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -15,7 +14,7 @@ func makeDestination(ip string) v2net.Destination { } func TestChinaIP(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := NewChinaIPRule("tag") assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn diff --git a/app/router/rules/chinasites_json_test.go b/app/router/rules/chinasites_json_test.go index dd5ceecb9..e974e8b7c 100644 --- a/app/router/rules/chinasites_json_test.go +++ b/app/router/rules/chinasites_json_test.go @@ -6,18 +6,17 @@ import ( "testing" . "github.com/v2ray/v2ray-core/app/router/rules" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestChinaSitesJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinasites", "outboundTag": "y" }`)) - assert.StringLiteral(rule.Tag).Equals("y") + assert.String(rule.Tag).Equals("y") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() diff --git a/app/router/rules/chinasites_test.go b/app/router/rules/chinasites_test.go index dafb1bc57..9f0985458 100644 --- a/app/router/rules/chinasites_test.go +++ b/app/router/rules/chinasites_test.go @@ -5,7 +5,6 @@ import ( . "github.com/v2ray/v2ray-core/app/router/rules" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -14,7 +13,7 @@ func makeDomainDestination(domain string) v2net.Destination { } func TestChinaSites(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := NewChinaSitesRule("tag") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() diff --git a/app/router/rules/condition.go b/app/router/rules/condition.go index 262d85c37..36fa1e213 100644 --- a/app/router/rules/condition.go +++ b/app/router/rules/condition.go @@ -3,9 +3,9 @@ package rules import ( "net" "regexp" + "strings" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" ) type Condition interface { @@ -63,12 +63,12 @@ func (this *AnyCondition) Len() int { } type PlainDomainMatcher struct { - pattern serial.StringT + pattern string } func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher { return &PlainDomainMatcher{ - pattern: serial.StringT(pattern), + pattern: pattern, } } @@ -76,8 +76,8 @@ func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool { if !dest.Address().IsDomain() { return false } - domain := serial.StringT(dest.Address().Domain()) - return domain.Contains(this.pattern) + domain := dest.Address().Domain() + return strings.Contains(domain, this.pattern) } type RegexpDomainMatcher struct { @@ -98,8 +98,8 @@ func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool { if !dest.Address().IsDomain() { return false } - domain := serial.StringT(dest.Address().Domain()) - return this.pattern.MatchString(domain.ToLower().String()) + domain := dest.Address().Domain() + return this.pattern.MatchString(strings.ToLower(domain)) } type CIDRMatcher struct { diff --git a/app/router/rules/config_json_test.go b/app/router/rules/config_json_test.go index 41478b0e9..256358564 100644 --- a/app/router/rules/config_json_test.go +++ b/app/router/rules/config_json_test.go @@ -7,12 +7,11 @@ import ( . "github.com/v2ray/v2ray-core/app/router/rules" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestDomainRule(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "field", @@ -33,7 +32,7 @@ func TestDomainRule(t *testing.T) { } func TestIPRule(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "field", diff --git a/app/router/rules/router.go b/app/router/rules/router.go index cbe3a8eaf..71b982f6d 100644 --- a/app/router/rules/router.go +++ b/app/router/rules/router.go @@ -108,10 +108,11 @@ func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, erro } func (this *Router) TakeDetour(dest v2net.Destination) (string, error) { - rawEntry := this.cache.Get(dest) + destStr := dest.String() + rawEntry := this.cache.Get(destStr) if rawEntry == nil { tag, err := this.takeDetourWithoutCache(dest) - this.cache.Set(dest, newCacheEntry(tag, err)) + this.cache.Set(destStr, newCacheEntry(tag, err)) return tag, err } entry := rawEntry.(*cacheEntry) diff --git a/app/router/rules/router_test.go b/app/router/rules/router_test.go index 0b369fb26..542e7a89c 100644 --- a/app/router/rules/router_test.go +++ b/app/router/rules/router_test.go @@ -11,12 +11,11 @@ import ( "github.com/v2ray/v2ray-core/app/router" . "github.com/v2ray/v2ray-core/app/router/rules" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestSimpleRouter(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) config := &RouterRuleConfig{ Rules: []*Rule{ @@ -37,5 +36,5 @@ func TestSimpleRouter(t *testing.T) { tag, err := r.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() - assert.StringLiteral(tag).Equals("test") + assert.String(tag).Equals("test") } diff --git a/common/alloc/buffer_test.go b/common/alloc/buffer_test.go index 2b5ba9cda..c595438f6 100644 --- a/common/alloc/buffer_test.go +++ b/common/alloc/buffer_test.go @@ -4,12 +4,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/alloc" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestBufferClear(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := NewBuffer().Clear() defer buffer.Release() @@ -23,7 +22,7 @@ func TestBufferClear(t *testing.T) { } func TestBufferIsFull(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := NewBuffer() defer buffer.Release() @@ -35,7 +34,7 @@ func TestBufferIsFull(t *testing.T) { } func TestBufferPrepend(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := NewBuffer().Clear() defer buffer.Release() @@ -51,11 +50,11 @@ func TestBufferPrepend(t *testing.T) { } func TestBufferString(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := NewBuffer().Clear() defer buffer.Release() buffer.AppendString("Test String") - assert.String(buffer).Equals("Test String") + assert.String(buffer.String()).Equals("Test String") } diff --git a/common/collect/validity_map.go b/common/collect/validity_map.go index faf19b5ae..f714786d9 100644 --- a/common/collect/validity_map.go +++ b/common/collect/validity_map.go @@ -3,8 +3,6 @@ package collect import ( "sync" "sync/atomic" - - "github.com/v2ray/v2ray-core/common/serial" ) type Validity interface { @@ -51,9 +49,9 @@ func (this *ValidityMap) cleanup() { } } -func (this *ValidityMap) Set(key serial.String, value Validity) { +func (this *ValidityMap) Set(key string, value Validity) { this.Lock() - this.cache[key.String()] = value + this.cache[key] = value this.Unlock() opCount := atomic.AddInt32(&this.opCount, 1) if opCount > 1000 { @@ -62,10 +60,10 @@ func (this *ValidityMap) Set(key serial.String, value Validity) { } } -func (this *ValidityMap) Get(key serial.String) Validity { +func (this *ValidityMap) Get(key string) Validity { this.RLock() defer this.RUnlock() - if value, found := this.cache[key.String()]; found { + if value, found := this.cache[key]; found { return value } return nil diff --git a/common/crypto/chacha20_test.go b/common/crypto/chacha20_test.go index cfa50d67b..8805b4b87 100644 --- a/common/crypto/chacha20_test.go +++ b/common/crypto/chacha20_test.go @@ -6,7 +6,6 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/crypto" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -19,7 +18,7 @@ func mustDecodeHex(s string) []byte { } func TestChaCha20Stream(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var cases = []struct { key []byte @@ -58,7 +57,7 @@ func TestChaCha20Stream(t *testing.T) { } func TestChaCha20Decoding(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) key := make([]byte, 32) rand.Read(key) diff --git a/common/io/buffered_reader_test.go b/common/io/buffered_reader_test.go index 3616ab1a8..ca937e525 100644 --- a/common/io/buffered_reader_test.go +++ b/common/io/buffered_reader_test.go @@ -5,12 +5,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" . "github.com/v2ray/v2ray-core/common/io" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestBufferedReader(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) content := alloc.NewLargeBuffer() len := content.Len() diff --git a/common/io/buffered_writer_test.go b/common/io/buffered_writer_test.go index ae368e26a..c7df4524f 100644 --- a/common/io/buffered_writer_test.go +++ b/common/io/buffered_writer_test.go @@ -5,12 +5,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" . "github.com/v2ray/v2ray-core/common/io" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestBufferedWriter(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) content := alloc.NewLargeBuffer().Clear() diff --git a/common/io/reader_test.go b/common/io/reader_test.go index 0ff53475a..e8fb4f6a2 100644 --- a/common/io/reader_test.go +++ b/common/io/reader_test.go @@ -6,12 +6,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" . "github.com/v2ray/v2ray-core/common/io" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestAdaptiveReader(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawContent := make([]byte, 1024*1024) diff --git a/common/io/writer_test.go b/common/io/writer_test.go index 1bf8be4c7..e800e9b14 100644 --- a/common/io/writer_test.go +++ b/common/io/writer_test.go @@ -7,12 +7,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" . "github.com/v2ray/v2ray-core/common/io" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestAdaptiveWriter(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) lb := alloc.NewLargeBuffer() rand.Read(lb.Value) diff --git a/common/log/access.go b/common/log/access.go index f4501f614..3a2121a54 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -1,8 +1,9 @@ package log import ( + "fmt" + "github.com/v2ray/v2ray-core/common/log/internal" - "github.com/v2ray/v2ray-core/common/serial" ) // AccessStatus is the status of an access request from clients. @@ -29,7 +30,7 @@ func InitAccessLogger(file string) error { } // Access writes an access log. -func Access(from, to serial.String, status AccessStatus, reason serial.String) { +func Access(from, to fmt.Stringer, status AccessStatus, reason fmt.Stringer) { accessLoggerInstance.Log(&internal.AccessLog{ From: from, To: to, diff --git a/common/log/internal/log_entry.go b/common/log/internal/log_entry.go index abaa92d74..8e87d8910 100644 --- a/common/log/internal/log_entry.go +++ b/common/log/internal/log_entry.go @@ -5,12 +5,11 @@ import ( "github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common/alloc" - "github.com/v2ray/v2ray-core/common/serial" ) type LogEntry interface { common.Releasable - serial.String + fmt.Stringer } type ErrorLog struct { @@ -37,7 +36,7 @@ func (this *ErrorLog) String() string { b.AppendString(typedVal) case *string: b.AppendString(*typedVal) - case serial.String: + case fmt.Stringer: b.AppendString(typedVal.String()) case error: b.AppendString(typedVal.Error()) @@ -49,10 +48,10 @@ func (this *ErrorLog) String() string { } type AccessLog struct { - From serial.String - To serial.String + From fmt.Stringer + To fmt.Stringer Status string - Reason serial.String + Reason fmt.Stringer } func (this *AccessLog) Release() { diff --git a/common/log/internal/log_entry_test.go b/common/log/internal/log_entry_test.go index 61ce83c88..c28a6db59 100644 --- a/common/log/internal/log_entry_test.go +++ b/common/log/internal/log_entry_test.go @@ -5,12 +5,11 @@ import ( . "github.com/v2ray/v2ray-core/common/log/internal" "github.com/v2ray/v2ray-core/common/serial" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestAccessLog(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) entry := &AccessLog{ From: serial.StringT("test_from"), @@ -20,8 +19,8 @@ func TestAccessLog(t *testing.T) { } entryStr := entry.String() - assert.StringLiteral(entryStr).Contains(serial.StringT("test_from")) - assert.StringLiteral(entryStr).Contains(serial.StringT("test_to")) - assert.StringLiteral(entryStr).Contains(serial.StringT("test_reason")) - assert.StringLiteral(entryStr).Contains(serial.StringT("Accepted")) + assert.String(entryStr).Contains("test_from") + assert.String(entryStr).Contains("test_to") + assert.String(entryStr).Contains("test_reason") + assert.String(entryStr).Contains("Accepted") } diff --git a/common/net/address_json_test.go b/common/net/address_json_test.go index 03766d8ce..1344b3ef4 100644 --- a/common/net/address_json_test.go +++ b/common/net/address_json_test.go @@ -8,12 +8,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestIPParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := "\"8.8.8.8\"" var address AddressJson @@ -25,7 +24,7 @@ func TestIPParsing(t *testing.T) { } func TestDomainParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := "\"v2ray.com\"" var address AddressJson @@ -33,11 +32,11 @@ func TestDomainParsing(t *testing.T) { assert.Error(err).IsNil() assert.Bool(address.Address.IsIPv4()).IsFalse() assert.Bool(address.Address.IsDomain()).IsTrue() - assert.StringLiteral(address.Address.Domain()).Equals("v2ray.com") + assert.String(address.Address.Domain()).Equals("v2ray.com") } func TestInvalidAddressJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := "1234" var address AddressJson diff --git a/common/net/address_test.go b/common/net/address_test.go index 962601d3e..2767d9d0c 100644 --- a/common/net/address_test.go +++ b/common/net/address_test.go @@ -5,12 +5,11 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestIPv4Address(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) ip := []byte{byte(1), byte(2), byte(3), byte(4)} addr := v2net.IPAddress(ip) @@ -19,11 +18,11 @@ func TestIPv4Address(t *testing.T) { assert.Address(addr).IsNotIPv6() assert.Address(addr).IsNotDomain() assert.Bytes(addr.IP()).Equals(ip) - assert.String(addr).Equals("1.2.3.4") + assert.Address(addr).EqualsString("1.2.3.4") } func TestIPv6Address(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) ip := []byte{ byte(1), byte(2), byte(3), byte(4), @@ -36,12 +35,12 @@ func TestIPv6Address(t *testing.T) { assert.Address(addr).IsIPv6() assert.Address(addr).IsNotIPv4() assert.Address(addr).IsNotDomain() - assert.Bytes(addr.IP()).Equals(ip) - assert.String(addr).Equals("[102:304:102:304:102:304:102:304]") + assert.IP(addr.IP()).Equals(net.IP(ip)) + assert.Address(addr).EqualsString("[102:304:102:304:102:304:102:304]") } func TestIPv4Asv6(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) ip := []byte{ byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), @@ -49,11 +48,11 @@ func TestIPv4Asv6(t *testing.T) { byte(1), byte(2), byte(3), byte(4), } addr := v2net.IPAddress(ip) - assert.String(addr).Equals("1.2.3.4") + assert.Address(addr).EqualsString("1.2.3.4") } func TestDomainAddress(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) domain := "v2ray.com" addr := v2net.DomainAddress(domain) @@ -61,37 +60,37 @@ func TestDomainAddress(t *testing.T) { assert.Address(addr).IsDomain() assert.Address(addr).IsNotIPv6() assert.Address(addr).IsNotIPv4() - assert.StringLiteral(addr.Domain()).Equals(domain) - assert.String(addr).Equals("v2ray.com") + assert.String(addr.Domain()).Equals(domain) + assert.Address(addr).EqualsString("v2ray.com") } func TestNetIPv4Address(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) ip := net.IPv4(1, 2, 3, 4) addr := v2net.IPAddress(ip) assert.Address(addr).IsIPv4() - assert.String(addr).Equals("1.2.3.4") + assert.Address(addr).EqualsString("1.2.3.4") } func TestIPv4AddressEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) addr := v2net.IPAddress([]byte{1, 2, 3, 4}) - assert.Bool(addr.Equals(nil)).IsFalse() + assert.Address(addr).NotEquals(nil) addr2 := v2net.IPAddress([]byte{1, 2, 3, 4}) - assert.Bool(addr.Equals(addr2)).IsTrue() + assert.Address(addr).Equals(addr2) addr3 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) - assert.Bool(addr.Equals(addr3)).IsFalse() + assert.Address(addr).NotEquals(addr3) addr4 := v2net.IPAddress([]byte{1, 2, 3, 5}) - assert.Bool(addr.Equals(addr4)).IsFalse() + assert.Address(addr).NotEquals(addr4) } func TestIPv6AddressEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) addr := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Bool(addr.Equals(nil)).IsFalse() @@ -107,7 +106,7 @@ func TestIPv6AddressEquals(t *testing.T) { } func TestDomainAddressEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) addr := v2net.DomainAddress("v2ray.com") assert.Bool(addr.Equals(nil)).IsFalse() diff --git a/common/net/destination_test.go b/common/net/destination_test.go index 80166b681..7fcc22f19 100644 --- a/common/net/destination_test.go +++ b/common/net/destination_test.go @@ -4,30 +4,29 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestTCPDestination(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Destination(dest).IsTCP() assert.Destination(dest).IsNotUDP() - assert.String(dest).Equals("tcp:1.2.3.4:80") + assert.Destination(dest).EqualsString("tcp:1.2.3.4:80") } func TestUDPDestination(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) dest := v2net.UDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53) assert.Destination(dest).IsNotTCP() assert.Destination(dest).IsUDP() - assert.String(dest).Equals("udp:[2001:4860:4860::8888]:53") + assert.Destination(dest).EqualsString("udp:[2001:4860:4860::8888]:53") } func TestTCPDestinationEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Bool(dest.Equals(nil)).IsFalse() @@ -43,7 +42,7 @@ func TestTCPDestinationEquals(t *testing.T) { } func TestUDPDestinationEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) dest := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Bool(dest.Equals(nil)).IsFalse() diff --git a/common/net/ipnet_test.go b/common/net/ipnet_test.go index 137a64838..623111f17 100644 --- a/common/net/ipnet_test.go +++ b/common/net/ipnet_test.go @@ -5,18 +5,19 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func parseCIDR(str string) *net.IPNet { _, ipNet, err := net.ParseCIDR(str) - assert.Error(err).IsNil() + if err != nil { + panic(err) + } return ipNet } func TestIPNet(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) ipNet := NewIPNet() ipNet.Add(parseCIDR(("0.0.0.0/8"))) diff --git a/common/net/network_json_test.go b/common/net/network_json_test.go index e8f6b6cff..c66e9cbd5 100644 --- a/common/net/network_json_test.go +++ b/common/net/network_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestArrayNetworkList(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("[\"Tcp\"]"), &list) @@ -22,7 +21,7 @@ func TestArrayNetworkList(t *testing.T) { } func TestStringNetworkList(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("\"TCP, ip\""), &list) @@ -32,7 +31,7 @@ func TestStringNetworkList(t *testing.T) { } func TestInvalidNetworkJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("0"), &list) diff --git a/common/net/port.go b/common/net/port.go index da548b659..da3e34cc3 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -13,7 +13,7 @@ var ( ) // Port represents a network port in TCP and UDP protocol. -type Port serial.Uint16Literal +type Port uint16 // PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order. // @unsafe Caller must ensure that the byte array has at least 2 elements. @@ -47,12 +47,12 @@ func (this Port) Value() uint16 { // Bytes returns the correspoding bytes of this Port, in big endian order. func (this Port) Bytes() []byte { - return serial.Uint16Literal(this).Bytes() + return serial.Uint16ToBytes(this.Value()) } // String returns the string presentation of this Port. func (this Port) String() string { - return serial.Uint16Literal(this).String() + return serial.Uint16ToString(this.Value()) } // PortRange represents a range of ports. diff --git a/common/net/port_json_test.go b/common/net/port_json_test.go index cf5301f70..2279a7b0b 100644 --- a/common/net/port_json_test.go +++ b/common/net/port_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestIntPort(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var portRange PortRange err := json.Unmarshal([]byte("1234"), &portRange) @@ -23,7 +22,7 @@ func TestIntPort(t *testing.T) { } func TestOverRangeIntPort(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var portRange PortRange err := json.Unmarshal([]byte("70000"), &portRange) @@ -34,7 +33,7 @@ func TestOverRangeIntPort(t *testing.T) { } func TestSingleStringPort(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var portRange PortRange err := json.Unmarshal([]byte("\"1234\""), &portRange) @@ -45,7 +44,7 @@ func TestSingleStringPort(t *testing.T) { } func TestStringPairPort(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var portRange PortRange err := json.Unmarshal([]byte("\"1234-5678\""), &portRange) @@ -56,7 +55,7 @@ func TestStringPairPort(t *testing.T) { } func TestOverRangeStringPort(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var portRange PortRange err := json.Unmarshal([]byte("\"65536\""), &portRange) diff --git a/common/net/port_test.go b/common/net/port_test.go index d1570a4d6..80f52c3ca 100644 --- a/common/net/port_test.go +++ b/common/net/port_test.go @@ -4,12 +4,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestPortRangeContains(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) portRange := &PortRange{ From: Port(53), diff --git a/common/net/timed_io_test.go b/common/net/timed_io_test.go index ecf8fed81..305a93de8 100644 --- a/common/net/timed_io_test.go +++ b/common/net/timed_io_test.go @@ -4,12 +4,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestTimeOutSettings(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) reader := NewTimeOutReader(8, nil) assert.Int(reader.GetTimeOut()).Equals(8) diff --git a/common/protocol/headers.go b/common/protocol/headers.go index 2308a2f23..e45a51e70 100644 --- a/common/protocol/headers.go +++ b/common/protocol/headers.go @@ -2,7 +2,6 @@ package protocol import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/uuid" ) @@ -49,7 +48,7 @@ type CommandSwitchAccount struct { Host v2net.Address Port v2net.Port ID *uuid.UUID - AlterIds serial.Uint16Literal + AlterIds uint16 Level UserLevel ValidMin byte } diff --git a/common/protocol/id_test.go b/common/protocol/id_test.go index d251496e6..5df9d87e5 100644 --- a/common/protocol/id_test.go +++ b/common/protocol/id_test.go @@ -6,12 +6,11 @@ import ( . "github.com/v2ray/v2ray-core/common/protocol" "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/uuid" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestCmdKey(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) id := NewID(uuid.New()) assert.Bool(serial.BytesT(id.CmdKey()).All(0)).IsFalse() diff --git a/common/protocol/raw/commands.go b/common/protocol/raw/commands.go index a1b947cf5..01f4e9285 100644 --- a/common/protocol/raw/commands.go +++ b/common/protocol/raw/commands.go @@ -99,7 +99,7 @@ func (this *CommandSwitchAccountFactory) Marshal(command interface{}, writer io. idBytes := cmd.ID.Bytes() writer.Write(idBytes) - writer.Write(cmd.AlterIds.Bytes()) + writer.Write(serial.Uint16ToBytes(cmd.AlterIds)) writer.Write([]byte{byte(cmd.Level)}) writer.Write([]byte{cmd.ValidMin}) @@ -132,7 +132,7 @@ func (this *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, er if len(data) < alterIdStart+2 { return nil, transport.ErrorCorruptedPacket } - cmd.AlterIds = serial.BytesT(data[alterIdStart : alterIdStart+2]).Uint16() + cmd.AlterIds = serial.BytesToUint16(data[alterIdStart : alterIdStart+2]) levelStart := alterIdStart + 2 if len(data) < levelStart+1 { return nil, transport.ErrorCorruptedPacket diff --git a/common/protocol/raw/commands_test.go b/common/protocol/raw/commands_test.go index 7a553959e..ab334e2db 100644 --- a/common/protocol/raw/commands_test.go +++ b/common/protocol/raw/commands_test.go @@ -7,12 +7,11 @@ import ( "github.com/v2ray/v2ray-core/common/protocol" . "github.com/v2ray/v2ray-core/common/protocol/raw" "github.com/v2ray/v2ray-core/common/uuid" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestSwitchAccount(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, @@ -34,8 +33,8 @@ func TestSwitchAccount(t *testing.T) { assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() assert.Port(sa.Port).Equals(sa2.Port) - assert.String(sa.ID).Equals(sa2.ID.String()) - assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) + assert.String(sa.ID.String()).Equals(sa2.ID.String()) + assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) } diff --git a/common/protocol/raw/encoding_test.go b/common/protocol/raw/encoding_test.go index a8fd2394f..7164b7f46 100644 --- a/common/protocol/raw/encoding_test.go +++ b/common/protocol/raw/encoding_test.go @@ -8,12 +8,11 @@ import ( "github.com/v2ray/v2ray-core/common/protocol" . "github.com/v2ray/v2ray-core/common/protocol/raw" "github.com/v2ray/v2ray-core/common/uuid" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestRequestSerialization(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) user := protocol.NewUser( protocol.NewID(uuid.New()), diff --git a/common/protocol/time_test.go b/common/protocol/time_test.go index d72ed10ed..5fd7d6b55 100644 --- a/common/protocol/time_test.go +++ b/common/protocol/time_test.go @@ -5,12 +5,11 @@ import ( "time" . "github.com/v2ray/v2ray-core/common/protocol" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestGenerateRandomInt64InRange(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) base := time.Now().Unix() delta := 100 diff --git a/common/protocol/user_json_test.go b/common/protocol/user_json_test.go index 0d3dd0558..aac35a7cc 100644 --- a/common/protocol/user_json_test.go +++ b/common/protocol/user_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/protocol" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestUserParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{ @@ -22,12 +21,12 @@ func TestUserParsing(t *testing.T) { "alterId": 100 }`), user) assert.Error(err).IsNil() - assert.String(user.ID).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09") + assert.String(user.ID.String()).Equals("96edb838-6d68-42ef-a933-25f7ac3a9d09") assert.Byte(byte(user.Level)).Equals(1) } func TestInvalidUserJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{"id": 1234}`), user) @@ -35,7 +34,7 @@ func TestInvalidUserJson(t *testing.T) { } func TestInvalidIdJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{"id": "1234"}`), user) diff --git a/common/retry/retry_test.go b/common/retry/retry_test.go index 0b245cdfb..c2976850b 100644 --- a/common/retry/retry_test.go +++ b/common/retry/retry_test.go @@ -6,7 +6,6 @@ import ( "time" . "github.com/v2ray/v2ray-core/common/retry" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -15,7 +14,7 @@ var ( ) func TestNoRetry(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) startTime := time.Now().Unix() err := Timed(10, 100000).On(func() error { @@ -28,7 +27,7 @@ func TestNoRetry(t *testing.T) { } func TestRetryOnce(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) startTime := time.Now() called := 0 @@ -46,7 +45,7 @@ func TestRetryOnce(t *testing.T) { } func TestRetryMultiple(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) startTime := time.Now() called := 0 @@ -64,7 +63,7 @@ func TestRetryMultiple(t *testing.T) { } func TestRetryExhausted(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) startTime := time.Now() called := 0 diff --git a/common/serial/bytes.go b/common/serial/bytes.go index ec623d6f6..c086933f7 100644 --- a/common/serial/bytes.go +++ b/common/serial/bytes.go @@ -2,10 +2,24 @@ package serial import ( "bytes" + "encoding/hex" + "strings" ) -type Bytes interface { - Bytes() []byte +func ByteToHexString(value byte) string { + return hex.EncodeToString([]byte{value}) +} + +func BytesToUint16(value []byte) uint16 { + return uint16(value[0])<<8 + uint16(value[1]) +} + +func BytesToHexString(value []byte) string { + strs := make([]string, len(value)) + for i, b := range value { + strs[i] = hex.EncodeToString([]byte{b}) + } + return "[" + strings.Join(strs, ",") + "]" } type BytesT []byte diff --git a/common/serial/interface.go b/common/serial/interface.go new file mode 100644 index 000000000..a032dbeb3 --- /dev/null +++ b/common/serial/interface.go @@ -0,0 +1,9 @@ +package serial + +import ( + "fmt" +) + +func PointerToString(value interface{}) string { + return fmt.Sprint(value) +} diff --git a/common/serial/numbers.go b/common/serial/numbers.go index fe5965639..62013f869 100644 --- a/common/serial/numbers.go +++ b/common/serial/numbers.go @@ -4,6 +4,22 @@ import ( "strconv" ) +func Uint16ToBytes(value uint16) []byte { + return []byte{byte(value >> 8), byte(value)} +} + +func Uint16ToString(value uint16) string { + return strconv.Itoa(int(value)) +} + +func IntToString(value int) string { + return Int64ToString(int64(value)) +} + +func Int64ToString(value int64) string { + return strconv.FormatInt(value, 10) +} + type Uint16 interface { Value() uint16 } diff --git a/common/serial/string_json_test.go b/common/serial/string_json_test.go index d69bb2b23..a39115c34 100644 --- a/common/serial/string_json_test.go +++ b/common/serial/string_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/serial" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestInvalidStringTJson(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var s StringT err := json.Unmarshal([]byte("1"), &s) @@ -20,10 +19,10 @@ func TestInvalidStringTJson(t *testing.T) { } func TestStringTParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var s StringT err := json.Unmarshal([]byte("\"1\""), &s) assert.Error(err).IsNil() - assert.String(s).Equals("1") + assert.String(s.String()).Equals("1") } diff --git a/common/serial/string_list_json_test.go b/common/serial/string_list_json_test.go index 70bdaab47..39a561044 100644 --- a/common/serial/string_list_json_test.go +++ b/common/serial/string_list_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/serial" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestStringListUnmarshalError(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `1234` list := new(StringTList) diff --git a/common/serial/string_test.go b/common/serial/string_test.go deleted file mode 100644 index e197203f7..000000000 --- a/common/serial/string_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package serial_test - -import ( - "testing" - - . "github.com/v2ray/v2ray-core/common/serial" - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -type TestString struct { - value string -} - -func (this *TestString) String() string { - return this.value -} - -func TestNewStringSerial(t *testing.T) { - v2testing.Current(t) - - testString := &TestString{value: "abcd"} - assert.String(NewStringT(testString)).Equals("abcd") -} diff --git a/common/uuid/uuid_test.go b/common/uuid/uuid_test.go index f38d1743c..309f89017 100644 --- a/common/uuid/uuid_test.go +++ b/common/uuid/uuid_test.go @@ -4,26 +4,25 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/uuid" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestParseBytes(t *testing.T) { - v2testing.Current(t) + assert := assert.On(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.Error(err).IsNil() - assert.String(uuid).Equals(str) + assert.String(uuid.String()).Equals(str) _, err = ParseBytes([]byte{1, 3, 2, 4}) assert.Error(err).Equals(ErrorInvalidID) } func TestParseString(t *testing.T) { - v2testing.Current(t) + assert := assert.On(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} @@ -40,28 +39,28 @@ func TestParseString(t *testing.T) { } func TestNewUUID(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) uuid := New() uuid2, err := ParseString(uuid.String()) assert.Error(err).IsNil() - assert.StringLiteral(uuid.String()).Equals(uuid2.String()) + assert.String(uuid.String()).Equals(uuid2.String()) assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes()) } func TestRandom(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) uuid := New() uuid2 := New() - assert.StringLiteral(uuid.String()).NotEquals(uuid2.String()) + assert.String(uuid.String()).NotEquals(uuid2.String()) assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes()) } func TestEquals(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) var uuid *UUID = nil var uuid2 *UUID = nil @@ -70,7 +69,7 @@ func TestEquals(t *testing.T) { } func TestNext(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) uuid := New() uuid2 := uuid.Next() diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index fa84cbaa9..a37486516 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -12,14 +12,13 @@ import ( v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" . "github.com/v2ray/v2ray-core/proxy/dokodemo" "github.com/v2ray/v2ray-core/proxy/freedom" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" "github.com/v2ray/v2ray-core/testing/servers/udp" ) func TestDokodemoTCP(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) tcpServer := &tcp.Server{ Port: v2nettesting.PickPort(), @@ -73,11 +72,11 @@ func TestDokodemoTCP(t *testing.T) { assert.Error(err).IsNil() tcpClient.Close() - assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) + assert.String("Processed: " + data2Send).Equals(string(response[:nBytes])) } func TestDokodemoUDP(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) udpServer := &udp.Server{ Port: v2nettesting.PickPort(), diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index cd75c75f9..f4aa5a66f 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -15,14 +15,13 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" . "github.com/v2ray/v2ray-core/proxy/freedom" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" "github.com/v2ray/v2ray-core/transport/ray" ) func TestSinglePacket(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) port := v2nettesting.PickPort() tcpServer := &tcp.Server{ @@ -56,7 +55,7 @@ func TestSinglePacket(t *testing.T) { } func TestUnreachableDestination(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) freedom := NewFreedomConnection(&Config{}, app.NewSpace()) traffic := ray.NewRay() @@ -68,7 +67,7 @@ func TestUnreachableDestination(t *testing.T) { } func TestIPResolution(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) diff --git a/proxy/http/config_json_test.go b/proxy/http/config_json_test.go index a34baf547..854bdf265 100644 --- a/proxy/http/config_json_test.go +++ b/proxy/http/config_json_test.go @@ -8,12 +8,11 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" . "github.com/v2ray/v2ray-core/proxy/http" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestOwnHosts(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `{ "ownHosts": [ diff --git a/proxy/http/server_test.go b/proxy/http/server_test.go index e3625452e..41dcff927 100644 --- a/proxy/http/server_test.go +++ b/proxy/http/server_test.go @@ -9,12 +9,11 @@ import ( testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" . "github.com/v2ray/v2ray-core/proxy/http" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestHopByHopHeadersStrip(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawRequest := `GET /pkg/net/http/ HTTP/1.1 Host: golang.org @@ -33,22 +32,22 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3 b := bufio.NewReader(strings.NewReader(rawRequest)) req, err := http.ReadRequest(b) assert.Error(err).IsNil() - assert.StringLiteral(req.Header.Get("Foo")).Equals("foo") - assert.StringLiteral(req.Header.Get("Bar")).Equals("bar") - assert.StringLiteral(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar") - assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("keep-alive") - assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("abc") + assert.String(req.Header.Get("Foo")).Equals("foo") + assert.String(req.Header.Get("Bar")).Equals("bar") + assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar") + assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive") + assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc") StripHopByHopHeaders(req) - assert.StringLiteral(req.Header.Get("Connection")).Equals("close") - assert.StringLiteral(req.Header.Get("Foo")).Equals("") - assert.StringLiteral(req.Header.Get("Bar")).Equals("") - assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("") - assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("") + assert.String(req.Header.Get("Connection")).Equals("close") + assert.String(req.Header.Get("Foo")).Equals("") + assert.String(req.Header.Get("Bar")).Equals("") + assert.String(req.Header.Get("Proxy-Connection")).Equals("") + assert.String(req.Header.Get("Proxy-Authenticate")).Equals("") } func TestNormalGetRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) diff --git a/proxy/internal/config/config_cache_test.go b/proxy/internal/config/config_cache_test.go index 82ad2b274..1c5066845 100644 --- a/proxy/internal/config/config_cache_test.go +++ b/proxy/internal/config/config_cache_test.go @@ -3,12 +3,11 @@ package config import ( "testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestRegisterInboundConfig(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) initializeConfigCache() protocol := "test_protocol" @@ -29,7 +28,7 @@ func TestRegisterInboundConfig(t *testing.T) { } func TestRegisterOutboundConfig(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) initializeConfigCache() protocol := "test_protocol" diff --git a/proxy/shadowsocks/config_json_test.go b/proxy/shadowsocks/config_json_test.go index 36563b108..9f4783289 100644 --- a/proxy/shadowsocks/config_json_test.go +++ b/proxy/shadowsocks/config_json_test.go @@ -6,12 +6,11 @@ import ( "encoding/json" "testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestConfigParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `{ "method": "aes-128-cfb", diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index 44564ea96..4488e8d2c 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -5,12 +5,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" . "github.com/v2ray/v2ray-core/proxy/shadowsocks" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestNormalChunkReading(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes( 0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18) diff --git a/proxy/shadowsocks/protocol_test.go b/proxy/shadowsocks/protocol_test.go index fbcad62fc..0c0e3c10f 100644 --- a/proxy/shadowsocks/protocol_test.go +++ b/proxy/shadowsocks/protocol_test.go @@ -7,13 +7,12 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy" . "github.com/v2ray/v2ray-core/proxy/shadowsocks" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/transport" ) func TestNormalRequestParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80) @@ -26,7 +25,7 @@ func TestNormalRequestParsing(t *testing.T) { } func TestEmptyPayload(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() _, err := ReadRequest(buffer, nil, false) @@ -34,7 +33,7 @@ func TestEmptyPayload(t *testing.T) { } func TestSingleBytePayload(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1) _, err := ReadRequest(buffer, nil, false) @@ -42,7 +41,7 @@ func TestSingleBytePayload(t *testing.T) { } func TestWrongAddressType(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(5) _, err := ReadRequest(buffer, nil, false) @@ -50,7 +49,7 @@ func TestWrongAddressType(t *testing.T) { } func TestInsufficientAddressRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1, 1) _, err := ReadRequest(buffer, nil, false) @@ -66,7 +65,7 @@ func TestInsufficientAddressRequest(t *testing.T) { } func TestInsufficientPortRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1, 1, 2, 3, 4, 5) _, err := ReadRequest(buffer, nil, false) @@ -74,7 +73,7 @@ func TestInsufficientPortRequest(t *testing.T) { } func TestOTARequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 0) @@ -89,7 +88,7 @@ func TestOTARequest(t *testing.T) { } func TestInvalidOTARequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 1) @@ -102,7 +101,7 @@ func TestInvalidOTARequest(t *testing.T) { } func TestUDPRequestParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80, 1, 2, 3, 4, 5, 6) @@ -116,7 +115,7 @@ func TestUDPRequestParsing(t *testing.T) { } func TestUDPRequestWithOTA(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() buffer.AppendBytes( diff --git a/proxy/socks/config_json_test.go b/proxy/socks/config_json_test.go index 603390557..c392f0ef8 100644 --- a/proxy/socks/config_json_test.go +++ b/proxy/socks/config_json_test.go @@ -7,16 +7,15 @@ import ( "github.com/v2ray/v2ray-core/proxy/internal/config" "github.com/v2ray/v2ray-core/proxy/socks" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestDefaultIPAddress(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) socksConfig, err := config.CreateInboundConfig("socks", []byte(`{ "auth": "noauth" }`)) assert.Error(err).IsNil() - assert.String(socksConfig.(*socks.Config).Address).Equals("127.0.0.1") + assert.Address(socksConfig.(*socks.Config).Address).EqualsString("127.0.0.1") } diff --git a/proxy/socks/protocol/socks4_test.go b/proxy/socks/protocol/socks4_test.go index a7105f5c7..88895bb84 100644 --- a/proxy/socks/protocol/socks4_test.go +++ b/proxy/socks/protocol/socks4_test.go @@ -6,12 +6,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestSocks4AuthenticationRequestRead(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawRequest := []byte{ 0x04, // version @@ -21,14 +20,14 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) { } _, request4, err := ReadAuthentication(bytes.NewReader(rawRequest)) assert.Error(err).Equals(Socks4Downgrade) - assert.Byte(request4.Version).Named("Version").Equals(0x04) - assert.Byte(request4.Command).Named("Command").Equals(0x01) - assert.Port(request4.Port).Named("Port").Equals(v2net.Port(53)) - assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72}) + assert.Byte(request4.Version).Equals(0x04) + assert.Byte(request4.Command).Equals(0x01) + assert.Port(request4.Port).Equals(v2net.Port(53)) + assert.Bytes(request4.IP[:]).Equals([]byte{0x72, 0x72, 0x72, 0x72}) } func TestSocks4AuthenticationResponseToBytes(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4}) diff --git a/proxy/socks/protocol/socks_test.go b/proxy/socks/protocol/socks_test.go index 68046c729..6fb2acd2e 100644 --- a/proxy/socks/protocol/socks_test.go +++ b/proxy/socks/protocol/socks_test.go @@ -8,13 +8,12 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/proxy" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/transport" ) func TestHasAuthenticationMethod(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) request := Socks5AuthenticationRequest{ version: socksVersion, @@ -29,7 +28,7 @@ func TestHasAuthenticationMethod(t *testing.T) { } func TestAuthenticationRequestRead(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes( 0x05, // version @@ -38,13 +37,13 @@ func TestAuthenticationRequestRead(t *testing.T) { ) request, _, err := ReadAuthentication(buffer) 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) + assert.Byte(request.version).Equals(0x05) + assert.Byte(request.nMethods).Equals(0x01) + assert.Byte(request.authMethods[0]).Equals(0x02) } func TestAuthenticationResponseWrite(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) response := NewAuthenticationResponse(byte(0x05)) @@ -54,7 +53,7 @@ func TestAuthenticationResponseWrite(t *testing.T) { } func TestRequestRead(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawRequest := []byte{ 0x05, // version @@ -66,15 +65,15 @@ func TestRequestRead(t *testing.T) { } request, err := ReadRequest(bytes.NewReader(rawRequest)) 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) - assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72}) - assert.Port(request.Port).Named("Port").Equals(v2net.Port(53)) + assert.Byte(request.Version).Equals(0x05) + assert.Byte(request.Command).Equals(0x01) + assert.Byte(request.AddrType).Equals(0x01) + assert.Bytes(request.IPv4[:]).Equals([]byte{0x72, 0x72, 0x72, 0x72}) + assert.Port(request.Port).Equals(v2net.Port(53)) } func TestResponseWrite(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) response := Socks5Response{ socksVersion, @@ -97,11 +96,11 @@ func TestResponseWrite(t *testing.T) { 0x72, 0x72, 0x72, 0x72, byte(0x00), byte(0x035), } - assert.Bytes(buffer.Value).Named("raw response").Equals(expectedBytes) + assert.Bytes(buffer.Value).Equals(expectedBytes) } func TestSetIPv6(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) response := NewSocks5Response() response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}) @@ -114,7 +113,7 @@ func TestSetIPv6(t *testing.T) { } func TestSetDomain(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) response := NewSocks5Response() response.SetDomain("v2ray.com") @@ -127,28 +126,28 @@ func TestSetDomain(t *testing.T) { } func TestEmptyAuthRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) _, _, err := ReadAuthentication(alloc.NewBuffer().Clear()) assert.Error(err).Equals(io.EOF) } func TestSingleByteAuthRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1))) assert.Error(err).Equals(transport.ErrorCorruptedPacket) } func TestZeroAuthenticationMethod(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes(5, 0) _, _, err := ReadAuthentication(buffer) assert.Error(err).Equals(proxy.ErrorInvalidAuthentication) } func TestWrongProtocolVersion(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes(6, 1, 0) _, _, err := ReadAuthentication(buffer) @@ -156,14 +155,14 @@ func TestWrongProtocolVersion(t *testing.T) { } func TestEmptyRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) _, err := ReadRequest(alloc.NewBuffer().Clear()) assert.Error(err).Equals(io.EOF) } func TestIPv6Request(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) request, err := ReadRequest(alloc.NewBuffer().Clear().AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8)) assert.Error(err).IsNil() diff --git a/proxy/socks/protocol/udp_test.go b/proxy/socks/protocol/udp_test.go index 152aadfb2..d65acd728 100644 --- a/proxy/socks/protocol/udp_test.go +++ b/proxy/socks/protocol/udp_test.go @@ -4,13 +4,12 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/transport" ) func TestSingleByteUDPRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) request, err := ReadUDPRequest(make([]byte, 1)) if request != nil { @@ -20,7 +19,7 @@ func TestSingleByteUDPRequest(t *testing.T) { } func TestDomainAddressRequest(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) payload := make([]byte, 0, 1024) payload = append(payload, 0, 0, 1, AddrTypeDomain, byte(len("v2ray.com"))) @@ -32,7 +31,7 @@ func TestDomainAddressRequest(t *testing.T) { assert.Error(err).IsNil() assert.Byte(request.Fragment).Equals(1) - assert.String(request.Address).Equals("v2ray.com") + assert.Address(request.Address).EqualsString("v2ray.com") assert.Port(request.Port).Equals(v2net.Port(80)) assert.Bytes(request.Data.Value).Equals([]byte("Actual payload")) } diff --git a/proxy/socks/server_test.go b/proxy/socks/server_test.go index 224c44638..ccfd2f150 100644 --- a/proxy/socks/server_test.go +++ b/proxy/socks/server_test.go @@ -17,12 +17,11 @@ import ( proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/shell/point" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestSocksTcpConnect(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") @@ -82,11 +81,11 @@ func TestSocksTcpConnect(t *testing.T) { assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes()) assert.Bytes(dataReturned).Equals(connInput) - assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr()) + assert.String(targetServer).Equals(och.Destination.NetAddr()) } func TestSocksTcpConnectWithUserPass(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") @@ -149,11 +148,11 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes()) assert.Bytes(dataReturned).Equals(connInput) - assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr()) + assert.String(targetServer).Equals(och.Destination.NetAddr()) } func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") @@ -206,7 +205,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { } func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) port := v2nettesting.PickPort() connInput := []byte("The data to be returned to socks server.") diff --git a/proxy/vmess/inbound/command.go b/proxy/vmess/inbound/command.go index 0f234ca42..b614ff2ec 100644 --- a/proxy/vmess/inbound/command.go +++ b/proxy/vmess/inbound/command.go @@ -3,7 +3,6 @@ package inbound import ( "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/protocol" - "github.com/v2ray/v2ray-core/common/serial" ) func (this *VMessInboundHandler) generateCommand(request *protocol.RequestHeader) protocol.ResponseCommand { @@ -23,7 +22,7 @@ func (this *VMessInboundHandler) generateCommand(request *protocol.RequestHeader return &protocol.CommandSwitchAccount{ Port: inboundHandler.Port(), ID: user.ID.UUID(), - AlterIds: serial.Uint16Literal(len(user.AlterIDs)), + AlterIds: uint16(len(user.AlterIDs)), Level: user.Level, ValidMin: byte(availableMin), } diff --git a/proxy/vmess/io/io_test.go b/proxy/vmess/io/io_test.go index c271e2867..232d95448 100644 --- a/proxy/vmess/io/io_test.go +++ b/proxy/vmess/io/io_test.go @@ -9,12 +9,11 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" v2io "github.com/v2ray/v2ray-core/common/io" . "github.com/v2ray/v2ray-core/proxy/vmess/io" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestAuthenticate(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) buffer := alloc.NewBuffer().Clear() buffer.AppendBytes(1, 2, 3, 4) @@ -27,7 +26,7 @@ func TestAuthenticate(t *testing.T) { } func TestSingleIO(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) content := bytes.NewBuffer(make([]byte, 0, 1024*1024)) @@ -42,7 +41,7 @@ func TestSingleIO(t *testing.T) { } func TestLargeIO(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) content := make([]byte, 1024*1024) rand.Read(content) diff --git a/proxy/vmess/io/writer.go b/proxy/vmess/io/writer.go index 1bff745c0..ac4b6f3ec 100644 --- a/proxy/vmess/io/writer.go +++ b/proxy/vmess/io/writer.go @@ -35,5 +35,5 @@ func Authenticate(buffer *alloc.Buffer) { buffer.SliceBack(4) fnvHash.Sum(buffer.Value[:0]) - buffer.Prepend(serial.Uint16Literal(uint16(buffer.Len())).Bytes()) + buffer.Prepend(serial.Uint16ToBytes(uint16(buffer.Len()))) } diff --git a/proxy/vmess/outbound/command.go b/proxy/vmess/outbound/command.go index f7fabf6e0..6701c543a 100644 --- a/proxy/vmess/outbound/command.go +++ b/proxy/vmess/outbound/command.go @@ -7,7 +7,7 @@ import ( func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitchAccount) { primary := protocol.NewID(cmd.ID) - alters := protocol.NewAlterIDs(primary, cmd.AlterIds.Value()) + alters := protocol.NewAlterIDs(primary, cmd.AlterIds) user := protocol.NewUser(primary, alters, cmd.Level, "") dest := v2net.TCPDestination(cmd.Host, cmd.Port) this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin) diff --git a/proxy/vmess/outbound/receiver_json_test.go b/proxy/vmess/outbound/receiver_json_test.go index bffbc21cd..b8b08f86d 100644 --- a/proxy/vmess/outbound/receiver_json_test.go +++ b/proxy/vmess/outbound/receiver_json_test.go @@ -7,12 +7,11 @@ import ( "testing" . "github.com/v2ray/v2ray-core/proxy/vmess/outbound" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestConfigTargetParsing(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `{ "address": "127.0.0.1", @@ -29,7 +28,7 @@ func TestConfigTargetParsing(t *testing.T) { receiver := new(Receiver) err := json.Unmarshal([]byte(rawJson), &receiver) assert.Error(err).IsNil() - assert.String(receiver.Destination).Equals("tcp:127.0.0.1:80") + assert.Destination(receiver.Destination).EqualsString("tcp:127.0.0.1:80") assert.Int(len(receiver.Accounts)).Equals(1) - assert.String(receiver.Accounts[0].ID).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019") + assert.String(receiver.Accounts[0].ID.String()).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019") } diff --git a/proxy/vmess/outbound/receiver_test.go b/proxy/vmess/outbound/receiver_test.go index 53fe8a0e5..dfeb34793 100644 --- a/proxy/vmess/outbound/receiver_test.go +++ b/proxy/vmess/outbound/receiver_test.go @@ -7,12 +7,11 @@ import ( "github.com/v2ray/v2ray-core/common/protocol" "github.com/v2ray/v2ray-core/common/uuid" . "github.com/v2ray/v2ray-core/proxy/vmess/outbound" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestReceiverUser(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) id := protocol.NewID(uuid.New()) alters := protocol.NewAlterIDs(id, 100) diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index 326f677ee..370a41ad2 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -17,12 +17,11 @@ import ( _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" "github.com/v2ray/v2ray-core/shell/point" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestVMessInAndOut(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() diff --git a/shell/point/config_json_test.go b/shell/point/config_json_test.go index a2cdb4f3c..74d6a3d52 100644 --- a/shell/point/config_json_test.go +++ b/shell/point/config_json_test.go @@ -11,12 +11,11 @@ import ( _ "github.com/v2ray/v2ray-core/app/router/rules" . "github.com/v2ray/v2ray-core/shell/point" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestClientSampleConfig(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config") @@ -28,15 +27,15 @@ func TestClientSampleConfig(t *testing.T) { assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() - assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("socks") + assert.String(pointConfig.InboundConfig.Protocol).Equals("socks") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() - assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("vmess") + assert.String(pointConfig.OutboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() } func TestServerSampleConfig(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config") @@ -48,15 +47,15 @@ func TestServerSampleConfig(t *testing.T) { assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() - assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") + assert.String(pointConfig.InboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() - assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") + assert.String(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() } func TestDefaultValueOfRandomAllocation(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rawJson := `{ "protocol": "vmess", @@ -70,7 +69,7 @@ func TestDefaultValueOfRandomAllocation(t *testing.T) { inboundDetourConfig := new(InboundDetourConfig) err := json.Unmarshal([]byte(rawJson), inboundDetourConfig) assert.Error(err).IsNil() - assert.StringLiteral(inboundDetourConfig.Allocation.Strategy).Equals(AllocationStrategyRandom) + assert.String(inboundDetourConfig.Allocation.Strategy).Equals(AllocationStrategyRandom) assert.Int(inboundDetourConfig.Allocation.Concurrency).Equals(3) assert.Int(inboundDetourConfig.Allocation.Refresh).Equals(5) } diff --git a/testing/assert/address.go b/testing/assert/address.go index 4e2729b64..53b8afa38 100644 --- a/testing/assert/address.go +++ b/testing/assert/address.go @@ -2,11 +2,16 @@ package assert import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" ) -func Address(value v2net.Address) *AddressSubject { - return &AddressSubject{value: value} +func (this *Assert) Address(value v2net.Address) *AddressSubject { + return &AddressSubject{ + Subject: Subject{ + disp: value.String(), + a: this, + }, + value: value, + } } type AddressSubject struct { @@ -14,53 +19,62 @@ type AddressSubject struct { value v2net.Address } -func (subject *AddressSubject) Named(name string) *AddressSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *AddressSubject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) +func (subject *AddressSubject) NotEquals(another v2net.Address) { + if subject.value.Equals(another) { + subject.Fail("not equals to", another.String()) + } } func (subject *AddressSubject) Equals(another v2net.Address) { if !subject.value.Equals(another) { - subject.Fail(subject.DisplayString(), "equals to", another) + subject.Fail("equals to", another.String()) + } +} + +func (subject *AddressSubject) NotEqualsString(another string) { + if subject.value.String() == another { + subject.Fail("not equals to string", another) + } +} + +func (subject *AddressSubject) EqualsString(another string) { + if subject.value.String() != another { + subject.Fail("equals to string", another) } } func (subject *AddressSubject) IsIPv4() { if !subject.value.IsIPv4() { - subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv4 address")) + subject.Fail("is", "an IPv4 address") } } func (subject *AddressSubject) IsNotIPv4() { if subject.value.IsIPv4() { - subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv4 address")) + subject.Fail("is not", "an IPv4 address") } } func (subject *AddressSubject) IsIPv6() { if !subject.value.IsIPv6() { - subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv6 address")) + subject.Fail("is", "an IPv6 address") } } func (subject *AddressSubject) IsNotIPv6() { if subject.value.IsIPv6() { - subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv6 address")) + subject.Fail("is not", "an IPv6 address") } } func (subject *AddressSubject) IsDomain() { if !subject.value.IsDomain() { - subject.Fail(subject.DisplayString(), "is", serial.StringT("a domain address")) + subject.Fail("is", "a domain address") } } func (subject *AddressSubject) IsNotDomain() { if subject.value.IsDomain() { - subject.Fail(subject.DisplayString(), "is not", serial.StringT("a domain address")) + subject.Fail("is not", "a domain address") } } diff --git a/testing/global.go b/testing/assert/assert.go similarity index 88% rename from testing/global.go rename to testing/assert/assert.go index 39ea234b6..eaa5f682f 100644 --- a/testing/global.go +++ b/testing/assert/assert.go @@ -1,4 +1,4 @@ -package unit +package assert import ( "bytes" @@ -8,10 +8,19 @@ import ( "testing" ) -var tGlobal *testing.T +func On(t *testing.T) *Assert { + return &Assert{ + t: t, + } +} -func Current(t *testing.T) { - tGlobal = t +type Assert struct { + t *testing.T +} + +func (this *Assert) Fail(message string) { + fmt.Println(decorate(message)) + this.t.Fail() } func getCaller() (string, int) { @@ -59,8 +68,3 @@ func decorate(s string) string { buf.WriteByte('\n') return buf.String() } - -func Fail(message string) { - fmt.Println(decorate(message)) - tGlobal.Fail() -} diff --git a/testing/assert/bool.go b/testing/assert/bool.go new file mode 100644 index 000000000..070b8932a --- /dev/null +++ b/testing/assert/bool.go @@ -0,0 +1,42 @@ +package assert + +import ( + "strconv" +) + +// Assert on a boolean variable. +func (this *Assert) Bool(value bool) *BoolSubject { + return &BoolSubject{ + Subject: Subject{ + disp: strconv.FormatBool(value), + a: this, + }, + value: value, + } +} + +type BoolSubject struct { + Subject + value bool +} + +// to be equal to another boolean variable. +func (subject *BoolSubject) Equals(expectation bool) { + if subject.value != expectation { + subject.Fail("is equal to", strconv.FormatBool(expectation)) + } +} + +// to be true. +func (subject *BoolSubject) IsTrue() { + if subject.value != true { + subject.Fail("is", "True") + } +} + +// to be false. +func (subject *BoolSubject) IsFalse() { + if subject.value != false { + subject.Fail("is", "False") + } +} diff --git a/testing/assert/boolsubject.go b/testing/assert/boolsubject.go deleted file mode 100644 index 0eed2bcb7..000000000 --- a/testing/assert/boolsubject.go +++ /dev/null @@ -1,49 +0,0 @@ -package assert - -import ( - "strconv" -) - -// Assert on a boolean variable. -func Bool(value bool) *BoolSubject { - return &BoolSubject{value: value} -} - -type BoolSubject struct { - Subject - value bool -} - -func (subject *BoolSubject) Named(name string) *BoolSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *BoolSubject) Fail(verb string, other bool) { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatBool(other) + ">.") -} - -func (subject *BoolSubject) DisplayString() string { - return subject.Subject.DisplayString(strconv.FormatBool(subject.value)) -} - -// to be equal to another boolean variable. -func (subject *BoolSubject) Equals(expectation bool) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) - } -} - -// to be true. -func (subject *BoolSubject) IsTrue() { - if subject.value != true { - subject.Fail("is", true) - } -} - -// to be false. -func (subject *BoolSubject) IsFalse() { - if subject.value != false { - subject.Fail("is", false) - } -} diff --git a/testing/assert/byte.go b/testing/assert/byte.go new file mode 100644 index 000000000..11ea5eda8 --- /dev/null +++ b/testing/assert/byte.go @@ -0,0 +1,38 @@ +package assert + +import ( + "github.com/v2ray/v2ray-core/common/serial" +) + +func (this *Assert) Byte(value byte) *ByteSubject { + return &ByteSubject{ + Subject: Subject{ + disp: serial.ByteToHexString(value), + a: this, + }, + value: value, + } +} + +type ByteSubject struct { + Subject + value byte +} + +func (subject *ByteSubject) Equals(expectation byte) { + if subject.value != expectation { + subject.Fail("is equal to", serial.ByteToHexString(expectation)) + } +} + +func (subject *ByteSubject) GreaterThan(expectation byte) { + if subject.value <= expectation { + subject.Fail("is greater than", serial.ByteToHexString(expectation)) + } +} + +func (subject *ByteSubject) LessThan(expectation byte) { + if subject.value >= expectation { + subject.Fail("is less than", serial.ByteToHexString(expectation)) + } +} diff --git a/testing/assert/bytes.go b/testing/assert/bytes.go new file mode 100644 index 000000000..3b59c75fd --- /dev/null +++ b/testing/assert/bytes.go @@ -0,0 +1,34 @@ +package assert + +import ( + "bytes" + + "github.com/v2ray/v2ray-core/common/serial" +) + +func (this *Assert) Bytes(value []byte) *BytesSubject { + return &BytesSubject{ + Subject: Subject{ + disp: serial.BytesToHexString(value), + a: this, + }, + value: value, + } +} + +type BytesSubject struct { + Subject + value []byte +} + +func (subject *BytesSubject) Equals(expectation []byte) { + if !bytes.Equal(subject.value, expectation) { + subject.Fail("is equal to", serial.BytesToHexString(expectation)) + } +} + +func (subject *BytesSubject) NotEquals(expectation []byte) { + if bytes.Equal(subject.value, expectation) { + subject.Fail("is not equal to", serial.BytesToHexString(expectation)) + } +} diff --git a/testing/assert/bytessubject.go b/testing/assert/bytessubject.go deleted file mode 100644 index 28940e0ec..000000000 --- a/testing/assert/bytessubject.go +++ /dev/null @@ -1,41 +0,0 @@ -package assert - -import ( - "bytes" - "fmt" -) - -func Bytes(value []byte) *BytesSubject { - return &BytesSubject{value: value} -} - -type BytesSubject struct { - Subject - value []byte -} - -func (subject *BytesSubject) Named(name string) *BytesSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *BytesSubject) Fail(verb string, other []byte) { - otherString := fmt.Sprintf("%v", other) - subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + otherString + ">.") -} - -func (subject *BytesSubject) DisplayString() string { - return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value)) -} - -func (subject *BytesSubject) Equals(expectation []byte) { - if !bytes.Equal(subject.value, expectation) { - subject.Fail("is equal to", expectation) - } -} - -func (subject *BytesSubject) NotEquals(expectation []byte) { - if bytes.Equal(subject.value, expectation) { - subject.Fail("is not equal to", expectation) - } -} diff --git a/testing/assert/bytesubject.go b/testing/assert/bytesubject.go deleted file mode 100644 index 6a7b60acf..000000000 --- a/testing/assert/bytesubject.go +++ /dev/null @@ -1,45 +0,0 @@ -package assert - -import ( - "strconv" -) - -func Byte(value byte) *ByteSubject { - return &ByteSubject{value: value} -} - -type ByteSubject struct { - Subject - value byte -} - -func (subject *ByteSubject) Named(name string) *ByteSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *ByteSubject) Fail(verb string, other byte) { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.Itoa(int(other)) + ">.") -} - -func (subject *ByteSubject) DisplayString() string { - return subject.Subject.DisplayString(strconv.Itoa(int(subject.value))) -} - -func (subject *ByteSubject) Equals(expectation byte) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) - } -} - -func (subject *ByteSubject) GreaterThan(expectation byte) { - if subject.value <= expectation { - subject.Fail("is greater than", expectation) - } -} - -func (subject *ByteSubject) LessThan(expectation byte) { - if subject.value >= expectation { - subject.Fail("is less than", expectation) - } -} diff --git a/testing/assert/destination.go b/testing/assert/destination.go index 4308857c1..c4ce949a1 100644 --- a/testing/assert/destination.go +++ b/testing/assert/destination.go @@ -2,11 +2,16 @@ package assert import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" ) -func Destination(value v2net.Destination) *DestinationSubject { - return &DestinationSubject{value: value} +func (this *Assert) Destination(value v2net.Destination) *DestinationSubject { + return &DestinationSubject{ + Subject: Subject{ + disp: value.String(), + a: this, + }, + value: value, + } } type DestinationSubject struct { @@ -14,35 +19,40 @@ type DestinationSubject struct { value v2net.Destination } -func (this *DestinationSubject) Named(name string) *DestinationSubject { - this.Subject.Named(name) - return this -} - -func (this *DestinationSubject) DisplayString() string { - return this.Subject.DisplayString(this.value.String()) -} - func (this *DestinationSubject) IsTCP() { if !this.value.IsTCP() { - this.Fail(this.DisplayString(), "is", serial.StringT("a TCP destination")) + this.Fail("is", "a TCP destination") } } func (this *DestinationSubject) IsNotTCP() { if this.value.IsTCP() { - this.Fail(this.DisplayString(), "is not", serial.StringT("a TCP destination")) + this.Fail("is not", "a TCP destination") } } func (this *DestinationSubject) IsUDP() { if !this.value.IsUDP() { - this.Fail(this.DisplayString(), "is", serial.StringT("a UDP destination")) + this.Fail("is", "a UDP destination") } } func (this *DestinationSubject) IsNotUDP() { if this.value.IsUDP() { - this.Fail(this.DisplayString(), "is not", serial.StringT("a UDP destination")) + this.Fail("is not", "a UDP destination") } } + +func (this *DestinationSubject) EqualsString(another string) { + if this.value.String() != another { + this.Fail("not equals to string", another) + } +} + +func (this *DestinationSubject) HasAddress() *AddressSubject { + return this.a.Address(this.value.Address()) +} + +func (this *DestinationSubject) HasPort() *PortSubject { + return this.a.Port(this.value.Port()) +} diff --git a/testing/assert/error.go b/testing/assert/error.go new file mode 100644 index 000000000..166abd6e0 --- /dev/null +++ b/testing/assert/error.go @@ -0,0 +1,38 @@ +package assert + +func (this *Assert) Error(value error) *ErrorSubject { + valueStr := "" + if value != nil { + valueStr = value.Error() + } + return &ErrorSubject{ + Subject: Subject{ + a: this, + disp: valueStr, + }, + value: value, + } +} + +type ErrorSubject struct { + Subject + value error +} + +func (subject *ErrorSubject) Equals(expectation error) { + if subject.value != expectation { + subject.Fail("is equal to", expectation.Error()) + } +} + +func (subject *ErrorSubject) IsNil() { + if subject.value != nil { + subject.Fail("is", "nil") + } +} + +func (subject *ErrorSubject) IsNotNil() { + if subject.value == nil { + subject.Fail("is not", "nil") + } +} diff --git a/testing/assert/errorsubject.go b/testing/assert/errorsubject.go deleted file mode 100644 index 2acaab6f6..000000000 --- a/testing/assert/errorsubject.go +++ /dev/null @@ -1,41 +0,0 @@ -package assert - -func Error(value error) *ErrorSubject { - return &ErrorSubject{value: value} -} - -type ErrorSubject struct { - Subject - value error -} - -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.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.") - } -} - -func (subject *ErrorSubject) IsNotNil() { - if subject.value == nil { - subject.FailWithMessage("Not true that the error is not nil.") - } -} diff --git a/testing/assert/int64.go b/testing/assert/int64.go new file mode 100644 index 000000000..09a76f46d --- /dev/null +++ b/testing/assert/int64.go @@ -0,0 +1,44 @@ +package assert + +import ( + "github.com/v2ray/v2ray-core/common/serial" +) + +func (this *Assert) Int64(value int64) *Int64Subject { + return &Int64Subject{ + Subject: Subject{ + a: this, + disp: serial.Int64ToString(value), + }, + value: value, + } +} + +type Int64Subject struct { + Subject + value int64 +} + +func (subject *Int64Subject) Equals(expectation int64) { + if subject.value != expectation { + subject.Fail("is equal to", serial.Int64ToString(expectation)) + } +} + +func (subject *Int64Subject) GreaterThan(expectation int64) { + if subject.value <= expectation { + subject.Fail("is greater than", serial.Int64ToString(expectation)) + } +} + +func (subject *Int64Subject) AtMost(expectation int64) { + if subject.value > expectation { + subject.Fail("is at most", serial.Int64ToString(expectation)) + } +} + +func (subject *Int64Subject) AtLeast(expectation int64) { + if subject.value < expectation { + subject.Fail("is at least", serial.Int64ToString(expectation)) + } +} diff --git a/testing/assert/int64subject.go b/testing/assert/int64subject.go deleted file mode 100644 index 4b3414a62..000000000 --- a/testing/assert/int64subject.go +++ /dev/null @@ -1,51 +0,0 @@ -package assert - -import ( - "strconv" -) - -func Int64(value int64) *Int64Subject { - return &Int64Subject{value: value} -} - -type Int64Subject struct { - Subject - value int64 -} - -func (subject *Int64Subject) Named(name string) *Int64Subject { - subject.Subject.Named(name) - return subject -} - -func (subject *Int64Subject) Fail(verb string, other int64) { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + strconv.FormatInt(other, 10) + ">.") -} - -func (subject *Int64Subject) DisplayString() string { - return subject.Subject.DisplayString(strconv.FormatInt(subject.value, 10)) -} - -func (subject *Int64Subject) Equals(expectation int64) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) - } -} - -func (subject *Int64Subject) GreaterThan(expectation int64) { - if subject.value <= expectation { - subject.Fail("is greater than", expectation) - } -} - -func (subject *Int64Subject) AtMost(expectation int64) { - if subject.value > expectation { - subject.Fail("is at most", expectation) - } -} - -func (subject *Int64Subject) AtLeast(expectation int64) { - if subject.value < expectation { - subject.Fail("is at least", expectation) - } -} diff --git a/testing/assert/intsubject.go b/testing/assert/intsubject.go index 36a5b6672..bc118346c 100644 --- a/testing/assert/intsubject.go +++ b/testing/assert/intsubject.go @@ -4,38 +4,35 @@ import ( "github.com/v2ray/v2ray-core/common/serial" ) -func Int(value int) *IntSubject { - return &IntSubject{value: serial.IntLiteral(value)} +func (this *Assert) Int(value int) *IntSubject { + return &IntSubject{ + Subject: Subject{ + a: this, + disp: serial.IntToString(value), + }, + value: value, + } } type IntSubject struct { Subject - value serial.IntLiteral -} - -func (subject *IntSubject) Named(name string) *IntSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *IntSubject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) + value int } func (subject *IntSubject) Equals(expectation int) { - if subject.value.Value() != expectation { - subject.Fail(subject.DisplayString(), "is equal to", serial.IntLiteral(expectation)) + if subject.value != expectation { + subject.Fail("is equal to", serial.IntToString(expectation)) } } func (subject *IntSubject) GreaterThan(expectation int) { - if subject.value.Value() <= expectation { - subject.Fail(subject.DisplayString(), "is greater than", serial.IntLiteral(expectation)) + if subject.value <= expectation { + subject.Fail("is greater than", serial.IntToString(expectation)) } } func (subject *IntSubject) LessThan(expectation int) { - if subject.value.Value() >= expectation { - subject.Fail(subject.DisplayString(), "is less than", serial.IntLiteral(expectation)) + if subject.value >= expectation { + subject.Fail("is less than", serial.IntToString(expectation)) } } diff --git a/testing/assert/ip.go b/testing/assert/ip.go index edb3ddfba..85dad1585 100644 --- a/testing/assert/ip.go +++ b/testing/assert/ip.go @@ -3,12 +3,16 @@ package assert import ( "bytes" "net" - - "github.com/v2ray/v2ray-core/common/serial" ) -func IP(value net.IP) *IPSubject { - return &IPSubject{value: value} +func (this *Assert) IP(value net.IP) *IPSubject { + return &IPSubject{ + Subject: Subject{ + a: this, + disp: value.String(), + }, + value: value, + } } type IPSubject struct { @@ -16,23 +20,14 @@ type IPSubject struct { value net.IP } -func (subject *IPSubject) Named(name string) *IPSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *IPSubject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) -} - func (subject *IPSubject) IsNil() { if subject.value != nil { - subject.Fail(subject.DisplayString(), "is", serial.StringT("nil")) + subject.Fail("is", "nil") } } func (subject *IPSubject) Equals(ip net.IP) { if !bytes.Equal([]byte(subject.value), []byte(ip)) { - subject.Fail(subject.DisplayString(), "equals to", ip) + subject.Fail("equals to", ip.String()) } } diff --git a/testing/assert/pointer.go b/testing/assert/pointer.go new file mode 100644 index 000000000..87cb3657f --- /dev/null +++ b/testing/assert/pointer.go @@ -0,0 +1,38 @@ +package assert + +import ( + "github.com/v2ray/v2ray-core/common/serial" +) + +func (this *Assert) Pointer(value interface{}) *PointerSubject { + return &PointerSubject{ + Subject: Subject{ + a: this, + disp: serial.PointerToString(value), + }, + value: value, + } +} + +type PointerSubject struct { + Subject + value interface{} +} + +func (subject *PointerSubject) Equals(expectation interface{}) { + if subject.value != expectation { + subject.Fail("is equal to", serial.PointerToString(expectation)) + } +} + +func (subject *PointerSubject) IsNil() { + if subject.value != nil { + subject.Fail("is", "nil") + } +} + +func (subject *PointerSubject) IsNotNil() { + if subject.value == nil { + subject.Fail("is not", "nil") + } +} diff --git a/testing/assert/pointersubject.go b/testing/assert/pointersubject.go deleted file mode 100644 index 5faa37249..000000000 --- a/testing/assert/pointersubject.go +++ /dev/null @@ -1,45 +0,0 @@ -package assert - -import ( - "fmt" -) - -func Pointer(value interface{}) *PointerSubject { - return &PointerSubject{value: value} -} - -type PointerSubject struct { - Subject - value interface{} -} - -func (subject *PointerSubject) Named(name string) *PointerSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *PointerSubject) Fail(verb string, other interface{}) { - subject.FailWithMessage(fmt.Sprintf("Not true that %s %s <%v>.", subject.DisplayString(), verb, other)) -} - -func (subject *PointerSubject) DisplayString() string { - return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value)) -} - -func (subject *PointerSubject) Equals(expectation interface{}) { - if subject.value != expectation { - subject.Fail("is equal to", expectation) - } -} - -func (subject *PointerSubject) IsNil() { - if subject.value != nil { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.") - } -} - -func (subject *PointerSubject) IsNotNil() { - if subject.value == nil { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " is not nil.") - } -} diff --git a/testing/assert/port.go b/testing/assert/port.go index f9ead7278..b23d9d80b 100644 --- a/testing/assert/port.go +++ b/testing/assert/port.go @@ -2,11 +2,16 @@ package assert import ( v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" ) -func Port(value v2net.Port) *PortSubject { - return &PortSubject{value: value} +func (this *Assert) Port(value v2net.Port) *PortSubject { + return &PortSubject{ + Subject: Subject{ + a: this, + disp: value.String(), + }, + value: value, + } } type PortSubject struct { @@ -14,35 +19,26 @@ type PortSubject struct { value v2net.Port } -func (subject *PortSubject) Named(name string) *PortSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *PortSubject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) -} - func (subject *PortSubject) Equals(expectation v2net.Port) { if subject.value.Value() != expectation.Value() { - subject.Fail(subject.DisplayString(), "is equal to", expectation) + subject.Fail("is equal to", expectation.String()) } } func (subject *PortSubject) GreaterThan(expectation v2net.Port) { if subject.value.Value() <= expectation.Value() { - subject.Fail(subject.DisplayString(), "is greater than", expectation) + subject.Fail("is greater than", expectation.String()) } } func (subject *PortSubject) LessThan(expectation v2net.Port) { if subject.value.Value() >= expectation.Value() { - subject.Fail(subject.DisplayString(), "is less than", expectation) + subject.Fail("is less than", expectation.String()) } } func (subject *PortSubject) IsValid() { if subject.value == 0 { - subject.Fail(subject.DisplayString(), "is", serial.StringT("a valid port")) + subject.Fail("is", "a valid port") } } diff --git a/testing/assert/string.go b/testing/assert/string.go new file mode 100644 index 000000000..32a15efdf --- /dev/null +++ b/testing/assert/string.go @@ -0,0 +1,44 @@ +package assert + +import ( + "strings" +) + +func (this *Assert) String(value string) *StringSubject { + return &StringSubject{ + Subject: Subject{ + a: this, + disp: value, + }, + value: value, + } +} + +type StringSubject struct { + Subject + value string +} + +func (subject *StringSubject) Equals(expectation string) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *StringSubject) NotEquals(expectation string) { + if subject.value == expectation { + subject.Fail("is not equal to ", expectation) + } +} + +func (subject *StringSubject) Contains(substring string) { + if !strings.Contains(subject.value, substring) { + subject.Fail("contains", substring) + } +} + +func (subject *StringSubject) NotContains(substring string) { + if strings.Contains(subject.value, substring) { + subject.Fail("doesn't contain", substring) + } +} diff --git a/testing/assert/stringsubject.go b/testing/assert/stringsubject.go deleted file mode 100644 index 64e3b37d2..000000000 --- a/testing/assert/stringsubject.go +++ /dev/null @@ -1,53 +0,0 @@ -package assert - -import ( - "strings" - - "github.com/v2ray/v2ray-core/common/serial" -) - -func StringLiteral(value string) *StringSubject { - return String(serial.StringT((value))) -} - -func String(value serial.String) *StringSubject { - return &StringSubject{value: value} -} - -type StringSubject struct { - Subject - value serial.String -} - -func (subject *StringSubject) Named(name string) *StringSubject { - subject.Subject.Named(name) - return subject -} - -func (subject *StringSubject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) -} - -func (subject *StringSubject) Equals(expectation string) { - if subject.value.String() != expectation { - subject.Fail(subject.DisplayString(), "is equal to", serial.StringT(expectation)) - } -} - -func (subject *StringSubject) NotEquals(expectation string) { - if subject.value.String() == expectation { - subject.Fail(subject.DisplayString(), "is not equal to ", serial.StringT(expectation)) - } -} - -func (subject *StringSubject) Contains(substring serial.String) { - if !strings.Contains(subject.value.String(), substring.String()) { - subject.Fail(subject.DisplayString(), "contains", substring) - } -} - -func (subject *StringSubject) NotContains(substring serial.String) { - if strings.Contains(subject.value.String(), substring.String()) { - subject.Fail(subject.DisplayString(), "doesn't contain", substring) - } -} diff --git a/testing/assert/subject.go b/testing/assert/subject.go index cdd8e2d99..b54e403c1 100644 --- a/testing/assert/subject.go +++ b/testing/assert/subject.go @@ -1,38 +1,22 @@ package assert -import ( - "github.com/v2ray/v2ray-core/common/serial" - v2testing "github.com/v2ray/v2ray-core/testing" -) - type Subject struct { - name string + disp string + a *Assert } -func NewSubject() *Subject { - return &Subject{ - name: "", - } -} - -func (subject *Subject) Fail(displayString string, verb string, other serial.String) { - subject.FailWithMessage("Not true that " + displayString + " " + verb + " <" + other.String() + ">.") +func (subject *Subject) Fail(verb string, other string) { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other + ">.") } func (subject *Subject) FailWithMessage(message string) { - v2testing.Fail(message) + subject.a.Fail(message) } -func (subject *Subject) Named(name string) { - subject.name = name -} - -func (subject *Subject) DisplayString(value string) string { +func (subject *Subject) DisplayString() string { + value := subject.disp if len(value) == 0 { value = "unknown" } - if len(subject.name) == 0 { - return "<" + value + ">" - } - return subject.name + "(<" + value + ">)" + return "<" + value + ">" } diff --git a/testing/assert/uint16.go b/testing/assert/uint16.go new file mode 100644 index 000000000..0789037ff --- /dev/null +++ b/testing/assert/uint16.go @@ -0,0 +1,50 @@ +package assert + +import ( + "github.com/v2ray/v2ray-core/common/serial" +) + +func (this *Assert) Uint16(value uint16) *Uint16Subject { + return &Uint16Subject{ + Subject: Subject{ + a: this, + disp: serial.Uint16ToString(value), + }, + value: value, + } +} + +type Uint16Subject struct { + Subject + value uint16 +} + +func (subject *Uint16Subject) Equals(expectation uint16) { + if subject.value != expectation { + subject.Fail("is equal to", serial.Uint16ToString(expectation)) + } +} + +func (subject *Uint16Subject) GreaterThan(expectation uint16) { + if subject.value <= expectation { + subject.Fail("is greater than", serial.Uint16ToString(expectation)) + } +} + +func (subject *Uint16Subject) LessThan(expectation uint16) { + if subject.value >= expectation { + subject.Fail("is less than", serial.Uint16ToString(expectation)) + } +} + +func (subject *Uint16Subject) IsPositive() { + if subject.value <= 0 { + subject.Fail("is", "positive") + } +} + +func (subject *Uint16Subject) IsNegative() { + if subject.value >= 0 { + subject.Fail("is not", "negative") + } +} diff --git a/testing/assert/uint16subject.go b/testing/assert/uint16subject.go deleted file mode 100644 index 8f0747e3c..000000000 --- a/testing/assert/uint16subject.go +++ /dev/null @@ -1,47 +0,0 @@ -package assert - -import ( - "github.com/v2ray/v2ray-core/common/serial" -) - -func Uint16(value uint16) *Uint16Subject { - return &Uint16Subject{value: serial.Uint16Literal(value)} -} - -type Uint16Subject struct { - Subject - value serial.Uint16Literal -} - -func (subject *Uint16Subject) Named(name string) *Uint16Subject { - subject.Subject.Named(name) - return subject -} - -func (subject *Uint16Subject) DisplayString() string { - return subject.Subject.DisplayString(subject.value.String()) -} - -func (subject *Uint16Subject) Equals(expectation uint16) { - if subject.value.Value() != expectation { - subject.Fail(subject.DisplayString(), "is equal to", serial.Uint16Literal(expectation)) - } -} - -func (subject *Uint16Subject) GreaterThan(expectation uint16) { - if subject.value.Value() <= expectation { - subject.Fail(subject.DisplayString(), "is greater than", serial.Uint16Literal(expectation)) - } -} - -func (subject *Uint16Subject) LessThan(expectation uint16) { - if subject.value.Value() >= expectation { - subject.Fail(subject.DisplayString(), "is less than", serial.Uint16Literal(expectation)) - } -} - -func (subject *Uint16Subject) Positive() { - if subject.value.Value() <= 0 { - subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.") - } -} diff --git a/testing/scenarios/dokodemo_test.go b/testing/scenarios/dokodemo_test.go index 3c4958db5..53ed87aca 100644 --- a/testing/scenarios/dokodemo_test.go +++ b/testing/scenarios/dokodemo_test.go @@ -5,13 +5,12 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" ) func TestDokodemoTCP(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) tcpServer := &tcp.Server{ Port: v2net.Port(50016), @@ -47,7 +46,7 @@ func TestDokodemoTCP(t *testing.T) { response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + assert.String("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() } diff --git a/testing/scenarios/dynamic_vmess_test.go b/testing/scenarios/dynamic_vmess_test.go index c61c8cf84..cec69c0d9 100644 --- a/testing/scenarios/dynamic_vmess_test.go +++ b/testing/scenarios/dynamic_vmess_test.go @@ -7,13 +7,12 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" ) func TestDynamicVMess(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) tcpServer := &tcp.Server{ Port: v2net.Port(50032), @@ -47,7 +46,7 @@ func TestDynamicVMess(t *testing.T) { response := bytes.NewBuffer(nil) _, err = io.Copy(response, conn) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response.Bytes())) + assert.String("Processed: " + payload).Equals(string(response.Bytes())) conn.Close() } diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 5b3b19b2d..0934e6f29 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -7,13 +7,12 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" v2http "github.com/v2ray/v2ray-core/testing/servers/http" ) func TestHttpProxy(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) httpServer := &v2http.Server{ Port: v2net.Port(50042), @@ -41,7 +40,7 @@ func TestHttpProxy(t *testing.T) { content, err := ioutil.ReadAll(resp.Body) assert.Error(err).IsNil() - assert.StringLiteral(string(content)).Equals("Home") + assert.String(string(content)).Equals("Home") CloseAllServers() } diff --git a/testing/scenarios/router_test.go b/testing/scenarios/router_test.go index 910de48ad..05e9753da 100644 --- a/testing/scenarios/router_test.go +++ b/testing/scenarios/router_test.go @@ -5,13 +5,12 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" ) func TestRouter(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) tcpServer := &tcp.Server{ Port: v2net.Port(50024), @@ -56,7 +55,7 @@ func TestRouter(t *testing.T) { response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + assert.String("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() conn, err = net.DialTCP("tcp", nil, &net.TCPAddr{ diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 498e87b68..fc8af5ee8 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -5,7 +5,6 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" @@ -13,7 +12,7 @@ import ( ) func TestShadowsocksTCP(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) tcpServer := &tcp.Server{ Port: v2net.Port(50052), @@ -47,7 +46,7 @@ func TestShadowsocksTCP(t *testing.T) { response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + assert.String("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() cipher, err = ssclient.NewCipher("aes-128-cfb", "v2ray-another") @@ -66,7 +65,7 @@ func TestShadowsocksTCP(t *testing.T) { response = make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + assert.String("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() cipher, err = ssclient.NewCipher("chacha20", "new-password") @@ -85,7 +84,7 @@ func TestShadowsocksTCP(t *testing.T) { response = make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() - assert.StringLiteral("Processed: " + payload).Equals(string(response[:nBytes])) + assert.String("Processed: " + payload).Equals(string(response[:nBytes])) conn.Close() CloseAllServers() diff --git a/testing/scenarios/socks_end_test.go b/testing/scenarios/socks_end_test.go index 3831e15f2..0f874d998 100644 --- a/testing/scenarios/socks_end_test.go +++ b/testing/scenarios/socks_end_test.go @@ -6,14 +6,13 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" "github.com/v2ray/v2ray-core/testing/servers/udp" ) func TestTCPConnection(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) targetPort := v2nettesting.PickPort() tcpServer := &tcp.Server{ @@ -79,7 +78,7 @@ func TestTCPConnection(t *testing.T) { nResponse += nBytes conn.CloseWrite() - assert.StringLiteral(string(actualResponse[:nResponse])).Equals("Processed: Request to target server.Processed: Request to target server again.") + assert.String(string(actualResponse[:nResponse])).Equals("Processed: Request to target server.Processed: Request to target server again.") conn.Close() } @@ -88,7 +87,7 @@ func TestTCPConnection(t *testing.T) { } func TestTCPBind(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) targetPort := v2nettesting.PickPort() tcpServer := &tcp.Server{ @@ -139,7 +138,7 @@ func TestTCPBind(t *testing.T) { } func TestUDPAssociate(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) targetPort := v2nettesting.PickPort() udpServer := &udp.Server{ diff --git a/tools/build/build_test.go b/tools/build/build_test.go index e3589a581..bed8a1fbf 100644 --- a/tools/build/build_test.go +++ b/tools/build/build_test.go @@ -6,7 +6,6 @@ import ( "path/filepath" "testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -32,7 +31,7 @@ func allFilesExists(files ...string) bool { } func TestBuildMacOS(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) binPath = filepath.Join(os.Getenv("GOPATH"), "testing") cleanBinPath() diff --git a/tools/build/env_test.go b/tools/build/env_test.go index fccbd435f..4dbfd84e9 100644 --- a/tools/build/env_test.go +++ b/tools/build/env_test.go @@ -3,12 +3,11 @@ package main import ( "testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestParseOS(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) assert.Pointer(parseOS("windows")).Equals(Windows) assert.Pointer(parseOS("macos")).Equals(MacOS) @@ -17,7 +16,7 @@ func TestParseOS(t *testing.T) { } func TestParseArch(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) assert.Pointer(parseArch("x86")).Equals(X86) assert.Pointer(parseArch("x64")).Equals(Amd64) diff --git a/tools/build/go_test.go b/tools/build/go_test.go index bd6c4f426..9f12fc8ef 100644 --- a/tools/build/go_test.go +++ b/tools/build/go_test.go @@ -10,12 +10,11 @@ import ( "testing" "time" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestBuildAndRun(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) gopath := os.Getenv("GOPATH") goOS := parseOS(runtime.GOOS) @@ -42,7 +41,7 @@ func TestBuildAndRun(t *testing.T) { errStr := string(errBuffer.Bytes()) assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue() - assert.StringLiteral(errStr).Equals("") + assert.String(errStr).Equals("") os.Remove(target) } diff --git a/tools/git/git_test.go b/tools/git/git_test.go index df8c34134..4006f200b 100644 --- a/tools/git/git_test.go +++ b/tools/git/git_test.go @@ -3,12 +3,11 @@ package git import ( "testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) func TestRevParse(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) rev, err := RevParse("HEAD") assert.Error(err).IsNil() @@ -16,7 +15,7 @@ func TestRevParse(t *testing.T) { } func TestRepoVersion(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) version, err := RepoVersionHead() assert.Error(err).IsNil() diff --git a/transport/dialer/dialer_test.go b/transport/dialer/dialer_test.go index 358af891b..66ef3cf30 100644 --- a/transport/dialer/dialer_test.go +++ b/transport/dialer/dialer_test.go @@ -5,14 +5,13 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" - v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" . "github.com/v2ray/v2ray-core/transport/dialer" ) func TestDialDomain(t *testing.T) { - v2testing.Current(t) + assert := assert.On(t) server := &tcp.Server{ Port: v2nettesting.PickPort(), @@ -23,6 +22,6 @@ func TestDialDomain(t *testing.T) { conn, err := Dial(v2net.TCPDestination(v2net.DomainAddress("local.v2ray.com"), dest.Port())) assert.Error(err).IsNil() - assert.StringLiteral(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port().String()) + assert.String(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port().String()) conn.Close() }