forked from gliderlabs/ssh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcpip_test.go
175 lines (149 loc) · 4.19 KB
/
tcpip_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
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
package ssh
import (
"bytes"
"context"
"io/ioutil"
"net"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
gossh "golang.org/x/crypto/ssh"
)
var sampleServerResponse = []byte("Hello world")
func sampleTCPSocketServer() net.Listener {
l := newLocalTCPListener()
go func() {
conn, err := l.Accept()
if err != nil {
return
}
conn.Write(sampleServerResponse)
conn.Close()
}()
return l
}
func newTestSessionWithForwarding(t *testing.T, forwardingEnabled bool) (net.Listener, *gossh.Client, func()) {
l := sampleTCPSocketServer()
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
LocalPortForwardingCallback: func(ctx Context, destinationHost string, destinationPort uint32) bool {
addr := net.JoinHostPort(destinationHost, strconv.FormatInt(int64(destinationPort), 10))
if addr != l.Addr().String() {
panic("unexpected destinationHost: " + addr)
}
return forwardingEnabled
},
}, nil)
return l, client, func() {
cleanup()
l.Close()
}
}
func TestLocalPortForwardingWorks(t *testing.T) {
t.Parallel()
l, client, cleanup := newTestSessionWithForwarding(t, true)
defer cleanup()
conn, err := client.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("Error connecting to %v: %v", l.Addr().String(), err)
}
result, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, sampleServerResponse) {
t.Fatalf("result = %#v; want %#v", result, sampleServerResponse)
}
}
func TestLocalPortForwardingRespectsCallback(t *testing.T) {
t.Parallel()
l, client, cleanup := newTestSessionWithForwarding(t, false)
defer cleanup()
_, err := client.Dial("tcp", l.Addr().String())
if err == nil {
t.Fatalf("Expected error connecting to %v but it succeeded", l.Addr().String())
}
if !strings.Contains(err.Error(), "port forwarding is disabled") {
t.Fatalf("Expected permission error but got %#v", err)
}
}
func TestReverseTCPForwardingWorks(t *testing.T) {
t.Parallel()
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
ReversePortForwardingCallback: func(ctx Context, bindHost string, bindPort uint32) bool {
if bindHost != "127.0.0.1" {
panic("unexpected bindHost: " + bindHost)
}
if bindPort != 0 {
panic("unexpected bindPort: " + strconv.Itoa(int(bindPort)))
}
return true
},
}, nil)
defer cleanup()
l, err := client.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("failed to listen on a random TCP port over SSH: %v", err)
}
defer l.Close()
go func() {
conn, err := l.Accept()
if err != nil {
return
}
conn.Write(sampleServerResponse)
conn.Close()
}()
// Dial the listener that should've been created by the server.
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("Error connecting to %v: %v", l.Addr().String(), err)
}
result, err := ioutil.ReadAll(conn)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, sampleServerResponse) {
t.Fatalf("result = %#v; want %#v", result, sampleServerResponse)
}
// Close the listener and make sure that the port is no longer in use.
err = l.Close()
if err != nil {
t.Fatalf("failed to close remote listener: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
var d net.Dialer
_, err = d.DialContext(ctx, "tcp", l.Addr().String())
if err == nil {
t.Fatalf("expected error connecting to %v but it succeeded", l.Addr().String())
}
}
func TestReverseTCPForwardingRespectsCallback(t *testing.T) {
t.Parallel()
var called int64
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
ReversePortForwardingCallback: func(ctx Context, bindHost string, bindPort uint32) bool {
atomic.AddInt64(&called, 1)
if bindHost != "127.0.0.1" {
panic("unexpected bindHost: " + bindHost)
}
if bindPort != 0 {
panic("unexpected bindPort: " + strconv.Itoa(int(bindPort)))
}
return false
},
}, nil)
defer cleanup()
_, err := client.Listen("tcp", "127.0.0.1:0")
if err == nil {
t.Fatalf("Expected error listening on random port but it succeeded")
}
if atomic.LoadInt64(&called) != 1 {
t.Fatalf("Expected callback to be called once but it was called %d times", called)
}
}