Browse Source

remove dependency on assert lib

pull/1511/head
Darien Raymond 6 years ago
parent
commit
163776b182
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 25
      app/stats/stats_test.go
  2. 8
      common/net/port_test.go
  3. 12
      common/protocol/id_test.go
  4. 8
      common/protocol/time_test.go
  5. 9
      common/serial/string_test.go
  6. 3
      common/signal/notifier_test.go
  7. 7
      common/strmatcher/matchers_test.go
  8. 13
      proxy/socks/protocol_test.go
  9. 93
      testing/scenarios/transport_test.go
  10. 101
      testing/scenarios/vmess_test.go
  11. 14
      transport/internet/dialer_test.go

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

8
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")
}
}

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

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

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

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

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

13
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) {

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

101
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) {

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

Loading…
Cancel
Save