box_test.go 4.8 KB

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