-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
177 lines (144 loc) · 4.24 KB
/
integration_test.go
File metadata and controls
177 lines (144 loc) · 4.24 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"context"
"net"
"sync"
"testing"
"time"
"github.com/techmagister/socks5proxy/internal/socks5"
"golang.org/x/net/proxy"
)
// setupTestServer creates a test server that echoes data back to clients
func setupTestServer(t *testing.T) (net.Listener, func()) {
testServer, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Failed to create test server: %v", err)
}
// Start accepting connections (echo server)
go func() {
conn, err := testServer.Accept()
if err != nil {
return // Server closed
}
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
return
}
conn.Write(buf[:n]) // Echo back
conn.Close()
testServer.Close()
}()
cleanup := func() {
testServer.Close()
}
return testServer, cleanup
}
// setupProxy creates and starts a SOCKS5 proxy server
func setupProxy(t *testing.T, config socks5.Config) (string, context.CancelFunc, *sync.WaitGroup) {
proxyListener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Failed to find available port: %v", err)
}
proxyAddr := proxyListener.Addr().String()
proxyListener.Close()
ctxProxy, cancelProxy := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
config.Addr = proxyAddr // Override address
go func() {
defer wg.Done()
server := socks5.NewServer(config)
ctx, cancel := context.WithTimeout(ctxProxy, 10*time.Second)
defer cancel()
if err := server.ListenAndServe(ctx); err != context.Canceled && err != nil {
t.Errorf("Proxy server failed: %v", err)
}
}()
// Give the proxy time to start
time.Sleep(100 * time.Millisecond)
return proxyAddr, cancelProxy, &wg
}
// runIntegrationTest runs the actual proxy integration test
func runIntegrationTest(t *testing.T, proxyAddr, testAddr string, auth *proxy.Auth, testMessage string) {
socksDialer, err := proxy.SOCKS5("tcp", proxyAddr, auth, proxy.Direct)
if err != nil {
t.Fatalf("Failed to create SOCKS5 dialer: %v", err)
}
conn, err := socksDialer.Dial("tcp", testAddr)
if err != nil {
t.Fatalf("Failed to connect through SOCKS5 proxy: %v", err)
}
defer conn.Close()
message := []byte(testMessage)
_, err = conn.Write(message)
if err != nil {
t.Errorf("Failed to write through proxy: %v", err)
}
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
t.Errorf("Failed to read through proxy: %v", err)
}
if string(buf[:n]) != testMessage {
t.Errorf("Expected echo %q, got %q", testMessage, string(buf[:n]))
}
}
func TestSOCKS5ProxyIntegration(t *testing.T) {
// Setup test server
testServer, cleanup := setupTestServer(t)
defer cleanup()
testAddr := testServer.Addr().String()
// Setup proxy without authentication
config := socks5.Config{}
proxyAddr, cancelProxy, wg := setupProxy(t, config)
// Run integration test
runIntegrationTest(t, proxyAddr, testAddr, nil, "test")
cancelProxy()
wg.Wait()
}
// TestUnauthenticatedConnectionRejected tests that when auth is required,
// clients that don't provide authentication are rejected
func TestUnauthenticatedConnectionRejected(t *testing.T) {
// Setup test server
testServer, cleanup := setupTestServer(t)
defer cleanup()
testAddr := testServer.Addr().String()
// Setup proxy without authentication
config := socks5.Config{
Username: "testuser",
Password: "testpass",
}
proxyAddr, cancelProxy, wg := setupProxy(t, config)
// Run integration test
socksDialer, err := proxy.SOCKS5("tcp", proxyAddr, nil, proxy.Direct)
if err != nil {
t.Fatalf("Failed to create SOCKS5 dialer: %v", err)
}
if _, err := socksDialer.Dial("tcp", testAddr); err == nil {
t.Fatalf("Auth is not working.")
}
cancelProxy()
wg.Wait()
}
func TestSOCKS5ProxyIntegrationWithAuth(t *testing.T) {
// Setup test server
testServer, cleanup := setupTestServer(t)
defer cleanup()
testAddr := testServer.Addr().String()
// Setup proxy with authentication
config := socks5.Config{
Username: "testuser",
Password: "testpass",
}
proxyAddr, cancelProxy, wg := setupProxy(t, config)
// Setup authentication for client
auth := &proxy.Auth{
User: config.Username,
Password: config.Password,
}
// Run integration test with authentication
runIntegrationTest(t, proxyAddr, testAddr, auth, "hello authenticated proxy")
cancelProxy()
wg.Wait()
}