forked from gliderlabs/ssh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlocal_test.go
203 lines (174 loc) · 4.99 KB
/
streamlocal_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
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
package ssh
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
gossh "golang.org/x/crypto/ssh"
)
// tempDirUnixSocket returns a temporary directory that can safely hold unix
// sockets.
//
// On all platforms other than darwin this just returns t.TempDir(). On darwin
// we manually make a temporary directory in /tmp because t.TempDir() returns a
// very long directory name, and the path length limit for Unix sockets on
// darwin is 104 characters.
func tempDirUnixSocket(t *testing.T) string {
t.Helper()
if runtime.GOOS == "darwin" {
testName := strings.ReplaceAll(t.Name(), "/", "_")
dir, err := os.MkdirTemp("/tmp", fmt.Sprintf("gliderlabs-ssh-test-%s-", testName))
if err != nil {
t.Fatalf("create temp dir for test: %v", err)
}
t.Cleanup(func() {
err := os.RemoveAll(dir)
if err != nil {
t.Errorf("remove temp dir %s: %v", dir, err)
}
})
return dir
}
return t.TempDir()
}
func newLocalUnixListener(t *testing.T) net.Listener {
path := filepath.Join(tempDirUnixSocket(t), "socket.sock")
l, err := net.Listen("unix", path)
if err != nil {
t.Fatalf("failed to listen on a unix socket %q: %v", path, err)
}
return l
}
func sampleUnixSocketServer(t *testing.T) net.Listener {
l := newLocalUnixListener(t)
go func() {
conn, err := l.Accept()
if err != nil {
return
}
conn.Write(sampleServerResponse)
conn.Close()
}()
return l
}
func newTestSessionWithUnixForwarding(t *testing.T, forwardingEnabled bool) (net.Listener, *gossh.Client, func()) {
l := sampleUnixSocketServer(t)
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
LocalUnixForwardingCallback: func(ctx Context, socketPath string) bool {
if socketPath != l.Addr().String() {
panic("unexpected socket path: " + socketPath)
}
return forwardingEnabled
},
}, nil)
return l, client, func() {
cleanup()
l.Close()
}
}
func TestLocalUnixForwardingWorks(t *testing.T) {
t.Parallel()
l, client, cleanup := newTestSessionWithUnixForwarding(t, true)
defer cleanup()
conn, err := client.Dial("unix", 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 TestLocalUnixForwardingRespectsCallback(t *testing.T) {
t.Parallel()
l, client, cleanup := newTestSessionWithUnixForwarding(t, false)
defer cleanup()
_, err := client.Dial("unix", l.Addr().String())
if err == nil {
t.Fatalf("Expected error connecting to %v but it succeeded", l.Addr().String())
}
if !strings.Contains(err.Error(), "unix forwarding is disabled") {
t.Fatalf("Expected permission error but got %#v", err)
}
}
func TestReverseUnixForwardingWorks(t *testing.T) {
t.Parallel()
remoteSocketPath := filepath.Join(tempDirUnixSocket(t), "remote.sock")
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
ReverseUnixForwardingCallback: func(ctx Context, socketPath string) bool {
if socketPath != remoteSocketPath {
panic("unexpected socket path: " + socketPath)
}
return true
},
}, nil)
defer cleanup()
l, err := client.ListenUnix(remoteSocketPath)
if err != nil {
t.Fatalf("failed to listen on a unix socket over SSH %q: %v", remoteSocketPath, 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("unix", remoteSocketPath)
if err != nil {
t.Fatalf("Error connecting to %v: %v", remoteSocketPath, 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 Unix socket is gone.
err = l.Close()
if err != nil {
t.Fatalf("failed to close remote listener: %v", err)
}
_, err = os.Stat(remoteSocketPath)
if err == nil && !os.IsNotExist(err) {
t.Fatalf("expected remote socket to be gone but it still exists: %v", err)
}
}
func TestReverseUnixForwardingRespectsCallback(t *testing.T) {
t.Parallel()
remoteSocketPath := filepath.Join(tempDirUnixSocket(t), "remote.sock")
var called int64
_, client, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {},
ReverseUnixForwardingCallback: func(ctx Context, socketPath string) bool {
atomic.AddInt64(&called, 1)
if socketPath != remoteSocketPath {
panic("unexpected socket path: " + socketPath)
}
return false
},
}, nil)
defer cleanup()
_, err := client.ListenUnix(remoteSocketPath)
if err == nil {
t.Fatalf("Expected error listening on %q but it succeeded", remoteSocketPath)
}
if atomic.LoadInt64(&called) != 1 {
t.Fatalf("Expected callback to be called once but it was called %d times", called)
}
}