sing-box/test/box_test.go

183 lines
6.7 KiB
Go
Raw Normal View History

2022-07-09 00:01:23 +08:00
package main
import (
"context"
2023-10-25 12:00:00 +08:00
"crypto/tls"
"io"
2022-07-09 00:01:23 +08:00
"net"
2023-10-25 12:00:00 +08:00
"net/http"
2022-07-09 00:01:23 +08:00
"testing"
2022-07-10 19:17:44 +08:00
"time"
2022-07-09 00:01:23 +08:00
2023-10-25 12:00:00 +08:00
"github.com/sagernet/quic-go"
"github.com/sagernet/quic-go/http3"
2022-07-09 00:01:23 +08:00
"github.com/sagernet/sing-box"
2023-07-11 14:03:55 +08:00
C "github.com/sagernet/sing-box/constant"
2024-11-18 18:59:19 +08:00
"github.com/sagernet/sing-box/include"
2022-07-09 00:01:23 +08:00
"github.com/sagernet/sing-box/option"
2022-08-16 23:37:51 +08:00
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/debug"
2022-07-09 00:01:23 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/protocol/socks"
"github.com/stretchr/testify/require"
2022-09-23 13:14:31 +08:00
"go.uber.org/goleak"
2022-07-09 00:01:23 +08:00
)
2022-09-23 13:14:31 +08:00
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
2024-11-18 18:59:19 +08:00
var globalCtx context.Context
func init() {
globalCtx = box.Context(context.Background(), include.InboundRegistry(), include.OutboundRegistry(), include.EndpointRegistry())
}
2022-09-23 13:14:31 +08:00
func startInstance(t *testing.T, options option.Options) *box.Box {
if debug.Enabled {
options.Log = &option.LogOptions{
Level: "trace",
}
2022-09-07 15:41:19 +08:00
} else {
options.Log = &option.LogOptions{
Level: "warning",
}
}
2024-11-18 18:59:19 +08:00
ctx, cancel := context.WithCancel(globalCtx)
2022-08-01 12:23:34 +08:00
var instance *box.Box
2022-07-18 20:40:14 +08:00
var err error
for retry := 0; retry < 3; retry++ {
instance, err = box.New(box.Options{
Context: ctx,
Options: options,
})
2022-07-18 20:40:14 +08:00
require.NoError(t, err)
err = instance.Start()
if err != nil {
2022-08-11 23:59:22 +08:00
time.Sleep(time.Second)
2022-07-18 20:40:14 +08:00
continue
}
2022-08-01 12:23:34 +08:00
break
2022-07-18 20:40:14 +08:00
}
2022-07-09 00:01:23 +08:00
require.NoError(t, err)
2022-08-01 12:23:34 +08:00
t.Cleanup(func() {
instance.Close()
2022-09-23 13:14:31 +08:00
cancel()
2022-08-01 12:23:34 +08:00
})
2022-09-23 13:14:31 +08:00
return instance
2022-07-09 00:01:23 +08:00
}
func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
dialUDP := func() (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
2022-09-12 17:30:54 +08:00
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
// require.NoError(t, testPacketConnTimeout(t, dialUDP))
}
2023-10-25 12:00:00 +08:00
func testQUIC(t *testing.T, clientPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
client := &http.Client{
Transport: &http3.RoundTripper{
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
destination := M.ParseSocksaddr(addr)
udpConn, err := dialer.DialContext(ctx, N.NetworkUDP, destination)
if err != nil {
return nil, err
}
return quic.DialEarly(ctx, udpConn.(net.PacketConn), destination, tlsCfg, cfg)
},
},
}
response, err := client.Get("https://cloudflare.com/cdn-cgi/trace")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode)
content, err := io.ReadAll(response.Body)
require.NoError(t, err)
println(string(content))
}
2023-08-21 18:11:44 +08:00
func testSuitLargeUDP(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
dialUDP := func() (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
require.NoError(t, testLargeDataWithPacketConnSize(t, testPort, 4096, dialUDP))
2023-08-21 18:11:44 +08:00
}
2022-08-10 20:19:16 +08:00
func testTCP(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
2022-08-22 12:02:16 +08:00
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
2022-08-24 10:21:56 +08:00
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
2022-08-10 20:19:16 +08:00
}
2022-08-29 10:10:41 +08:00
func testSuitSimple(t *testing.T, clientPort uint16, testPort uint16) {
2022-07-09 00:01:23 +08:00
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
2022-07-18 12:32:31 +08:00
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
2022-07-09 00:01:23 +08:00
}
2022-07-18 12:32:31 +08:00
dialUDP := func() (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
2022-07-09 00:01:23 +08:00
}
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
2022-09-12 17:30:54 +08:00
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
2022-08-16 23:37:51 +08:00
}
2022-09-13 10:41:10 +08:00
func testSuitSimple1(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
dialUDP := func() (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
2023-07-11 14:03:55 +08:00
if !C.IsDarwin {
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
}
2022-09-13 10:41:10 +08:00
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
2023-07-11 14:03:55 +08:00
if !C.IsDarwin {
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
}
2022-09-13 10:41:10 +08:00
}
2022-08-16 23:37:51 +08:00
func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
}
dialUDP := func() (net.PacketConn, error) {
conn, err := dialer.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
if err != nil {
return nil, err
}
return bufio.NewUnbindPacketConn(conn), nil
}
2022-09-13 10:41:10 +08:00
require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
2022-08-01 12:23:34 +08:00
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
2022-07-09 00:01:23 +08:00
}