remove common/compare package

This commit is contained in:
Darien Raymond
2019-01-07 00:12:04 +01:00
parent 4468c60b95
commit d26700a2fe
16 changed files with 81 additions and 139 deletions
+4 -3
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)
}
}
-29
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
}
-43
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)
}
}
}
+6 -5
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)
}
}
+4 -3
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{
-8
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)
+2 -3
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")