1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

remove common/compare package

This commit is contained in:
Darien Raymond 2019-01-07 00:12:04 +01:00
parent 4468c60b95
commit d26700a2fe
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
16 changed files with 81 additions and 139 deletions

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -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{

View File

@ -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)

View File

@ -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")

View File

@ -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)
}
}

View File

@ -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

View File

@ -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())

View File

@ -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()

View File

@ -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)
}
}()
}

View File

@ -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)
}
}

View File

@ -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)
}
}()
}

View File

@ -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)
}
}()
}

View File

@ -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)
}
}