Chore: cleanup test code

This commit is contained in:
gVisor bot 2022-05-21 17:37:06 +08:00
parent 9b365f6c05
commit 461896a35d
11 changed files with 164 additions and 245 deletions

16
test/.golangci.yaml Normal file
View file

@ -0,0 +1,16 @@
linters:
disable-all: true
enable:
- gofumpt
- govet
- gci
- staticcheck
linters-settings:
gci:
sections:
- standard
- prefix(github.com/Dreamacro/clash)
- default
staticcheck:
go: '1.18'

View file

@ -1,8 +1,9 @@
lint: lint:
golangci-lint run --disable-all -E govet -E gofumpt -E megacheck ./... GOOS=darwin golangci-lint run ./...
GOOS=linux golangci-lint run ./...
test: test:
go test -p 1 -v ./... go test -p 1 -v ./...
benchmark: benchmark:
go test -benchmem -run=^$ -bench . go test -benchmem -run=^$$ -bench .

View file

@ -24,6 +24,7 @@ import (
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -52,13 +53,10 @@ var (
{HostPort: "10002", HostIP: "0.0.0.0"}, {HostPort: "10002", HostIP: "0.0.0.0"},
}, },
} }
isDarwin = runtime.GOOS == "darwin"
) )
func init() { func init() {
if runtime.GOOS == "darwin" {
isDarwin = true
}
currentDir, err := os.Getwd() currentDir, err := os.Getwd()
if err != nil { if err != nil {
panic(err) panic(err)
@ -110,6 +108,7 @@ func init() {
continue continue
} }
println("pulling image:", image)
imageStream, err := c.ImagePull(context.Background(), image, types.ImagePullOptions{}) imageStream, err := c.ImagePull(context.Background(), image, types.ImagePullOptions{})
if err != nil { if err != nil {
panic(err) panic(err)
@ -214,46 +213,35 @@ func testPingPongWithSocksPort(t *testing.T, port int) {
pingCh, pongCh, test := newPingPongPair() pingCh, pongCh, test := newPingPongPair()
go func() { go func() {
l, err := Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer l.Close() defer l.Close()
c, err := l.Accept() c, err := l.Accept()
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
buf := make([]byte, 4) buf := make([]byte, 4)
if _, err := io.ReadFull(c, buf); err != nil { _, err = io.ReadFull(c, buf)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
pingCh <- buf pingCh <- buf
if _, err := c.Write([]byte("pong")); err != nil { _, err = c.Write([]byte("pong"))
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
}() }()
go func() { go func() {
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port)) c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer c.Close() defer c.Close()
if _, err := socks5.ClientHandshake(c, socks5.ParseAddr("127.0.0.1:10001"), socks5.CmdConnect, nil); err != nil { _, err = socks5.ClientHandshake(c, socks5.ParseAddr("127.0.0.1:10001"), socks5.CmdConnect, nil)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
if _, err := c.Write([]byte("ping")); err != nil { _, err = c.Write([]byte("ping"))
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
buf := make([]byte, 4) buf := make([]byte, 4)
if _, err := io.ReadFull(c, buf); err != nil { _, err = io.ReadFull(c, buf)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
pongCh <- buf pongCh <- buf
}() }()
@ -304,9 +292,7 @@ func testPingPongWithConn(t *testing.T, c net.Conn) error {
func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error { func testPingPongWithPacketConn(t *testing.T, pc net.PacketConn) error {
l, err := ListenPacket("udp", ":10001") l, err := ListenPacket("udp", ":10001")
if err != nil { require.NoError(t, err)
return err
}
defer l.Close() defer l.Close()
rAddr := &net.UDPAddr{IP: localIP, Port: 10001} rAddr := &net.UDPAddr{IP: localIP, Port: 10001}
@ -349,9 +335,7 @@ type hashPair struct {
func testLargeDataWithConn(t *testing.T, c net.Conn) error { func testLargeDataWithConn(t *testing.T, c net.Conn) error {
l, err := Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { require.NoError(t, err)
return err
}
defer l.Close() defer l.Close()
times := 100 times := 100
@ -443,9 +427,7 @@ func testLargeDataWithConn(t *testing.T, c net.Conn) error {
func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error { func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error {
l, err := ListenPacket("udp", ":10001") l, err := ListenPacket("udp", ":10001")
if err != nil { require.NoError(t, err)
return err
}
defer l.Close() defer l.Close()
rAddr := &net.UDPAddr{IP: localIP, Port: 10001} rAddr := &net.UDPAddr{IP: localIP, Port: 10001}
@ -541,7 +523,7 @@ func testLargeDataWithPacketConn(t *testing.T, pc net.PacketConn) error {
func testPacketConnTimeout(t *testing.T, pc net.PacketConn) error { func testPacketConnTimeout(t *testing.T, pc net.PacketConn) error {
err := pc.SetReadDeadline(time.Now().Add(time.Millisecond * 300)) err := pc.SetReadDeadline(time.Now().Add(time.Millisecond * 300))
assert.NoError(t, err) require.NoError(t, err)
errCh := make(chan error, 1) errCh := make(chan error, 1)
go func() { go func() {
@ -564,9 +546,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypDomainName, AddrType: socks5.AtypDomainName,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer conn.Close() defer conn.Close()
assert.NoError(t, testPingPongWithConn(t, conn)) assert.NoError(t, testPingPongWithConn(t, conn))
@ -575,9 +555,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypDomainName, AddrType: socks5.AtypDomainName,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer conn.Close() defer conn.Close()
assert.NoError(t, testLargeDataWithConn(t, conn)) assert.NoError(t, testLargeDataWithConn(t, conn))
@ -591,9 +569,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypIPv4, AddrType: socks5.AtypIPv4,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer pc.Close() defer pc.Close()
assert.NoError(t, testPingPongWithPacketConn(t, pc)) assert.NoError(t, testPingPongWithPacketConn(t, pc))
@ -604,9 +580,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypIPv4, AddrType: socks5.AtypIPv4,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer pc.Close() defer pc.Close()
assert.NoError(t, testLargeDataWithPacketConn(t, pc)) assert.NoError(t, testLargeDataWithPacketConn(t, pc))
@ -617,9 +591,7 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypIPv4, AddrType: socks5.AtypIPv4,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
defer pc.Close() defer pc.Close()
assert.NoError(t, testPacketConnTimeout(t, pc)) assert.NoError(t, testPacketConnTimeout(t, pc))
@ -627,15 +599,13 @@ func testSuit(t *testing.T, proxy C.ProxyAdapter) {
func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) { func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) {
l, err := Listen("tcp", ":10001") l, err := Listen("tcp", ":10001")
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
defer l.Close() defer l.Close()
go func() { go func() {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
assert.FailNow(b, err.Error()) return
} }
defer c.Close() defer c.Close()
@ -650,16 +620,15 @@ func benchmarkProxy(b *testing.B, proxy C.ProxyAdapter) {
DstPort: "10001", DstPort: "10001",
AddrType: socks5.AtypDomainName, AddrType: socks5.AtypDomainName,
}) })
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
} _, err = conn.Write([]byte("skip protocol handshake"))
require.NoError(b, err)
b.SetBytes(chunkSize) b.SetBytes(chunkSize)
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := conn.Write(chunk); err != nil { conn.Write(chunk)
assert.FailNow(b, err.Error())
}
} }
} }
@ -669,12 +638,11 @@ mixed-port: 10000
log-level: silent log-level: silent
` `
if err := parseAndApply(basic); err != nil { err := parseAndApply(basic)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
defer cleanup() defer cleanup()
time.Sleep(waitTime) require.True(t, TCPing(net.JoinHostPort(localIP.String(), "10000")))
testPingPongWithSocksPort(t, 10000) testPingPongWithSocksPort(t, 10000)
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func exchange(address, domain string, tp uint16) ([]dns.RR, error) { func exchange(address, domain string, tp uint16) ([]dns.RR, error) {
@ -30,18 +31,15 @@ dns:
- 119.29.29.29 - 119.29.29.29
` `
if err := parseAndApply(basic); err != nil { err := parseAndApply(basic)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
defer cleanup() defer cleanup()
time.Sleep(waitTime) time.Sleep(waitTime)
rr, err := exchange("127.0.0.1:8553", "1.1.1.1.nip.io", dns.TypeA) rr, err := exchange("127.0.0.1:8553", "1.1.1.1.nip.io", dns.TypeA)
assert.NoError(t, err) assert.NoError(t, err)
if !assert.NotEmpty(t, rr) { assert.NotEmptyf(t, rr, "record empty")
assert.FailNow(t, "record empty")
}
record := rr[0].(*dns.A) record := rr[0].(*dns.A)
assert.Equal(t, record.A.String(), "1.1.1.1") assert.Equal(t, record.A.String(), "1.1.1.1")
@ -68,9 +66,8 @@ dns:
- 119.29.29.29 - 119.29.29.29
` `
if err := parseAndApply(basic); err != nil { err := parseAndApply(basic)
assert.FailNow(t, err.Error()) require.NoError(t, err)
}
defer cleanup() defer cleanup()
time.Sleep(waitTime) time.Sleep(waitTime)

View file

@ -8,8 +8,6 @@ import (
"github.com/docker/docker/client" "github.com/docker/docker/client"
) )
var isDarwin = false
func startContainer(cfg *container.Config, hostCfg *container.HostConfig, name string) (string, error) { func startContainer(cfg *container.Config, hostCfg *container.HostConfig, name string) (string, error) {
c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil { if err != nil {

View file

@ -9,7 +9,7 @@ import (
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestClash_SnellObfsHTTP(t *testing.T) { func TestClash_SnellObfsHTTP(t *testing.T) {
@ -24,9 +24,7 @@ func TestClash_SnellObfsHTTP(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "snell-http") id, err := startContainer(cfg, hostCfg, "snell-http")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -41,9 +39,7 @@ func TestClash_SnellObfsHTTP(t *testing.T) {
"mode": "http", "mode": "http",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -61,9 +57,7 @@ func TestClash_SnellObfsTLS(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "snell-tls") id, err := startContainer(cfg, hostCfg, "snell-tls")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -78,9 +72,7 @@ func TestClash_SnellObfsTLS(t *testing.T) {
"mode": "tls", "mode": "tls",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -98,9 +90,7 @@ func TestClash_Snell(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "snell") id, err := startContainer(cfg, hostCfg, "snell")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -112,9 +102,7 @@ func TestClash_Snell(t *testing.T) {
Port: 10002, Port: 10002,
Psk: "password", Psk: "password",
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -132,9 +120,7 @@ func TestClash_Snellv3(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "snell") id, err := startContainer(cfg, hostCfg, "snell")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -148,9 +134,7 @@ func TestClash_Snellv3(t *testing.T) {
UDP: true, UDP: true,
Version: 3, Version: 3,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -168,9 +152,7 @@ func Benchmark_Snell(b *testing.B) {
} }
id, err := startContainer(cfg, hostCfg, "snell-http") id, err := startContainer(cfg, hostCfg, "snell-http")
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
b.Cleanup(func() { b.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -185,9 +167,7 @@ func Benchmark_Snell(b *testing.B) {
"mode": "http", "mode": "http",
}, },
}) })
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
benchmarkProxy(b, proxy) benchmarkProxy(b, proxy)

View file

@ -1,13 +1,14 @@
package main package main
import ( import (
"net"
"testing" "testing"
"time" "time"
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outbound"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestClash_Shadowsocks(t *testing.T) { func TestClash_Shadowsocks(t *testing.T) {
@ -22,9 +23,7 @@ func TestClash_Shadowsocks(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "ss") id, err := startContainer(cfg, hostCfg, "ss")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -38,9 +37,7 @@ func TestClash_Shadowsocks(t *testing.T) {
Cipher: "chacha20-ietf-poly1305", Cipher: "chacha20-ietf-poly1305",
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -60,9 +57,7 @@ func TestClash_ShadowsocksObfsHTTP(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "ss-obfs-http") id, err := startContainer(cfg, hostCfg, "ss-obfs-http")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -80,9 +75,7 @@ func TestClash_ShadowsocksObfsHTTP(t *testing.T) {
"mode": "http", "mode": "http",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -102,9 +95,7 @@ func TestClash_ShadowsocksObfsTLS(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "ss-obfs-tls") id, err := startContainer(cfg, hostCfg, "ss-obfs-tls")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -122,9 +113,7 @@ func TestClash_ShadowsocksObfsTLS(t *testing.T) {
"mode": "tls", "mode": "tls",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -144,9 +133,7 @@ func TestClash_ShadowsocksV2RayPlugin(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "ss-v2ray-plugin") id, err := startContainer(cfg, hostCfg, "ss-v2ray-plugin")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -164,9 +151,7 @@ func TestClash_ShadowsocksV2RayPlugin(t *testing.T) {
"mode": "websocket", "mode": "websocket",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -184,9 +169,7 @@ func Benchmark_Shadowsocks(b *testing.B) {
} }
id, err := startContainer(cfg, hostCfg, "ss") id, err := startContainer(cfg, hostCfg, "ss")
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
b.Cleanup(func() { b.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -200,10 +183,8 @@ func Benchmark_Shadowsocks(b *testing.B) {
Cipher: "aes-256-gcm", Cipher: "aes-256-gcm",
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime) require.True(b, TCPing(net.JoinHostPort(localIP.String(), "10002")))
benchmarkProxy(b, proxy) benchmarkProxy(b, proxy)
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"net"
"testing" "testing"
"time" "time"
@ -9,7 +10,7 @@ import (
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestClash_Trojan(t *testing.T) { func TestClash_Trojan(t *testing.T) {
@ -27,9 +28,7 @@ func TestClash_Trojan(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "trojan") id, err := startContainer(cfg, hostCfg, "trojan")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -44,9 +43,7 @@ func TestClash_Trojan(t *testing.T) {
SkipCertVerify: true, SkipCertVerify: true,
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -67,10 +64,10 @@ func TestClash_TrojanGrpc(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "trojan-grpc") id, err := startContainer(cfg, hostCfg, "trojan-grpc")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewTrojan(outbound.TrojanOption{ proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan", Name: "trojan",
@ -85,9 +82,7 @@ func TestClash_TrojanGrpc(t *testing.T) {
GrpcServiceName: "example", GrpcServiceName: "example",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -108,10 +103,10 @@ func TestClash_TrojanWebsocket(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "trojan-ws") id, err := startContainer(cfg, hostCfg, "trojan-ws")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewTrojan(outbound.TrojanOption{ proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan", Name: "trojan",
@ -123,9 +118,7 @@ func TestClash_TrojanWebsocket(t *testing.T) {
UDP: true, UDP: true,
Network: "ws", Network: "ws",
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -145,10 +138,8 @@ func Benchmark_Trojan(b *testing.B) {
}, },
} }
id, err := startContainer(cfg, hostCfg, "trojan") id, err := startContainer(cfg, hostCfg, "trojan-bench")
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
b.Cleanup(func() { b.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -163,10 +154,8 @@ func Benchmark_Trojan(b *testing.B) {
SkipCertVerify: true, SkipCertVerify: true,
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime) require.True(b, TCPing(net.JoinHostPort(localIP.String(), "10002")))
benchmarkProxy(b, proxy) benchmarkProxy(b, proxy)
} }

View file

@ -35,3 +35,16 @@ func ListenPacket(network, address string) (net.PacketConn, error) {
} }
return nil, lastErr return nil, lastErr
} }
func TCPing(addr string) bool {
for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
return true
}
time.Sleep(time.Millisecond * 500)
}
return false
}

View file

@ -4,9 +4,9 @@ package main
import ( import (
"errors" "errors"
"net" "net/netip"
) )
func defaultRouteIP() (net.IP, error) { func defaultRouteIP() (netip.Addr, error) {
return nil, errors.New("not supported") return netip.Addr{}, errors.New("not supported")
} }

View file

@ -9,7 +9,7 @@ import (
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func TestClash_Vmess(t *testing.T) { func TestClash_Vmess(t *testing.T) {
@ -25,9 +25,7 @@ func TestClash_Vmess(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess") id, err := startContainer(cfg, hostCfg, "vmess")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
t.Cleanup(func() { t.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -41,9 +39,7 @@ func TestClash_Vmess(t *testing.T) {
Cipher: "auto", Cipher: "auto",
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -64,10 +60,10 @@ func TestClash_VmessTLS(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-tls") id, err := startContainer(cfg, hostCfg, "vmess-tls")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -80,9 +76,7 @@ func TestClash_VmessTLS(t *testing.T) {
ServerName: "example.org", ServerName: "example.org",
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -103,10 +97,10 @@ func TestClash_VmessHTTP2(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-http2") id, err := startContainer(cfg, hostCfg, "vmess-http2")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -124,9 +118,7 @@ func TestClash_VmessHTTP2(t *testing.T) {
Path: "/test", Path: "/test",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -145,10 +137,10 @@ func TestClash_VmessHTTP(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-http") id, err := startContainer(cfg, hostCfg, "vmess-http")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -176,9 +168,7 @@ func TestClash_VmessHTTP(t *testing.T) {
}, },
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -197,10 +187,10 @@ func TestClash_VmessWebsocket(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-ws") id, err := startContainer(cfg, hostCfg, "vmess-ws")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -211,9 +201,7 @@ func TestClash_VmessWebsocket(t *testing.T) {
Network: "ws", Network: "ws",
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -234,10 +222,10 @@ func TestClash_VmessWebsocketTLS(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-ws") id, err := startContainer(cfg, hostCfg, "vmess-ws")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -250,9 +238,7 @@ func TestClash_VmessWebsocketTLS(t *testing.T) {
SkipCertVerify: true, SkipCertVerify: true,
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -273,10 +259,10 @@ func TestClash_VmessGrpc(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-grpc") id, err := startContainer(cfg, hostCfg, "vmess-grpc")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -293,9 +279,7 @@ func TestClash_VmessGrpc(t *testing.T) {
GrpcServiceName: "example!", GrpcServiceName: "example!",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -314,10 +298,10 @@ func TestClash_VmessWebsocket0RTT(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-ws-0rtt") id, err := startContainer(cfg, hostCfg, "vmess-ws-0rtt")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -333,9 +317,7 @@ func TestClash_VmessWebsocket0RTT(t *testing.T) {
EarlyDataHeaderName: "Sec-WebSocket-Protocol", EarlyDataHeaderName: "Sec-WebSocket-Protocol",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
@ -354,10 +336,10 @@ func TestClash_VmessWebsocketXray0RTT(t *testing.T) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-xray-ws-0rtt") id, err := startContainer(cfg, hostCfg, "vmess-xray-ws-0rtt")
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error()) t.Cleanup(func() {
} cleanContainer(id)
defer cleanContainer(id) })
proxy, err := outbound.NewVmess(outbound.VmessOption{ proxy, err := outbound.NewVmess(outbound.VmessOption{
Name: "vmess", Name: "vmess",
@ -372,16 +354,14 @@ func TestClash_VmessWebsocketXray0RTT(t *testing.T) {
Path: "/?ed=2048", Path: "/?ed=2048",
}, },
}) })
if err != nil { require.NoError(t, err)
assert.FailNow(t, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
testSuit(t, proxy) testSuit(t, proxy)
} }
func Benchmark_Vmess(b *testing.B) { func Benchmark_Vmess(b *testing.B) {
configPath := C.Path.Resolve("vmess-aead.json") configPath := C.Path.Resolve("vmess.json")
cfg := &container.Config{ cfg := &container.Config{
Image: ImageVmess, Image: ImageVmess,
@ -393,9 +373,7 @@ func Benchmark_Vmess(b *testing.B) {
} }
id, err := startContainer(cfg, hostCfg, "vmess-aead") id, err := startContainer(cfg, hostCfg, "vmess-aead")
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
b.Cleanup(func() { b.Cleanup(func() {
cleanContainer(id) cleanContainer(id)
@ -410,9 +388,7 @@ func Benchmark_Vmess(b *testing.B) {
AlterID: 0, AlterID: 0,
UDP: true, UDP: true,
}) })
if err != nil { require.NoError(b, err)
assert.FailNow(b, err.Error())
}
time.Sleep(waitTime) time.Sleep(waitTime)
benchmarkProxy(b, proxy) benchmarkProxy(b, proxy)