diff --git a/common/buf/readv_test.go b/common/buf/readv_test.go index 13ea35f00..190e83d0e 100644 --- a/common/buf/readv_test.go +++ b/common/buf/readv_test.go @@ -7,10 +7,11 @@ import ( "net" "testing" + "github.com/google/go-cmp/cmp" + "golang.org/x/sync/errgroup" "v2ray.com/core/common" . "v2ray.com/core/common/buf" - "v2ray.com/core/common/compare" "v2ray.com/core/testing/servers/tcp" ) @@ -65,7 +66,7 @@ func TestReadvReader(t *testing.T) { rdata := make([]byte, size) SplitBytes(rmb, rdata) - if err := compare.BytesEqualWithDetail(data, rdata); err != nil { - t.Fatal(err) + if r := cmp.Diff(data, rdata); r != "" { + t.Fatal(r) } } diff --git a/common/compare/bytes.go b/common/compare/bytes.go deleted file mode 100644 index fe7202bae..000000000 --- a/common/compare/bytes.go +++ /dev/null @@ -1,29 +0,0 @@ -package compare - -import "v2ray.com/core/common/errors" - -func BytesEqualWithDetail(a []byte, b []byte) error { - if len(a) != len(b) { - return errors.New("mismatch array length ", len(a), " vs ", len(b)) - } - for idx, v := range a { - if b[idx] != v { - return errors.New("mismatch array value at index [", idx, "]: ", v, " vs ", b[idx]) - } - } - return nil -} - -func BytesEqual(a []byte, b []byte) bool { - return BytesEqualWithDetail(a, b) == nil -} - -func BytesAll(arr []byte, value byte) bool { - for _, v := range arr { - if v != value { - return false - } - } - - return true -} diff --git a/common/compare/bytes_test.go b/common/compare/bytes_test.go deleted file mode 100644 index 21dffa196..000000000 --- a/common/compare/bytes_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package compare_test - -import ( - "testing" - - . "v2ray.com/core/common/compare" -) - -func TestBytesEqual(t *testing.T) { - testCases := []struct { - Input1 []byte - Input2 []byte - Result bool - }{ - { - Input1: []byte{}, - Input2: []byte{1}, - Result: false, - }, - { - Input1: nil, - Input2: []byte{}, - Result: true, - }, - { - Input1: []byte{1}, - Input2: []byte{1}, - Result: true, - }, - { - Input1: []byte{1, 2}, - Input2: []byte{1, 3}, - Result: false, - }, - } - - for _, testCase := range testCases { - cmp := BytesEqual(testCase.Input1, testCase.Input2) - if cmp != testCase.Result { - t.Errorf("unexpected result %v from %v", cmp, testCase) - } - } -} diff --git a/common/crypto/chacha20_test.go b/common/crypto/chacha20_test.go index 6d43a0c2e..d7a7c6b7c 100644 --- a/common/crypto/chacha20_test.go +++ b/common/crypto/chacha20_test.go @@ -5,8 +5,9 @@ import ( "encoding/hex" "testing" + "github.com/google/go-cmp/cmp" + "v2ray.com/core/common" - "v2ray.com/core/common/compare" . "v2ray.com/core/common/crypto" ) @@ -49,8 +50,8 @@ func TestChaCha20Stream(t *testing.T) { input := make([]byte, len(c.output)) actualOutout := make([]byte, len(c.output)) s.XORKeyStream(actualOutout, input) - if err := compare.BytesEqualWithDetail(c.output, actualOutout); err != nil { - t.Fatal(err) + if r := cmp.Diff(c.output, actualOutout); r != "" { + t.Fatal(r) } } } @@ -70,7 +71,7 @@ func TestChaCha20Decoding(t *testing.T) { stream2 := NewChaCha20Stream(key, iv) stream2.XORKeyStream(x, x) - if err := compare.BytesEqualWithDetail(x, payload); err != nil { - t.Fatal(err) + if r := cmp.Diff(x, payload); r != "" { + t.Fatal(r) } } diff --git a/common/net/address.go b/common/net/address.go index 1794ce7ff..5993e907b 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -1,10 +1,9 @@ package net import ( + "bytes" "net" "strings" - - "v2ray.com/core/common/compare" ) var ( @@ -90,6 +89,8 @@ func ParseAddress(addr string) Address { return DomainAddress(addr) } +var bytes0 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + // IPAddress creates an Address with given IP. func IPAddress(ip []byte) Address { switch len(ip) { @@ -97,7 +98,7 @@ func IPAddress(ip []byte) Address { var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]} return addr case net.IPv6len: - if compare.BytesAll(ip[0:10], 0) && compare.BytesAll(ip[10:12], 0xff) { + if bytes.Equal(ip[:10], bytes0) && ip[10] == 0xff && ip[11] == 0xff { return IPAddress(ip[12:16]) } var addr ipv6Address = [16]byte{ diff --git a/common/protocol/id_test.go b/common/protocol/id_test.go index a412cef9a..9e52cbbbf 100644 --- a/common/protocol/id_test.go +++ b/common/protocol/id_test.go @@ -3,19 +3,11 @@ package protocol_test import ( "testing" - "v2ray.com/core/common/compare" . "v2ray.com/core/common/protocol" "v2ray.com/core/common/uuid" . "v2ray.com/ext/assert" ) -func TestCmdKey(t *testing.T) { - assert := With(t) - - id := NewID(uuid.New()) - assert(compare.BytesAll(id.CmdKey(), 0), IsFalse) -} - func TestIdEquals(t *testing.T) { assert := With(t) diff --git a/common/uuid/uuid_test.go b/common/uuid/uuid_test.go index 3fdbd87b1..30ed53706 100644 --- a/common/uuid/uuid_test.go +++ b/common/uuid/uuid_test.go @@ -6,7 +6,6 @@ import ( "github.com/google/go-cmp/cmp" "v2ray.com/core/common" - "v2ray.com/core/common/compare" . "v2ray.com/core/common/uuid" . "v2ray.com/ext/assert" ) @@ -33,8 +32,8 @@ func TestParseString(t *testing.T) { uuid, err := ParseString(str) common.Must(err) - if err := compare.BytesEqualWithDetail(expectedBytes, uuid.Bytes()); err != nil { - t.Fatal(err) + if r := cmp.Diff(expectedBytes, uuid.Bytes); r != "" { + t.Fatal(r) } _, err = ParseString("2418d087") diff --git a/proxy/mtproto/auth_test.go b/proxy/mtproto/auth_test.go index 8f97a8d9d..c796076ec 100644 --- a/proxy/mtproto/auth_test.go +++ b/proxy/mtproto/auth_test.go @@ -5,8 +5,9 @@ import ( "crypto/rand" "testing" + "github.com/google/go-cmp/cmp" + "v2ray.com/core/common" - "v2ray.com/core/common/compare" . "v2ray.com/core/proxy/mtproto" . "v2ray.com/ext/assert" ) @@ -24,8 +25,8 @@ func TestInverse(t *testing.T) { } bii := Inverse(bi) - if err := compare.BytesEqualWithDetail(bii, b); err != nil { - t.Fatal(err) + if r := cmp.Diff(bii, b); r != "" { + t.Fatal(r) } } diff --git a/proxy/mtproto/server.go b/proxy/mtproto/server.go index 699320600..20771b930 100644 --- a/proxy/mtproto/server.go +++ b/proxy/mtproto/server.go @@ -1,13 +1,13 @@ package mtproto import ( + "bytes" "context" "time" "v2ray.com/core" "v2ray.com/core/common" "v2ray.com/core/common/buf" - "v2ray.com/core/common/compare" "v2ray.com/core/common/crypto" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -63,11 +63,14 @@ func (s *Server) Network() []net.Network { return []net.Network{net.Network_TCP} } +var ctype1 = []byte{0xef, 0xef, 0xef, 0xef} +var ctype2 = []byte{0xee, 0xee, 0xee, 0xee} + func isValidConnectionType(c [4]byte) bool { - if compare.BytesAll(c[:], 0xef) { + if bytes.Equal(c[:], ctype1) { return true } - if compare.BytesAll(c[:], 0xee) { + if bytes.Equal(c[:], ctype2) { return true } return false diff --git a/testing/scenarios/command_test.go b/testing/scenarios/command_test.go index daf0d664e..dcff9a6e9 100644 --- a/testing/scenarios/command_test.go +++ b/testing/scenarios/command_test.go @@ -8,6 +8,8 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" + "google.golang.org/grpc" "v2ray.com/core" @@ -18,7 +20,6 @@ import ( "v2ray.com/core/app/router" "v2ray.com/core/app/stats" statscmd "v2ray.com/core/app/stats/command" - "v2ray.com/core/common/compare" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/common/serial" @@ -120,8 +121,8 @@ func TestCommanderRemoveHandler(t *testing.T) { response := make([]byte, 1024) nBytes, err = conn.Read(response) assert(err, IsNil) - if err := compare.BytesEqualWithDetail(response[:nBytes], xor([]byte(payload))); err != nil { - t.Fatal(err) + if r := cmp.Diff(response[:nBytes], xor([]byte(payload))); r != "" { + t.Fatal(r) } } @@ -514,8 +515,8 @@ func TestCommanderStats(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Fatal("failed to read response: ", err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Fatal("failed to read response: ", r) } cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock()) diff --git a/testing/scenarios/reverse_test.go b/testing/scenarios/reverse_test.go index bb1020ea3..aed7755b5 100644 --- a/testing/scenarios/reverse_test.go +++ b/testing/scenarios/reverse_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "v2ray.com/core" "v2ray.com/core/app/log" "v2ray.com/core/app/policy" @@ -13,7 +14,6 @@ import ( "v2ray.com/core/app/reverse" "v2ray.com/core/app/router" "v2ray.com/core/common" - "v2ray.com/core/common/compare" clog "v2ray.com/core/common/log" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -213,8 +213,8 @@ func TestReverseProxy(t *testing.T) { } response := readFrom(conn, time.Second*20, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } @@ -428,8 +428,8 @@ func TestReverseProxyLongRunning(t *testing.T) { } response := readFrom(conn, time.Second*5, 1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } conn.Close() diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 207d02e71..ac046088f 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -13,7 +13,6 @@ import ( "v2ray.com/core/app/log" "v2ray.com/core/app/proxyman" "v2ray.com/core/common" - "v2ray.com/core/common/compare" "v2ray.com/core/common/errors" clog "v2ray.com/core/common/log" "v2ray.com/core/common/net" @@ -395,8 +394,8 @@ func TestShadowsocksChacha20TCP(t *testing.T) { } response := readFrom(conn, time.Second*20, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } @@ -506,8 +505,8 @@ func TestShadowsocksChacha20Poly1305TCP(t *testing.T) { } response := readFrom(conn, time.Second*20, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } diff --git a/testing/scenarios/tls_test.go b/testing/scenarios/tls_test.go index c2d19b38a..ce714d5e4 100644 --- a/testing/scenarios/tls_test.go +++ b/testing/scenarios/tls_test.go @@ -8,9 +8,13 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" + "golang.org/x/sync/errgroup" + "v2ray.com/core" "v2ray.com/core/app/proxyman" "v2ray.com/core/common" + "v2ray.com/core/common/errors" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol/tls/cert" @@ -31,8 +35,6 @@ import ( ) func TestSimpleTLSConnection(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } @@ -127,21 +129,35 @@ func TestSimpleTLSConnection(t *testing.T) { common.Must(err) defer CloseAllServers(servers) - { + var errg errgroup.Group + errg.Go(func() error { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert(err, IsNil) + if err != nil { + return err + } + defer conn.Close() + + payload := make([]byte, 1024) + common.Must2(rand.Read(payload)) - payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + common.Must(err) + if nBytes != len(payload) { + return errors.New("expected ", len(payload), " written, but actually ", nBytes) + } response := readFrom(conn, time.Second*2, len(payload)) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) + if r := cmp.Diff(response, xor(payload)); r != "" { + return errors.New(r) + } + return nil + }) + + if err := errg.Wait(); err != nil { + t.Fatal(err) } } diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index 5e3d3e0b6..50f39ce32 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -8,12 +8,12 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "v2ray.com/core/transport/internet/headers/wechat" "v2ray.com/core" "v2ray.com/core/app/log" "v2ray.com/core/app/proxyman" - "v2ray.com/core/common/compare" clog "v2ray.com/core/common/log" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -427,8 +427,8 @@ func TestVMessQuic(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*40, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index d00667163..46f98784d 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -7,11 +7,11 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" "v2ray.com/core" "v2ray.com/core/app/log" "v2ray.com/core/app/proxyman" "v2ray.com/core/common" - "v2ray.com/core/common/compare" clog "v2ray.com/core/common/log" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -289,8 +289,8 @@ func TestVMessGCM(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*40, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } @@ -420,8 +420,8 @@ func TestVMessGCMReadv(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*40, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + t.Error(r) } }() } @@ -934,8 +934,8 @@ func TestVMessKCP(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Minute*2, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor(payload)); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor(payload)); r != "" { + t.Error(r) } }() } @@ -1105,8 +1105,8 @@ func TestVMessKCPLarge(t *testing.T) { assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Minute*10, 10240*1024) - if err := compare.BytesEqualWithDetail(response, xor(payload)); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor(payload)); r != "" { + t.Error(r) } }() } diff --git a/transport/internet/sockopt_test.go b/transport/internet/sockopt_test.go index 11af684d5..2bef2b2e9 100644 --- a/transport/internet/sockopt_test.go +++ b/transport/internet/sockopt_test.go @@ -4,9 +4,9 @@ import ( "context" "testing" + "github.com/google/go-cmp/cmp" "v2ray.com/core/common" "v2ray.com/core/common/buf" - "v2ray.com/core/common/compare" "v2ray.com/core/testing/servers/tcp" . "v2ray.com/core/transport/internet" ) @@ -34,7 +34,7 @@ func TestTCPFastOpen(t *testing.T) { b := buf.New() common.Must2(b.ReadFrom(conn)) - if err := compare.BytesEqualWithDetail(b.Bytes(), []byte("abcd")); err != nil { - t.Fatal(err) + if r := cmp.Diff(b.Bytes(), []byte("abcd")); r != "" { + t.Fatal(r) } }