-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_unix_test.go
221 lines (200 loc) · 5.13 KB
/
test_unix_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux netbsd openbsd plan9
package sshctl
// functional test harness for unix.
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"testing"
"text/template"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/testdata"
)
var ssh_configs = map[string]string{
"sshd_config": `
Protocol 2
HostKey {{.Dir}}/id_rsa
HostKey {{.Dir}}/id_dsa
HostKey {{.Dir}}/id_ecdsa
HostCertificate {{.Dir}}/id_rsa-cert.pub
Pidfile {{.Dir}}/sshd.pid
SyslogFacility AUTH
#LogLevel DEBUG2
LoginGraceTime 120
PermitRootLogin no
StrictModes no
PubkeyAuthentication yes
AuthorizedKeysFile {{.Dir}}/authorized_keys
TrustedUserCAKeys {{.Dir}}/id_ecdsa.pub
IgnoreRhosts yes
HostbasedAuthentication no
PubkeyAcceptedKeyTypes=*
`,
"ssh_config": `
ProxyCommand -
ControlMaster yes
ControlPath {{.Dir}}/ctrl.sock
IdentityFile {{.Dir}}/id_rsa
UpdateHostKeys no
UserKnownHostsFile {{.Dir}}/known_hosts
BatchMode yes
`,
}
type server struct {
t *testing.T
cleanup func() // executed during Shutdown
configfile string
testdir string
sshdcmd *exec.Cmd
sshcmd *exec.Cmd
output bytes.Buffer // holds stderr from sshd/ssh processes
// Control Socket to ssh
ctrlSock string
}
func username() string {
var username string
if user, err := user.Current(); err == nil {
username = user.Username
} else {
// user.Current() currently requires cgo. If an error is
// returned attempt to get the username from the environment.
log.Printf("user.Current: %v; falling back on $USER", err)
username = os.Getenv("USER")
}
if username == "" {
panic("Unable to get username")
}
return username
}
func (s *server) TryRun() (string, error) {
sshd, err := exec.LookPath("sshd")
if err != nil {
s.t.Skipf("skipping test: %v", err)
}
ssh, err := exec.LookPath("ssh")
if err != nil {
s.t.Skipf("skipping test: %v", err)
}
s.sshdcmd = exec.Command(sshd, "-f", s.testdir+"/sshd_config", "-i", "-e")
s.sshcmd = exec.Command(ssh, "-F", s.testdir+"/ssh_config", "-N", username()+"@dummy")
s.sshdcmd.Stdin, _ = s.sshcmd.StdoutPipe()
s.sshcmd.Stdin, _ = s.sshdcmd.StdoutPipe()
s.sshdcmd.Stderr = &s.output
s.sshcmd.Stderr = &s.output
if err := s.sshdcmd.Start(); err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("s.sshdcmd.Start: %v", err)
}
if err := s.sshcmd.Start(); err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("s.sshcmd.Start: %v", err)
}
s.ctrlSock = s.testdir + "/ctrl.sock"
// Wait for control socket
for i := 1; i < 100; i++ {
time.Sleep(10 * time.Millisecond)
if _, err := os.Stat(s.ctrlSock); err == nil {
return s.ctrlSock, nil
}
}
s.t.Fatalf("ssh did not create control socket %s", s.ctrlSock)
return "", nil
}
func (s *server) Run() string {
conn, err := s.TryRun()
if err != nil {
s.t.Fail()
s.Shutdown()
s.t.Fatalf("ssh.Client: %v", err)
}
return conn
}
func (s *server) Shutdown() {
for _, cmd := range []*exec.Cmd{s.sshdcmd, s.sshcmd} {
if cmd != nil && cmd.Process != nil {
// Don't check for errors; if it fails it's most
// likely "os: process already finished", and we don't
// care about that. Use os.Interrupt, so child
// processes are killed too.
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
}
}
if s.t.Failed() {
// log any output from sshd process
s.t.Logf("sshd: %s", s.output.String())
}
s.cleanup()
}
func writeFile(path string, contents []byte) {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err := f.Write(contents); err != nil {
panic(err)
}
}
// newServer returns a new mock ssh--->sshd server.
func newServer(t *testing.T) *server {
if testing.Short() {
t.Skip("skipping test due to -short")
}
dir, err := ioutil.TempDir("", "sshctltest")
if err != nil {
t.Fatal(err)
}
for cname, conf := range ssh_configs {
f, err := os.Create(filepath.Join(dir, cname))
if err != nil {
t.Fatal(err)
}
configTmpl := template.Must(template.New("").Parse(conf))
err = configTmpl.Execute(f, map[string]string{
"Dir": dir,
})
if err != nil {
t.Fatal(err)
}
f.Close()
}
var known_hosts bytes.Buffer
for k, v := range testdata.PEMBytes {
filename := "id_" + k
writeFile(filepath.Join(dir, filename), v)
writeFile(filepath.Join(dir, filename+".pub"), ssh.MarshalAuthorizedKey(testPublicKeys[k]))
known_hosts.WriteString("dummy ")
known_hosts.Write(ssh.MarshalAuthorizedKey(testPublicKeys[k]))
known_hosts.WriteString("\n")
}
writeFile(filepath.Join(dir, "known_hosts"), known_hosts.Bytes())
for k, v := range testdata.SSHCertificates {
filename := "id_" + k + "-cert.pub"
writeFile(filepath.Join(dir, filename), v)
}
var authkeys bytes.Buffer
for k, _ := range testdata.PEMBytes {
authkeys.Write(ssh.MarshalAuthorizedKey(testPublicKeys[k]))
}
writeFile(filepath.Join(dir, "authorized_keys"), authkeys.Bytes())
return &server{
t: t,
testdir: dir,
cleanup: func() {
if err := os.RemoveAll(dir); err != nil {
t.Error(err)
}
},
}
}