Skip to content

Commit

Permalink
remove dependency on assert lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 8, 2019
1 parent edd71de commit 163776b
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 203 deletions.
25 changes: 14 additions & 11 deletions app/stats/stats_test.go
Expand Up @@ -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 changes: 4 additions & 4 deletions common/net/port_test.go
Expand Up @@ -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 changes: 7 additions & 5 deletions common/protocol/id_test.go
Expand Up @@ -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 changes: 3 additions & 5 deletions common/protocol/time_test.go
Expand Up @@ -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 changes: 5 additions & 4 deletions common/serial/string_test.go
Expand Up @@ -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{}
Expand All @@ -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)
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions common/signal/notifier_test.go
Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions common/strmatcher/matchers_test.go
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 7 additions & 6 deletions proxy/socks/protocol_test.go
Expand Up @@ -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{
Expand All @@ -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) {
Expand Down
93 changes: 21 additions & 72 deletions 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"
Expand All @@ -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())
Expand Down Expand Up @@ -131,38 +127,24 @@ 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) {
if runtime.GOOS == "windows" {
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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()
}

0 comments on commit 163776b

Please sign in to comment.