dial_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proxy
  5. import (
  6. "context"
  7. "fmt"
  8. "os"
  9. "testing"
  10. "time"
  11. "golang.org/x/net/internal/sockstest"
  12. "golang.org/x/net/nettest"
  13. )
  14. func TestDial(t *testing.T) {
  15. ResetProxyEnv()
  16. t.Run("DirectWithCancel", func(t *testing.T) {
  17. defer ResetProxyEnv()
  18. l, err := nettest.NewLocalListener("tcp")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer l.Close()
  23. ctx, cancel := context.WithCancel(context.Background())
  24. defer cancel()
  25. c, err := Dial(ctx, l.Addr().Network(), l.Addr().String())
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. c.Close()
  30. })
  31. t.Run("DirectWithTimeout", func(t *testing.T) {
  32. defer ResetProxyEnv()
  33. l, err := nettest.NewLocalListener("tcp")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. defer l.Close()
  38. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  39. defer cancel()
  40. c, err := Dial(ctx, l.Addr().Network(), l.Addr().String())
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. c.Close()
  45. })
  46. t.Run("DirectWithTimeoutExceeded", func(t *testing.T) {
  47. defer ResetProxyEnv()
  48. l, err := nettest.NewLocalListener("tcp")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer l.Close()
  53. ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
  54. time.Sleep(time.Millisecond)
  55. defer cancel()
  56. c, err := Dial(ctx, l.Addr().Network(), l.Addr().String())
  57. if err == nil {
  58. defer c.Close()
  59. t.Fatal("failed to timeout")
  60. }
  61. })
  62. t.Run("SOCKS5", func(t *testing.T) {
  63. defer ResetProxyEnv()
  64. s, err := sockstest.NewServer(sockstest.NoAuthRequired, sockstest.NoProxyRequired)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. defer s.Close()
  69. if err = os.Setenv("ALL_PROXY", fmt.Sprintf("socks5://%s", s.Addr().String())); err != nil {
  70. t.Fatal(err)
  71. }
  72. c, err := Dial(context.Background(), s.TargetAddr().Network(), s.TargetAddr().String())
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. c.Close()
  77. })
  78. t.Run("SOCKS5WithTimeout", func(t *testing.T) {
  79. defer ResetProxyEnv()
  80. s, err := sockstest.NewServer(sockstest.NoAuthRequired, sockstest.NoProxyRequired)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. defer s.Close()
  85. if err = os.Setenv("ALL_PROXY", fmt.Sprintf("socks5://%s", s.Addr().String())); err != nil {
  86. t.Fatal(err)
  87. }
  88. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  89. defer cancel()
  90. c, err := Dial(ctx, s.TargetAddr().Network(), s.TargetAddr().String())
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. c.Close()
  95. })
  96. t.Run("SOCKS5WithTimeoutExceeded", func(t *testing.T) {
  97. defer ResetProxyEnv()
  98. s, err := sockstest.NewServer(sockstest.NoAuthRequired, sockstest.NoProxyRequired)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. defer s.Close()
  103. if err = os.Setenv("ALL_PROXY", fmt.Sprintf("socks5://%s", s.Addr().String())); err != nil {
  104. t.Fatal(err)
  105. }
  106. ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
  107. time.Sleep(time.Millisecond)
  108. defer cancel()
  109. c, err := Dial(ctx, s.TargetAddr().Network(), s.TargetAddr().String())
  110. if err == nil {
  111. defer c.Close()
  112. t.Fatal("failed to timeout")
  113. }
  114. })
  115. }