-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_test.go
148 lines (123 loc) · 3.23 KB
/
proxy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2022 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tornado
import (
"context"
"errors"
"io"
"log"
"os"
"testing"
"time"
"golang.org/x/net/proxy"
"github.com/xorcare/tornado/internal/deadlock"
"github.com/xorcare/tornado/internal/torproject"
)
const TestProxyServerStartupTimeout = 300 * time.Second
var WithTestTorrOptions = WithTorrcOption(os.Getenv("TORNADO_TEST_TORRC_OPTIONS"))
var _ proxy.Dialer = (*Proxy)(nil)
var _ proxy.ContextDialer = (*Proxy)(nil)
var _ io.Closer = (*Proxy)(nil)
func TestNewProxy(t *testing.T) {
t.Parallel()
t.Run("Should be get error impossible dial because set deadlock context dialer", func(t *testing.T) {
t.Parallel()
// arrange
ctx, done := context.WithTimeout(context.Background(), TestProxyServerStartupTimeout)
t.Cleanup(done)
prx, err := NewProxy(
ctx,
WithTestTorrOptions,
WithForwardContextDialer(deadlock.Dealer{}),
)
if err != nil {
t.Fatalf("cannot make proxy: %v", err)
}
// act
_, err = prx.DialContext(ctx, "tcp", "localhost:80")
// assert
if !errors.Is(err, deadlock.ErrDeadlockDial) {
const format = "deadlock.ErrDeadlockDial error was expected, but got another one: %v"
log.Fatalf(format, err)
}
})
t.Run("Should get a successful tor ip checking result", func(t *testing.T) {
t.Parallel()
// arrange
ctx, done := context.WithTimeout(context.Background(), TestProxyServerStartupTimeout)
t.Cleanup(done)
prx, err := NewProxy(
ctx,
WithTestTorrOptions,
)
if err != nil {
t.Fatalf("cannot make proxy: %v", err)
}
defer prx.Close()
t.Run("Check that the dial is working", func(t *testing.T) {
// act
cr, err := torproject.CheckDialer(prx)
if err != nil {
t.Fatalf("failed make torproject check: %v", err)
}
// assert
t.Log("ip address received as a result of checking", cr.IP)
if !cr.IsTor {
t.Fatal("tor proxy server was not used")
}
})
t.Run("Check that the dial context is working", func(t *testing.T) {
// act
cr, err := torproject.CheckContextDialer(prx)
if err != nil {
t.Fatalf("failed make torproject check: %v", err)
}
// assert
t.Log("ip address received as a result of checking", cr.IP)
if !cr.IsTor {
t.Fatal("tor proxy server was not used")
}
})
})
}
func TestProxy_Close(t *testing.T) {
t.Parallel()
newProxy := func(t *testing.T) *Proxy {
ctx, done := context.WithTimeout(context.Background(), TestProxyServerStartupTimeout)
t.Cleanup(done)
pool, err := NewProxy(ctx, WithTestTorrOptions)
if err != nil {
t.Fatal(err)
}
return pool
}
t.Run("Single calls to the close method do not return an error", func(t *testing.T) {
t.Parallel()
// arrange
pool := newProxy(t)
// act
err := pool.Close()
// assert
if err != nil {
t.Fatal("should not get an error:", err)
}
})
t.Run("Multiple calls to the close method do not return an error", func(t *testing.T) {
t.Parallel()
// arrange
pool := newProxy(t)
var err error
// act
for i := 0; i < 10; i++ {
err = pool.Close()
if err != nil {
break
}
}
// assert
if err != nil {
t.Fatal("should not get an error:", err)
}
})
}