1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

remove dependency on assert lib

This commit is contained in:
Darien Raymond 2019-01-08 23:27:02 +01:00
parent edd71de1c3
commit 163776b182
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
11 changed files with 92 additions and 205 deletions

View File

@ -7,26 +7,29 @@ import (
. "v2ray.com/core/app/stats" . "v2ray.com/core/app/stats"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/features/stats" "v2ray.com/core/features/stats"
. "v2ray.com/ext/assert"
) )
func TestInternface(t *testing.T) { func TestInternface(t *testing.T) {
assert := With(t) _ = (stats.Manager)(new(Manager))
assert((*Manager)(nil), Implements, (*stats.Manager)(nil))
} }
func TestStatsCounter(t *testing.T) { func TestStatsCounter(t *testing.T) {
assert := With(t)
raw, err := common.CreateObject(context.Background(), &Config{}) raw, err := common.CreateObject(context.Background(), &Config{})
assert(err, IsNil) common.Must(err)
m := raw.(stats.Manager) m := raw.(stats.Manager)
c, err := m.RegisterCounter("test.counter") c, err := m.RegisterCounter("test.counter")
assert(err, IsNil) common.Must(err)
assert(c.Add(1), Equals, int64(1)) if v := c.Add(1); v != 1 {
assert(c.Set(0), Equals, int64(1)) t.Fatal("unpexcted Add(1) return: ", v, ", wanted ", 1)
assert(c.Value(), Equals, int64(0)) }
if v := c.Set(0); v != 1 {
t.Fatal("unexpected Set(0) return: ", v, ", wanted ", 1)
}
if v := c.Value(); v != 0 {
t.Fatal("unexpected Value() return: ", v, ", wanted ", 0)
}
} }

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
. "v2ray.com/core/common/net" . "v2ray.com/core/common/net"
. "v2ray.com/ext/assert"
) )
func TestPortRangeContains(t *testing.T) { func TestPortRangeContains(t *testing.T) {
assert := With(t)
portRange := &PortRange{ portRange := &PortRange{
From: 53, From: 53,
To: 53, To: 53,
} }
assert(portRange.Contains(Port(53)), IsTrue)
if !portRange.Contains(Port(53)) {
t.Error("expected port range containing 53, but actually not")
}
} }

View File

@ -5,15 +5,17 @@ import (
. "v2ray.com/core/common/protocol" . "v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid" "v2ray.com/core/common/uuid"
. "v2ray.com/ext/assert"
) )
func TestIdEquals(t *testing.T) { func TestIdEquals(t *testing.T) {
assert := With(t)
id1 := NewID(uuid.New()) id1 := NewID(uuid.New())
id2 := NewID(id1.UUID()) id2 := NewID(id1.UUID())
assert(id1.Equals(id2), IsTrue) if !id1.Equals(id2) {
assert(id1.String(), Equals, id2.String()) t.Error("expected id1 to equal id2, but actually not")
}
if !id1.String() != id2.String() {
t.Error(id1.String(), " != ", id2.String())
}
} }

View File

@ -5,19 +5,17 @@ import (
"time" "time"
. "v2ray.com/core/common/protocol" . "v2ray.com/core/common/protocol"
. "v2ray.com/ext/assert"
) )
func TestGenerateRandomInt64InRange(t *testing.T) { func TestGenerateRandomInt64InRange(t *testing.T) {
assert := With(t)
base := time.Now().Unix() base := time.Now().Unix()
delta := 100 delta := 100
generator := NewTimestampGenerator(Timestamp(base), delta) generator := NewTimestampGenerator(Timestamp(base), delta)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
val := int64(generator()) val := int64(generator())
assert(val, AtMost, base+int64(delta)) if val > base+int64(delta) || val < base-int64(delta) {
assert(val, AtLeast, base-int64(delta)) t.Error(val, " not between ", base-int64(delta), " and ", base+int64(delta))
}
} }
} }

View File

@ -4,13 +4,12 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/google/go-cmp/cmp"
. "v2ray.com/core/common/serial" . "v2ray.com/core/common/serial"
. "v2ray.com/ext/assert"
) )
func TestToString(t *testing.T) { func TestToString(t *testing.T) {
assert := With(t)
s := "a" s := "a"
data := []struct { data := []struct {
Value interface{} Value interface{}
@ -23,7 +22,9 @@ func TestToString(t *testing.T) {
} }
for _, c := range data { for _, c := range data {
assert(ToString(c.Value), Equals, c.String) if r := cmp.Diff(ToString(c.Value), c.String); r != "" {
t.Error(r)
}
} }
} }

View File

@ -4,12 +4,9 @@ import (
"testing" "testing"
. "v2ray.com/core/common/signal" . "v2ray.com/core/common/signal"
//. "v2ray.com/ext/assert"
) )
func TestNotifierSignal(t *testing.T) { func TestNotifierSignal(t *testing.T) {
//assert := With(t)
n := NewNotifier() n := NewNotifier()
w := n.Wait() w := n.Wait()

View File

@ -5,12 +5,9 @@ import (
"v2ray.com/core/common" "v2ray.com/core/common"
. "v2ray.com/core/common/strmatcher" . "v2ray.com/core/common/strmatcher"
ast "v2ray.com/ext/assert"
) )
func TestMatcher(t *testing.T) { func TestMatcher(t *testing.T) {
assert := ast.With(t)
cases := []struct { cases := []struct {
pattern string pattern string
mType Type mType Type
@ -69,6 +66,8 @@ func TestMatcher(t *testing.T) {
for _, test := range cases { for _, test := range cases {
matcher, err := test.mType.New(test.pattern) matcher, err := test.mType.New(test.pattern)
common.Must(err) 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)
}
} }
} }

View File

@ -4,18 +4,17 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
_ "v2ray.com/core/common/net/testing" _ "v2ray.com/core/common/net/testing"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
. "v2ray.com/core/proxy/socks" . "v2ray.com/core/proxy/socks"
. "v2ray.com/ext/assert"
) )
func TestUDPEncoding(t *testing.T) { func TestUDPEncoding(t *testing.T) {
assert := With(t)
b := buf.New() b := buf.New()
request := &protocol.RequestHeader{ request := &protocol.RequestHeader{
@ -27,13 +26,15 @@ func TestUDPEncoding(t *testing.T) {
content := []byte{'a'} content := []byte{'a'}
payload := buf.New() payload := buf.New()
payload.Write(content) payload.Write(content)
assert(writer.WriteMultiBuffer(buf.MultiBuffer{payload}), IsNil) common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload}))
reader := NewUDPReader(b) reader := NewUDPReader(b)
decodedPayload, err := reader.ReadMultiBuffer() decodedPayload, err := reader.ReadMultiBuffer()
assert(err, IsNil) common.Must(err)
assert(decodedPayload[0].Bytes(), Equals, content) if r := cmp.Diff(decodedPayload[0].Bytes(), content); r != "" {
t.Error(r)
}
} }
func TestReadUsernamePassword(t *testing.T) { func TestReadUsernamePassword(t *testing.T) {

View File

@ -1,19 +1,17 @@
package scenarios package scenarios
import ( import (
"crypto/rand"
"os" "os"
"runtime" "runtime"
"sync"
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp" "golang.org/x/sync/errgroup"
"v2ray.com/core/transport/internet/headers/wechat"
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
clog "v2ray.com/core/common/log" clog "v2ray.com/core/common/log"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -28,19 +26,17 @@ import (
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/domainsocket" "v2ray.com/core/transport/internet/domainsocket"
"v2ray.com/core/transport/internet/headers/http" "v2ray.com/core/transport/internet/headers/http"
"v2ray.com/core/transport/internet/headers/wechat"
"v2ray.com/core/transport/internet/quic" "v2ray.com/core/transport/internet/quic"
tcptransport "v2ray.com/core/transport/internet/tcp" tcptransport "v2ray.com/core/transport/internet/tcp"
. "v2ray.com/ext/assert"
) )
func TestHttpConnectionHeader(t *testing.T) { func TestHttpConnectionHeader(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
userID := protocol.NewID(uuid.New()) userID := protocol.NewID(uuid.New())
@ -131,24 +127,12 @@ func TestHttpConnectionHeader(t *testing.T) {
} }
servers, err := InitializeServerConfigs(serverConfig, clientConfig) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil) common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
IP: []byte{127, 0, 0, 1}, t.Error(err)
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)
CloseAllServers(servers)
} }
func TestDomainSocket(t *testing.T) { func TestDomainSocket(t *testing.T) {
@ -156,13 +140,11 @@ func TestDomainSocket(t *testing.T) {
t.Skip("Not supported on windows") t.Skip("Not supported on windows")
return return
} }
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
const dsPath = "/tmp/ds_scenario" const dsPath = "/tmp/ds_scenario"
@ -258,34 +240,20 @@ func TestDomainSocket(t *testing.T) {
} }
servers, err := InitializeServerConfigs(serverConfig, clientConfig) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil) common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
IP: []byte{127, 0, 0, 1}, t.Error(err)
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)
CloseAllServers(servers)
} }
func TestVMessQuic(t *testing.T) { func TestVMessQuic(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
userID := protocol.NewID(uuid.New()) userID := protocol.NewID(uuid.New())
@ -406,31 +374,12 @@ func TestVMessQuic(t *testing.T) {
} }
defer CloseAllServers(servers) defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
wg.Add(1) errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
go func() { }
defer wg.Done()
if err := errg.Wait(); err != nil {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ t.Error(err)
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)
}
}()
} }
wg.Wait()
} }

View File

@ -31,13 +31,11 @@ import (
) )
func TestVMessDynamicPort(t *testing.T) { func TestVMessDynamicPort(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
userID := protocol.NewID(uuid.New()) userID := protocol.NewID(uuid.New())
@ -141,38 +139,22 @@ func TestVMessDynamicPort(t *testing.T) {
} }
servers, err := InitializeServerConfigs(serverConfig, clientConfig) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil) common.Must(err)
defer CloseAllServers(servers)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
IP: []byte{127, 0, 0, 1}, t.Error(err)
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)
} }
CloseAllServers(servers)
} }
func TestVMessGCM(t *testing.T) { func TestVMessGCM(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
userID := protocol.NewID(uuid.New()) 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) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
if err != nil { if err != nil {
t.Fatal("Failed to initialize all servers: ", err.Error()) t.Fatal("Failed to initialize all servers: ", err.Error())
} }
defer CloseAllServers(servers) defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
wg.Add(1) errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
go func() { }
defer wg.Done()
if err := errg.Wait(); err != nil {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ t.Error(err)
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)
}
}()
} }
wg.Wait()
} }
func TestVMessGCMReadv(t *testing.T) { func TestVMessGCMReadv(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
userID := protocol.NewID(uuid.New()) userID := protocol.NewID(uuid.New())
@ -400,33 +355,13 @@ func TestVMessGCMReadv(t *testing.T) {
} }
defer CloseAllServers(servers) defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
wg.Add(10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go func() { errg.Go(testTCPConn(clientPort, 10240*1024, time.Second*40))
defer wg.Done() }
if err := errg.Wait(); err != nil {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ t.Error(err)
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)
}
}()
} }
wg.Wait()
} }
func TestVMessGCMUDP(t *testing.T) { func TestVMessGCMUDP(t *testing.T) {

View File

@ -4,22 +4,24 @@ import (
"context" "context"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/tcp"
. "v2ray.com/core/transport/internet" . "v2ray.com/core/transport/internet"
. "v2ray.com/ext/assert"
) )
func TestDialWithLocalAddr(t *testing.T) { func TestDialWithLocalAddr(t *testing.T) {
assert := With(t)
server := &tcp.Server{} server := &tcp.Server{}
dest, err := server.Start() dest, err := server.Start()
assert(err, IsNil) common.Must(err)
defer server.Close() defer server.Close()
conn, err := DialSystem(context.Background(), net.TCPDestination(net.LocalHostIP, dest.Port), nil) conn, err := DialSystem(context.Background(), net.TCPDestination(net.LocalHostIP, dest.Port), nil)
assert(err, IsNil) common.Must(err)
assert(conn.RemoteAddr().String(), Equals, "127.0.0.1:"+dest.Port.String()) if r := cmp.Diff(conn.RemoteAddr().String(), "127.0.0.1:"+dest.Port.String()); r != "" {
t.Error(r)
}
conn.Close() conn.Close()
} }