-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathportforward_integration_test.go
More file actions
230 lines (194 loc) · 5.28 KB
/
portforward_integration_test.go
File metadata and controls
230 lines (194 loc) · 5.28 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package ecsta
import (
"context"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
)
// TestTCPProxyToLocalhost tests the core TCP proxy functionality
func TestTCPProxyToLocalhost(t *testing.T) {
// Start a mock backend server on localhost (this simulates Session Manager Plugin)
backendListener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("Failed to start backend server:", err)
}
defer backendListener.Close()
backendPort := backendListener.Addr().(*net.TCPAddr).Port
// Start mock backend server that echoes data
var backendWg sync.WaitGroup
backendCtx, backendCancel := context.WithCancel(context.Background())
defer backendCancel()
backendWg.Go(func() {
for {
select {
case <-backendCtx.Done():
return
default:
}
// Set a short timeout for Accept to allow context checking
backendListener.(*net.TCPListener).SetDeadline(time.Now().Add(100 * time.Millisecond))
conn, err := backendListener.Accept()
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue // timeout, check context again
}
return // Listener closed or other error
}
// Reset deadline for the connection
backendListener.(*net.TCPListener).SetDeadline(time.Time{})
go func(c net.Conn) {
defer c.Close()
// Echo server: read and write back
buffer := make([]byte, 1024)
n, err := c.Read(buffer)
if err != nil {
return
}
_, err = c.Write(buffer[:n])
if err != nil {
return
}
}(conn)
}
})
// Get a different port for proxy frontend
proxyListener, err := net.Listen("tcp", "0.0.0.0:0")
if err != nil {
t.Fatal("Failed to get proxy port:", err)
}
proxyPort := proxyListener.Addr().(*net.TCPAddr).Port
proxyListener.Close() // Free the port for proxy to use
// Create Ecsta instance and start TCP proxy
app := &Ecsta{}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
var proxyWg sync.WaitGroup
proxyWg.Go(func() {
err := app.startTCPProxyToLocalhost(ctx, "0.0.0.0", proxyPort, backendPort)
if err != nil && err != context.DeadlineExceeded && err != context.Canceled {
t.Logf("Proxy ended with: %v", err)
}
})
// Wait a bit for proxy to start
time.Sleep(50 * time.Millisecond)
// Test client connection to proxy
clientConn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", proxyPort))
if err != nil {
t.Fatal("Failed to connect to proxy:", err)
}
defer clientConn.Close()
// Test data transfer
testData := "Hello, TCP Proxy!"
// Send data
_, err = clientConn.Write([]byte(testData))
if err != nil {
t.Fatal("Failed to write data:", err)
}
// Read echoed data
buffer := make([]byte, len(testData))
_, err = io.ReadFull(clientConn, buffer)
if err != nil {
t.Fatal("Failed to read data:", err)
}
// Verify data
if string(buffer) != testData {
t.Errorf("Data mismatch: got %q, want %q", string(buffer), testData)
}
// Close client connection before cleanup
clientConn.Close()
// Cleanup: cancel contexts and wait for goroutines
cancel()
backendCancel()
// Wait for goroutines to finish with timeout
done := make(chan struct{})
go func() {
proxyWg.Wait()
backendWg.Wait()
close(done)
}()
select {
case <-done:
// All goroutines finished successfully
case <-time.After(2 * time.Second):
t.Log("Warning: goroutines did not finish within timeout")
}
}
// TestHandleProxyConnection tests the actual handleProxyConnection function
func TestHandleProxyConnection(t *testing.T) {
// Start backend server
backendListener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("Failed to start backend:", err)
}
defer backendListener.Close()
backendPort := backendListener.Addr().(*net.TCPAddr).Port
// Simple echo server with context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
backendListener.(*net.TCPListener).SetDeadline(time.Now().Add(100 * time.Millisecond))
conn, err := backendListener.Accept()
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue
}
return
}
go func(c net.Conn) {
defer c.Close()
io.Copy(c, c) // Echo
}(conn)
}
}()
// Create mock client-server connection pair
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
defer serverConn.Close()
app := &Ecsta{}
proxyCtx, proxyCancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer proxyCancel()
// Start handleProxyConnection
var proxyWg sync.WaitGroup
proxyWg.Go(func() {
app.handleProxyConnection(proxyCtx, serverConn, backendPort)
})
// Test communication
testData := "test data"
go func() {
clientConn.Write([]byte(testData))
}()
buffer := make([]byte, len(testData))
_, err = io.ReadFull(clientConn, buffer)
if err != nil {
t.Fatal("Failed to read:", err)
}
if string(buffer) != testData {
t.Errorf("Got %q, want %q", string(buffer), testData)
}
// Clean shutdown
clientConn.Close()
serverConn.Close()
proxyCancel()
cancel()
// Wait for proxy goroutine with timeout
done := make(chan struct{})
go func() {
proxyWg.Wait()
close(done)
}()
select {
case <-done:
// Success
case <-time.After(1 * time.Second):
t.Log("Warning: proxy goroutine did not finish within timeout")
}
}