box_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "context"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/sagernet/sing-box"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing/common/bufio"
  10. "github.com/sagernet/sing/common/debug"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/sagernet/sing/protocol/socks"
  14. "github.com/stretchr/testify/require"
  15. "go.uber.org/goleak"
  16. )
  17. func TestMain(m *testing.M) {
  18. goleak.VerifyTestMain(m)
  19. }
  20. func startInstance(t *testing.T, options option.Options) *box.Box {
  21. if debug.Enabled {
  22. options.Log = &option.LogOptions{
  23. Level: "trace",
  24. }
  25. } else {
  26. options.Log = &option.LogOptions{
  27. Level: "warning",
  28. }
  29. }
  30. // ctx := context.Background()
  31. ctx, cancel := context.WithCancel(context.Background())
  32. var instance *box.Box
  33. var err error
  34. for retry := 0; retry < 3; retry++ {
  35. instance, err = box.New(ctx, options)
  36. require.NoError(t, err)
  37. err = instance.Start()
  38. if err != nil {
  39. time.Sleep(time.Second)
  40. continue
  41. }
  42. break
  43. }
  44. require.NoError(t, err)
  45. t.Cleanup(func() {
  46. instance.Close()
  47. cancel()
  48. })
  49. return instance
  50. }
  51. func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
  52. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  53. dialTCP := func() (net.Conn, error) {
  54. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  55. }
  56. dialUDP := func() (net.PacketConn, error) {
  57. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  58. }
  59. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  60. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  61. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  62. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  63. // require.NoError(t, testPacketConnTimeout(t, dialUDP))
  64. }
  65. func testTCP(t *testing.T, clientPort uint16, testPort uint16) {
  66. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  67. dialTCP := func() (net.Conn, error) {
  68. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  69. }
  70. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  71. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  72. }
  73. func testSuitSimple(t *testing.T, clientPort uint16, testPort uint16) {
  74. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  75. dialTCP := func() (net.Conn, error) {
  76. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  77. }
  78. dialUDP := func() (net.PacketConn, error) {
  79. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  80. }
  81. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  82. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  83. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  84. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  85. }
  86. func testSuitSimple1(t *testing.T, clientPort uint16, testPort uint16) {
  87. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  88. dialTCP := func() (net.Conn, error) {
  89. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  90. }
  91. dialUDP := func() (net.PacketConn, error) {
  92. return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
  93. }
  94. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  95. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  96. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  97. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  98. }
  99. func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) {
  100. dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
  101. dialTCP := func() (net.Conn, error) {
  102. return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  103. }
  104. dialUDP := func() (net.PacketConn, error) {
  105. conn, err := dialer.DialContext(context.Background(), "udp", M.ParseSocksaddrHostPort("10.0.0.1", testPort))
  106. if err != nil {
  107. return nil, err
  108. }
  109. return bufio.NewUnbindPacketConn(conn), nil
  110. }
  111. require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
  112. require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
  113. require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
  114. require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
  115. }