-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhost_key_test.go
81 lines (67 loc) · 2.27 KB
/
host_key_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
package proxy_test
import (
proxy "github.com/cloudfoundry/socks5-proxy"
"golang.org/x/crypto/ssh"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("HostKey", func() {
Describe("Get", func() {
var (
hostKey proxy.HostKey
key ssh.PublicKey
sshServerAddr string
)
BeforeEach(func() {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
Expect(err).NotTo(HaveOccurred())
key = signer.PublicKey()
sshServerAddr = proxy.StartTestSSHServer("", privateKey, "")
hostKey = proxy.NewHostKey()
})
It("returns the host key", func() {
hostKey, err := hostKey.Get("", privateKey, sshServerAddr)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey).To(Equal(key))
})
Context("when a username has been set", func() {
BeforeEach(func() {
sshServerAddr = proxy.StartTestSSHServer("", privateKey, "different-username")
hostKey = proxy.NewHostKey()
})
It("returns the host key", func() {
hostKey, err := hostKey.Get("different-username", privateKey, sshServerAddr)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey).To(Equal(key))
})
})
Context("failure cases", func() {
Context("when parse private key fails", func() {
It("returns an error", func() {
_, err := hostKey.Get("", "%%%", sshServerAddr)
Expect(err).To(MatchError("ssh: no key found"))
})
})
Context("when dial fails", func() {
It("returns an error", func() {
_, err := hostKey.Get("", privateKey, "some-bad-url")
Expect(err).To(MatchError("dial tcp: address some-bad-url: missing port in address"))
})
})
Context("when the wrong private key is used", func() {
It("returns an error", func() {
_, err := hostKey.Get("", anotherPrivateKey, sshServerAddr)
Expect(err).To(MatchError(ContainSubstring("ssh: handshake failed")))
})
})
Context("when the wrong private key is used twice", func() {
It("returns an error twice", func() {
_, firstErr := hostKey.Get("", anotherPrivateKey, sshServerAddr)
Expect(firstErr).To(MatchError(ContainSubstring("ssh: handshake failed")))
_, secondErr := hostKey.Get("", anotherPrivateKey, sshServerAddr)
Expect(secondErr).To(MatchError(ContainSubstring("ssh: handshake failed")))
})
})
})
})
})