remove dep on assert lib

pull/1546/head
Darien Raymond 2019-02-03 19:46:53 +01:00
parent 9d7efdd2e1
commit 66a60dbfa3
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
14 changed files with 231 additions and 549 deletions

View File

@ -6,26 +6,23 @@ import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
. "v2ray.com/core/common/protocol/http"
. "v2ray.com/ext/assert"
)
func TestParseXForwardedFor(t *testing.T) {
assert := With(t)
header := http.Header{}
header.Add("X-Forwarded-For", "129.78.138.66, 129.78.64.103")
addrs := ParseXForwardedFor(header)
assert(len(addrs), Equals, 2)
assert(addrs[0].String(), Equals, "129.78.138.66")
assert(addrs[1].String(), Equals, "129.78.64.103")
if r := cmp.Diff(addrs, []net.Address{net.ParseAddress("129.78.138.66"), net.ParseAddress("129.78.64.103")}); r != "" {
t.Error(r)
}
}
func TestHopByHopHeadersRemoving(t *testing.T) {
assert := With(t)
rawRequest := `GET /pkg/net/http/ HTTP/1.1
Host: golang.org
Connection: keep-alive,Foo, Bar
@ -42,18 +39,44 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3
b := bufio.NewReader(strings.NewReader(rawRequest))
req, err := http.ReadRequest(b)
common.Must(err)
assert(req.Header.Get("Foo"), Equals, "foo")
assert(req.Header.Get("Bar"), Equals, "bar")
assert(req.Header.Get("Connection"), Equals, "keep-alive,Foo, Bar")
assert(req.Header.Get("Proxy-Connection"), Equals, "keep-alive")
assert(req.Header.Get("Proxy-Authenticate"), Equals, "abc")
headers := []struct {
Key string
Value string
}{
{
Key: "Foo",
Value: "foo",
},
{
Key: "Bar",
Value: "bar",
},
{
Key: "Connection",
Value: "keep-alive,Foo, Bar",
},
{
Key: "Proxy-Connection",
Value: "keep-alive",
},
{
Key: "Proxy-Authenticate",
Value: "abc",
},
}
for _, header := range headers {
if v := req.Header.Get(header.Key); v != header.Value {
t.Error("header ", header.Key, " = ", v, " want ", header.Value)
}
}
RemoveHopByHopHeaders(req.Header)
assert(req.Header.Get("Connection"), IsEmpty)
assert(req.Header.Get("Foo"), IsEmpty)
assert(req.Header.Get("Bar"), IsEmpty)
assert(req.Header.Get("Proxy-Connection"), IsEmpty)
assert(req.Header.Get("Proxy-Authenticate"), IsEmpty)
for _, header := range []string{"Connection", "Foo", "Bar", "Proxy-Connection", "Proxy-Authenticate"} {
if v := req.Header.Get(header); v != "" {
t.Error("header ", header, " = ", v)
}
}
}
func TestParseHost(t *testing.T) {

View File

@ -3,7 +3,6 @@ package scenarios
import (
"context"
"fmt"
"io"
"testing"
"time"
@ -29,12 +28,9 @@ import (
"v2ray.com/core/proxy/vmess/inbound"
"v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/servers/tcp"
. "v2ray.com/ext/assert"
)
func TestCommanderRemoveHandler(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -99,31 +95,11 @@ func TestCommanderRemoveHandler(t *testing.T) {
servers, err := InitializeServerConfigs(clientConfig)
common.Must(err)
defer CloseAllServers(servers)
{
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
if err != nil {
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Fatal(err)
}
defer conn.Close() // nolint: errcheck
payload := "commander request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
if r := cmp.Diff(response[:nBytes], xor([]byte(payload))); r != "" {
t.Fatal(r)
}
}
cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock())
common.Must(err)
@ -134,20 +110,22 @@ func TestCommanderRemoveHandler(t *testing.T) {
Tag: "d",
})
common.Must(err)
assert(resp, IsNotNil)
if resp == nil {
t.Error("unexpected nil response")
}
{
_, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert(err, IsNotNil)
if err == nil {
t.Error("unexpected nil error")
}
}
}
func TestCommanderAddRemoveUser(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -284,24 +262,10 @@ func TestCommanderAddRemoveUser(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
CloseAllServers(servers)
{
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "commander request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert(nBytes, Equals, 0)
assert(err, Equals, io.EOF)
assert(conn.Close(), IsNil)
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Fatal(err)
}
cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock())
@ -323,40 +287,25 @@ func TestCommanderAddRemoveUser(t *testing.T) {
}),
})
common.Must(err)
assert(resp, IsNotNil)
if resp == nil {
t.Fatal("nil response")
}
{
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "commander request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Fatal(err)
}
resp, err = hsClient.AlterInbound(context.Background(), &command.AlterInboundRequest{
Tag: "v",
Operation: serial.ToTypedMessage(&command.RemoveUserOperation{Email: "test@v2ray.com"}),
})
assert(resp, IsNotNil)
common.Must(err)
CloseAllServers(servers)
if resp == nil {
t.Fatal("nil response")
}
}
func TestCommanderStats(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -515,20 +464,30 @@ func TestCommanderStats(t *testing.T) {
Reset_: true,
})
common.Must(err)
assert(sresp.Stat.Name, Equals, name)
assert(sresp.Stat.Value, Equals, int64(10240*1024))
if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
Name: name,
Value: 10240 * 1024,
}); r != "" {
t.Error(r)
}
sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
Name: name,
})
common.Must(err)
assert(sresp.Stat.Name, Equals, name)
assert(sresp.Stat.Value, Equals, int64(0))
if r := cmp.Diff(sresp.Stat, &statscmd.Stat{
Name: name,
Value: 0,
}); r != "" {
t.Error(r)
}
sresp, err = sClient.GetStats(context.Background(), &statscmd.GetStatsRequest{
Name: "inbound>>>vmess>>>traffic>>>uplink",
Reset_: true,
})
common.Must(err)
assert(sresp.Stat.Value, GreaterThan, int64(10240*1024))
if sresp.Stat.Value <= 10240*1024 {
t.Error("value < 10240*1024: ", sresp.Stat.Value)
}
}

View File

@ -160,6 +160,27 @@ func testTCPConn(port net.Port, payloadSize int, timeout time.Duration) func() e
}
defer conn.Close()
return testTCPConn2(conn, payloadSize, timeout)()
}
}
func testUDPConn(port net.Port, payloadSize int, timeout time.Duration) func() error {
return func() error {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
if err != nil {
return err
}
defer conn.Close()
return testTCPConn2(conn, payloadSize, timeout)()
}
}
func testTCPConn2(conn net.Conn, payloadSize int, timeout time.Duration) func() error {
return func() error {
payload := make([]byte, payloadSize)
common.Must2(rand.Read(payload))

View File

@ -3,6 +3,7 @@ package scenarios
import (
"fmt"
"testing"
"time"
xproxy "golang.org/x/net/proxy"
"v2ray.com/core"
@ -16,12 +17,9 @@ import (
"v2ray.com/core/proxy/freedom"
"v2ray.com/core/proxy/socks"
"v2ray.com/core/testing/servers/tcp"
. "v2ray.com/ext/assert"
)
func TestResolveIP(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -85,24 +83,17 @@ func TestResolveIP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
common.Must(err)
conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port))
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
CloseAllServers(servers)
}

View File

@ -4,6 +4,8 @@ import (
"testing"
"time"
"golang.org/x/sync/errgroup"
"v2ray.com/core"
"v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman"
@ -20,7 +22,6 @@ import (
"v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/testing/servers/udp"
. "v2ray.com/ext/assert"
)
func TestDokodemoTCP(t *testing.T) {
@ -121,8 +122,6 @@ func TestDokodemoTCP(t *testing.T) {
}
func TestDokodemoUDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -198,25 +197,13 @@ func TestDokodemoUDP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
var errg errgroup.Group
for port := clientPort; port <= clientPort+clientPortRange; port++ {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(port),
})
common.Must(err)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
errg.Go(testUDPConn(net.Port(port), 1024, time.Second*5))
}
if err := errg.Wait(); err != nil {
t.Error(err)
}
CloseAllServers(servers)
}

View File

@ -33,12 +33,9 @@ import (
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/testing/servers/udp"
"v2ray.com/core/transport/internet"
. "v2ray.com/ext/assert"
)
func TestPassiveConnection(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
SendFirst: []byte("send first"),
@ -73,6 +70,7 @@ func TestPassiveConnection(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
@ -84,27 +82,14 @@ func TestPassiveConnection(t *testing.T) {
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
common.Must(err)
assert(string(response[:nBytes]), Equals, "send first")
if string(response[:nBytes]) != "send first" {
t.Error("unexpected first response: ", string(response[:nBytes]))
}
}
payload := "dokodemo request."
{
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
{
response := make([]byte, 1024)
nBytes, err := conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
}
assert(conn.Close(), IsNil)
CloseAllServers(servers)
}
func TestProxy(t *testing.T) {
@ -240,8 +225,6 @@ func TestProxy(t *testing.T) {
}
func TestProxyOverKCP(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -377,30 +360,14 @@ func TestProxyOverKCP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
CloseAllServers(servers)
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
func TestBlackhole(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -472,35 +439,14 @@ func TestBlackhole(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(serverPort2),
})
common.Must(err)
payload := "dokodemo request."
{
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
if err := testTCPConn(serverPort2, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
{
response := make([]byte, 1024)
_, err := conn.Read(response)
assert(err, IsNotNil)
}
assert(conn.Close(), IsNil)
CloseAllServers(servers)
}
func TestForward(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -542,31 +488,22 @@ func TestForward(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
common.Must(err)
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
CloseAllServers(servers)
}
func TestUDPConnection(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -600,56 +537,20 @@ func TestUDPConnection(t *testing.T) {
servers, err := InitializeServerConfigs(clientConfig)
common.Must(err)
CloseAllServers(servers)
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "dokodemo request."
for i := 0; i < 5; i++ {
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
}
assert(conn.Close(), IsNil)
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
time.Sleep(20 * time.Second)
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
CloseAllServers(servers)
}
func TestDomainSniffing(t *testing.T) {
assert := With(t)
sniffingPort := tcp.PickPort()
httpPort := tcp.PickPort()
serverConfig := &core.Config{
@ -722,6 +623,7 @@ func TestDomainSniffing(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -736,17 +638,14 @@ func TestDomainSniffing(t *testing.T) {
resp, err := client.Get("https://www.github.com/")
common.Must(err)
assert(resp.StatusCode, Equals, 200)
assert(resp.Write(ioutil.Discard), IsNil)
if resp.StatusCode != 200 {
t.Error("unexpected status code: ", resp.StatusCode)
}
common.Must(resp.Write(ioutil.Discard))
}
CloseAllServers(servers)
}
func TestDialV2Ray(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -822,23 +721,16 @@ func TestDialV2Ray(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
client, err := core.New(clientConfig)
common.Must(err)
conn, err := core.Dial(context.Background(), client, dest)
common.Must(err)
defer conn.Close()
payload := "commander request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
CloseAllServers(servers)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}

View File

@ -2,7 +2,6 @@ package scenarios
import (
"crypto/rand"
"sync"
"testing"
"time"
@ -23,7 +22,6 @@ import (
"v2ray.com/core/proxy/shadowsocks"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/testing/servers/udp"
. "v2ray.com/ext/assert"
)
func TestShadowsocksAES256TCP(t *testing.T) {
@ -538,8 +536,6 @@ func TestShadowsocksAES256GCMTCP(t *testing.T) {
}
func TestShadowsocksAES128GCMUDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -626,38 +622,18 @@ func TestShadowsocksAES128GCMUDP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
var wg sync.WaitGroup
wg.Add(10)
var errg errgroup.Group
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
errg.Go(testUDPConn(clientPort, 1024, time.Second*5))
}
if err := errg.Wait(); err != nil {
t.Error(err)
}
wg.Wait()
CloseAllServers(servers)
}
func TestShadowsocksAES128GCMUDPMux(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -750,33 +726,15 @@ func TestShadowsocksAES128GCMUDPMux(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
var wg sync.WaitGroup
wg.Add(10)
var errg errgroup.Group
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
wg.Done()
}()
errg.Go(testUDPConn(clientPort, 1024, time.Second*5))
}
if err := errg.Wait(); err != nil {
t.Error(err)
}
wg.Wait()
CloseAllServers(servers)
}
func TestShadowsocksNone(t *testing.T) {

View File

@ -20,7 +20,6 @@ import (
"v2ray.com/core/proxy/socks"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/testing/servers/udp"
. "v2ray.com/ext/assert"
)
func TestSocksBridgeTCP(t *testing.T) {
@ -105,8 +104,6 @@ func TestSocksBridgeTCP(t *testing.T) {
}
func TestSocksBridageUDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -180,30 +177,14 @@ func TestSocksBridageUDP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
CloseAllServers(servers)
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
func TestSocksBridageUDPWithRouting(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -283,30 +264,14 @@ func TestSocksBridageUDPWithRouting(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
CloseAllServers(servers)
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
func TestSocksConformance(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -356,23 +321,18 @@ func TestSocksConformance(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct)
common.Must(err)
conn, err := noAuthDialer.Dial("tcp", dest.NetAddr())
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
{
@ -380,52 +340,32 @@ func TestSocksConformance(t *testing.T) {
common.Must(err)
conn, err := authDialer.Dial("tcp", dest.NetAddr())
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
{
dialer := socks4.DialSocksProxy(socks4.SOCKS4, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
conn, err := dialer("tcp", dest.NetAddr())
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
{
dialer := socks4.DialSocksProxy(socks4.SOCKS4A, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr())
conn, err := dialer("tcp", net.TCPDestination(net.LocalHostDomain, tcpServer.Port).NetAddr())
common.Must(err)
defer conn.Close()
payload := "test payload"
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
common.Must(err)
assert(response[:nBytes], Equals, xor([]byte(payload)))
assert(conn.Close(), IsNil)
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
t.Error(err)
}
}
CloseAllServers(servers)
}

View File

@ -1,9 +1,7 @@
package scenarios
import (
"crypto/rand"
"os"
"sync"
"testing"
"time"
@ -26,7 +24,6 @@ import (
"v2ray.com/core/testing/servers/udp"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/kcp"
. "v2ray.com/ext/assert"
)
func TestVMessDynamicPort(t *testing.T) {
@ -364,8 +361,6 @@ func TestVMessGCMReadv(t *testing.T) {
}
func TestVMessGCMUDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
@ -457,43 +452,15 @@ func TestVMessGCMUDP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := make([]byte, 1024)
rand.Read(payload)
nBytes, err := conn.Write([]byte(payload))
common.Must(err)
assert(nBytes, Equals, len(payload))
payload1 := make([]byte, 1024)
rand.Read(payload1)
nBytes, err = conn.Write([]byte(payload1))
common.Must(err)
assert(nBytes, Equals, len(payload1))
response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload)))
response = readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload1)))
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
CloseAllServers(servers)
var errg errgroup.Group
for i := 0; i < 10; i++ {
errg.Go(testTCPConn(clientPort, 1024, time.Second*5))
}
if err := errg.Wait(); err != nil {
t.Error(err)
}
}
func TestVMessChacha20(t *testing.T) {
@ -1076,8 +1043,6 @@ func TestVMessGCMMux(t *testing.T) {
}
func TestVMessGCMMuxUDP(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -1196,71 +1161,17 @@ func TestVMessGCMMuxUDP(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)
for range "abcd" {
var wg sync.WaitGroup
const nConnection = 16
wg.Add(nConnection * 2)
for i := 0; i < nConnection; i++ {
go func() {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
common.Must(err)
payload := make([]byte, 10240)
rand.Read(payload)
xorpayload := xor(payload)
nBytes, err := conn.Write(payload)
common.Must(err)
assert(nBytes, Equals, len(payload))
response := readFrom(conn, time.Second*20, 10240)
assert(response, Equals, xorpayload)
assert(conn.Close(), IsNil)
wg.Done()
}()
var errg errgroup.Group
for i := 0; i < 16; i++ {
errg.Go(testTCPConn(clientPort, 10240, time.Second*20))
errg.Go(testUDPConn(clientUDPPort, 1024, time.Second*10))
}
for i := 0; i < nConnection; i++ {
go func() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientUDPPort),
})
common.Must(err)
conn.SetDeadline(time.Now().Add(time.Second * 10))
payload := make([]byte, 1024)
rand.Read(payload)
xorpayload := xor(payload)
for j := 0; j < 2; j++ {
nBytes, _, err := conn.WriteMsgUDP(payload, nil, nil)
common.Must(err)
assert(nBytes, Equals, len(payload))
if err := errg.Wait(); err != nil {
t.Error(err)
}
response := make([]byte, 1024)
oob := make([]byte, 16)
for j := 0; j < 2; j++ {
nBytes, _, _, _, err := conn.ReadMsgUDP(response, oob)
common.Must(err)
assert(nBytes, Equals, 1024)
assert(response, Equals, xorpayload)
}
assert(conn.Close(), IsNil)
wg.Done()
}()
}
wg.Wait()
time.Sleep(time.Second)
}
CloseAllServers(servers)
}

View File

@ -11,31 +11,30 @@ import (
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
. "v2ray.com/core/transport/internet/headers/http"
. "v2ray.com/ext/assert"
)
func TestReaderWriter(t *testing.T) {
assert := With(t)
cache := buf.New()
b := buf.New()
common.Must2(b.WriteString("abcd" + ENDING))
writer := NewHeaderWriter(b)
err := writer.Write(cache)
common.Must(err)
assert(cache.Len(), Equals, int32(8))
if v := cache.Len(); v != 8 {
t.Error("cache len: ", v)
}
_, err = cache.Write([]byte{'e', 'f', 'g'})
common.Must(err)
reader := &HeaderReader{}
buffer, err := reader.Read(cache)
common.Must(err)
assert(buffer.Bytes(), Equals, []byte{'e', 'f', 'g'})
if buffer.String() != "efg" {
t.Error("buffer: ", buffer.String())
}
}
func TestRequestHeader(t *testing.T) {
assert := With(t)
auth, err := NewHttpAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Uri: []string{"/"},
@ -53,7 +52,9 @@ func TestRequestHeader(t *testing.T) {
err = auth.GetClientWriter().Write(cache)
common.Must(err)
assert(cache.String(), Equals, "GET / HTTP/1.1\r\nTest: Value\r\n\r\n")
if cache.String() != "GET / HTTP/1.1\r\nTest: Value\r\n\r\n" {
t.Error("cache: ", cache.String())
}
}
func TestLongRequestHeader(t *testing.T) {
@ -71,8 +72,6 @@ func TestLongRequestHeader(t *testing.T) {
}
func TestConnection(t *testing.T) {
assert := With(t)
auth, err := NewHttpAuthenticator(context.Background(), &Config{
Request: &RequestConfig{
Method: &Method{Value: "Post"},
@ -110,7 +109,9 @@ func TestConnection(t *testing.T) {
b := make([]byte, 256)
for {
n, err := authConn.Read(b)
common.Must(err)
if err != nil {
break
}
_, err = authConn.Write(b[:n])
common.Must(err)
}
@ -120,6 +121,8 @@ func TestConnection(t *testing.T) {
common.Must(err)
authConn := auth.Client(conn)
defer authConn.Close()
authConn.Write([]byte("Test payload"))
authConn.Write([]byte("Test payload 2"))
@ -136,5 +139,7 @@ func TestConnection(t *testing.T) {
}
}
assert(string(actualResponse[:totalBytes]), Equals, expectedResponse)
if string(actualResponse[:totalBytes]) != expectedResponse {
t.Error("response: ", string(actualResponse[:totalBytes]))
}
}

View File

@ -7,12 +7,9 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/transport/internet/headers/tls"
. "v2ray.com/ext/assert"
)
func TestDTLSWrite(t *testing.T) {
assert := With(t)
content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
dtlsRaw, err := New(context.Background(), &PacketConfig{})
common.Must(err)
@ -23,5 +20,7 @@ func TestDTLSWrite(t *testing.T) {
dtls.Serialize(payload.Extend(dtls.Size()))
payload.Write(content)
assert(payload.Len(), Equals, int32(len(content))+dtls.Size())
if payload.Len() != int32(len(content))+dtls.Size() {
t.Error("payload len: ", payload.Len(), " want ", int32(len(content))+dtls.Size())
}
}

View File

@ -7,7 +7,6 @@ import (
"v2ray.com/core/common/buf"
. "v2ray.com/core/transport/internet/kcp"
. "v2ray.com/ext/assert"
)
type NoOpCloser int
@ -17,8 +16,6 @@ func (NoOpCloser) Close() error {
}
func TestConnectionReadTimeout(t *testing.T) {
assert := With(t)
conn := NewConnection(ConnMetadata{Conversation: 1}, &KCPPacketWriter{
Writer: buf.DiscardBytes,
}, NoOpCloser(0), &Config{})
@ -26,17 +23,16 @@ func TestConnectionReadTimeout(t *testing.T) {
b := make([]byte, 1024)
nBytes, err := conn.Read(b)
assert(nBytes, Equals, 0)
assert(err, IsNotNil)
if nBytes != 0 || err == nil {
t.Error("unexpected read: ", nBytes, err)
}
conn.Terminate()
}
func TestConnectionInterface(t *testing.T) {
assert := With(t)
assert((*Connection)(nil), Implements, (*io.Writer)(nil))
assert((*Connection)(nil), Implements, (*io.Reader)(nil))
assert((*Connection)(nil), Implements, (*buf.Reader)(nil))
assert((*Connection)(nil), Implements, (*buf.Writer)(nil))
_ = (io.Writer)(new(Connection))
_ = (io.Reader)(new(Connection))
_ = (buf.Reader)(new(Connection))
_ = (buf.Writer)(new(Connection))
}

View File

@ -11,12 +11,9 @@ import (
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/transport/internet"
. "v2ray.com/core/transport/internet/tcp"
. "v2ray.com/ext/assert"
)
func TestGetOriginalDestination(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{}
dest, err := tcpServer.Start()
common.Must(err)
@ -29,5 +26,7 @@ func TestGetOriginalDestination(t *testing.T) {
defer conn.Close()
originalDest, err := GetOriginalDestination(conn)
assert(dest == originalDest || strings.Contains(err.Error(), "failed to call getsockopt"), IsTrue)
if !(dest == originalDest || strings.Contains(err.Error(), "failed to call getsockopt")) {
t.Error("unexpected state")
}
}

View File

@ -14,7 +14,6 @@ import (
"v2ray.com/core/transport"
. "v2ray.com/core/transport/internet/udp"
"v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
)
type TestDispatcher struct {
@ -38,8 +37,6 @@ func (*TestDispatcher) Type() interface{} {
}
func TestSameDestinationDispatching(t *testing.T) {
assert := With(t)
ctx, cancel := context.WithCancel(context.Background())
uplinkReader, uplinkWriter := pipe.New(pipe.WithSizeLimit(1024))
downlinkReader, downlinkWriter := pipe.New(pipe.WithSizeLimit(1024))
@ -80,6 +77,10 @@ func TestSameDestinationDispatching(t *testing.T) {
time.Sleep(time.Second)
cancel()
assert(count, Equals, uint32(1))
assert(atomic.LoadUint32(&msgCount), Equals, uint32(6))
if count != 1 {
t.Error("count: ", count)
}
if v := atomic.LoadUint32(&msgCount); v != 6 {
t.Error("msgCount: ", v)
}
}