chore: cleanup test code

This commit is contained in:
Dreamacro 2022-05-21 17:37:06 +08:00 committed by 世界
parent b9e6de45e6
commit 7079116aa8
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
10 changed files with 161 additions and 242 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 (
@ -54,13 +55,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)
@ -114,6 +112,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)
@ -218,46 +217,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
}() }()
@ -312,9 +300,7 @@ func testPingPongWithConn(t *testing.T, cc func() 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.AsSlice(), Port: 10001} rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001}
@ -357,9 +343,7 @@ type hashPair struct {
func testLargeDataWithConn(t *testing.T, cc func() net.Conn) error { func testLargeDataWithConn(t *testing.T, cc func() 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
@ -454,9 +438,7 @@ func testLargeDataWithConn(t *testing.T, cc func() 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.AsSlice(), Port: 10001} rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: 10001}
@ -552,7 +534,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() {
@ -576,9 +558,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())
}
return conn return conn
})) }))
@ -588,9 +568,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())
}
return conn return conn
})) }))
@ -604,9 +582,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))
@ -617,9 +593,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))
@ -630,9 +604,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))
@ -640,15 +612,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()
@ -663,16 +633,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())
}
} }
} }
@ -682,12 +651,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

@ -9,8 +9,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

@ -8,7 +8,7 @@ import (
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outbound"
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) {
@ -23,9 +23,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)
@ -40,9 +38,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)
@ -60,9 +56,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)
@ -77,9 +71,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)
@ -97,9 +89,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)
@ -111,9 +101,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)
@ -131,9 +119,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)
@ -147,9 +133,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)
@ -167,9 +151,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)
@ -184,9 +166,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,12 +1,13 @@
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) {
@ -21,9 +22,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)
@ -37,9 +36,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)
@ -59,9 +56,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)
@ -79,9 +74,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)
@ -101,9 +94,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)
@ -121,9 +112,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)
@ -143,9 +132,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)
@ -163,9 +150,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)
@ -183,9 +168,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)
@ -199,10 +182,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,13 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"net"
"testing" "testing"
"time" "time"
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outbound"
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) {
@ -26,9 +27,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)
@ -43,9 +42,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)
@ -66,10 +63,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",
@ -84,9 +81,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)
@ -107,10 +102,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",
@ -122,9 +117,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)
@ -184,10 +177,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)
@ -202,10 +193,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

@ -8,7 +8,7 @@ import (
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outbound"
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) {
@ -24,9 +24,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)
@ -40,9 +38,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)
@ -63,10 +59,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",
@ -79,9 +75,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)
@ -102,10 +96,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",
@ -123,9 +117,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)
@ -144,10 +136,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",
@ -175,9 +167,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)
@ -196,10 +186,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",
@ -210,9 +200,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)
@ -233,10 +221,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",
@ -249,9 +237,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)
@ -272,10 +258,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",
@ -292,9 +278,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)
@ -313,10 +297,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",
@ -332,9 +316,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)
@ -353,10 +335,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",
@ -371,16 +353,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,
@ -392,9 +372,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)
@ -409,9 +387,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)