proxy_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2011 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. "bytes"
  7. "fmt"
  8. "net/url"
  9. "os"
  10. "strings"
  11. "testing"
  12. "golang.org/x/net/internal/socks"
  13. "golang.org/x/net/internal/sockstest"
  14. )
  15. type proxyFromEnvTest struct {
  16. allProxyEnv string
  17. noProxyEnv string
  18. wantTypeOf Dialer
  19. }
  20. func (t proxyFromEnvTest) String() string {
  21. var buf bytes.Buffer
  22. space := func() {
  23. if buf.Len() > 0 {
  24. buf.WriteByte(' ')
  25. }
  26. }
  27. if t.allProxyEnv != "" {
  28. fmt.Fprintf(&buf, "all_proxy=%q", t.allProxyEnv)
  29. }
  30. if t.noProxyEnv != "" {
  31. space()
  32. fmt.Fprintf(&buf, "no_proxy=%q", t.noProxyEnv)
  33. }
  34. return strings.TrimSpace(buf.String())
  35. }
  36. func TestFromEnvironment(t *testing.T) {
  37. ResetProxyEnv()
  38. type dummyDialer struct {
  39. direct
  40. }
  41. RegisterDialerType("irc", func(_ *url.URL, _ Dialer) (Dialer, error) {
  42. return dummyDialer{}, nil
  43. })
  44. proxyFromEnvTests := []proxyFromEnvTest{
  45. {allProxyEnv: "127.0.0.1:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
  46. {allProxyEnv: "ftp://example.com:8000", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
  47. {allProxyEnv: "socks5://example.com:8080", noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: &PerHost{}},
  48. {allProxyEnv: "socks5h://example.com", wantTypeOf: &socks.Dialer{}},
  49. {allProxyEnv: "irc://example.com:8000", wantTypeOf: dummyDialer{}},
  50. {noProxyEnv: "localhost, 127.0.0.1", wantTypeOf: direct{}},
  51. {wantTypeOf: direct{}},
  52. }
  53. for _, tt := range proxyFromEnvTests {
  54. os.Setenv("ALL_PROXY", tt.allProxyEnv)
  55. os.Setenv("NO_PROXY", tt.noProxyEnv)
  56. ResetCachedEnvironment()
  57. d := FromEnvironment()
  58. if got, want := fmt.Sprintf("%T", d), fmt.Sprintf("%T", tt.wantTypeOf); got != want {
  59. t.Errorf("%v: got type = %T, want %T", tt, d, tt.wantTypeOf)
  60. }
  61. }
  62. }
  63. func TestFromURL(t *testing.T) {
  64. ss, err := sockstest.NewServer(sockstest.NoAuthRequired, sockstest.NoProxyRequired)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. defer ss.Close()
  69. url, err := url.Parse("socks5://user:password@" + ss.Addr().String())
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. proxy, err := FromURL(url, nil)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. c, err := proxy.Dial("tcp", "fqdn.doesnotexist:5963")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. c.Close()
  82. }
  83. func TestSOCKS5(t *testing.T) {
  84. ss, err := sockstest.NewServer(sockstest.NoAuthRequired, sockstest.NoProxyRequired)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer ss.Close()
  89. proxy, err := SOCKS5("tcp", ss.Addr().String(), nil, nil)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. c, err := proxy.Dial("tcp", ss.TargetAddr().String())
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. c.Close()
  98. }
  99. func ResetProxyEnv() {
  100. for _, env := range []*envOnce{allProxyEnv, noProxyEnv} {
  101. for _, v := range env.names {
  102. os.Setenv(v, "")
  103. }
  104. }
  105. ResetCachedEnvironment()
  106. }
  107. func ResetCachedEnvironment() {
  108. allProxyEnv.reset()
  109. noProxyEnv.reset()
  110. }