2022-07-09 00:01:23 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
2022-07-10 19:17:44 +08:00
|
|
|
"time"
|
2022-07-09 00:01:23 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-10 14:22:28 +08:00
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-07-15 11:18:14 +08:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mkPort(t *testing.T) uint16 {
|
2022-07-10 14:22:28 +08:00
|
|
|
var lc net.ListenConfig
|
|
|
|
lc.Control = control.ReuseAddr()
|
2022-07-09 00:01:23 +08:00
|
|
|
for {
|
2022-07-15 11:18:14 +08:00
|
|
|
tcpListener, err := lc.Listen(context.Background(), "tcp", ":0")
|
2022-07-09 00:01:23 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
listenPort := M.SocksaddrFromNet(tcpListener.Addr()).Port
|
|
|
|
tcpListener.Close()
|
2022-07-15 11:18:14 +08:00
|
|
|
udpListener, err := lc.Listen(context.Background(), "tcp", F.ToString(":", listenPort))
|
2022-07-09 00:01:23 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
udpListener.Close()
|
|
|
|
return listenPort
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startInstance(t *testing.T, options option.Options) {
|
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++ {
|
2022-08-01 12:23:34 +08:00
|
|
|
instance, err = box.New(context.Background(), options)
|
2022-07-18 20:40:14 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = instance.Start()
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
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() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
instance.Close()
|
|
|
|
})
|
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, "", "")
|
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
|
|
|
}
|
2022-08-01 12:23:34 +08:00
|
|
|
/*t.Run("tcp", func(t *testing.T) {
|
2022-07-18 12:32:31 +08:00
|
|
|
t.Parallel()
|
2022-07-18 18:50:19 +08:00
|
|
|
var err error
|
|
|
|
for retry := 0; retry < 3; retry++ {
|
|
|
|
err = testLargeDataWithConn(t, testPort, dialTCP)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2022-07-18 12:32:31 +08:00
|
|
|
})
|
|
|
|
t.Run("udp", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2022-07-18 18:50:19 +08:00
|
|
|
var err error
|
|
|
|
for retry := 0; retry < 3; retry++ {
|
|
|
|
err = testLargeDataWithPacketConn(t, testPort, dialUDP)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2022-08-01 12:23:34 +08:00
|
|
|
})*/
|
|
|
|
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
|
|
|
|
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
|
2022-07-18 12:32:31 +08:00
|
|
|
// require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
|
|
|
|
// require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
|
|
|
|
// require.NoError(t, testPacketConnTimeout(t, dialUDP))
|
2022-07-09 00:01:23 +08:00
|
|
|
}
|