Repository navigation
Expand file tree
/
Copy pathforward_test.go
More file actions
187 lines (155 loc) · 3.8 KB
/
Copy pathforward_test.go
File metadata and controls
187 lines (155 loc) · 3.8 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
package sshlib
import (
"net"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"golang.org/x/crypto/ssh"
)
func TestGetDisplay(t *testing.T) {
for _, tc := range []struct {
expect int
input string
}{
{0, ":0.0"},
{123, ":123.0"},
{123, ":123"},
{0, "xxx"},
{11, "localhost:11.0"},
{123, "randomhost:123.0"},
} {
if act := getX11DisplayNumber(tc.input); act != tc.expect {
t.Errorf(`unexpected result for getX11Display("%s"), act="%v", exp="%v"`, tc.input, act, tc.expect)
}
}
}
func TestX11ConnectForwardedDisplayUsesTCP(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:6011")
if err != nil {
t.Fatalf("Listen() error = %v", err)
}
defer listener.Close()
accepted := make(chan struct{}, 1)
go func() {
conn, err := listener.Accept()
if err == nil {
accepted <- struct{}{}
_ = conn.Close()
}
}()
conn, err := x11Connect("127.0.0.1:11.0")
if err != nil {
t.Fatalf("x11Connect() error = %v", err)
}
_ = conn.Close()
select {
case <-accepted:
case <-time.After(time.Second):
t.Fatal("x11Connect() did not connect to forwarded TCP display")
}
}
func TestX11ConnectPathDisplayUsesUnixSocket(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("unix domain sockets are not used for X11 paths on Windows")
}
socketPath := filepath.Join("/tmp", "sshlib-x11-path.sock")
_ = os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
t.Fatalf("Listen() error = %v", err)
}
defer func() {
_ = listener.Close()
_ = os.Remove(socketPath)
}()
accepted := make(chan struct{}, 1)
go func() {
conn, err := listener.Accept()
if err == nil {
accepted <- struct{}{}
_ = conn.Close()
}
}()
conn, err := x11Connect(socketPath)
if err != nil {
t.Fatalf("x11Connect() error = %v", err)
}
_ = conn.Close()
select {
case <-accepted:
case <-time.After(time.Second):
t.Fatal("x11Connect() did not connect to Unix socket display path")
}
}
func TestX11ConnectUnixDisplayUsesDefaultSocketPath(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("unix display sockets are not available on Windows")
}
dir := "/tmp/.X11-unix"
if err := os.MkdirAll(dir, 0o777); err != nil {
t.Fatalf("MkdirAll() error = %v", err)
}
socketPath := filepath.Join(dir, "X77")
_ = os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
t.Fatalf("Listen() error = %v", err)
}
defer func() {
_ = listener.Close()
_ = os.Remove(socketPath)
}()
accepted := make(chan struct{}, 1)
go func() {
conn, err := listener.Accept()
if err == nil {
accepted <- struct{}{}
_ = conn.Close()
}
}()
conn, err := x11Connect(":77.0")
if err != nil {
t.Fatalf("x11Connect() error = %v", err)
}
_ = conn.Close()
select {
case <-accepted:
case <-time.After(time.Second):
t.Fatal("x11Connect() did not connect to default Unix display socket")
}
}
func ExampleConnect_TCPLocalForward() {
// host
host := "target.com"
port := "22"
user := "user"
key := "~/.ssh/id_rsa"
// port forwarding
localAddr := "localhost:10022"
remoteAddr := "localhost:22"
// Create ssh.AuthMethod
authMethod, _ := CreateAuthMethodPublicKey(key, "")
// Create sshlib.Connect
con := &Connect{}
// PortForward
con.TCPLocalForward(localAddr, remoteAddr)
// Connect ssh server
con.CreateClient(host, user, port, []ssh.AuthMethod{authMethod})
}
func ExampleConnect_UnixLocalForward() {
host := "target.com"
port := "22"
user := "user"
key := "~/.ssh/id_rsa"
localPath := "/tmp/local.sock"
remotePath := "/tmp/remote.sock"
authMethod, _ := CreateAuthMethodPublicKey(key, "")
con := &Connect{}
con.UnixLocalForward(localPath, remotePath)
con.CreateClient(host, user, port, []ssh.AuthMethod{authMethod})
}
func TestConnect_X11Forward(t *testing.T) {
t.Skip("requires a live SSH session and X11 environment")
}