Skip to content

Commit

Permalink
remove common/compare package
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 6, 2019
1 parent 4468c60 commit d26700a
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 139 deletions.
7 changes: 4 additions & 3 deletions common/buf/readv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
29 changes: 0 additions & 29 deletions common/compare/bytes.go

This file was deleted.

43 changes: 0 additions & 43 deletions common/compare/bytes_test.go

This file was deleted.

11 changes: 6 additions & 5 deletions common/crypto/chacha20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
7 changes: 4 additions & 3 deletions common/net/address.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package net

import (
"bytes"
"net"
"strings"

"v2ray.com/core/common/compare"
)

var (
Expand Down Expand Up @@ -90,14 +89,16 @@ 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) {
case net.IPv4len:
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{
Expand Down
8 changes: 0 additions & 8 deletions common/protocol/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions common/uuid/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions proxy/mtproto/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
}

Expand Down
9 changes: 6 additions & 3 deletions proxy/mtproto/server.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions testing/scenarios/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"

"google.golang.org/grpc"

"v2ray.com/core"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions testing/scenarios/reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"v2ray.com/core"
"v2ray.com/core/app/log"
"v2ray.com/core/app/policy"
"v2ray.com/core/app/proxyman"
"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"
Expand Down Expand Up @@ -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)
}
}()
}
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions testing/scenarios/shadowsocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}()
}
Expand Down Expand Up @@ -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)
}
}()
}
Expand Down
Loading

0 comments on commit d26700a

Please sign in to comment.