diff --git a/app/stats/stats_test.go b/app/stats/stats_test.go index 917db3df..2a0d9b91 100644 --- a/app/stats/stats_test.go +++ b/app/stats/stats_test.go @@ -7,26 +7,29 @@ import ( . "v2ray.com/core/app/stats" "v2ray.com/core/common" "v2ray.com/core/features/stats" - . "v2ray.com/ext/assert" ) func TestInternface(t *testing.T) { - assert := With(t) - - assert((*Manager)(nil), Implements, (*stats.Manager)(nil)) + _ = (stats.Manager)(new(Manager)) } func TestStatsCounter(t *testing.T) { - assert := With(t) - raw, err := common.CreateObject(context.Background(), &Config{}) - assert(err, IsNil) + common.Must(err) m := raw.(stats.Manager) c, err := m.RegisterCounter("test.counter") - assert(err, IsNil) + common.Must(err) + + if v := c.Add(1); v != 1 { + t.Fatal("unpexcted Add(1) return: ", v, ", wanted ", 1) + } + + if v := c.Set(0); v != 1 { + t.Fatal("unexpected Set(0) return: ", v, ", wanted ", 1) + } - assert(c.Add(1), Equals, int64(1)) - assert(c.Set(0), Equals, int64(1)) - assert(c.Value(), Equals, int64(0)) + if v := c.Value(); v != 0 { + t.Fatal("unexpected Value() return: ", v, ", wanted ", 0) + } } diff --git a/common/net/port_test.go b/common/net/port_test.go index 187cdf1f..f668e0ec 100644 --- a/common/net/port_test.go +++ b/common/net/port_test.go @@ -4,15 +4,15 @@ import ( "testing" . "v2ray.com/core/common/net" - . "v2ray.com/ext/assert" ) func TestPortRangeContains(t *testing.T) { - assert := With(t) - portRange := &PortRange{ From: 53, To: 53, } - assert(portRange.Contains(Port(53)), IsTrue) + + if !portRange.Contains(Port(53)) { + t.Error("expected port range containing 53, but actually not") + } } diff --git a/common/protocol/id_test.go b/common/protocol/id_test.go index 9e52cbbb..2d25ca05 100644 --- a/common/protocol/id_test.go +++ b/common/protocol/id_test.go @@ -5,15 +5,17 @@ import ( . "v2ray.com/core/common/protocol" "v2ray.com/core/common/uuid" - . "v2ray.com/ext/assert" ) func TestIdEquals(t *testing.T) { - assert := With(t) - id1 := NewID(uuid.New()) id2 := NewID(id1.UUID()) - assert(id1.Equals(id2), IsTrue) - assert(id1.String(), Equals, id2.String()) + if !id1.Equals(id2) { + t.Error("expected id1 to equal id2, but actually not") + } + + if !id1.String() != id2.String() { + t.Error(id1.String(), " != ", id2.String()) + } } diff --git a/common/protocol/time_test.go b/common/protocol/time_test.go index 42b1c99d..6ddeed73 100644 --- a/common/protocol/time_test.go +++ b/common/protocol/time_test.go @@ -5,19 +5,17 @@ import ( "time" . "v2ray.com/core/common/protocol" - . "v2ray.com/ext/assert" ) func TestGenerateRandomInt64InRange(t *testing.T) { - assert := With(t) - base := time.Now().Unix() delta := 100 generator := NewTimestampGenerator(Timestamp(base), delta) for i := 0; i < 100; i++ { val := int64(generator()) - assert(val, AtMost, base+int64(delta)) - assert(val, AtLeast, base-int64(delta)) + if val > base+int64(delta) || val < base-int64(delta) { + t.Error(val, " not between ", base-int64(delta), " and ", base+int64(delta)) + } } } diff --git a/common/serial/string_test.go b/common/serial/string_test.go index af90c26c..6fe8d99d 100644 --- a/common/serial/string_test.go +++ b/common/serial/string_test.go @@ -4,13 +4,12 @@ import ( "errors" "testing" + "github.com/google/go-cmp/cmp" + . "v2ray.com/core/common/serial" - . "v2ray.com/ext/assert" ) func TestToString(t *testing.T) { - assert := With(t) - s := "a" data := []struct { Value interface{} @@ -23,7 +22,9 @@ func TestToString(t *testing.T) { } for _, c := range data { - assert(ToString(c.Value), Equals, c.String) + if r := cmp.Diff(ToString(c.Value), c.String); r != "" { + t.Error(r) + } } } diff --git a/common/signal/notifier_test.go b/common/signal/notifier_test.go index b9e0b127..2b58acbb 100644 --- a/common/signal/notifier_test.go +++ b/common/signal/notifier_test.go @@ -4,12 +4,9 @@ import ( "testing" . "v2ray.com/core/common/signal" - //. "v2ray.com/ext/assert" ) func TestNotifierSignal(t *testing.T) { - //assert := With(t) - n := NewNotifier() w := n.Wait() diff --git a/common/strmatcher/matchers_test.go b/common/strmatcher/matchers_test.go index f952c0b6..4d615b14 100644 --- a/common/strmatcher/matchers_test.go +++ b/common/strmatcher/matchers_test.go @@ -5,12 +5,9 @@ import ( "v2ray.com/core/common" . "v2ray.com/core/common/strmatcher" - ast "v2ray.com/ext/assert" ) func TestMatcher(t *testing.T) { - assert := ast.With(t) - cases := []struct { pattern string mType Type @@ -69,6 +66,8 @@ func TestMatcher(t *testing.T) { for _, test := range cases { matcher, err := test.mType.New(test.pattern) common.Must(err) - assert(matcher.Match(test.input) == test.output, ast.IsTrue) + if m := matcher.Match(test.input); m != test.output { + t.Error("unexpected output: ", m, " for test case ", test) + } } } diff --git a/proxy/socks/protocol_test.go b/proxy/socks/protocol_test.go index ff9218f9..3463b655 100644 --- a/proxy/socks/protocol_test.go +++ b/proxy/socks/protocol_test.go @@ -4,18 +4,17 @@ import ( "bytes" "testing" + "github.com/google/go-cmp/cmp" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/net" _ "v2ray.com/core/common/net/testing" "v2ray.com/core/common/protocol" . "v2ray.com/core/proxy/socks" - . "v2ray.com/ext/assert" ) func TestUDPEncoding(t *testing.T) { - assert := With(t) - b := buf.New() request := &protocol.RequestHeader{ @@ -27,13 +26,15 @@ func TestUDPEncoding(t *testing.T) { content := []byte{'a'} payload := buf.New() payload.Write(content) - assert(writer.WriteMultiBuffer(buf.MultiBuffer{payload}), IsNil) + common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload})) reader := NewUDPReader(b) decodedPayload, err := reader.ReadMultiBuffer() - assert(err, IsNil) - assert(decodedPayload[0].Bytes(), Equals, content) + common.Must(err) + if r := cmp.Diff(decodedPayload[0].Bytes(), content); r != "" { + t.Error(r) + } } func TestReadUsernamePassword(t *testing.T) { diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index 50f39ce3..65ba8760 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -1,19 +1,17 @@ package scenarios import ( - "crypto/rand" "os" "runtime" - "sync" "testing" "time" - "github.com/google/go-cmp/cmp" - "v2ray.com/core/transport/internet/headers/wechat" + "golang.org/x/sync/errgroup" "v2ray.com/core" "v2ray.com/core/app/log" "v2ray.com/core/app/proxyman" + "v2ray.com/core/common" clog "v2ray.com/core/common/log" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -28,19 +26,17 @@ import ( "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/domainsocket" "v2ray.com/core/transport/internet/headers/http" + "v2ray.com/core/transport/internet/headers/wechat" "v2ray.com/core/transport/internet/quic" tcptransport "v2ray.com/core/transport/internet/tcp" - . "v2ray.com/ext/assert" ) func TestHttpConnectionHeader(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -131,24 +127,12 @@ func TestHttpConnectionHeader(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) - - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - - payload := "dokodemo request." - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) - - response := readFrom(conn, time.Second*2, len(payload)) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) + common.Must(err) + defer CloseAllServers(servers) - CloseAllServers(servers) + if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil { + t.Error(err) + } } func TestDomainSocket(t *testing.T) { @@ -156,13 +140,11 @@ func TestDomainSocket(t *testing.T) { t.Skip("Not supported on windows") return } - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() const dsPath = "/tmp/ds_scenario" @@ -258,34 +240,20 @@ func TestDomainSocket(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) - - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - - payload := "dokodemo request." - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) - - response := readFrom(conn, time.Second*2, len(payload)) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) + common.Must(err) + defer CloseAllServers(servers) - CloseAllServers(servers) + if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil { + t.Error(err) + } } func TestVMessQuic(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -406,31 +374,12 @@ func TestVMessQuic(t *testing.T) { } defer CloseAllServers(servers) - var wg sync.WaitGroup + var errg errgroup.Group for i := 0; i < 10; i++ { - wg.Add(1) - go func() { - defer wg.Done() - - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - defer conn.Close() // nolint: errcheck - - payload := make([]byte, 10240*1024) - rand.Read(payload) - - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40)) + } - response := readFrom(conn, time.Second*40, 10240*1024) - if r := cmp.Diff(response, xor([]byte(payload))); r != "" { - t.Error(r) - } - }() + if err := errg.Wait(); err != nil { + t.Error(err) } - wg.Wait() } diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index 82c62a68..3b0b54a1 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -31,13 +31,11 @@ import ( ) func TestVMessDynamicPort(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -141,38 +139,22 @@ func TestVMessDynamicPort(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) + common.Must(err) + defer CloseAllServers(servers) for i := 0; i < 10; i++ { - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - - payload := "dokodemo request." - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) - - response := make([]byte, 1024) - nBytes, err = conn.Read(response) - assert(err, IsNil) - assert(response[:nBytes], Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) + if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil { + t.Error(err) + } } - - CloseAllServers(servers) } func TestVMessGCM(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -257,55 +239,28 @@ func TestVMessGCM(t *testing.T) { }, } - /* - const envName = "V2RAY_VMESS_PADDING" - common.Must(os.Setenv(envName, "1")) - defer os.Unsetenv(envName) - */ - servers, err := InitializeServerConfigs(serverConfig, clientConfig) if err != nil { t.Fatal("Failed to initialize all servers: ", err.Error()) } defer CloseAllServers(servers) - var wg sync.WaitGroup + var errg errgroup.Group for i := 0; i < 10; i++ { - wg.Add(1) - go func() { - defer wg.Done() - - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - defer conn.Close() // nolint: errcheck - - payload := make([]byte, 10240*1024) - rand.Read(payload) - - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40)) + } - response := readFrom(conn, time.Second*40, 10240*1024) - if r := cmp.Diff(response, xor([]byte(payload))); r != "" { - t.Error(r) - } - }() + if err := errg.Wait(); err != nil { + t.Error(err) } - wg.Wait() } func TestVMessGCMReadv(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -400,33 +355,13 @@ func TestVMessGCMReadv(t *testing.T) { } defer CloseAllServers(servers) - var wg sync.WaitGroup - wg.Add(10) + var errg errgroup.Group for i := 0; i < 10; i++ { - go func() { - defer wg.Done() - - conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ - IP: []byte{127, 0, 0, 1}, - Port: int(clientPort), - }) - assert(err, IsNil) - defer conn.Close() // nolint: errcheck - - payload := make([]byte, 10240*1024) - rand.Read(payload) - - nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) - - response := readFrom(conn, time.Second*40, 10240*1024) - if r := cmp.Diff(response, xor([]byte(payload))); r != "" { - t.Error(r) - } - }() + errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40)) + } + if err := errg.Wait(); err != nil { + t.Error(err) } - wg.Wait() } func TestVMessGCMUDP(t *testing.T) { diff --git a/transport/internet/dialer_test.go b/transport/internet/dialer_test.go index f0cc5204..e9d91367 100644 --- a/transport/internet/dialer_test.go +++ b/transport/internet/dialer_test.go @@ -4,22 +4,24 @@ import ( "context" "testing" + "github.com/google/go-cmp/cmp" + + "v2ray.com/core/common" "v2ray.com/core/common/net" "v2ray.com/core/testing/servers/tcp" . "v2ray.com/core/transport/internet" - . "v2ray.com/ext/assert" ) func TestDialWithLocalAddr(t *testing.T) { - assert := With(t) - server := &tcp.Server{} dest, err := server.Start() - assert(err, IsNil) + common.Must(err) defer server.Close() conn, err := DialSystem(context.Background(), net.TCPDestination(net.LocalHostIP, dest.Port), nil) - assert(err, IsNil) - assert(conn.RemoteAddr().String(), Equals, "127.0.0.1:"+dest.Port.String()) + common.Must(err) + if r := cmp.Diff(conn.RemoteAddr().String(), "127.0.0.1:"+dest.Port.String()); r != "" { + t.Error(r) + } conn.Close() }