-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh_test.go
More file actions
137 lines (116 loc) · 3.93 KB
/
Copy pathssh_test.go
File metadata and controls
137 lines (116 loc) · 3.93 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
package main
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"errors"
"path/filepath"
"testing"
)
func TestBuildAuthMethodSpecsPrivateKeyOnly(t *testing.T) {
t.Setenv("SSH_AUTH_SOCK", "")
_, privateKeyPKCS8 := marshalTestPrivateKey(t)
auth := buildTestAuthMethods(t, SSHConfig{PrivateKey: privateKeyPKCS8})
if len(auth.specs) != 1 {
t.Fatalf("auth method count = %d, want 1", len(auth.specs))
}
if auth.specs[0].kind != authMethodPrivateKey {
t.Fatalf("specs[0].kind = %q, want %q", auth.specs[0].kind, authMethodPrivateKey)
}
}
func TestBuildAuthMethodSpecsPasswordOnly(t *testing.T) {
t.Setenv("SSH_AUTH_SOCK", "")
auth := buildTestAuthMethods(t, SSHConfig{Passphrase: "secret"})
if len(auth.specs) != 1 {
t.Fatalf("auth method count = %d, want 1", len(auth.specs))
}
if auth.specs[0].kind != authMethodPassword {
t.Fatalf("specs[0].kind = %q, want %q", auth.specs[0].kind, authMethodPassword)
}
}
func TestBuildAuthMethodSpecsSkipsStaleEnvAgent(t *testing.T) {
staleSocketPath := filepath.Join(t.TempDir(), "agent.sock")
t.Setenv("SSH_AUTH_SOCK", staleSocketPath)
_, privateKeyPKCS8 := marshalTestPrivateKey(t)
auth := buildTestAuthMethods(t, SSHConfig{PrivateKey: privateKeyPKCS8})
if len(auth.specs) != 1 {
t.Fatalf("auth method count = %d, want 1", len(auth.specs))
}
if auth.specs[0].kind != authMethodPrivateKey {
t.Fatalf("specs[0].kind = %q, want %q", auth.specs[0].kind, authMethodPrivateKey)
}
auth = buildTestAuthMethods(t, SSHConfig{Passphrase: "secret"})
if len(auth.specs) != 1 {
t.Fatalf("auth method count = %d, want 1", len(auth.specs))
}
if auth.specs[0].kind != authMethodPassword {
t.Fatalf("specs[0].kind = %q, want %q", auth.specs[0].kind, authMethodPassword)
}
if _, err := buildAuthMethods(t.Context(), SSHConfig{
SSHAuthSock: staleSocketPath,
Passphrase: "secret",
}); err == nil {
t.Fatal("buildAuthMethods() with explicit stale agent error = nil, want error")
}
}
func TestBuildAuthMethodSpecsReturnsCanceledContext(t *testing.T) {
t.Setenv("SSH_AUTH_SOCK", filepath.Join(t.TempDir(), "agent.sock"))
ctx, cancel := context.WithCancel(t.Context())
cancel()
_, err := buildAuthMethods(ctx, SSHConfig{Passphrase: "secret"})
if !errors.Is(err, context.Canceled) {
t.Fatalf("buildAuthMethods() error = %v, want %v", err, context.Canceled)
}
}
func TestDialSSHReturnsCanceledContext(t *testing.T) {
t.Setenv("SSH_AUTH_SOCK", "")
ctx, cancel := context.WithCancel(t.Context())
cancel()
_, err := DialSSH(ctx, SSHConfig{
Server: NetworkAddress{Host: "127.0.0.1", Port: "1"},
Username: "alice",
Passphrase: "secret",
})
if !errors.Is(err, context.Canceled) {
t.Fatalf("DialSSH() error = %v, want %v", err, context.Canceled)
}
}
func TestResolveSSHAuthSock(t *testing.T) {
t.Setenv("SSH_AUTH_SOCK", "/tmp/from-env.sock")
if got, implicit := resolveSSHAuthSock(SSHConfig{SSHAuthSock: "/tmp/from-config.sock"}); got != "/tmp/from-config.sock" || implicit {
t.Fatalf("resolveSSHAuthSock() = %q, want %q", got, "/tmp/from-config.sock")
}
if got, implicit := resolveSSHAuthSock(SSHConfig{}); got != "/tmp/from-env.sock" || !implicit {
t.Fatalf("resolveSSHAuthSock() = %q, want %q", got, "/tmp/from-env.sock")
}
}
func buildTestAuthMethods(t *testing.T, config SSHConfig) *authMethods {
t.Helper()
auth, err := buildAuthMethods(t.Context(), config)
if err != nil {
t.Fatalf("buildAuthMethods() error = %v", err)
}
t.Cleanup(func() {
if err := auth.close(); err != nil {
t.Fatalf("auth.close() error = %v", err)
}
})
return auth
}
func marshalTestPrivateKey(t *testing.T) (ed25519.PrivateKey, []byte) {
t.Helper()
_, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
t.Fatalf("generate ed25519 key: %v", err)
}
der, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
t.Fatalf("marshal private key: %v", err)
}
return privateKey, pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: der,
})
}