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